客户端调用服务端时,如何选择调用服务端的哪台机器上的服务呢。这就设计到负载均衡策略了。
默认使用的是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];
}