4

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
SunflowerToadTheOrbiter
  • 1,285
  • 4
  • 15
  • 25
  • One (spring boot) application only listens on one port. If you want a spring boot application to listen to 2 ports, start 2 up with server.port=8080 and the other one on server.port=80 i.e. seperate application.properties files. – Luke Kroon Aug 16 '18 at 11:55
  • It is discussed in [another thread](https://stackoverflow.com/questions/36357135/configure-spring-boot-with-two-ports/69190413) – Karmakulov Kirill Sep 15 '21 at 09:31

2 Answers2

6

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

Jan Galinski
  • 11,768
  • 8
  • 54
  • 77
Oreste Viron
  • 3,592
  • 3
  • 22
  • 34
0

You can run it by using below mentioned command:

mvn spring-boot:run -Dspring-boot.run.arguments=--server.port=8080

mvn spring-boot:run -Dspring-boot.run.arguments=--server.port=8081

Just change the port number and run it on another terminal. At a same time you can run the multiple instances of same spring boot app.

Now one spring boot app is running on 2 ports, one is on 8080 and another one is on 8081.

Maninder
  • 1,539
  • 1
  • 10
  • 12