package org.apache.dubbo.rpc.cluster;@SPI(RandomLoadBalance.NAME)publicinterfaceLoadBalance{
/**
* select one invoker in list.
*/@Adaptive("loadbalance")<T> Invoker<T>select(List<Invoker<T>> invokers, URL url, Invocation invocation)throws RpcException;}
2. AbstractLoadBalance抽象类
使用模板模式执行公共业务
各个实现类实现抽象方法(doSelect)
getWeight:从URL中获取各个服务提供者的权重
package org.apache.dubbo.rpc.cluster.loadbalance;
public abstract class AbstractLoadBalance implements LoadBalance {
@Override
public <T> Invoker<T> select(List<Invoker<T>> invokers, URL url, Invocation invocation) {
if (CollectionUtils.isEmpty(invokers)) {
return null;
}
if (invokers.size() == 1) {
return invokers.get(0);
}
return doSelect(invokers, url, invocation);
}
protected abstract <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation);
/**
* Get the weight of the invoker's invocation which takes warmup time into account
* if the uptime is within the warmup time, the weight will be reduce proportionally
*
* @param invoker the invoker
* @param invocation the invocation of this invoker
* @return weight
*/
int getWeight(Invoker&l