Start Spring Boot App on a Random Port Number

In this tutorial, you will learn how to start your Spring Boot application on a random port number.

This is very helpful when you need to start multiple instances of the same Spring Boot application and then balance HTTP requests between the running instances.

If you are interested to learn how to balance HTTP requests between multiple instances of the same Spring Boot application, then check my video course Spring Boot Microservices and Spring Cloud.

To start your application on a random port number you will need to set the server.port number to 0(zero).  To do that,

  • Open application.properties file of your Spring Boot Web application and,
  • Set the server.port property to zero.
server.port=0

Another way to run your Spring Boot application on a random port number is to pass server.port number as a run time argument when starting your spring boot application. To do that,

  • Open the terminal window on your computer,
  • Change the current directory to a home folder of your Spring Boot project,
  • Run the Spring Boot application and pass the server.port configuration as a run-time argument.
mvn spring-boot:run -Dspring-boot.run.arguments="--server.port=0"

Video Tutorial

I hope this tutorial was helpful to you. If you are interested in other Spring Boot tutorials and video lessons please check this page: Spring Boot and Spring MVC tutorials.

Happy learning 🙋🏻‍♂️


Leave a Reply

Your email address will not be published. Required fields are marked *