Add local.management.port when on same port
Add a `local.management.port` alias when the management server is running on the same port as the main server. Fixes gh-952
This commit is contained in:
parent
91bd78047e
commit
a91e85b848
|
@ -65,6 +65,8 @@ import org.springframework.context.annotation.Bean;
|
|||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.event.ContextClosedEvent;
|
||||
import org.springframework.context.event.ContextRefreshedEvent;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
|
@ -122,31 +124,17 @@ public class EndpointWebMvcAutoConfiguration implements ApplicationContextAware,
|
|||
@Override
|
||||
public void onApplicationEvent(ContextRefreshedEvent event) {
|
||||
if (event.getApplicationContext() == this.applicationContext) {
|
||||
if (ManagementServerPort.get(this.applicationContext) == ManagementServerPort.DIFFERENT
|
||||
ManagementServerPort managementPort = ManagementServerPort
|
||||
.get(this.applicationContext);
|
||||
if (managementPort == ManagementServerPort.DIFFERENT
|
||||
&& this.applicationContext instanceof WebApplicationContext) {
|
||||
createChildManagementContext();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Put Servlets and Filters in their own nested class so they don't force early
|
||||
// instantiation of ManagementServerProperties.
|
||||
@Configuration
|
||||
protected static class ApplicationContextFilterConfiguration {
|
||||
|
||||
@Bean
|
||||
public Filter applicationContextIdFilter(ApplicationContext context) {
|
||||
final String id = context.getId();
|
||||
return new OncePerRequestFilter() {
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request,
|
||||
HttpServletResponse response, FilterChain filterChain)
|
||||
throws ServletException, IOException {
|
||||
response.addHeader("X-Application-Context", id);
|
||||
filterChain.doFilter(request, response);
|
||||
}
|
||||
};
|
||||
if (managementPort == ManagementServerPort.SAME
|
||||
&& this.applicationContext.getEnvironment() instanceof ConfigurableEnvironment) {
|
||||
addLocalManagementPortPropertyAlias((ConfigurableEnvironment) this.applicationContext
|
||||
.getEnvironment());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -232,6 +220,46 @@ public class EndpointWebMvcAutoConfiguration implements ApplicationContextAware,
|
|||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Add an alias for 'local.management.port' that actually resolves using
|
||||
* 'local.server.port'.
|
||||
* @param environment the environment
|
||||
*/
|
||||
private void addLocalManagementPortPropertyAlias(
|
||||
final ConfigurableEnvironment environment) {
|
||||
environment.getPropertySources().addLast(
|
||||
new PropertySource<Object>("Management Server") {
|
||||
@Override
|
||||
public Object getProperty(String name) {
|
||||
if ("local.management.port".equals(name)) {
|
||||
return environment.getProperty("local.server.port");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Put Servlets and Filters in their own nested class so they don't force early
|
||||
// instantiation of ManagementServerProperties.
|
||||
@Configuration
|
||||
protected static class ApplicationContextFilterConfiguration {
|
||||
|
||||
@Bean
|
||||
public Filter applicationContextIdFilter(ApplicationContext context) {
|
||||
final String id = context.getId();
|
||||
return new OncePerRequestFilter() {
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request,
|
||||
HttpServletResponse response, FilterChain filterChain)
|
||||
throws ServletException, IOException {
|
||||
response.addHeader("X-Application-Context", id);
|
||||
filterChain.doFilter(request, response);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
protected static enum ManagementServerPort {
|
||||
|
||||
DISABLE, SAME, DIFFERENT;
|
||||
|
|
|
@ -38,6 +38,7 @@ import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebAppl
|
|||
import org.springframework.boot.context.embedded.EmbeddedServletContainer;
|
||||
import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
|
||||
import org.springframework.boot.test.EnvironmentTestUtils;
|
||||
import org.springframework.boot.test.ServerPortInfoApplicationContextInitializer;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
@ -55,6 +56,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
|||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
|
@ -172,6 +174,43 @@ public class EndpointWebMvcAutoConfigurationTests {
|
|||
assertAllClosed();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void portPropertiesOnSamePort() throws Exception {
|
||||
this.applicationContext.register(RootConfig.class, BaseConfiguration.class,
|
||||
ServerPortConfig.class, EndpointWebMvcAutoConfiguration.class);
|
||||
new ServerPortInfoApplicationContextInitializer()
|
||||
.initialize(this.applicationContext);
|
||||
this.applicationContext.refresh();
|
||||
Integer localServerPort = this.applicationContext.getEnvironment().getProperty(
|
||||
"local.server.port", Integer.class);
|
||||
Integer localManagementPort = this.applicationContext.getEnvironment()
|
||||
.getProperty("local.management.port", Integer.class);
|
||||
assertThat(localServerPort, notNullValue());
|
||||
assertThat(localManagementPort, notNullValue());
|
||||
assertThat(localServerPort, equalTo(localManagementPort));
|
||||
this.applicationContext.close();
|
||||
assertAllClosed();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void portPropertiesOnDifferentPort() throws Exception {
|
||||
new ServerPortInfoApplicationContextInitializer()
|
||||
.initialize(this.applicationContext);
|
||||
this.applicationContext.register(RootConfig.class, DifferentPortConfig.class,
|
||||
BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class,
|
||||
ErrorMvcAutoConfiguration.class);
|
||||
this.applicationContext.refresh();
|
||||
Integer localServerPort = this.applicationContext.getEnvironment().getProperty(
|
||||
"local.server.port", Integer.class);
|
||||
Integer localManagementPort = this.applicationContext.getEnvironment()
|
||||
.getProperty("local.management.port", Integer.class);
|
||||
assertThat(localServerPort, notNullValue());
|
||||
assertThat(localManagementPort, notNullValue());
|
||||
assertThat(localServerPort, not(equalTo(localManagementPort)));
|
||||
this.applicationContext.close();
|
||||
assertAllClosed();
|
||||
}
|
||||
|
||||
private void assertAllClosed() throws Exception {
|
||||
assertContent("/controller", ports.get().server, null);
|
||||
assertContent("/endpoint", ports.get().server, null);
|
||||
|
|
Loading…
Reference in New Issue