Microservices load balancing using Spring Cloud Netflix Eureka Server ; Practical Example

We have two microservices: Employee-Service and Department-Service. When the Employee-Service is called, it retrieves employee information along with the department details associated with that employee. Internally, the Employee-Service communicates with the Department-Service to fetch the required department information for the respective employee.

To demonstrate load balancing, we will create two instances of the Department-Service.
  1. First Scenario: Both instances of the Department-Service will be running simultaneously. We will make requests to the Employee-Service using Postman and observe how the load is distributed between the two instances.
  2. Second Scenario: We will stop one instance of the Department-Service and make requests again using Postman. This will help us verify how the load balancer redirects traffic to the remaining active instance.
Used Technologies and Tools-
  • Spring boot 3.3.4
  • Spring Cloud Netflix Eureka Server
  • IntelliJ IDEA
  • MySQL
  • Postman
Step 1: Set the application name in the configuration file (Application.properties). Remember this name is registered in Service Registry(Netflix Eureka Server). 



Step 2: OpenFeign library used for communication among microservices. We created an OpenFeign client inside Employee-Service that makes a call to Department-Service and gets the department info of a particular employee.


Step 3: Employee-Service Call Department-Service to get department info of a employee.


Up to Step 3, everything is done. In the below steps, we just test the load balancing. We will create an extra .jar file of Department-Service and run that one also, so we will get two instances of Department-Service. One is from IDE, and another is from the .jar file. Then, we will test load balancing by stopping one instance.

Step 4: Create a .jar file of Department-Service by following below screenshot.


Step 5: Check in /target folder that .jar file created or not.

Step 6: Run the above .jar file in either IDE's Terminal or CMD in a different port (8093). Here we have run by CMD.
  • Open CMD : win + R (type cmd)
  • Go to the microservice directory (just before /target folder)
    • cd RMN\Projects\Java_Spring_Boot\Micro-services\Payroll\dept_service_py
    • D:   [This to channge the directory]
  • Finally run in 8093 port using below command
    • java -jar -Dserver.port=8093 target/dept_service_py-0.0.1-SNAPSHOT.jar


Step 7: Open Netflix Eureka server in the browser and the two instances created for Department-Service. Netflix Eureka server will automatically load balance between two instance of Department-Service. If any instance is down it will go for running instance.

Step 8: Now test from the Postman client by running both instances of Department-Service.
Successfully return the response with department info. Here is department info coming from Department-Service.

Step 9: Now stop one instance of Department-Service. Let's stop the IDE one that runs on port 8092.


IDE one (Department-Service) is down - 8092

Step 10: Now a single instance is available of  Department-Service, that running from .jar file in port 8093.


Step 11: Now again, check from the Postman client. It's getting the same response from server.



Successfully Load balanced. Done 👍




Post a Comment

0 Comments