'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 1:[1]
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
Solution 2:[2]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Jan Galinski |
Solution 2 | Maninder |