Running two spring boot instances
Asked Answered
G

7

16

I am using Intellij Idea IDE version 2019.2.3 and I want to run two instances of my spring boot application in different ports, but I got just one running instance. I start the application in the port 8081 and after it runs, I change the port to 8083 and run it again in parallel with the previous one but i got this error :

`The Tomcat connector configured to listen on port 8083 failed to start. The port may already be in use or the connector may be misconfigured.

And when I come to the browser i found that the first port stopped working, when the application runs successfully in the last port`

I have tried to add a new "run configuration" but got the same problem.

enter image description here

Gangrel answered 11/10, 2019 at 21:6 Comment(3)
Where do you set the port?Panegyric
In bootstrap.properties fileGangrel
Try using 2 different run configurations with the port set in the VM Options field like this: -Dserver.port=9090. Use different port for each configuration so that you can run 2 instances.Panegyric
P
30

Use the VM options field of the Sprint Boot run/debug configuration to define the port via the

-Dserver.port=9090

property. If the ports are different, you'll be able to start multiple instances:

Running 2 instances on different ports

The screenshot shows 2 run configurations started at the same time, one has -Dserver.port=9090 VM option, another has -Dserver.port=9091.

Panegyric answered 11/10, 2019 at 22:40 Comment(1)
Along with this I also had to mark the "Allow multiple instances" option in Run configuration as mentioned in the next answer.Shortage
R
7

It's possible to use 'Allow parallel run' run/debug configuration option in combination with server.port: ${random.int(value,[max])} spring boot's property. For instance, server.port: ${random.int(8088,8099)} so that while running the config the idea will pick some random port from the provided range as result you don't need to spawn multiple run/debug configurations if it's not necessary. In order not to get in collision please use a wider range. enter image description here

Revise answered 28/5, 2020 at 9:45 Comment(0)
S
2

Here are the steps for the latest Intellij version where VM options might not be shown by default in Run Configuration

  1. Edit Run/Debug Configuration

Edit Configuration

  1. On the right side click "Modify Option" and choose from the drop-down "Add VM options" Add VM options
  2. Set the following command and the desired port in the newly appeared filed Add server port
  3. Save and run the project
Sauceda answered 31/10, 2022 at 17:11 Comment(0)
P
0

you can create instance or copy of an application by clicking "Run Button -> Run Configuration".

Then right click on your "Application name" -> click on "Duplicate".

Then choose "Arguments" tab -> In "VM Arguments" type this.

-Dserver.port=8001

8001 is the port number you can mention your port over there. screenshot below:

enter image description here enter image description here enter image description here

Paint answered 26/7, 2022 at 9:19 Comment(1)
Or change the server port in the properties file between runs. add a properties file: application.yml OR application.properties if you choose .yml server: port: 8080 if you choose .properties server.port=8080 start an instans with default port 8080 then change the port without stopping the first instance. if you choose .yml server: port: 9090 if you choose .properties server.port=9090 and start a seccond instance eather way you should bee good to go :)Conjoin
C
0

This video gives one option that works. Make several instances with different port numbers. Alternatively, you can set server.port=0 and each instance will have a random port that will be different.

https://www.youtube.com/watch?v=pRb1io4Ewuc

Carrasco answered 15/3, 2023 at 19:8 Comment(0)
C
0

Or change the server port in the properties file between runs. add a properties file:

application.yml OR application.properties

if you choose .yml:

server:
  port: 8080

if you choose .properties:

server.port=8080

start an instans with default port 8080 then change the port without stopping the first instance.

server:
  port: 9090

server.port=9090

and start a seccond instance eather way you should bee good to go :)

read more here:

https://www.baeldung.com/spring-boot-change-port

https://www.baeldung.com/spring-boot-yaml-vs-properties

Conjoin answered 3/5, 2024 at 8:48 Comment(0)
H
-1

another straight way is to use:

server.port=0.   (in application.properties file) or
-Dserver.port=0 (in VM arguments)

This way random port number will automatically be assigned to the newly created instance

Haihaida answered 15/2, 2023 at 5:58 Comment(1)
True. You will get random port numbers but, you will still not be allowed to run in parallel and you won't be able to open a 2nd instance.Carrasco

© 2022 - 2025 — McMap. All rights reserved.