Spring cloud uses Ribbon to achieve client load balancing

Keywords: Programming Spring


Spring cloud uses Ribbon to achieve client load balancing


In Spring cloud, when multiple services of the same type start to register in the service registry, the frequency service is the cluster When the consumer (client) consumes, it needs to select and call the service.

Service registration click

The default call in Spring cloud relies on eureka's ribbon to implement the client load balancing strategy

Let's register two services first

Then, it uses ribbon to realize the load balancing strategy

ribbon dependence

ribbon relies on eureka for load balancing, so we do not need to import packages. By default, it exists in eureka dependency

Load balancing

Just add @ LoadBalanced annotation on it

public class EurekaclientApplication {

    //Turn on load balancing
    public RestTemplate restTemplate(){
        return new RestTemplate();

    public static void main(String[] args) {
        SpringApplication.run(EurekaclientApplication.class, args);


Consumption call

public class GetUserController {

    RestTemplate restTemplate;

    //Service discovery object
    DiscoveryClient discoveryClient;

    public User getUser(int id){
        //The name of the user microservice is used to send requests to this service
        String  servceId = "user-server";

        String url = "http://" + servceId  + "/getUser?id=" + id;
        System.out.println(url +  " =  " +  url);
        User forObject = restTemplate.getForObject(url, User.class);



Print the url and port of each call

    LoadBalancerClient loadBalancerClient;

        for (int i = 0; i < 100; i++) {

            ServiceInstance choose = loadBalancerClient.choose("user-server");
            System.out.println("The first " + (i+1) + "Secondary execution " +  choose.getPort() + choose.getUri());
        return forObject;

give the result as follows

We can see that each time it is a round robin call, and then a round robin call request (2 services in total).

Default policy

There is no configuration policy specified in ribbon auto configuration to implement rotation training policy by default

Set random policy

Set load balancing as random strategy

serviceId.ribbon.NFLoadBalancerRuleClassName = custom load balancing policy class

Configure in configuration file

user-server: # This configuration is the requested service name
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

Or by hard coding

public class RibbonConfiguration {
    public IRule ribbonRule() {
        // Load balancing rule, change to random
        return new RandomRule();

By default, the configuration file is larger than hard coding

test result

Random access per request

Custom policy

By default, ribbon implements load balancing strategy through IRule interface, and by default, it implements load rotation training through zoneaavoidancerule

  • Structural drawing

In addition, the self-contained load balancing strategy

Realizing Ribbon load balancing without relying on eureka

ribbon can realize load balancing without eureka as follows

#Cancel the use of Eureka by Ribbon

#Configure the microservice nodes that the Ribbon can access. Multiple nodes are separated by commas

Problems that may come up in the end

May report an error

When the spring cloud ribbon implements load balancing, it prompts the Request URI does not contain a valid hostname: http://PRODUCT_SERVICE/

The reason is that the service name cannot be underlined

Code address Portal

Posted by CreativeWebDsign on Wed, 18 Mar 2020 05:08:16 -0700