In-depth understanding of client load balancing Ribbon of spring cloud

1. Load balancing

Load Balance: Based on the existing network structure, it provides a cheap, effective and transparent method to expand the bandwidth of network devices and servers, increase throughput, strengthen network data processing capabilities, improve network flexibility and availability. It means that it is allocated to multiple operation units for execution, such as Web server, FTP server, enterprise key application server and other key mission servers, etc., so as to complete work tasks together.

1. Server-side load balancing: the client requests to the load balancing server, the load balancing server forwards the request to a server that actually provides services according to its own algorithm, the server sends the response data to the load balancing server, and the load balancing server finally sends the request. The data is returned to the client. (nginx)

2. Client-side load balancing: Client-based load balancing, in short, is to set a scheduling algorithm in the client program. When a request is made to the server, the scheduling algorithm is first executed to calculate which server to send the request to. Then make a request to the server.

Features of client-based load balancing:

It is implemented by the client's internal program and does not require additional load balancer software and hardware investment.

The program needs to solve the problem of unavailability of the business server, and the server failure has little transparency to the application.

Internally, the program needs to solve the problem of overloading the business server.

2. Ribbon realizes the load balancing of the client

We use spring boot to test.

pom file:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
  < modelVersion >4.0.0</ modelVersion >
  < groupId >com.jalja.org</ groupId >
  < artifactId >spring-consumer-server-ribbon</ artifactId >
  < version >0.0.1-SNAPSHOT</ version >
  
   < parent >
     < groupId >org.springframework.boot</ groupId >
     < artifactId >spring-boot-starter-parent</ artifactId >
     < version >1.5.2.RELEASE</ version >
   </ parent >
   < properties >
     < project.build.sourceEncoding >UTF-8</ project.build.sourceEncoding >
     < project.reporting.outputEncoding >UTF-8</ project.reporting.outputEncoding >
     < java.version >1.8</ java.version >
   </ properties >
   < dependencyManagement >
     < dependencies >
       < dependency >
         < groupId >org.springframework.cloud</ groupId >
         < artifactId >spring-cloud-dependencies</ artifactId >
         < version >Camden.SR4</ version >
         < type >pom</ type >
         < scope >import</ scope >
       </ dependency >
     </ dependencies >
   </ dependencyManagement >
   < dependencies >
      < dependency >
       < groupId >org.springframework.cloud</ groupId >
       < artifactId >spring-cloud-starter-ribbon</ artifactId >
     </ dependency >
     < dependency >
       < groupId >org.springframework.boot</ groupId >
       < artifactId >spring-boot-starter-web</ artifactId >
     </ dependency >
   </ dependencies >
   
</ project >

application.yml

?
1
2
3
stores:
  ribbon:
   listOfServers: www.baidu.com,www.jalja.org,www.163.com

Ribbon's load balancing strategy

1. RoundRobinRule (polling mode)

public class RoundRobinRule extends AbstractLoadBalancerRule The roundRobin method polls and selects the server. Polls the index and selects the server corresponding to the index. This strategy is also the default strategy of the ribbon

SpringCloudRibbonApplication.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class SpringCloudRibbonApplication {
   public static void main(String[] args) {
     SpringApplication.run(SpringCloudRibbonApplication.class, args);
   }
   @Autowired
   private LoadBalancerClient loadBalancer;
   @RequestMapping(value="static")
   public String staticRibbon(){
      ServiceInstance instance = loadBalancer.choose("stores");
      URI storesUri = URI.create(String.format("http://%s:%s", instance.getHost(), instance.getPort()));
      System.out.println(storesUri);
     return "static";
   }
}
 
连续请求6次执行结果:
 

2、RandomRule(随机策略)

public class RandomRule extends AbstractLoadBalancerRule 随机选择一个server 在index上随机,选择index对应位置的server。

在配置文件application.yml加入

?
1
2
3
4
5
6
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
stores:
  ribbon:
   listOfServers: www.baidu.com,www.jalja.org,www.163.org
   #随机
   NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

在SpringCloudRibbonApplication.java 中加入

?
1
2
3
4
@Bean
   public IRule ribbonRule() {
     return new RandomRule();//这里配置策略,和配置文件对应
   }

执行6次的结果:

3、BestAvailableRule(并发量)

public class BestAvailableRule extends ClientConfigEnabledRoundRobinRule 选择一个最小的并发请求的server 逐个考察Server,如果Server被tripped了,则忽略,在选择其中ActiveRequestsCount最小的server

在配置文件application.yml加入

NFLoadBalancerRuleClassName: com.netflix.loadbalancer.BestAvailableRule

在SpringCloudRibbonApplication.java 中加入

?
1
2
3
4
@Bean
   public IRule ribbonRule() {
     return new BestAvailableRule();//这里配置策略,和配置文件对应
   }

执行6次的结果:

4、AvailabilityFilteringRule(服务器状态)

public class AvailabilityFilteringRule extends PredicateBasedRule 过滤掉那些因为一直连接失败的被标记为circuit tripped的后端server,并过滤掉那些高并发的的后端server(active connections 超过配置的阈值) 使用一个AvailabilityPredicate来包含过滤server的逻辑,其实就就是检查status里记录的各个server的运行状态

5、WeightedResponseTimeRule(根据响应时间)

public class WeightedResponseTimeRule extends RoundRobinRule 根据响应时间分配一个weight,相应时间越长,weight越小,被选中的可能性越低。 一个后台线程定期的从status里面读取评价响应时间,为每个server计算一个weight。Weight的计算也比较简单responsetime 减去每个server自己平均的responsetime是server的权重。当刚开始运行,没有形成statas时,使用roubine策略选择server。

6、RetryRule(根据策略+重试)

public class RetryRule extends AbstractLoadBalancerRule 对选定的负载均衡策略机上重试机制。
在一个配置时间段内当选择server不成功,则一直尝试使用subRule的方式选择一个可用的server

7、ZoneAvoidanceRule(Zone状态+服务状态)

public class ZoneAvoidanceRule extends PredicateBasedRule
复合判断server所在区域的性能和server的可用性选择server
使用ZoneAvoidancePredicate和AvailabilityPredicate来判断是否选择某个server,前一个判断判定一个zone的运行性能是否可用,剔除不可用的zone(的所有server),AvailabilityPredicate用于过滤掉连接数过多的Server。

 

http://www.jb51.net/article/115969.htm

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327101176&siteId=291194637