| 
                         使慢的提供者收到更少请求,因为越慢的提供者的调用前后计数差会越大。 
    - 遍历所有的invoker
 
    - 获取当前invoker的活跃数,调用的是RpcStatus的getStatus方法,过滤器里面会记录每个方法的活跃数
 
    - 获取当前invoker的权重
 
    - 如果是第一次进来或者是当前invoker的活跃数比最小的活跃数还小
 
    - 那么把leastActive设置为当前invoker的活跃数,设置leastCount为1,leastIndexes数组的第一个位置设置为1,记录一下totalWeight和firstWeight
 
    - 如果不满足第4点的条件,那么判断当前invoker的活跃数和最小的活跃数是否相等
 
    - 如果满足第6点,那么把当前的权重加入到totalWeight中,并把leastIndexes数组中记录一下最小活跃数相同的下标;再看一下是否所有的权重相同
 
    - 如果invoker集合中只有一个invoker活跃数是最小的,那么直接返回
 
    - 如果权重不相等,随机权重后,判断在哪个 Invoker 的权重区间中
 
    - 权重相等,直接随机选择 Invoker 即可
 
 
- 最小活跃数算法实现: 
 - 假定有3台dubbo provider: 
 - 10.0.0.1:20884, weight=2,active=2 
 - 10.0.0.1:20886, weight=3,active=4 
 - 10.0.0.1:20888, weight=4,active=3 
 - active=2最小,且只有一个2,所以选择10.0.0.1:20884 
 - 假定有3台dubbo provider: 
 - 10.0.0.1:20884, weight=2,active=2 
 - 10.0.0.1:20886, weight=3,active=2 
 - 10.0.0.1:20888, weight=4,active=3 
 - active=2最小,且有2个,所以从[10.0.0.1:20884,10.0.0.1:20886 ]中选择; 
 - 接下来的算法与随机算法类似: 
 - 假设offset=1(即random.nextInt(5)=1) 
 - 1-2=-1<0?是,所以选中 10.0.0.1:20884, weight=2 
 - 假设offset=4(即random.nextInt(5)=4) 
 - 4-2=2<0?否,这时候offset=2, 2-3<0?是,所以选中 10.0.0.1:20886, weight=3 
 -  1: public class LeastActiveLoadBalance extends AbstractLoadBalance { 
 -  2:  
 -  3: public static final String NAME = "leastactive"; 
 -  4:  
 -  5: private final Random random = new Random(); 
 -  6:  
 -  7: @Override 
 -  8: protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) { 
 -  9: int length = invokers.size(); // 总个数 
 - 10: int leastActive = -1; // 最小的活跃数 
 - 11: int leastCount = 0; // 相同最小活跃数的个数 
 - 12: int[] leastIndexes = new int[length]; // 相同最小活跃数的下标 
 - 13: int totalWeight = 0; // 总权重 
 - 14: int firstWeight = 0; // 第一个权重,用于于计算是否相同 
 - 15: boolean sameWeight = true; // 是否所有权重相同 
 - 16: // 计算获得相同最小活跃数的数组和个数 
 - 17: for (int i = 0; i < length; i++) { 
 - 18: Invoker<T> invoker = invokers.get(i); 
 - 19: int active = RpcStatus.getStatus(invoker.getUrl(), invocation.getMethodName()).getActive(); // 活跃数 
 - 20: int weight = invoker.getUrl().getMethodParameter(invocation.getMethodName(), Constants.WEIGHT_KEY, Constants.DEFAULT_WEIGHT); // 权重 
 - 21: if (leastActive == -1 || active < leastActive) { // 发现更小的活跃数,重新开始 
 - 22: leastActive = active; // 记录最小活跃数 
 - 23: leastCount = 1; // 重新统计相同最小活跃数的个数 
 - 24: leastIndexes[0] = i; // 重新记录最小活跃数下标 
 - 25: totalWeight = weight; // 重新累计总权重 
 - 26: firstWeight = weight; // 记录第一个权重 
 - 27: sameWeight = true; // 还原权重相同标识 
 - 28: } else if (active == leastActive) { // 累计相同最小的活跃数 
 - 29: leastIndexes[leastCount++] = i; // 累计相同最小活跃数下标 
 - 30: totalWeight += weight; // 累计总权重 
 - 31: // 判断所有权重是否一样 
 - 32: if (sameWeight && weight != firstWeight) { 
 - 33: sameWeight = false; 
 - 34: } 
 - 35: } 
 - 36: } 
 - 37: // assert(leastCount > 0) 
 - 38: if (leastCount == 1) { 
 - 39: // 如果只有一个最小则直接返回 
 - 40: return invokers.get(leastIndexes[0]); 
 - 41: } 
 - 42: if (!sameWeight && totalWeight > 0) { 
 - 43: // 如果权重不相同且权重大于0则按总权重数随机 
 - 44: int offsetWeight = random.nextInt(totalWeight); 
 - 45: // 并确定随机值落在哪个片断上 
 - 46: for (int i = 0; i < leastCount; i++) { 
 - 47: int leastIndex = leastIndexes[i]; 
 - 48: offsetWeight -= getWeight(invokers.get(leastIndex), invocation); 
 - 49: if (offsetWeight <= 0) { 
 - 50: return invokers.get(leastIndex); 
 - 51: } 
 - 52: } 
 - 53: } 
 - 54: // 如果权重相同或权重为0则均等随机 
 - 55: return invokers.get(leastIndexes[random.nextInt(leastCount)]); 
 - 56: } 
 - 57:  
 - 58: } 
 
  
ConsistentHashLoadBalance 一致性 Hash                         (编辑:泰州站长网) 
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! 
                     |