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):
The interface of Eureka Server will now look like this:
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:
Result:
I will configure to register this service with Eureka Server first:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.huongdanjava.springcloud.ribbon; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class SpringCloudRibbonApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudRibbonApplication.class, args); } } |
1 2 3 4 5 |
spring.application.name=Ribbon Example eureka.client.service-url.default-zone=http://localhost:8761 server.port=8082 |
Result:
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.huongdanjava.springcloud.ribbon; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class RibbonController { @GetMapping("/load-balancing") String loadBalancing() { String serviceInstanceUrl = null; return serviceInstanceUrl; } } |
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:
1 2 |
@Autowired private LoadBalancerClient loadBalancerClient; |
Then use this LoadBalancerClient object to request the instance of the service “Eureka Client Example”, as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com.huongdanjava.springcloud.ribbon; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class RibbonController { @Autowired private LoadBalancerClient loadBalancerClient; @GetMapping("/load-balancing") String loadBalancing() { ServiceInstance serviceInstance = loadBalancerClient.choose("Eureka Client Example"); return serviceInstance.getUri().toString(); } } |
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. :
And
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!