Commit 336b96b8 authored by Dave Syer's avatar Dave Syer Committed by Phillip Webb

Copy server customization to management context

If the actuator endpoints are configured on a different port then there
are some settings in the main ServerProperties that we would like to
re-use (e.g. the access log). The easiest way to do that is to just
configure the management server using the same ServerProperties instance
and then overwrite the things that are different (and stored in
ManagementServerProperties).

Fixes gh-1581
parent 304920df
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
package org.springframework.boot.actuate.autoconfigure; package org.springframework.boot.actuate.autoconfigure;
import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
...@@ -36,6 +37,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; ...@@ -36,6 +37,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.SearchStrategy; import org.springframework.boot.autoconfigure.condition.SearchStrategy;
import org.springframework.boot.autoconfigure.web.ErrorAttributes; import org.springframework.boot.autoconfigure.web.ErrorAttributes;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters; import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer; import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainer; import org.springframework.boot.context.embedded.EmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
...@@ -75,13 +77,23 @@ public class EndpointWebMvcChildContextConfiguration { ...@@ -75,13 +77,23 @@ public class EndpointWebMvcChildContextConfiguration {
// instances get their callback very early in the context lifecycle. // instances get their callback very early in the context lifecycle.
private ManagementServerProperties managementServerProperties; private ManagementServerProperties managementServerProperties;
private ServerProperties server;
@Override @Override
public void customize(ConfigurableEmbeddedServletContainer container) { public void customize(ConfigurableEmbeddedServletContainer container) {
if (this.managementServerProperties == null) { if (this.managementServerProperties == null) {
this.managementServerProperties = BeanFactoryUtils this.managementServerProperties = BeanFactoryUtils
.beanOfTypeIncludingAncestors(this.beanFactory, .beanOfTypeIncludingAncestors(this.beanFactory,
ManagementServerProperties.class); ManagementServerProperties.class);
this.server = BeanFactoryUtils
.beanOfTypeIncludingAncestors(this.beanFactory,
ServerProperties.class);
} }
// Customize as per the parent context first (so e.g. the access logs go to the same place)
server.customize(container);
// Then reset the error pages
container.setErrorPages(Collections.<ErrorPage>emptySet());
// and add the management-specific bits
container.setPort(this.managementServerProperties.getPort()); container.setPort(this.managementServerProperties.getPort());
container.setAddress(this.managementServerProperties.getAddress()); container.setAddress(this.managementServerProperties.getAddress());
container.setContextPath(this.managementServerProperties.getContextPath()); container.setContextPath(this.managementServerProperties.getContextPath());
......
...@@ -35,6 +35,7 @@ import org.springframework.boot.autoconfigure.web.ServerProperties; ...@@ -35,6 +35,7 @@ import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration; import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext; import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainer; import org.springframework.boot.context.embedded.EmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent; import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.boot.test.EnvironmentTestUtils;
...@@ -222,6 +223,7 @@ public class EndpointWebMvcAutoConfigurationTests { ...@@ -222,6 +223,7 @@ public class EndpointWebMvcAutoConfigurationTests {
assertThat(localServerPort, notNullValue()); assertThat(localServerPort, notNullValue());
assertThat(localManagementPort, notNullValue()); assertThat(localManagementPort, notNullValue());
assertThat(localServerPort, not(equalTo(localManagementPort))); assertThat(localServerPort, not(equalTo(localManagementPort)));
assertThat(applicationContext.getBean(ServerPortConfig.class).getCount(), equalTo(2));
this.applicationContext.close(); this.applicationContext.close();
assertAllClosed(); assertAllClosed();
} }
...@@ -303,10 +305,22 @@ public class EndpointWebMvcAutoConfigurationTests { ...@@ -303,10 +305,22 @@ public class EndpointWebMvcAutoConfigurationTests {
@Configuration @Configuration
public static class ServerPortConfig { public static class ServerPortConfig {
private int count = 0;
public int getCount() {
return count;
}
@Bean @Bean
public ServerProperties serverProperties() { public ServerProperties serverProperties() {
ServerProperties properties = new ServerProperties(); ServerProperties properties = new ServerProperties() {
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
count++;
super.customize(container);
}
};
properties.setPort(ports.get().server); properties.setPort(ports.get().server);
return properties; return properties;
} }
......
logging.file: /tmp/logs/app.log logging.file: /tmp/logs/app.log
logging.level.org.springframework.security: INFO logging.level.org.springframework.security: INFO
management.address: 127.0.0.1 management.address: 127.0.0.1
#management.port: 8181
endpoints.shutdown.enabled: true endpoints.shutdown.enabled: true
server.tomcat.basedir: target/tomcat server.tomcat.basedir: target/tomcat
server.tomcat.access_log_enabled: true server.tomcat.access_log_enabled: true
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment