by BehindJava

How to scale up a Microservice into multiple instances to check Load Balancer

Home » microservices » How to scale up a Microservice into multiple instances to check Load Balancer

In the tutorial we are going to learn about configuring multiple instances of a microservices and see how load balancer works.

In the last tutorial we have learnt about configuring the manual routes in UserMicroservie application. Now we will scale up the UserMicroservie into multiple instances by adding the following properties under src/main/resources in the of UserMicroservie.

Sample Code Snippet:


To scale up the UserMicroservie we can simply run the same application again and it will not throw any exception or error because we have configured server.port as “0”. But we see only one instance registered in the Eureka server but port number changes when you run the same UserMicroservie for the second time.

To overcome this, we have added eurka.instance.instance-id into our of UserMicroservie application so that Eureka can register multiple instances as shown above.

Random Configuration Property Values

We can produce random values in our configuration file. We can get a random value by using ${random.} as a value for a configuration property. At the moment, only String, Integer, Long or UUID types are supported. You can optionally add a range filter at the end e.g.: ${[,]}

Now start the Eureka server followed by UserMicroservie twice where you can find two instances running up in the Eureka server as shown below and like wise we can scale up our UserMicroservie instances up to N.


Now we will test the Spring Cloud API Gateway load balancer by adding the below code snippet in our controller.

Sample Code Snippet:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

public class UserRestController {

	private Environment env;
	public String status() {
		return "UserMicroService  is Running on Port No:" + env.getProperty("local.server.port");	}

Open post man and hit the HTTP GET request and it responds back with the port no as shown in the below image. Since two instances of UserMicroservie are running up we can see change in the port no to first instance and second instance and vice versa where the traffic of incoming requests in balanced equally between the two instances registered in Eureka server.