客戶端調用服務端時,如何選擇調用服務端的哪臺機器上的服務呢。這就設計到負載均衡策略了。
默認使用的是RoundRobinLoadBalance,輪訓策略,或者說是加權輪訓策略
除此之外,還有隨機策略,加權隨機策略
最不活躍策略
以及一致性hash策略
還有在DubboInvoker裏面我們可以看到,我們與服務端的某臺機器建立的是多個連接(默認是兩個),那麼需要從連接裏面選一個出來。
@Override
protected Result doInvoke(final Invocation invocation) throws Throwable {
RpcInvocation inv = (RpcInvocation) invocation;
final String methodName = RpcUtils.getMethodName(invocation);
inv.setAttachment(Constants.PATH_KEY, getUrl().getPath());
inv.setAttachment(Constants.VERSION_KEY, version);
ExchangeClient currentClient;
if (clients.length == 1) {
currentClient = clients[0];
} else {
currentClient = clients[index.getAndIncrement() % clients.length];
}