Search code examples
springspring-bootportapplication.properties

Spring Boot Multiple Ports?


How can I have a spring boot web application running on multiple ports? for example 8080 and 80
how can I achive this?
application.properties

server.port=8080, 80

Solution

  • Instead of running multiple applications, you can add listeners. For example, if you use undertow :

    @Configuration
    public class PortConfig {
    
        @Value("${server.http.port}")
        private int httpPort;
    
        @Bean
        public UndertowEmbeddedServletContainerFactory embeddedServletContainerFactory() {
            UndertowEmbeddedServletContainerFactory factory = new UndertowEmbeddedServletContainerFactory();
            factory.addBuilderCustomizers(new UndertowBuilderCustomizer() {
    
                @Override
                public void customize(Undertow.Builder builder) {
                    builder.addHttpListener(httpPort, "0.0.0.0");
                }
    
            });
            return factory;
        }
    }
    

    I have use this to listen to http port AND https port.

    For Tomcat you will find the same kind of configurations : https://docs.spring.io/spring-boot/docs/1.2.1.RELEASE/api/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainerFactory.html