android系统定制开发springcloud ribbon 配置负载均衡策略以及自定义策略

一、android系统定制开发系统内置的策略有以下几种。

 android系统定制开发这个负载策略配置说白了就是让 android系统定制开发这个客户端负载均衡器怎么进行访问服务提供者列表。是轮流访问?随机访问?权重?等。

Ribbon 的策略
策略类    命名    说明
RandomRule    随机策略    随机选择 Server
RoundRobinRule    轮训策略    按顺序循环选择 Server
RetryRule    重试策略    在一个配置时问段内当选择 Server 不成功,则一直尝试选择一个可用的 Server
BestAvailableRule    最低并发策略    逐个考察 Server,如果 Server 断路器打开,则忽略,再选择其中并发连接最低的 Server
AvailabilityFilteringRule    可用过滤策略    过滤掉一直连接失败并被标记为 circuit tripped 的 Server,过滤掉那些高并发连接的 Server(active connections 超过配置的网值)
ResponseTimeWeightedRule    响应时间加权策略    根据 Server 的响应时间分配权重。响应时间越长,权重越低,被选择到的概率就越低;响应时间越短,权重越高,被选择到的概率就越高。这个策略很贴切,综合了各种因素,如:网络、磁盘、IO等,这些因素直接影响着响应时间
ZoneAvoidanceRule    区域权衡策略    综合判断 Server 所在区域的性能和 Server 的可用性轮询选择 Server,并且判定一个 AWS Zone 的运行性能是否可用,剔除不可用的 Zone 中的所有 Server
 

二、示例

1.只要在启动时创建一个IRule对象,注入容器,即可。因为系统内置的容器需要在真正 发起请求时才会创建,并且判断系统容器中没有IRule对象。

 

  1. @SpringBootApplication
  2. @Slf4j
  3. @EnableDiscoveryClient
  4. @RestController
  5. public class ConsumeApplication {
  6. @Autowired
  7. RestTemplate restTemplate;
  8. @Bean
  9. @LoadBalanced
  10. public RestTemplate getRestTemplate(){
  11. HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory();
  12. clientHttpRequestFactory.setConnectTimeout(5 * 1000);
  13. clientHttpRequestFactory.setReadTimeout(5 * 1000);
  14. return new RestTemplate(clientHttpRequestFactory);
  15. }
  16. @Bean
  17. public IRule ribbonRule() {
  18. return new RandomRule();
  19. }
  20. public static void main(String[] args) {
  21. SpringApplication.run(ConsumeApplication.class, args);
  22. }
  23. @GetMapping("test")
  24. public String getContent(){
  25. log.info("发起请求");
  26. String ret = restTemplate.getForObject("http://provider/provider",String.class);
  27. return ret;
  28. }
  29. }

2.在创建ribbonLoadBalancer时就会使用最新的RULE

三、在发起调用时触发RULE的choose方法

 1.源码  ,这个很简单,就是随机取一个实例。

  1. package com.netflix.loadbalancer;
  2. import java.util.List;
  3. import java.util.Random;
  4. import com.netflix.client.config.IClientConfig;
  5. /**
  6. * A loadbalacing strategy that randomly distributes traffic amongst existing
  7. * servers.
  8. *
  9. * @author stonse
  10. *
  11. */
  12. public class RandomRule extends AbstractLoadBalancerRule {
  13. Random rand;
  14. public RandomRule() {
  15. rand = new Random();
  16. }
  17. /**
  18. * Randomly choose from all living servers
  19. */
  20. @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE")
  21. public Server choose(ILoadBalancer lb, Object key) {
  22. if (lb == null) {
  23. return null;
  24. }
  25. Server server = null;
  26. while (server == null) {
  27. if (Thread.interrupted()) {
  28. return null;
  29. }
  30. List<Server> upList = lb.getReachableServers();
  31. List<Server> allList = lb.getAllServers();
  32. int serverCount = allList.size();
  33. if (serverCount == 0) {
  34. /*
  35. * No servers. End regardless of pass, because subsequent passes
  36. * only get more restrictive.
  37. */
  38. return null;
  39. }
  40. int index = rand.nextInt(serverCount);
  41. server = upList.get(index);
  42. if (server == null) {
  43. /*
  44. * The only time this should happen is if the server list were
  45. * somehow trimmed. This is a transient condition. Retry after
  46. * yielding.
  47. */
  48. Thread.yield();
  49. continue;
  50. }
  51. if (server.isAlive()) {
  52. return (server);
  53. }
  54. // Shouldn't actually happen.. but must be transient or a bug.
  55. server = null;
  56. Thread.yield();
  57. }
  58. return server;
  59. }

四、可以针对单个服务自定义IRULE规则。

1.编写IRULE

  1. package com.net.sample.consume;
  2. import com.netflix.client.config.IClientConfig;
  3. import com.netflix.loadbalancer.AbstractLoadBalancerRule;
  4. import com.netflix.loadbalancer.ILoadBalancer;
  5. import com.netflix.loadbalancer.IRule;
  6. import com.netflix.loadbalancer.Server;
  7. import java.util.List;
  8. import java.util.Random;
  9. public class FirstRule extends AbstractLoadBalancerRule {
  10. public FirstRule() {
  11. }
  12. /**
  13. * Randomly choose from all living servers
  14. */
  15. public Server choose(ILoadBalancer lb, Object key) {
  16. if (lb == null) {
  17. return null;
  18. }
  19. Server server = null;
  20. while (server == null) {
  21. if (Thread.interrupted()) {
  22. return null;
  23. }
  24. List<Server> upList = lb.getReachableServers();
  25. List<Server> allList = lb.getAllServers();
  26. int serverCount = allList.size();
  27. if (serverCount == 0) {
  28. /*
  29. * No servers. End regardless of pass, because subsequent passes
  30. * only get more restrictive.
  31. */
  32. return null;
  33. }
  34. int index = 0;
  35. server = upList.get(index);
  36. if (server == null) {
  37. /*
  38. * The only time this should happen is if the server list were
  39. * somehow trimmed. This is a transient condition. Retry after
  40. * yielding.
  41. */
  42. Thread.yield();
  43. continue;
  44. }
  45. if (server.isAlive()) {
  46. return (server);
  47. }
  48. // Shouldn't actually happen.. but must be transient or a bug.
  49. server = null;
  50. Thread.yield();
  51. }
  52. return server;
  53. }
  54. @Override
  55. public Server choose(Object key) {
  56. return choose(getLoadBalancer(), key);
  57. }
  58. @Override
  59. public void initWithNiwsConfig(IClientConfig clientConfig) {
  60. // TODO Auto-generated method stub
  61. }
  62. }

2.编写CONFIGIGATION,这个配置类不在程序启动初始化时加载,而是在请求时,由RIBBON内部的SpringClientFactory的子applicationContext来创建和实例化。

  1. package com.net.sample.consume;
  2. import com.netflix.client.config.IClientConfig;
  3. import com.netflix.loadbalancer.IRule;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import javax.annotation.Resource;
  7. @Configuration
  8. /** 用来标记使用的注解,方便排除或者引用 **/
  9. @AvoidScan
  10. public class RibbonFirstLoadBalancingConfiguration {
  11. @Bean
  12. public IRule ribbonRule() {
  13. return new FirstRule();
  14. }
  15. }

3.启动类加上,启动时排除上面的配置的包。RibbonClient针对每个服务端进行单独的配置,就是configuation实例,由这个内部来创建IRULE,IRuleFilter,ServerList都可以自定义。

  1. @SpringBootApplication
  2. @Slf4j
  3. @EnableDiscoveryClient
  4. @RestController
  5. @ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION,value = AvoidScan.class))
  6. @RibbonClient(name = "provider",configuration = RibbonFirstLoadBalancingConfiguration.class)
  7. public class ConsumeApplication {

 4.这里看到进入了我们自定义的IRULE规则选择服务实例。 

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发