Detailed explanation and practice of Robbin load balancing---SpringCloud components (3)

1Why use Robbin?

In the Eureka detailed explanation and practice - Spring Cloud component (2) case, we started a feign-provider-modules, and then called the feign interface through feign-consumer-8080 to access it.
But in actual environments, we often open many clusters of feign-provider-modules. At this time, there will be multiple services in the list of services we obtain. Which one should we access?
Generally, in this case, we need to write a load balancing algorithm and select from multiple instance lists.

2. Robbin Concept

Insert image description here

Ribbon is an in-process LB. It is just a class library integrated into the consumer process. The consumer uses it to obtain the address of the service provider.
So we only need to configure the load balancing policy on the consumer side.

In-process LB: Integrate LB into the consumer. The consumer learns which addresses are available from the service registration center, and then selects a suitable server from the available addresses.

Three load balancing practices

The load balancing in the case is implemented based on openfeign+eureka.
After the introduction of spring-cloud-starter-openfeign, there is no need to introduce additional dependencies when using Ribbon as a client load balancer, because Ribbon is integrated into the introduced spring-cloud-starter-openfeign dependency.

Implementation steps:

1. Start the eureka client
2. Start multiple provider services and register with eureka
3. Configure load balancing parameters on the consumer side

1. Start the eureka client

Insert image description here

2. Start multiple provider services and register with eureka

  1. In order to observe the load balancing effect, modify the getHandle method in the feign-provider-modules service.

    Insert image description here

  2. Start multiple provider instance projects

    Insert image description here
    Insert image description here

  3. Observing the eureka console, we found that 3 providers were successfully registered
    http://127.0.0.1:10086/
    Insert image description here

3. Configure load balancing parameters on the consumer side

feign-provider: # 服务名
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule  # 选择负载均衡策略,默认为轮询方式,当前配置为随机方式
    ConnectTimeout: 250                 # 连接超时时间
    ReadTimeout: 1000                   # ribbon 读取超时时间
    OkToRetryOnAllOperations: true      # 是否对所有操作都进行重试
    MaxAutoRetriesNextServer: 1         # 切换实例的重试次数
    MaxAutoRetries: 1                   # 对当前实例的重试次数

The observed results for multiple visits are random
http://localhost:8080/consumer/depart/get/2
Insert image description here

Four Robbin Source Code Analysis

Guess you like

Origin blog.csdn.net/weixin_43811057/article/details/130623251