Spring Cloud Microservices Discovery with Eureka on Random Ports

In this tutorial I am going to explain how to start Spring Cloud microservices on random ports. If you do not specify any port for each of your Spring Boot services, then they will run on the same port and only one application will start and rest of the applications will fail to start. One way to fix this problem is to specify the port for each application in their corresponding application.properties/yaml/yml file. This works fine and would be manageable when you have only a few microservices built using Spring Boot framework. But if you have lots of microservices then it would be a very difficult to manage these applications.

Another downside of specifying ports manually is the deployment. So when you deploy your microservices to a server, then you have to make sure that there is no port collision on the server application. This is a time consuming effort and would be difficult for future maintenance for the deployment team.

Let’s take another scenario, when you are deploying the applications in a docker container using container orchestration tool (Kubernetes), then you may not know upfront on which node the application will be deployed. In this situation your orchestration tool would run into port conflicts or fail to start the applications due to unavailability of ports.

To run your microservice applications on random port you can just put server.port=0 in application.properties file or below content in the application.yaml/yml file in the class path folder src/main/resources.

server:
   port:0

But when you see on your Eureka server’s portal, you will see that port 0 is assigned to all of your services as shown in the following image:

run spring cloud microservices on random ports

In the above image, when you click on the link then your link will open in the browser and there you will find the actual port for your services. So, it does not actually start on port 0, it starts on a random port.

Even you can find the port number without clicking on the link but hover your mouse cursor over the link then you will find the port number as shown in the below image:

run spring cloud microservices on random ports

As you see in the above image the actual port number for alert microservice is 56852.

Finally when you access your microservices APIs then you will see your three different microservices run on three different ports:

run spring cloud microservices on random ports

This is a disadvantage also while you are setting a random port for microservices. It will be difficult to access services, since these services will always start on random ports after restarting. So you can use Spring Cloud Gateway to address this issue.

Another way to start microservices on random ports is by extending WebServerFactoryCustomizer class. This way Eureka will not display ports 0 for microservices and it will allow Eureka (Service Discovery) to configure itself with random ports.

Therefore you can include the following class for each of the microservices:

@Configuration
public class ServerPortsRangeConfig implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {

	@Value("${port.number.min:7000}")
	private Integer minPort;

	@Value("${port.number.max:9000}")
	private Integer maxPort;

	@Override
	public void customize(ConfigurableServletWebServerFactory factory) {
		int port = SocketUtils.findAvailableTcpPort(minPort, maxPort);
		factory.setPort(port);
		System.getProperties().put("server.port", port);
	}

}

Now when you run each of the microservices, you will see that microservices have been started on different ports within the range of ports.

That’s all about how to start microservices on random ports.

Source Code

Download

Leave a Reply

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