简体   繁体   中英

How to register servlet on a different port in spring boot?

In my casual spring boot application I regiser servlet like this.

    import io.prometheus.client.exporter.*
    .....
    @Bean
    public ServletRegistrationBean<MetricsServlet> registerMetricsServlet(MetricRegistry metricRegistry) {
        return new ServletRegistrationBean<>(new MetricsServlet(), "/metrics");
    }

This servlet is registred on the same port as application. The issue is that my infrastructure(k8s and etc) is expecting that application and metrics endpoint works on a different ports(like actuator does).

How can I register third party servlet on a ddifferent port ?

You can do it with multiple connectors and request filter. (All connectors are handle same way)

  1. https://tech.asimio.net/2016/12/15/Configuring-Tomcat-to-Listen-on-Multiple-ports-using-Spring-Boot.html

pay attention to the lines from

   public FilterRegistrationBean javaMelodyRestrictingFilter(FilterRegistrationBean javaMelodyFilter) {...
  1. spring boot 2.0 listening on multiple ports

Original answer here. Configure Spring Boot with two ports

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM