Spring Cloud study notes [load balancing-Ribbon]

What is Spring Cloud Ribbon

Spring Cloud Ribbon is a Netflix Ribbon-based client-side load balancer, which is part of the Spring Cloud ecosystem to help developers build distributed systems with high availability and resilience.

Ribbon can distribute load among multiple service instances to improve application availability and performance. When using Ribbon, developers can define a list of available service instances and use a load balancing algorithm to select an instance from this list to handle client requests. In addition, Ribbon also provides some other functions, such as service instance health check and failover mechanism, to ensure that when a service instance fails, it can quickly switch to other available instances.

Spring Cloud Ribbon also integrates the Eureka service discovery component, which can obtain a list of available service instances from the Eureka registry. This makes it easier for developers to build microservices-based applications and use client-side load balancing and service discovery in the applications.

What is LB (Load Balancing)

LB (Load Balancing) is a technology that evenly distributes client requests to multiple servers or computer clusters to improve system performance, availability and scalability. In a load balancing system, a load balancer receives client requests and forwards the requests to available servers or computer nodes to balance server loads and improve system performance.

What is the difference between Ribbon local load balancing client VS Nginx server load balancing?

Ribbon local load balancing client and Nginx server load balancing are two different load balancing technologies, and there are the following differences between them:

1. The location is different: the Ribbon local load balancing client runs on the client side, while the Nginx server load balancing runs on the server side.

2. Different load balancing strategies: the Ribbon local load balancing client adopts the client load balancing strategy, and assigns client requests to different service instances according to a certain load balancing algorithm. Nginx server-side load balancing adopts a server-side load balancing strategy to forward client requests to the Nginx server first, and then the Nginx server distributes the requests to different back-end servers.

3. Different functions: the Ribbon local load balancing client can implement functions such as service discovery, load balancing, and failover under the microservice architecture, while the Nginx server load balancing can implement functions such as reverse proxy, SSL termination, and caching.

4. The scope of application is different: the Ribbon local load balancing client is suitable for Java applications and service calls in the Spring Cloud microservice architecture, while the Nginx server load balancing is suitable for any application, including web applications, databases, DNS, mail and other services.

Ribbon Architecture Workflow

The workflow of the Ribbon load balancing architecture is as follows:

1. The service consumer obtains a list of available services from the service registry.

2. The service consumer uses the Ribbon client load balancer to select a service instance from the available service list according to a certain load balancing strategy.

3. The service consumer initiates a request to the selected service instance.

4. If the selected service instance fails, the Ribbon client load balancer will automatically switch to other available service instances to ensure system availability and stability.
insert image description here

Building the Ribbon Demo

IRule rule

insert image description here

Built-in load balancing rule class Rule description
RoundRobinRule Simply poll the list of services to select a server. It is the default load balancing rule of Ribbon.
AvailabilityFilteringRule First filter out faulty instances, and then select instances with less concurrency
WeightedResponseTimeRule Assign a weight value to each server. The longer the server response time, the less weight this server has. This rule will randomly select a server, and this weight value will affect the server selection.
ZoneAvoidanceRule Server selection is based on the servers available in the region. Use Zone to classify servers. This Zone can be understood as a computer room, a rack, etc. Then poll multiple services in the Zone.
BestAvailableRule Ignore servers that are short-circuited and choose servers with lower concurrency.
RandomRule Randomly select an available server.
RetryRule Selection logic for the retry mechanism

The principle of Ribbon load balancing polling algorithm

The polling algorithm principle of Ribbon load balancing is as follows:

When the client makes a request to the server, the Ribbon first obtains the service instance list; the
Ribbon maintains a counter, and increments the counter by 1 for each request;
the Ribbon distributes the request to each instance in the service instance list in a round-robin manner, and distributes them cyclically in turn;
If a service instance cannot provide services due to failure or network reasons, Ribbon will remove the instance from the service list;
if all instances in the service instance list are unavailable, an error message will be returned.

Configure a custom IRule

The book is continued from the previous chapter and continues to be modified on the previous demo.
Modify the auth project.
The official document clearly gives a warning:
这个自定义配置类不能放在@ComponentScan所扫描的当前包下以及子包下,否则我们自定义的这个配置类就会被所有的Ribbon客户端所共享,达不到特殊化定制的目的了。
so we need to distinguish it from the path springcloud
insert image description here

Create a new MyRuleConfig configuration class

@Configuration
public class MyRuleConfig{
    
    
    @Bean
    public IRule myRule() {
    
    
        // 定义为随机
        return new RandomRule();
    }
}

Add @RibbonClient to the startup classinsert image description here

@RibbonClient(name = "LF-USER", configuration = MyRuleConfig.class)

test

insert image description here
If the access is successful and it is called multiple times, it will be found that the port is random each time. no longer poll

Ribbon hunger loading

In Ribbon, eager-loading is a preloading mechanism that reduces the delay time of the first request by obtaining the list of service instances in advance when the application starts. By default, Ribbon will only get the service instance list when the first request arrives, which will cause a high delay for the first request. To solve this problem, Ribbon provides a hunger loading mechanism, which can pre-obtain a list of service instances when the application starts to speed up the response time of the first request.

By enabling starvation loading when the application starts, Ribbon will obtain the list of services from the service registry and cache them in local memory. In this way, when the first request arrives, Ribbon can directly obtain the service instance list from the local cache without having to request the registration center, thereby reducing the delay of the first request.

Hunger loading can be enabled or disabled by configuring ribbon.eager-load.enabled, the default value is false. If set to true, the starvation loading mechanism is enabled, and Ribbon will pre-obtain the list of service instances when the application starts; if set to false, the starvation loading mechanism is disabled, and Ribbon will obtain the list of service instances when the first request arrives.
For example:

ribbon:
  eager-load:
    enabled: true

Guess you like

Origin blog.csdn.net/qq_33129875/article/details/129582465