在多个端口上为 Spring Boot Actuator 端点提供服务 [英] Serving a Spring Boot Actuator endpoint on multiple ports

查看:28
本文介绍了在多个端口上为 Spring Boot Actuator 端点提供服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的主要应用程序在端口 8443 上提供服务,我们使用 management.port 在端口 8444 上为我们的执行器端点提供服务.

Our main application is being served on port 8443, and we're using management.port to serve our actuator endpoints on port 8444.

有没有办法让一个端点(健康端点)同时在 8443 和 8444 上提供服务,同时将其余端点仅留在端口 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?

推荐答案

如果您使用的是内置的 Tomcat 容器,您可以:

Providing you're using the built in Tomcat container you could:

  1. 删除管理端口配置属性
  2. 添加一个额外的连接器来为端口 8444 提供服务
  3. 添加过滤器以仅允许在该端口上访问健康检查

您的代码可能如下所示.

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;
    }
}

这篇关于在多个端口上为 Spring Boot Actuator 端点提供服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆