Dubbo的轮询负载均衡现在采用的平滑权重轮询
如下是RoundBin负载均衡的步骤
(1)初始化权重缓存Map,以每个Invoker的URL为key,权重对象为value,生成ConcurrentMap,并把这个Map保存到全局的methodWeightMap中,ConcurrentMap
private int weight;//Invoker设定的权重 private AtomicLong current = new AtomicLong(0); private long lastUpdate;
(2)遍历所有Invoker,首先,在遍历的过程中把每个Invoker的数据填充到第一步生成的权重缓存Map中,其次,获取每个Invoker的预热权重,新版的框架RoundRobin也支持预热,通过和Random负载均衡中同样的方式获得预热权重,如果预热权重和设置的权重不相等,说明还在预热阶段,会以预热权重为基础,进行平滑轮询,每个Invoker会把权重加到自己的current属性上,并更新当前invoker的lastupdate,同时累加每个Invoker的权重到totalWeight,最终,遍历完之后,选出所有Invoker中current最大的作为最终要调用的节点
(3)清除已经没有使用的缓存节点,
(4)返回Invoker,返回之前把当前current减去总权重



