问题
Our main application is being served on port 8443, and we're using management.port
to serve our actuator endpoints on port 8444.
Is there a way to get a single endpoint (the health endpoint) to serve on both 8443 and 8444 while leaving the remaining endpoints on port 8444 only?
回答1:
Providing you're using the built in Tomcat container you could:
- Remove the management port config property
- Add an additional connector to serve port 8444
- Add a filter to allow only the health check be accessed on that port
Your code might look something like this.
@ComponentScan
@Configuration
@EnableAutoConfiguration
public class Application extends WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter implements EmbeddedServletContainerCustomizer {
@Autowired
private PortInterceptor portInterceptor;
public static void main(String[] args) throws Exception {
SpringApplication application = new SpringApplication(Application.class);
application.run(args);
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(portInterceptor);
}
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
TomcatEmbeddedServletContainerFactory tomcat = (TomcatEmbeddedServletContainerFactory)container;
Connector connector = new Connector();
connector.setPort(8444);
tomcat.addAdditionalTomcatConnectors(connector);
}
}
@Component
public class PortInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if(request.getLocalPort() == 8444){
return isHealthCheckRequest(request);
}
return true;
}
}
来源:https://stackoverflow.com/questions/36575407/serving-a-spring-boot-actuator-endpoint-on-multiple-ports