dubbo原始碼學習文章目錄
dubbo 的定位是分布式服務框架,為了避免單點壓力過大,服務的提供者通常部署多台,如何從服務提供者集群中選取乙個進行呼叫,就依賴於dubbo的負載均衡策略。
dubbo 負載均衡策略提供下列四種方式:
random loadbalance 隨機,按權重設定隨機概率。dubbo的預設負載均衡策略
在乙個截面上碰撞的概率高,但呼叫量越大分布越均勻,而且按概率使用權重後也比較均勻,有利於動態調整提供者權重。
roundrobin loadbalance 輪循,按公約後的權重設定輪循比率。
存在慢的提供者累積請求問題,比如:第二台機器很慢,但沒掛,當請求調到第二台時就卡在那,久而久之,所有請求都卡在調到第二台上。
leastactive loadbalance 最少活躍呼叫數,相同活躍數的隨機,活躍數指呼叫前後計數差。
使慢的提供者收到更少請求,因為越慢的提供者的呼叫前後計數差會越大。
consistenthash loadbalance 一致性hash,相同引數的請求總是發到同一提供者。
當某一台提供者掛時,原本發往該提供者的請求,基於虛擬節點,平攤到其它提供者,不會引起劇烈變動。
首先檢視 loadbalance 介面
invoker select(list> invokers, url url, invocation invocation) throws rpcexception;loadbalance 定義了乙個方法就是從 invokers 列表中選取乙個
abstractloadbalance 抽象類是所有負載均衡策略實現類的父類,實現了loadbalance介面 的方法,同時提供抽象方法交由子類實現,
public
invokerselect(list> invokers, url url, invocation invocation)
protected
abstract
invokerdoselect(list> invokers, url url, invocation invocation);
protected
invokerdoselect(list> invokers, url url, invocation invocation)
}if (totalweight > 0 && ! sameweight) }}
return invokers.get(random.nextint(length));
}
randomloadbalance 實現很簡單,如果每個提供者的權重都相同,那麼根據列表長度直接隨機選取乙個,
如果權重不同,累加權重值。根據0~累加的權重值 選取乙個隨機數,然後判斷該隨機數落在那個提供者上。
private
final concurrentmapsequences = new concurrenthashmap();
private
final concurrentmapweightsequences = new concurrenthashmap();
protected
invokerdoselect(list> invokers, url url, invocation invocation)
if (maxweight > 0 && minweight < maxweight)
int currentweight = weightsequence.getandincrement() % maxweight;
list> weightinvokers = new arraylist>();
for (invokerinvoker : invokers)
}int weightlength = weightinvokers.size();
if (weightlength == 1) else
if (weightlength > 1)
}atomicpositiveinteger sequence = sequences.get(key);
if (sequence == null)
return invokers.get(sequence.getandincrement() % length);
}
首先也是判斷權重是否一致,如果一致,通過維護乙個 atomicinteger 的增長 進行取模亂來輪訓。
如果權重不一致,通過維護乙個 atomicinteger 的增長 與最大權重取模作為當前權重,然後獲取大於當前權重的列表作為呼叫者列表,然後進行取模輪訓
leastactiveloadbalance 原始碼比較簡單就不列出了,思路主要是,獲取最小的活躍數,把活躍數等於最小活躍數的呼叫者維護成乙個陣列
如果權重一致隨機取出,如果不同則跟 randomloadbalance 一致,累加權重,然後隨機取出。
protected
invokerdoselect(list> invokers, url url, invocation invocation)
return selector.select(invocation);
}public
consistenthashselector(list> invokers, string methodname, int identityhashcode)
for (invokerinvoker : invokers) }}
}
通過doselect方法可以看出 consistenthashloadbalance 主要是通過內部類 consistenthashselector 來實現的,首先看consistenthashselector建構函式的原始碼可以看出
首先根據invokers的url獲取分片個數,建立相同大小的虛擬節點。
public invokerselect(invocation invocation)
private string tokey(object args)
}return buf.tostring();
}private invokersekectforkey(long hash) else
}invoker = virtualinvokers.get(key);
return invoker;
}
然後根據引數的md5值 獲取對應的提供者
Dubbo集群 負載均衡
你需要乙個dubbo admin的壓縮包 你需要再zookeeper服務啟動的情況下,啟動tomcate tomcate bin startup.bar 登入localhost 8080 dubbo admin 訪問頁面 載入服務專案到集群中 修改服務專案的埠,啟動 注意 服務名稱不變 代表是在同乙...
dubbo負載均衡演算法及原始碼解析
二 最少活躍呼叫數負載均衡策略 leastactive loadbalance 三 輪詢負載均衡策略 roundrobin loadbalance 四 隨機負載均衡策略 random loadbalance dubbo中的負載均衡策略有四種如下 隨機 random 輪詢 roundrobin 最少活...
Dubbo篇 負載均衡策略原始碼分析
在進行消費端服務呼叫的時候,看到初始化了loadbalance,通過負載均衡獲取乙個可用的節點。loadbalance也是乙個擴充套件點,dubbo內建了4種負載均衡演算法,都繼承自abstractloadbalance,abstractloadbalance中實現通用邏輯,留乙個抽象方法dosel...