Traditional server side load balancer has some drawbacks. It requires an additional hop from client to load balancer and then load balancer to service. Also there is a burden to run and manage load balancer itself. Client side load balancer is the solution to overcome these problems. In this architecture there is an addition inbuilt load balancer component that resides inside client.
In this example we will see how client side load balancer works with eureka discovery server.
1. When service (discovery client) boots up it registers itself with discovery server. Multiple instances register itself with discovery server. Here spring.application.name is use to identify a particular service
2. Client application will also resister itself to discovery client to find instance for load banacer
3. Now load balancer within client can find the service with the help of service name
Please find complete codes from github repository -
Discovery server code -
Ribbon service code -
Ribbon client code -
Step 1 - Start eureka discovery server, which will run on port 8761
Step 2 - Start 2 instances of ribbon service on port 4444, 5555. You can do that by passing server.port argument to start the services as shown below
Verify 2 service are running in eureka
Step 3 - Run Ribbon client which will run on default 8080 port
RestTemplate bean is created using @LoadBalanced annotation
Ribbon service is discovered using service name