• 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. # 自定义负载均衡策略  
      springboot-h2.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule // 自定义使用随机策略,springboot-h2是服务应用名 
       

    2、修改调用代码

    package com.chhliu.springboot.restful.controller;  
      
    import org.springframework.beans.factory.annotation.Autowired;  
    import org.springframework.cloud.client.ServiceInstance;  
    import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;  
    import org.springframework.web.bind.annotation.GetMapping;  
    import org.springframework.web.bind.annotation.PathVariable;  
    import org.springframework.web.bind.annotation.RestController;  
    import org.springframework.web.client.RestTemplate;  
      
    import com.chhliu.springboot.restful.vo.User;  
      
    @RestController  
    public class RestTemplateController {  
        @Autowired  
        private RestTemplate restTemplate;  
          
        @Autowired  
        private LoadBalancerClient loadBalancerClient;  
          
        @GetMapping("/template/{id}")  
        public User findById(@PathVariable Long id) {  
            ServiceInstance serviceInstance = this.loadBalancerClient.choose("springboot-h2");  
            System.out.println("===" + ":" + serviceInstance.getServiceId() + ":" + serviceInstance.getHost() + ":"  
                    + serviceInstance.getPort());// 打印当前调用服务的信息  
            User u = this.restTemplate.getForObject("http://springboot-h2/user/" + id, User.class);  
            System.out.println(u);  
            return u;  
        }  
    }  

    3、测试

    服务调用关系如下:

    测试结果如下:

     

     在CODE上查看代码片派生到我的代码片
    1. ===:springboot-h2:127.0.0.1:7902  
      User [id=2, username=user2, name=李四, age=20, balance=100.00]  
      ===:springboot-h2:127.0.0.1:7901  
      User [id=2, username=user2, name=李四, age=20, balance=100.00]  
      ===:springboot-h2:127.0.0.1:7902  
      User [id=2, username=user2, name=李四, age=20, balance=100.00]  
      ===:springboot-h2:127.0.0.1:7901  
      User [id=2, username=user2, name=李四, age=20, balance=100.00]  
      ===:springboot-h2:127.0.0.1:7902  
      User [id=2, username=user2, name=李四, age=20, balance=100.00]  
      ===:springboot-h2:127.0.0.1:7902  
      User [id=2, username=user2, name=李四, age=20, balance=100.00]  
      ===:springboot-h2:127.0.0.1:7902  
      User [id=2, username=user2, name=李四, age=20, balance=100.00]  
      ===:springboot-h2:127.0.0.1:7902  
      User [id=2, username=user2, name=李四, age=20, balance=100.00]  
      ===:springboot-h2:127.0.0.1:7902  
      User [id=2, username=user2, name=李四, age=20, balance=100.00]  
      ===:springboot-h2:127.0.0.1:7901  
      User [id=2, username=user2, name=李四, age=20, balance=100.00]  
      ===:springboot-h2:127.0.0.1:7901  
      User [id=2, username=user2, name=李四, age=20, balance=100.00]  
      ===:springboot-h2:127.0.0.1:7902  
      User [id=2, username=user2, name=李四, age=20, balance=100.00]  
      ===:springboot-h2:127.0.0.1:7902  
      User [id=2, username=user2, name=李四, age=20, balance=100.00]  
      ===:springboot-h2:127.0.0.1:7901  
      User [id=2, username=user2, name=李四, age=20, balance=100.00]  
      ===:springboot-h2:127.0.0.1:7901  
      User [id=2, username=user2, name=李四, age=20, balance=100.00]  
      发现选择7901端口服务和7902端口服务确实是随机的!
  • 相关阅读:
    POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】
    POJ 1797 ——Heavy Transportation——————【最短路、Dijkstra、最短边最大化】
    POJ 2253 ——Frogger——————【最短路、Dijkstra、最长边最小化】
    HDU 5505——GT and numbers——————【素数】
    int,long,long long的数据范围
    HDU 5501——The Highest Mark——————【贪心+dp】
    UVALive 4262——Trip Planning——————【Tarjan 求强连通分量个数】
    随手记
    工作流引擎资料/产品收集
    Vim 学习指南
  • 原文地址:https://www.cnblogs.com/ilinuxer/p/6582784.html
Copyright © 2020-2023  润新知