• Ribbon架构篇 - ZoneAvoidanceRule


    前言

    ZoneAvoidanceRule 基于分区下的服务器的可用性选出可用分区列表,再从可用分区列表中随机选择一个分区,采用轮询的策略选择该分区的一个服务器。

    ZoneAwareLoadBalancer

    属性 属性描述 默认值
    ZoneAwareNIWSDiscoveryLoadBalancer.enabled 是否开启ZoneAwareLoadBalancer true
    niws.loadbalancer.default.connectionFailureCountThreshold 连接失败的数量的阈值 3
    niws.loadbalancer.default.circuitTripTimeoutFactorSeconds 断路器打开的超时时间因子 10
    niws.loadbalancer.default.circuitTripMaxTimeoutSeconds 断路器打开的超时时间阈值 30
    ZoneAwareNIWSDiscoveryLoadBalancer.default.triggeringLoadPerServerThreshold 每台服务器的负载的阈值 0.2
    niws.loadbalancer.serverStats.activeRequestsCount.effectiveWindowSeconds 活跃请求数发生变化的超时时间 600
    ZoneAwareNIWSDiscoveryLoadBalancer.default.avoidZoneWithBlackoutPercetage 服务器故障率阈值 0.99999
    @Override
    public Server chooseServer(Object key) {
       
    		// 如果没有开启 ZoneAwareLoadBalancer 或者 LoadBalancerStats记录的可用分区数 <= 1
    		// 则执行 BaseLoadBalancer#chooseServer(...) 的逻辑
        if (!ENABLED.get() || getLoadBalancerStats().getAvailableZones().size() <= 1) {
       
            logger.debug("Zone aware logic disabled or there is only one zone");
            return super.chooseServer(key);
        }
    
        Server server = null;
        try {
       
            LoadBalancerStats lbStats = getLoadBalancerStats();
          	// 创建分区快照
            Map<String, ZoneSnapshot> zoneSnapshot = ZoneAvoidanceRule.createSnapshot(lbStats);
            logger.debug("Zone snapshots: {}", zoneSnapshot);
            if (triggeringLoad == null) {
       
                triggeringLoad = DynamicPropertyFactory.getInstance().getDoubleProperty(
                        "ZoneAwareNIWSDiscoveryLoadBalancer." + this.getName() + ".triggeringLoadPerServerThreshold", 0.2d);
            }
    
            if (triggeringBlackoutPercentage == null) {
       
                triggeringBlackoutPercentage = DynamicPropertyFactory.getInstance().getDoubleProperty(
                        "ZoneAwareNIWSDiscoveryLoadBalancer." + this.getName() + ".avoidZoneWithBlackoutPercetage", 0.99999d);
            }
          	// 获取可用的分区集合
            Set<String> availableZones = ZoneAvoidanceRule.getAvailableZones(zoneSnapshot, triggeringLoad.get(), triggeringBlackoutPercentage.get());
            logger.debug("Available zones: {}", availableZones);
            if (availableZones != null &&  availableZones.size() < zoneSnapshot.keySet().size()) {
       
              	// 从可用的分区集合中随机选择一个分区
                String zone = ZoneAvoidanceRule.randomChooseZone(zoneSnapshot, availableZones);
                logger.debug("Zone chosen: {}", zone);
                if (zone != null) {
       
                  	// 从缓存中获取分区对应的负载均衡器,初始会创建 BaseLoadBalancer 负载均衡器
                    BaseLoadBalancer zoneLoadBalancer = getLoadBalancer(zone);
                    // 默认采用轮询的策略从服务器列表中选择一个服务器
                    server = zoneLoadBalancer.chooseServer(key);
                }
            }
        } catch (Exception e) {
       
            logger.error("Error choosing server using zone aware logic for load balancer={}", name, e);
        }
        if (server != null) {
       
            return server;
        } else {
       
            logger.debug("Zone avoidance logic is not invoked.");
            return super.chooseServer(key);
        }
    }
    
    • 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
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    1、先获取可用分区集合。

    可用分区选取的策略如下:

    • 如果只有一个分区,则选择该分区作为可用分区。
    • 如果有多个分区,满足分区下的服务器数量大于0、服务器故障率小于阈值的、再随机排除一个服务器的负载大于平均负载的分区,作为可用分区,添加到可用分区集合中。

    2、从可用分区集合中选择一个分区。

    选择策略:累加每个可用分区中的服务器数量,基于这个总数生成一个随机数,找到对应服务器所在的分区。

    3、从缓存中获取分区对应的负载均衡器,默认采用轮询的策略从服务器列表中选择一个服务器。


    1.1 ZoneAvoidanceRule#create

  • 相关阅读:
    工业路由器项目应用(4g+5g两种工业路由器项目介绍)
    华为OD机考--TVL解码--GPU算力--猴子爬台阶--两个数组前K对最小和--勾股数C++实现
    蓝桥等考Python组别一级008
    数据治理-数据模型计分卡
    【学术写作规范】论文写作注意事项
    Spring Cloud Alibaba(二)
    python+vue+elementui在线跑腿系统django
    WPF自定义控件与样式(4)-CheckBox/RadioButton自定义样式
    Qt-FFmpeg开发-视频播放(3)
    做了3年Java,靠着这份面试题跟答案,我从15K变成了30K
  • 原文地址:https://blog.csdn.net/qq_34561892/article/details/127837128