④. Ribbon负载均衡算法底层原理
- ①. 理论知识点:
②. 源码分析(掌握)
Ribbon默认使用的负载均衡是轮询,IRule的一个具体实现类是使用轮询算法;在这里类里面,有一个原子整型类AtomicInteger,它会在无参构造函数中进行一个初始化的操作。我们会去调用它的choose方法查看使用负载均衡时候使用的是哪个Server;如果你当前参数传入进行的这个ILoadBalancer==null,就直接retrun null;如果不为null,那么先将server=null;然后调用它的获取健康服务的方法和获取所有服务的方法;得到这两个方法的size;如果说这两个size都不为0;那么我们就将所有服务的size作为参数传入进一个方法中;这个方法使用的是CAS的思想+自旋锁;将原子整型类AtomicInteger中的参数获取到为0; 我们将获取到的参数+1 % 传入这个方法的参数也就是集群的总量,获取到index,这个index就是我们轮询算法会去选择那个Server;进行比较并交换的处理,比较成功! 那么就return index;获取到index后,通过集合中的get(index)方法获取到具体要执行那个端口号的Server
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package com.netflix.loadbalancer; import com.netflix.client.config.IClientConfig; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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() { this.nextServerCyclicCounter = new AtomicInteger(0); } public RoundRobinRule(ILoadBalancer lb) { this(); this.setLoadBalancer(lb); } public Server choose(ILoadBalancer lb, Object key) { if (lb == null) { log.warn("no load balancer"); return null; } else { Server server = null; int count = 0; while(true) { if (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) { int nextServerIndex = this.incrementAndGetModulo(serverCount); server = (Server)allServers.get(nextServerIndex); if (server == null) { Thread.yield(); } else { if (server.isAlive() && server.isReadyToServe()) { return server; } server = null; } continue; } log.warn("No up servers available from load balancer: " + lb); return null; } if (count >= 10) { log.warn("No available alive servers after 10 tries from load balancer: " + lb); } return server; } } } private int incrementAndGetModulo(int modulo) { int current; int next; do { current = this.nextServerCyclicCounter.get(); next = (current + 1) % modulo; } while(!this.nextServerCyclicCounter.compareAndSet(current, next)); return next; } public Server choose(Object key) { return this.choose(this.getLoadBalancer(), key); } public void initWithNiwsConfig(IClientConfig clientConfig) { } }