I have a spring boot application with two services but i need to run one on port 8080 and the other on 8081. Now i'm developing with sts (Spring tool suite) and i run the application with the option "Run as spring boot app", so i don't know where change server configurations. Someone can help me?
-
1Check this duplicated [post](https://stackoverflow.com/questions/21083170/how-to-configure-port-for-a-spring-boot-application) You can do it via application.properties – pbuzulan Mar 07 '19 at 10:18
-
1I've seen that but i need to put one service in 8080 and another in 8081 with the same application – Alessio Pascucci Mar 07 '19 at 10:20
-
1Possible duplicate of [How to configure port for a Spring Boot application](https://stackoverflow.com/questions/21083170/how-to-configure-port-for-a-spring-boot-application) – Mickael Mar 07 '19 at 10:21
-
1It is discussed here: https://stackoverflow.com/questions/36357135/configure-spring-boot-with-two-ports/69190413 – Karmakulov Kirill Sep 15 '21 at 09:34
6 Answers
You can't run two different services under the same spring boot application in two different ports. If you want you can move one service to another spring boot application. But port number will not be same for both services.
- 364
- 1
- 6
- 14
The port can be defined on the application.properties or the application.yaml configuration file that you use.
In application.properties file, define it as below:
server.port=8090
Or in case you are using a application.yaml config, define it as below:
server:
port: 8090
- 11,063
- 1
- 29
- 49
-
So, if i need to access two different ports i need to run the application on two server placed on two different ports? – Alessio Pascucci Mar 07 '19 at 10:40
-
1@AlessioPascucci in case you must have two services on different ports, you need to run it as two different applications, each with it's own different port. – Madhu Bhat Mar 07 '19 at 11:05
you can configure SprintBoot support two ports.The common method configure is use application.properties or the application.yaml as @Madhu Bhat answer. In SprintBoot to configure another port code like this:
create a connector
int port = Integer.parseInt(probePort);
Connector httpConnector = new Connector(HTTP_PROTOCOL);
httpConnector.setPort(port);
Http11NioProtocol handler = (Http11NioProtocol) httpConnector.getProtocolHandler();
handler.setMaxThreads(10);
handler.setMinSpareThreads(4);
//handler.setAddress(InetAddress.getLocalHost());
handler.setAddress(StringTool.getInetAddress());
return httpConnector;
configure connector to
((TomcatEmbeddedServletContainerFactory) container).addAdditionalTomcatConnectors(connector);
- 1,326
- 1
- 9
- 18
-
1But this need to run two different application onto two different ports? – Alessio Pascucci Mar 07 '19 at 14:42
If you use Docker (most common solution), you can add the port or its full address as an Environment Variable
docker-compose.yml file like this:
application1:
image: 'application1:latest'
build:
context: ./
container_name: application
environment:
- HOST-APP2=localhost:8082
ports:
- 8091:8080
application2:
image: 'application2:latest'
build:
context: ./
container_name: application
environment:
- HOST-APP1=localhost:8081
ports:
- 8092:8080
or straight in the Dockerfile while building containers
check out here : https://vsupalov.com/docker-arg-env-variable-guide/ it's a good article
- 153
- 14
you can write below line in application.properties or application.yml
server.port=8080
- 454
- 2
- 7
Sure. You can do it in the application.properties file of spring boot project via setting server.port=number for each service.
- 144
- 1
- 8