Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 599
WetWer Avatar asked Sep 13 '25 00:09

WetWer


1 Answers

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

like image 149
Oreste Viron Avatar answered Sep 14 '25 23:09

Oreste Viron