Skip to content
Home » Set Random port in Spring Boot

Set Random port in Spring Boot

  • by

1. Overview

In this article, we will learn to set a random port in the Spring boot application. Assume you want to run multiple instances of the same app on your machine. Each instance should listen to a different port with no clashes at run time.

2. Set Random port in Spring boot application

By default, the embedded server listens for HTTP requests on port 8080. To know about changing the default port, refer to this detailed article.

Here, we will learn to configure a random port for your app. Whenever you set the server port as 0 (Zero), the Spring boot application scans for the free port and uses it.

2.1. Configure application properties

You can configure port 0 (zero) for the following property in your application.properties or application.yaml file:

server.port=0
server:
    port: 0

Usually, we place the file under src/main/resources for Maven or Gradle projects.

2.2. Random port in VM options \ System property

Alternatively, you can specify the port 0 (zero) using the VM argument -Dserver.port. In your IDE, navigate to Run -> Edit or Run Configurations -> VM options and specify port 0.

-Dserver.port=0
Random port in STS or Eclipse
STS or Eclipse
Random port in IntelliJ
IntelliJ

If you are running the jar file from the terminal instead of IDE, then pass the server port property in the command.

java -jar -Dserver.port=0 <path to jar file>

You can also programmatically set the System server port property.

public static void main(String[] args) {
   System.setProperty("server.port", "0");
   SpringApplication.run(HibernateApplication.class, args);
}

2.3. Random port using command line \ program argument

By default, SpringApplication converts any command-line option arguments to a property and adds them to the Spring Environment. If you are running the jar from the terminal, then add the command line argument in the command:

java -jar <path to Jar file> --server.port=0

Note that --server.port is a command-line or program argument that is passed in the args parameter of the SpringBoot application’s main method, whereas --Dserver.port is a JVM system property.

You can also set the program arguments in your IDE by navigating to Run -> Edit \ Run configurations -> Program arguments.

2.4. Using WebServerFactoryCustomizer from Spring 2.x and above

You can programmatically configure your embedded servlet container programmatically. Just register a Spring bean that implements the WebServerFactoryCustomizer interface. 

The beans of this type get a callback before the server start, so you can set the port.

This interface provides access to the ConfigurableServletWebServerFactory through which you can set the port to 0 (zero).

import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.stereotype.Component;

@Component
public class CustomizationBean implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {

    @Override
    public void customize(ConfigurableServletWebServerFactory server) {
        server.setPort(0);
    }

}

2.5. Using EmbeddedServletContainerCustomizer in Spring 1.x

The EmbeddedServletContainerCustomizer class is replaced by WebServerFactoryCustomizer in the Spring 2.x.x and above versions. So, you can use this only in Spring 1.x versions. The beans of type EmbeddedServletContainerCustomizer get a callback before the server start, so you can set the port.

@Component
public class CustomizationBean implements  EmbeddedServletContainerCustomizer {
    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) 
    {    
        container.setPort(0);
    }
}

(or)

@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
    return (container -> {
        container.setPort(0);
    });
}

2.6. Change port using the environment variable

The SERVER_PORT OS Environment variable is available to define the port:

UNIX Shell

> SERVER_PORT=0 
> java -jar <jar file>

Windows

> SET SERVER_PORT=0 
> java -jar <jar file>

3. Conclusion

To sum up, we have learned to use the random port for the embedded server in your Spring boot application.

Leave a Reply

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