Configure Spring Boot with two ports

前端 未结 4 2090
半阙折子戏
半阙折子戏 2020-11-29 08:31

I\'m trying configure an application in Spring Boot with two differents ports, but I haven\'t got still. My first aproximation has been with two controllers and I have defi

相关标签:
4条回答
  • 2020-11-29 08:56

    Since springboot 2, EmbeddedServletContainerFactory mentioned in ootero solution is no longer available, so you should use either TomcatServletWebServerFactory or TomcatReactiveWebServerFactory according to your context.

    The solution stays the same aside from the factory injection :

    @Bean
    public TomcatServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
        Connector[] additionalConnectors = this.additionalConnector();
        if (additionalConnectors != null && additionalConnectors.length > 0) {
            tomcat.addAdditionalTomcatConnectors(additionalConnectors);
        }
        return tomcat;
    }
    
    0 讨论(0)
  • 2020-11-29 09:08

    To change Actuator management port you can use property

    management.port=8081
    

    See full list of properties here

    Update: Actuator creates one more Embedded Tomcat(servlet container) instance in this case. See here and here

    0 讨论(0)
  • 2020-11-29 09:08

    To run 2 or more applications within a single project or change the default port, you can perform the action like this

    @SpringBootApplication
    public class NewApplication {
         public static void main(String[] args) {
             SpringApplication app = new SpringApplication(NewApplication .class);
             app.setDefaultProperties(Collections.singletonMap("server.port", "8083"));
             app.run(args);
         }
    }
    
    0 讨论(0)
  • 2020-11-29 09:13

    As is has been mentioned before, server.port and management.port along with management.context-path properties could be set to make the embedded container to listen on different ports (management-related properties to access Actuator endpoints).

    To listen on ports other than server.port and management.port:

    @Configuration
    public class EmbeddedTomcatConfiguration {
    
        @Value("${server.additionalPorts}")
        private String additionalPorts;
    
        @Bean
        public EmbeddedServletContainerFactory servletContainer() {
            TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
            Connector[] additionalConnectors = this.additionalConnector();
            if (additionalConnectors != null && additionalConnectors.length > 0) {
                tomcat.addAdditionalTomcatConnectors(additionalConnectors);
            }
            return tomcat;
        }
    
        private Connector[] additionalConnector() {
            if (StringUtils.isBlank(this.additionalPorts)) {
                return null;
            }
            String[] ports = this.additionalPorts.split(",");
            List<Connector> result = new ArrayList<>();
            for (String port : ports) {
                Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
                connector.setScheme("http");
                connector.setPort(Integer.valueOf(port));
                result.add(connector);
            }
            return result.toArray(new Connector[] {});
        }
    }
    

    application.yml

    server:
      port: ${appPort:8800}
      additionalPorts: 8881,8882
    

    Application.java

    @SpringBootApplication
    @ComponentScan(...)
    @Import(EmbeddedTomcatConfiguration.class)
    public Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application .class, args);
        }
    }
    

    I recently blogged about this topic at http://tech.asimio.net/2016/12/15/Configuring-Tomcat-to-Listen-on-Multiple-ports-using-Spring-Boot.html

    0 讨论(0)
提交回复
热议问题