在前文,我们了解到feign实现负载均衡需要两个重要的接口:
本文重点介绍这两个接口:
public interface ILoadBalancer {public void addServers(List newServers);// 选择一个可用的serverpublic Server chooseServer(Object key);public void markServerDown(Server server);@Deprecatedpublic List getServerList(boolean availableOnly);public List getReachableServers();public List getAllServers();
}
ILoadBalancer|-- AbstractLoadBalancer|-- BaseLoadBalancer|-- DynamicServerListLoadBalancer|-- ZoneAwareLoadBalancer|-- NoOpLoadBalancer
默认情况下使用的是ZoneAwareLoadBalancer实现类,是在RibbonClientConfiguration配置了里面装配的。
@Bean
@ConditionalOnMissingBean
public ILoadBalancer ribbonLoadBalancer(IClientConfig config,ServerList serverList, ServerListFilter serverListFilter,IRule rule, IPing ping, ServerListUpdater serverListUpdater) {if (this.propertiesFactory.isSet(ILoadBalancer.class, name)) {return this.propertiesFactory.get(ILoadBalancer.class, config, name);}return new ZoneAwareLoadBalancer<>(config, rule, ping, serverList,serverListFilter, serverListUpdater);
}
// 封装zone -> LoadBalancer映射关系
private ConcurrentHashMap balancers = new ConcurrentHashMap();public Server chooseServer(Object key) {// 当只有一个zone时,直接使用父类chooseServer方法选择serverif (!ENABLED.get() || getLoadBalancerStats().getAvailableZones().size() <= 1) {return super.chooseServer(key);}Server server = null;try {// LoadBalancerStats封装zone -> server集关系LoadBalancerStats lbStats = getLoadBalancerStats();Map zoneSnapshot = ZoneAvoidanceRule.createSnapshot(lbStats);// 获取可用的zone集Set availableZones = ZoneAvoidanceRule.getAvailableZones(zoneSnapshot, triggeringLoad.get(), triggeringBlackoutPercentage.get());if (availableZones != null && availableZones.size() < zoneSnapshot.keySet().size()) {// 随机一个zoneString zone = ZoneAvoidanceRule.randomChooseZone(zoneSnapshot, availableZones);if (zone != null) {// 从balancers获取该zone对应的LoadBalancerBaseLoadBalancer zoneLoadBalancer = getLoadBalancer(zone);// 用LoadBalancer选择一个可用serverserver = zoneLoadBalancer.chooseServer(key);}}} catch (Exception e) {logger.error("Error choosing server using zone aware logic for load balancer={}", name, e);}if (server != null) {return server;} else {return super.chooseServer(key);}
}
LoadBalancerStats类封装着zone -> server集和zone -> ZoneStats关系,如下:
public class LoadBalancerStats implements IClientConfigAware {String name;// Map serverStatsMap = new ConcurrentHashMap();// key都是zonevolatile Map zoneStatsMap = new ConcurrentHashMap();volatile Map> upServerListZoneMap =new ConcurrentHashMap>();// 连接失败数量阈值private volatile CachedDynamicIntProperty connectionFailureThreshold;// 断路器跳闸超时阈值private volatile CachedDynamicIntProperty circuitTrippedTimeoutFactor;// 最大断路器跳闸超时时长private volatile CachedDynamicIntProperty maxCircuitTrippedTimeout;private static final DynamicIntProperty SERVERSTATS_EXPIRE_MINUTES = DynamicPropertyFactory.getInstance().getIntProperty("niws.loadbalancer.serverStats.expire.minutes", 30);// 服务器节点 -> 服务器State关系private final LoadingCache serverStatsCache;// ...
}
ServerStats维护着服务节点的状态信息,包括:请求数量、失败数量、断路器跳闸状态等,提供判断服务节点可用(断路器是否打开)的方法,在负载均衡选择可用服务节点时会使用到这些状态信息。
ZoneStats类封装当前服务端节点状态:
public class ZoneStats {private final LoadBalancerStats loadBalancerStats;private final String zone;private static final String PREFIX = "ZoneStats_";private final Counter counter;final String monitorId;// ...
}
看一下super的chooseServer方法:
public Server chooseServer(Object key) {if (counter == null) {counter = createCounter();}counter.increment();if (rule == null) {return null;} else {try {// 使用IRule选择一个server// IRule里面封装负载均衡算法// 这个IRule是在创建LoadBalancer对象时传递进来的// 默认使用ZoneAvoidanceRulereturn rule.choose(key);} catch (Exception e) {return null;}}
}
Interface that defines a “Rule” for a LoadBalancer. A Rule can be thought of as a Strategy for loadbalacing. Well known loadbalancing strategies include Round Robin, Response Time based etc.
public interface IRule{/** choose one alive server from lb.allServers or* lb.upServers according to key*/public Server choose(Object key);public void setLoadBalancer(ILoadBalancer lb);public ILoadBalancer getLoadBalancer();
}
默认使用的是ZoneAvoidanceRule实现,是在RibbonClientConfiguration配置了里面装配的。
@Bean
@ConditionalOnMissingBean
public IRule ribbonRule(IClientConfig config) {if (this.propertiesFactory.isSet(IRule.class, name)) {return this.propertiesFactory.get(IRule.class, config, name);}ZoneAvoidanceRule rule = new ZoneAvoidanceRule();rule.initWithNiwsConfig(config);return rule;
}
这个类继承了PredicateBasedRule类,choose方法的实现在父类里面:
// PredicateBasedRule.choose
public Server choose(Object key) {ILoadBalancer lb = getLoadBalancer();Optional server = getPredicate().chooseRoundRobinAfterFiltering(lb.getAllServers(), key);if (server.isPresent()) {return server.get();} else {return null;}
}// AbstractServerPredicate.chooseRoundRobinAfterFiltering
public Optional chooseRoundRobinAfterFiltering(List servers, Object loadBalancerKey) {// 获取有资格的server集List eligible = getEligibleServers(servers, loadBalancerKey);if (eligible.size() == 0) {return Optional.absent();}// 轮询return Optional.of(eligible.get(incrementAndGetModulo(eligible.size())));
}
ZoneAvoidanceRule类实现了父类的getPredicate方法,返回CompositePredicate对象,封装着ZoneAvoidancePredicate和AvailabilityPredicate用于过滤可用server节点:
private CompositePredicate compositePredicate;public ZoneAvoidanceRule() {super();ZoneAvoidancePredicate zonePredicate = new ZoneAvoidancePredicate(this);AvailabilityPredicate availabilityPredicate = new AvailabilityPredicate(this);compositePredicate = createCompositePredicate(zonePredicate, availabilityPredicate);
}private CompositePredicate createCompositePredicate(ZoneAvoidancePredicate p1, AvailabilityPredicate p2) {return CompositePredicate.withPredicates(p1, p2).addFallbackPredicate(p2).addFallbackPredicate(AbstractServerPredicate.alwaysTrue()).build();
}
CompositePredicate是一个代理类,内部使用Predicate链获取可用Server集:
public List getEligibleServers(List servers, Object loadBalancerKey) {List result = super.getEligibleServers(servers, loadBalancerKey);Iterator i = fallbacks.iterator();while (!(result.size() >= minimalFilteredServers && result.size() > (int) (servers.size() * minimalFilteredPercentage))&& i.hasNext()) {AbstractServerPredicate predicate = i.next();result = predicate.getEligibleServers(servers, loadBalancerKey);}return result;
}
public boolean apply(PredicateKey input) {if (!ENABLED.get()) {return true;}String serverZone = input.getServer().getZone();if (serverZone == null) {// there is no zone information from the server, we do not want to filter// out this serverreturn true;}LoadBalancerStats lbStats = getLBStats();if (lbStats == null) {// no stats available, do not filterreturn true;}if (lbStats.getAvailableZones().size() <= 1) {// only one zone is available, do not filterreturn true;}Map zoneSnapshot = ZoneAvoidanceRule.createSnapshot(lbStats);if (!zoneSnapshot.keySet().contains(serverZone)) {// The server zone is unknown to the load balancer, do not filter it out return true;}Set availableZones = ZoneAvoidanceRule.getAvailableZones(zoneSnapshot, triggeringLoad.get(), triggeringBlackoutPercentage.get());if (availableZones != null) {return availableZones.contains(input.getServer().getZone());} else {return false;}
}
public boolean apply(PredicateKey input) {LoadBalancerStats stats = getLBStats();if (stats == null) {return true;}return !shouldSkipServer(stats.getSingleServerStat(input.getServer()));
}// 判断断路器的状态
private boolean shouldSkipServer(ServerStats stats) { if ((CIRCUIT_BREAKER_FILTERING.get() && stats.isCircuitBreakerTripped()) || stats.getActiveRequestsCount() >= activeConnectionsLimit.get()) {return true;}return false;
}
public class NacosRule extends AbstractLoadBalancerRule {@Autowiredprivate NacosDiscoveryProperties nacosDiscoveryProperties;@Autowiredprivate NacosServiceManager nacosServiceManager;@Overridepublic Server choose(Object key) {try {String clusterName = this.nacosDiscoveryProperties.getClusterName();String group = this.nacosDiscoveryProperties.getGroup();DynamicServerListLoadBalancer loadBalancer = (DynamicServerListLoadBalancer) getLoadBalancer();String name = loadBalancer.getName();// 使用NamingService服务发现查找Server集NamingService namingService = nacosServiceManager.getNamingService(nacosDiscoveryProperties.getNacosProperties());List instances = namingService.selectInstances(name, group, true);if (CollectionUtils.isEmpty(instances)) {return null;}// 使用clusterName过滤一下List instancesToChoose = instances;if (StringUtils.isNotBlank(clusterName)) {List sameClusterInstances = instances.stream().filter(instance -> Objects.equals(clusterName,instance.getClusterName())).collect(Collectors.toList());if (!CollectionUtils.isEmpty(sameClusterInstances)) {instancesToChoose = sameClusterInstances;}}// random-weight算法Instance instance = ExtendBalancer.getHostByRandomWeight2(instancesToChoose);return new NacosServer(instance);} catch (Exception e) {return null;}}@Overridepublic void initWithNiwsConfig(IClientConfig iClientConfig) {}
}
Strategy for DynamicServerListLoadBalancer to use for different ways of doing dynamic server list updates.
用于更新服务节点列表。
public interface ServerListUpdater {/*** an interface for the updateAction that actually executes a server list update*/public interface UpdateAction {void doUpdate();}/*** start the serverList updater with the given update action* This call should be idempotent.*/void start(UpdateAction updateAction);/*** stop the serverList updater. This call should be idempotent*/void stop();// 其他方法
}
PollingServerListUpdater实现类:
public synchronized void start(final UpdateAction updateAction) {if (isActive.compareAndSet(false, true)) {final Runnable wrapperRunnable = new Runnable() {@Overridepublic void run() {if (!isActive.get()) {if (scheduledFuture != null) {scheduledFuture.cancel(true);}return;}try {updateAction.doUpdate();lastUpdated = System.currentTimeMillis();} catch (Exception e) {logger.warn("Failed one update cycle", e);}}};// 启动周期调度scheduledFuture = getRefreshExecutor().scheduleWithFixedDelay(wrapperRunnable,initialDelayMs, // 1000refreshIntervalMs, // 30 * 1000TimeUnit.MILLISECONDS);}
}
Interface that defines the methods sed to obtain the List of Servers.
public interface ServerList {public List getInitialListOfServers();/*** Return updated list of servers. This is called say every 30 secs* (configurable) by the Loadbalancer's Ping cycle*/public List getUpdatedListOfServers();
}
NacosServerList实现类使用nacos的NamingService做服务发现。
这个实现类里面维护着ServerListUpdater和ServerList对象,启动ServerListUpdater做服务发现:
public class DynamicServerListLoadBalancer extends BaseLoadBalancer {// ...volatile ServerList serverListImpl;volatile ServerListFilter filter;protected final ServerListUpdater.UpdateAction updateAction = new ServerListUpdater.UpdateAction() {@Overridepublic void doUpdate() {updateListOfServers();}};protected volatile ServerListUpdater serverListUpdater;// ...public void updateListOfServers() {List servers = new ArrayList();if (serverListImpl != null) {servers = serverListImpl.getUpdatedListOfServers();if (filter != null) {servers = filter.getFilteredListOfServers(servers);}}updateAllServerList(servers);}
}
ServerListUpdater是在RibbonClientConfiguration中装配的:
@Bean
@ConditionalOnMissingBean
public ServerListUpdater ribbonServerListUpdater(IClientConfig config) {return new PollingServerListUpdater(config);
}
ServerList是在NacosRibbonClientConfiguration中装配的:
@Bean
@ConditionalOnMissingBean
public ServerList> ribbonServerList(IClientConfig config,NacosDiscoveryProperties nacosDiscoveryProperties) {if (this.propertiesFactory.isSet(ServerList.class, config.getClientName())) {ServerList serverList = this.propertiesFactory.get(ServerList.class, config,config.getClientName());return serverList;}NacosServerList serverList = new NacosServerList(nacosDiscoveryProperties);serverList.initWithNiwsConfig(config);return serverList;
}