2 ribbon client load balancing

I Client load balancer Ribbon

1. what is server-side load balancing

In the previous study of nacos, it was also said that server load balancing and client load balancing In fact, our commonly used server-side load balancing is nginx


Maintain a list of available service instances in load balancing. When a client request comes, the load balancing server selects one of the available service instances from the list to process the client request according to a configured rule (load balancing algorithm). This is service-side load balancing, such as nginx Load balancing is performed through nginx. The client sends the request value nginx. Nginx selects one of multiple servers for access through the load balancing algorithm The



2. what is client load balancing

Next, the ribbon we want to talk about belongs to client load balancing. The ribbon client will have a list of service instance addresses. Before sending a request, select a service instance through the load balancing algorithm, and then access it. This is client load balancing That is, the load balancing algorithm is allocated at the client

3. usage of client load balancing

First, let's take a look at the effect of ribbon load balancing

Step 1: add dependency

We copied an order project and added ribbon dependencies to it


Step 2: add annotation: @LoadBalance

Here, when instantiating RestTemplate, add an annotation @LoadBalanced The

When we studied zuul, we didn't seem to have configured this annotation. Why? Because feign is used when calling in zuul, this load balancing is implemented in feign

Step 3: call the product service in the order service using the restTemplate method

package com.lxl.www.gateway.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

public class UserController {
    private DiscoveryClient discoveryClient;
    private RestTemplate restTemplate;

    public String getUser() {
        String forObject = restTemplate.getForObject("http://product/config", String.class);
        return forObject;

Step 4: verify the effect

Start one order service console - the ribbon dependency configuration is added, and the port number is 8080

Start three product services with port numbers of 8081, 8082 and 8083


product details


Request service


We see that it is called 3 times, and each instance is called 1 time

II Configure ribbon load balancing policy

We have seen the basic usage of ribbon and know its basic implementation principle. Let's take a look at the basic configuration of ribbon


1. default load balancing policy of ribbon


ribbon's own debt balance strategies include six

  • RandomRule: is a random policy
  • RoundRobinRule: polling policy
  • RetryRule: retry policy
  • ZoneAvoidanceRule: intra region polling policy (default policy) Here, there is a zone on Amazon server, but there is no such concept on Alibaba cloud, so you can ignore the zone. Then this strategy is equivalent to polling
  • PredicateBaseRule: 
  • WeightedResponseTimeRule: weight response policy

Finally, let's take a look at the specific implementation of these strategies

2. fine grained custom configuration of ribbon - 36

Set the load balancing policy used in the configuration file

# Common to all instances
  NFLoadBalanceRuleClassName: com.netflex.loadbalance.RandomRule

#Specify service instance usage
    NFLoadBalanceRuleClassName: com.netflex.loadbalance.RandomRule

    NFLoadBalanceRuleClassName: com.netflex.loadbalance.RandomRobinRule


2. ribbon configuration



ReadTimeout: 1000
ConnectTimeout: 1000
MaxAutoRetries: 1
MaxAutoRetriesNextServer: 2
OkToRetryOnAllOperations: false
enabled: true

What does the MaxAutoRetries parameter mean? The

Number of retries per server, Press once without first call

This means that if the service call fails, Then retry 1 time

What does MaxAutoRetriesNextServer mean here?

Number of retry servers, Does not include the instance called for the first time

Meaning yes: If server 1 is selected for load balancing, Result call failed, Retry or fail. Then you can choose to retry other servers except this one. How many can I try again? Can retry two

What does OkToRetryOnAllOperations mean here?

This is right http Method to set retry, Can be set to true perhaps false

If set to true: Means post,put And so on. The problem with this is, Duplicate submission. To avoid this happening, To ensure idempotency of services

What is idempotency of a service? The

You can see from this article: https://www.cnblogs.com/QG-whz/p/10372458.html


3. Customize Ribbon load balancing

3.1 load balancing by weight -- 66 points

The weight of the server can be configured in nacos







Start service

Set the weight of the server with the nacos service instance




3.2 the principle of realizing the priority call of the same cluster --76

Try to avoid cross cluster calls

For example, Nanjing cluster's product calls Nanjing cluster's order first The product of Beijing cluster calls the order of Beijing cluster first


Realize the functions shown in the above figure



First determine the cluster name








3.3 Canary release



Canary publishing, also known as gray publishing, what does it mean?

First, we have 100 product service instances and 100 order service instances

Now they are all on the v1 version

Then a new function with great changes is developed, which needs to carry out gray scale test

Five were released on the product center and five on the order center

Now the user traffic is coming. If the requested traffic is the v1 version of the product center, all users should go through the v1 version, and the requested order center should also be the v1 version

If the incoming user requests v2 version traffic, then both product and order go through v2 version

The functions we want to implement are described as follows:



1. the same cluster and the same version are called first

2. if there is no service provider in the same cluster, cross cluster and same version calls should be made

3. calls between different versions are not allowed









































Tags: ribbon

Posted by dellwoodbu on Mon, 30 May 2022 12:17:56 +0530