Spring Cloud- Netflix Eureka + Ribbon Simple Example(client side load balancing)

In this post we make use of Netflix Ribbon for Client Side Load Balancing. In previous posts we made use of Netflix component Eureka for service registry and discovery.

Part 1 - Overview of Netflix components.

Part 2 - Develop an Student service to produce and consume REST API using Spring Boot

Part 3 - Use Eureka for Service Registration

Part 4 - Use Eureka for Service Discovery

THIS - Load balancing using Netflix Eureka + Ribbon

What is Load Balancing?

In computing, load balancing refers to the process of distributing a set of tasks over a set of resources, with the aim of making their overall processing more efficient. Load balancing aims to optimize resource use, maximize throughput, minimize response time, and avoid overload of any single resource. Using multiple components with load balancing instead of a single component may increase reliability and availability through redundancy.

What is Netflix Ribbon?

Netflix Ribbon is an Inter Process Communication (IPC) cloud library. Ribbon primarily provides client-side load balancing algorithms.

Apart from the client-side load balancing algorithms, Ribbon provides also other features:

  1. Service Discovery Integration
  2. Fault Tolerance
  3. Configurable load-balancing rules

In previous posts we developed two services student-producer and student-consumer. Suppose other modules are also calling and consuming student-producer module services. So the load at student-producer is high. To deal with this we this we deploy multiple instances of student-producer. Suppose two in this case. Now we will have to use a Load Balancer to route any incoming requests to either one of these two services.


Let’s begin-

Start the Eureka server module we developed in the previous tutorial. We will now be modifying the student-producer and student-consumer modules we developed previously.

Code changes for student producer-

In the application.properties add the instance id as follows:


We need to start the student-producer instance twice. So start the student-producer instance the first time. It will start on the default port 8080. Next in application.properties add the port as 8081 and start the student-producer again.


So now we have two instances of student producer running, one on port 8080 and the other on 8081. If we go to eureka server url http://localhost:8090/ we can see both instances of student-producer registered.


Code changes for student-consumer module

Modify the pom.xml to include the spring cloud ribbon starter dependency.


Next modify the ConsumerControllerClient code. Previously we were autowiring an DiscoveryClient bean, replace it with LoadBalancerClient.

public class ConsumerControllerClient {

    private LoadBalancerClient loadBalancer;

    @RequestMapping(value = "/student/consumer", method = RequestMethod.GET)
    public String getStudent() throws RestClientException, IOException {

        ServiceInstance serviceInstance=loadBalancer.choose("student-producer");

        System.out.println("Instance: " +serviceInstance.getUri());

        String baseUrl=serviceInstance.getUri().toString();


        System.out.println("Base URL: " +baseUrl);
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> response=null;
                HttpMethod.GET, getHeaders(),String.class);
        }catch (Exception ex)

        return ("connected to:" +baseUrl);

    private static HttpEntity<?> getHeaders() throws IOException {
        HttpHeaders headers = new HttpHeaders();
        headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
        return new HttpEntity<>(headers);

The load balancer will now call either of instances of student-producer service depending on its internal algorithm to perform load balancing. Start the student-consumer module.



So whatever calls are made using Ribbon through the ConsumerControllerClient service, are distributed among these three services. From the result, we can understand that the API response is receiving from different servers by identifying port change.

Read More