这是总的代码,我这里就用切代码方式一点点讲把。
public class RoundRobinRule extends AbstractLoadBalancerRule {
private AtomicInteger nextServerCyclicCounter;
private static final boolean AVAILABLE_ONLY_SERVERS = true;
private static final boolean ALL_SERVERS = false;
private static Logger log = LoggerFactory.getLogger(RoundRobinRule.class);
public RoundRobinRule() {
nextServerCyclicCounter = new AtomicInteger(0);
}
public RoundRobinRule(ILoadBalancer lb) {
this();
setLoadBalancer(lb);
}
public Server choose(ILoadBalancer lb, Object key) {
if (lb == null) {
log.warn("no load balancer");
return null;
}
Server server = null;
int count = 0;
while (server == null && count++ < 10) {
List<Server> reachableServers = lb.getReachableServers();
List<Server> allServers = lb.getAllServers();
int upCount = reachableServers.size();
int serverCount = allServers.size();
if ((upCount == 0) || (serverCount == 0)) {
log.warn("No up servers available from load balancer: " + lb);
return null;
}
int nextServerIndex = incrementAndGetModulo(serverCount);
server = allServers.get(nextServerIndex);
if (server == null) {
/* Transient. */
Thread.yield();
continue;
}
if (server.isAlive() && (server.isReadyToServe())) {
return (server);
}
// Next.
server = null;
}
if (count >= 10) {
log.warn("No available alive servers after 10 tries from load balancer: "
+ lb);
}
return server;
}
/**
* Inspired by the implementation of {@link AtomicInteger#incrementAndGet()}.
*
* @param modulo The modulo to bound the value of the counter.
* @return The next value.
*/
private int incrementAndGetModulo(int modulo) {
for (;;) {
int current = nextServerCyclicCounter.get();
int next = (current + 1) % modulo;
if (nextServerCyclicCounter.compareAndSet(current, next))
return next;
}
}
@Override
public Server choose(Object key) {
return choose(getLoadBalancer(), key);
}
@Override
public void initWithNiwsConfig(IClientConfig clientConfig) {
}
}
轮询的实现,其实本质上就是服务接口访问次数%集群数量=得到集群对应索引,然后根据索引去使用集群。
知道这点的情况下再来解释,
这里定义了AtomicInteger原子操作类,你可以理解成就是线程安全的int类型,然后定义服务存活为真,所有服务为假。
private AtomicInteger nextServerCyclicCounter;
private static final boolean AVAILABLE_ONLY_SERVERS = true;
private static final boolean ALL_SERVERS = false;
日志,没什么用,不讲,第二个是把我们刚开始的定义了线程安全的归为0,第三个是给我们的lb设定负载均衡。
private static Logger log = LoggerFactory.getLogger(RoundRobinRule.class);
public RoundRobinRule() {
nextServerCyclicCounter = new AtomicInteger(0);
}
public RoundRobinRule(ILoadBalancer lb) {
this();
setLoadBalancer(lb);
}
负载均衡未开启为null,就提示没有负载均衡,设置服务初始值为null,数目初始为0,然后从lb里面获取存活的服务连接和全部服务到reachableServers和allServers 上,用upcount和serverCount 存取其数量
public Server choose(ILoadBalancer lb, Object key) {
if (lb == null) {
log.warn("no load balancer");
return null;
}
Server server = null;
int count = 0;
while (server == null && count++ < 10) {
List<Server> reachableServers = lb.getReachableServers();
List<Server> allServers = lb.getAllServers();
int upCount = reachableServers.size();
int serverCount = allServers.size();
判断数量,都是0就返回没一个服务可以用,
if ((upCount == 0) || (serverCount == 0)) {
log.warn("No up servers available from load balancer: " + lb);
return null;
}
下面这个,我结合他调用的方法讲,这里就是把我们的集群数量放进方法incrementAndGetModulo里
int nextServerIndex = incrementAndGetModulo(serverCount);
然后这里来讲方法,这里的 for (;😉 不是用的死循环而是用了一个自旋锁,我们会从nextServerCyclicCounter中获取当前请求服务次数,就是0,上面讲了,不再赘述。第二个,把当前请求的次数加上1除以我们的集群数量,得到的就是下一个要用的集群索引为1 然后用cas进行确认和转换(compareAndSet 说白了就是,如果current是我期望的值,我就把他跟next进行交换,不是的话返回false然后继续循环)因为这里填入的current跟我们当前的curren一样为0,所以curren会变成1,然后回传给nextServerIndex。
private int incrementAndGetModulo(int modulo) {
for (;;) {
int current = nextServerCyclicCounter.get();
int next = (current + 1) % modulo;
if (nextServerCyclicCounter.compareAndSet(current, next))
return next;
}
}
接下来,server会从集群集合里面获取索引值为1的集群,就是第一个集群。然后如果没有该集群的话线程进入让步状态,如果有的话,并且集群还存活,就进入准备状态,并且返回这个要被调用的集群,然后再把服务重置为0
server = allServers.get(nextServerIndex);
if (server == null) {
/* Transient. */
Thread.yield();
continue;
}
if (server.isAlive() && (server.isReadyToServe())) {
return (server);
}
// Next.
server = null;
}