spring cloud中Ribbon自定义负载均衡策略

一、Ribbon中的负载均衡策略

1、Ribbon中支持的负载均衡策略

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

RandomRule:随机选择一个server

BestAvailabl:选择一个最小的并发请求的server,逐个考察Server,如果Server被tripped了,则忽略

RoundRobinRule:roundRobin方式轮询选择, 轮询index,选择index对应位置的server

WeightedResponseTimeRule:根据响应时间分配一个weight(权重),响应时间越长,weight越小,被选中的可能性越低

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

ZoneAvoidanceRule:复合判断server所在区域的性能和server的可用性选择server

ResponseTimeWeightedRule:作用同WeightedResponseTimeRule,二者作用是一样的,ResponseTimeWeightedRule后来改名为WeightedResponseTimeRule

二、验证

1、自定义负载均衡策略

  1. # 自定义负载均衡策略
  2. springboot-h2.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule // 自定义使用随机策略,springboot-h2是服务应用名
2、修改调用代码

  1. package com.chhliu.springboot.restful.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.cloud.client.ServiceInstance;
  4. import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.PathVariable;
  7. import org.springframework.web.bind.annotation.RestController;
  8. import org.springframework.web.client.RestTemplate;
  9. import com.chhliu.springboot.restful.vo.User;
  10. @RestController
  11. public class RestTemplateController {
  12. @Autowired
  13. private RestTemplate restTemplate;
  14. @Autowired
  15. private LoadBalancerClient loadBalancerClient;
  16. @GetMapping( "/template/{id}")
  17. public User findById(@PathVariable Long id) {
  18. ServiceInstance serviceInstance = this.loadBalancerClient.choose( "springboot-h2");
  19. System.out.println( "===" + ":" + serviceInstance.getServiceId() + ":" + serviceInstance.getHost() + ":"
  20. + serviceInstance.getPort()); // 打印当前调用服务的信息
  21. User u = this.restTemplate.getForObject( "http://springboot-h2/user/" + id, User.class);
  22. System.out.println(u);
  23. return u;
  24. }
  25. }
3、测试

服务调用关系如下:


测试结果如下:

  1. ===:springboot-h2: 127.0.0.1: 7902
  2. User [id= 2, username=user2, name=李四, age= 20, balance= 100.00]
  3. ===:springboot-h2: 127.0.0.1: 7901
  4. User [id= 2, username=user2, name=李四, age= 20, balance= 100.00]
  5. ===:springboot-h2: 127.0.0.1: 7902
  6. User [id= 2, username=user2, name=李四, age= 20, balance= 100.00]
  7. ===:springboot-h2: 127.0.0.1: 7901
  8. User [id= 2, username=user2, name=李四, age= 20, balance= 100.00]
  9. ===:springboot-h2: 127.0.0.1: 7902
  10. User [id= 2, username=user2, name=李四, age= 20, balance= 100.00]
  11. ===:springboot-h2: 127.0.0.1: 7902
  12. User [id= 2, username=user2, name=李四, age= 20, balance= 100.00]
  13. ===:springboot-h2: 127.0.0.1: 7902
  14. User [id= 2, username=user2, name=李四, age= 20, balance= 100.00]
  15. ===:springboot-h2: 127.0.0.1: 7902
  16. User [id= 2, username=user2, name=李四, age= 20, balance= 100.00]
  17. ===:springboot-h2: 127.0.0.1: 7902
  18. User [id= 2, username=user2, name=李四, age= 20, balance= 100.00]
  19. ===:springboot-h2: 127.0.0.1: 7901
  20. User [id= 2, username=user2, name=李四, age= 20, balance= 100.00]
  21. ===:springboot-h2: 127.0.0.1: 7901
  22. User [id= 2, username=user2, name=李四, age= 20, balance= 100.00]
  23. ===:springboot-h2: 127.0.0.1: 7902
  24. User [id= 2, username=user2, name=李四, age= 20, balance= 100.00]
  25. ===:springboot-h2: 127.0.0.1: 7902
  26. User [id= 2, username=user2, name=李四, age= 20, balance= 100.00]
  27. ===:springboot-h2: 127.0.0.1: 7901
  28. User [id= 2, username=user2, name=李四, age= 20, balance= 100.00]
  29. ===:springboot-h2: 127.0.0.1: 7901
  30. User [id= 2, username=user2, name=李四, age= 20, balance= 100.00]
发现选择7901端口服务和7902端口服务确实是随机的!

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/liuchuanhong1/article/details/54693124

一、Ribbon中的负载均衡策略

1、Ribbon中支持的负载均衡策略

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

RandomRule:随机选择一个server

BestAvailabl:选择一个最小的并发请求的server,逐个考察Server,如果Server被tripped了,则忽略

RoundRobinRule:roundRobin方式轮询选择, 轮询index,选择index对应位置的server

WeightedResponseTimeRule:根据响应时间分配一个weight(权重),响应时间越长,weight越小,被选中的可能性越低

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

ZoneAvoidanceRule:复合判断server所在区域的性能和server的可用性选择server

ResponseTimeWeightedRule:作用同WeightedResponseTimeRule,二者作用是一样的,ResponseTimeWeightedRule后来改名为WeightedResponseTimeRule

二、验证

1、自定义负载均衡策略

  1. # 自定义负载均衡策略
  2. springboot-h2.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule // 自定义使用随机策略,springboot-h2是服务应用名
2、修改调用代码

  1. package com.chhliu.springboot.restful.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.cloud.client.ServiceInstance;
  4. import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.PathVariable;
  7. import org.springframework.web.bind.annotation.RestController;
  8. import org.springframework.web.client.RestTemplate;
  9. import com.chhliu.springboot.restful.vo.User;
  10. @RestController
  11. public class RestTemplateController {
  12. @Autowired
  13. private RestTemplate restTemplate;
  14. @Autowired
  15. private LoadBalancerClient loadBalancerClient;
  16. @GetMapping( "/template/{id}")
  17. public User findById(@PathVariable Long id) {
  18. ServiceInstance serviceInstance = this.loadBalancerClient.choose( "springboot-h2");
  19. System.out.println( "===" + ":" + serviceInstance.getServiceId() + ":" + serviceInstance.getHost() + ":"
  20. + serviceInstance.getPort()); // 打印当前调用服务的信息
  21. User u = this.restTemplate.getForObject( "http://springboot-h2/user/" + id, User.class);
  22. System.out.println(u);
  23. return u;
  24. }
  25. }
3、测试

服务调用关系如下:


测试结果如下:

  1. ===:springboot-h2: 127.0.0.1: 7902
  2. User [id= 2, username=user2, name=李四, age= 20, balance= 100.00]
  3. ===:springboot-h2: 127.0.0.1: 7901
  4. User [id= 2, username=user2, name=李四, age= 20, balance= 100.00]
  5. ===:springboot-h2: 127.0.0.1: 7902
  6. User [id= 2, username=user2, name=李四, age= 20, balance= 100.00]
  7. ===:springboot-h2: 127.0.0.1: 7901
  8. User [id= 2, username=user2, name=李四, age= 20, balance= 100.00]
  9. ===:springboot-h2: 127.0.0.1: 7902
  10. User [id= 2, username=user2, name=李四, age= 20, balance= 100.00]
  11. ===:springboot-h2: 127.0.0.1: 7902
  12. User [id= 2, username=user2, name=李四, age= 20, balance= 100.00]
  13. ===:springboot-h2: 127.0.0.1: 7902
  14. User [id= 2, username=user2, name=李四, age= 20, balance= 100.00]
  15. ===:springboot-h2: 127.0.0.1: 7902
  16. User [id= 2, username=user2, name=李四, age= 20, balance= 100.00]
  17. ===:springboot-h2: 127.0.0.1: 7902
  18. User [id= 2, username=user2, name=李四, age= 20, balance= 100.00]
  19. ===:springboot-h2: 127.0.0.1: 7901
  20. User [id= 2, username=user2, name=李四, age= 20, balance= 100.00]
  21. ===:springboot-h2: 127.0.0.1: 7901
  22. User [id= 2, username=user2, name=李四, age= 20, balance= 100.00]
  23. ===:springboot-h2: 127.0.0.1: 7902
  24. User [id= 2, username=user2, name=李四, age= 20, balance= 100.00]
  25. ===:springboot-h2: 127.0.0.1: 7902
  26. User [id= 2, username=user2, name=李四, age= 20, balance= 100.00]
  27. ===:springboot-h2: 127.0.0.1: 7901
  28. User [id= 2, username=user2, name=李四, age= 20, balance= 100.00]
  29. ===:springboot-h2: 127.0.0.1: 7901
  30. User [id= 2, username=user2, name=李四, age= 20, balance= 100.00]
发现选择7901端口服务和7902端口服务确实是随机的!

猜你喜欢

转载自blog.csdn.net/aa1358075776/article/details/80929626