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.

Types of Load Balancing: There are two types of load balancing

  • Server Side Load Balancing: Server side load balancing is a monolithic It applies between the client and the server. It accepts incoming network, application traffic, and distributes the traffic across the multiple backend servers by using various methods. The middle component is responsible for distributing the client requests to the server.
  • Client-Side Load Balancing: The client holds the list of server’s IPs so that it can deliver the requests. The client selects an IP from the list, randomly, and forwards the request to the server.

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

Sample Code Snippet:

server.port=0
spring.application.name=user-ms1
eureka.client.service-url.defaultZone=http://localhost:8010/eureka
spring.devtools.restart.enabled=true
eureka.instance.instance-id=${spring.application.name}:${spring.application.instance_id:${random.int}}

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 application.properties 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.: ${random.int[,]}

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.

scale

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;

@RestController
@RequestMapping("/users")
public class UserRestController {

	@Autowired
	private Environment env;
	@GetMapping("/status/check")
	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.

first

second