Load balancing using Ribbon and Eureka Server of Spring Cloud Netflix

It is very important to ensure that services are always available to handle requests from other users and services in a Microservice system, in order to help our application work stably and efficiently. One problem to ensure the above problem is that when services cannot handle additional requests from users, we need to create the replicates for that service so they can handle those requests. The creation of copies for the service is very easy, we only need to deploy those services on many different machines, but for the way that we can forward the request from the user between copies of service, how can we do that? One of the options you can use that is: using Ribbon with Eureka Server of Spring Cloud Netflix. How is it in details? Let’s find out in this tutorial together!

OK, to start learning about Ribbon, the first thing I need to tell you about Ribbon is: it is a load balancer client library. This means, if your service needs to call a service under load balancing mechanism, your service needs to know all instances of that service. Calling to which instance of that service depends on your service, depending on the priority of the instance you define. Ribbon supports us all the features of a load balancer client. We can use the Ribbon without Eureka Server, but if you have Eureka Server, managing the instances of service is much simpler.

As an example for this tutorial, I will use Eureka Server that I created using Netflix Spring Cloud in the previous tutorial about Eureka Server with a service I created in the tutorial about Eureka Client.

I will create multiple copies for service “Eureka Client Example” by running this service on many different ports (8080 and 8081):

Load balancing using Ribbon and Eureka Server of Spring Cloud Netflix

The interface of Eureka Server will now look like this:

Load balancing using Ribbon and Eureka Server of Spring Cloud Netflix

As you can see, here we have 2 instances of the service “Eureka Client Example” including: “khanh-macbook-pro:Eureka Client Example”, “khanh-macbook-pro:Eureka Client Example:8081”.

Now, I will create another service with Eureka Client, Web and Ribbon dependencies as follows:

Load balancing using Ribbon and Eureka Server of Spring Cloud Netflix

Result:

Load balancing using Ribbon and Eureka Server of Spring Cloud Netflix

I will configure to register this service with Eureka Server first:

Result:

Load balancing using Ribbon and Eureka Server of Spring Cloud Netflix

Now, we will use the Ribbon to load balancing to call the “Eureka Client Example” service from the “Ribbon Example” service!

To do this, I will create a new controller as an example. This controller’s initial content is as follows:

This controller is defining a request URL that allows us can call the service “Eureka Client Example”. For simplicity, after obtaining the “Eureka Client Example” service instance information will handle the request, I will return that information.

With the Ribbon, we will use the LoadBalancerClient interface to call an instance of the service “Eureka Client Example”. You can autowired LoadBalancerClient to the RibbonController controller as follows:

Then use this LoadBalancerClient object to request the instance of the service “Eureka Client Example”, as follows:

As you can see, here we will use the choose() method of LoadBalancerClient object with the parameter name of the service “Eureka Client Example” to get an instance of this service.

Now, if you run this example and then request the URL http://localhost:8082/load-balancing multiple times, you will see the instance handle request information of the service “Eureka Client Example” will change as follows. :

Load balancing using Ribbon and Eureka Server of Spring Cloud Netflix

And

Load balancing using Ribbon and Eureka Server of Spring Cloud Netflix

As you can see, the instance handle request for service “Eureka Client Example” will change, not permanently. Using which service instance to handle requests will be based on certain algorithms, we will learn more about them in the incoming tutorial!

Add Comment