Serving a Spring Boot Actuator endpoint on multiple ports

纵然是瞬间 提交于 2019-12-22 12:15:17

问题


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:

  1. Remove the management port config property
  2. Add an additional connector to serve port 8444
  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!