SpringCloud Ribbon(六)之服务实例过滤器ServerListFilter

一、服务实例过滤器ServerListFilter

服务实例过滤器(ServerListFilter)为负载均衡器(Loadbalancer)提供从服务实例列表(ServerList)获取的服务实例过滤出符合要求的服务实例。

负载均衡器(Loadbalancer)通过服务实例列表(ServerList)从注册中心(register)或者配置文件(yaml或properties)上读取全部服务实例(server),然后以服务实例过滤器(ServerListFilter)的过滤方式进行筛选留下满足条件的服务实例,进而借助负载均衡策略(IRule)选择出一个合适的服务实例。

 

二、ServerListFilter实现类

ZoneAffinityServerListFilter     区区域相关性筛选服务实例列表过滤器

ZonePreferenceServerListFilter  首选本地区域服务实例列表过滤器

ServerListSubsetFilter   服务实例数限制为所有服务实例的子集 筛选过滤器

 

三、具体代码实现

(1)ZoneAffinityServerListFilter 

public class ZoneAffinityServerListFilter<T extends Server> extendsAbstractServerListFilter<T> implements IClientConfigAware {private volatile boolean zoneAffinity = DefaultClientConfigImpl.DEFAULT_ENABLE_ZONE_AFFINITY;private volatile boolean zoneExclusive = DefaultClientConfigImpl.DEFAULT_ENABLE_ZONE_EXCLUSIVITY;private DynamicDoubleProperty activeReqeustsPerServerThreshold;private DynamicDoubleProperty blackOutServerPercentageThreshold;private DynamicIntProperty availableServersThreshold;private Counter overrideCounter;private ZoneAffinityPredicate zoneAffinityPredicate = new ZoneAffinityPredicate();private static Logger logger = LoggerFactory.getLogger(ZoneAffinityServerListFilter.class);String zone;public ZoneAffinityServerListFilter() {      }public ZoneAffinityServerListFilter(IClientConfig niwsClientConfig) {initWithNiwsConfig(niwsClientConfig);}@Overridepublic void initWithNiwsConfig(IClientConfig niwsClientConfig) {String sZoneAffinity = "" + niwsClientConfig.getProperty(CommonClientConfigKey.EnableZoneAffinity, false);if (sZoneAffinity != null){zoneAffinity = Boolean.parseBoolean(sZoneAffinity);logger.debug("ZoneAffinity is set to {}", zoneAffinity);}String sZoneExclusive = "" + niwsClientConfig.getProperty(CommonClientConfigKey.EnableZoneExclusivity, false);if (sZoneExclusive != null){zoneExclusive = Boolean.parseBoolean(sZoneExclusive);}if (ConfigurationManager.getDeploymentContext() != null) {zone = ConfigurationManager.getDeploymentContext().getValue(ContextKey.zone);}activeReqeustsPerServerThreshold = DynamicPropertyFactory.getInstance().getDoubleProperty(niwsClientConfig.getClientName() + "." + niwsClientConfig.getNameSpace() + ".zoneAffinity.maxLoadPerServer", 0.6d);logger.debug("activeReqeustsPerServerThreshold: {}", activeReqeustsPerServerThreshold.get());blackOutServerPercentageThreshold = DynamicPropertyFactory.getInstance().getDoubleProperty(niwsClientConfig.getClientName() + "." + niwsClientConfig.getNameSpace() + ".zoneAffinity.maxBlackOutServesrPercentage", 0.8d);logger.debug("blackOutServerPercentageThreshold: {}", blackOutServerPercentageThreshold.get());availableServersThreshold = DynamicPropertyFactory.getInstance().getIntProperty(niwsClientConfig.getClientName() + "." + niwsClientConfig.getNameSpace() + ".zoneAffinity.minAvailableServers", 2);logger.debug("availableServersThreshold: {}", availableServersThreshold.get());overrideCounter = Monitors.newCounter("ZoneAffinity_OverrideCounter");Monitors.registerObject("NIWSServerListFilter_" + niwsClientConfig.getClientName());}private boolean shouldEnableZoneAffinity(List<T> filtered) {    if (!zoneAffinity && !zoneExclusive) {return false;}if (zoneExclusive) {return true;}LoadBalancerStats stats = getLoadBalancerStats();if (stats == null) {return zoneAffinity;} else {logger.debug("Determining if zone affinity should be enabled with given server list: {}", filtered);ZoneSnapshot snapshot = stats.getZoneSnapshot(filtered);double loadPerServer = snapshot.getLoadPerServer();int instanceCount = snapshot.getInstanceCount();            int circuitBreakerTrippedCount = snapshot.getCircuitTrippedCount();if (((double) circuitBreakerTrippedCount) / instanceCount >= blackOutServerPercentageThreshold.get() || loadPerServer >= activeReqeustsPerServerThreshold.get()|| (instanceCount - circuitBreakerTrippedCount) < availableServersThreshold.get()) {logger.debug("zoneAffinity is overriden. blackOutServerPercentage: {}, activeReqeustsPerServer: {}, availableServers: {}", new Object[] {(double) circuitBreakerTrippedCount / instanceCount,  loadPerServer, instanceCount - circuitBreakerTrippedCount});return false;} else {return true;}}}@Overridepublic List<T> getFilteredListOfServers(List<T> servers) {if (zone != null && (zoneAffinity || zoneExclusive) && servers !=null && servers.size() > 0){List<T> filteredServers = Lists.newArrayList(Iterables.filter(servers, this.zoneAffinityPredicate.getServerOnlyPredicate()));if (shouldEnableZoneAffinity(filteredServers)) {return filteredServers;} else if (zoneAffinity) {overrideCounter.increment();}}return servers;}@Overridepublic String toString(){StringBuilder sb = new StringBuilder("ZoneAffinityServerListFilter:");sb.append(", zone: ").append(zone).append(", zoneAffinity:").append(zoneAffinity);sb.append(", zoneExclusivity:").append(zoneExclusive);return sb.toString();       }
}

(2)ZonePreferenceServerListFilter

public class ZonePreferenceServerListFilter extends ZoneAffinityServerListFilter<Server> {private String zone;@Overridepublic void initWithNiwsConfig(IClientConfig niwsClientConfig) {super.initWithNiwsConfig(niwsClientConfig);if (ConfigurationManager.getDeploymentContext() != null) {this.zone = ConfigurationManager.getDeploymentContext().getValue(ContextKey.zone);}}@Overridepublic List<Server> getFilteredListOfServers(List<Server> servers) {List<Server> output = super.getFilteredListOfServers(servers);if (this.zone != null && output.size() == servers.size()) {List<Server> local = new ArrayList<>();for (Server server : output) {if (this.zone.equalsIgnoreCase(server.getZone())) {local.add(server);}}if (!local.isEmpty()) {return local;}}return output;}public String getZone() {return zone;}public void setZone(String zone) {this.zone = zone;}@Overridepublic boolean equals(Object o) {if (this == o) {return true;}if (o == null || getClass() != o.getClass()) {return false;}ZonePreferenceServerListFilter that = (ZonePreferenceServerListFilter) o;return Objects.equals(zone, that.zone);}@Overridepublic int hashCode() {return Objects.hash(zone);}@Overridepublic String toString() {return new StringBuilder("ZonePreferenceServerListFilter{").append("zone='").append(zone).append("'").append("}").toString();}}

(3)ServerListSubsetFilter 

public class ServerListSubsetFilter<T extends Server> extends ZoneAffinityServerListFilter<T> implements IClientConfigAware, Comparator<T>{private Random random = new Random();private volatile Set<T> currentSubset = Sets.newHashSet(); private DynamicIntProperty sizeProp = new DynamicIntProperty(DefaultClientConfigImpl.DEFAULT_PROPERTY_NAME_SPACE + ".ServerListSubsetFilter.size", 20);private DynamicFloatProperty eliminationPercent = new DynamicFloatProperty(DefaultClientConfigImpl.DEFAULT_PROPERTY_NAME_SPACE + ".ServerListSubsetFilter.forceEliminatePercent", 0.1f);private DynamicIntProperty eliminationFailureCountThreshold = new DynamicIntProperty(DefaultClientConfigImpl.DEFAULT_PROPERTY_NAME_SPACE + ".ServerListSubsetFilter.eliminationFailureThresold", 0);private DynamicIntProperty eliminationConnectionCountThreshold = new DynamicIntProperty(DefaultClientConfigImpl.DEFAULT_PROPERTY_NAME_SPACE + ".ServerListSubsetFilter.eliminationConnectionThresold", 0);@Overridepublic void initWithNiwsConfig(IClientConfig clientConfig) {super.initWithNiwsConfig(clientConfig);sizeProp = new DynamicIntProperty(clientConfig.getClientName() + "." + clientConfig.getNameSpace() + ".ServerListSubsetFilter.size", 20);eliminationPercent = new DynamicFloatProperty(clientConfig.getClientName() + "." + clientConfig.getNameSpace() + ".ServerListSubsetFilter.forceEliminatePercent", 0.1f);eliminationFailureCountThreshold = new DynamicIntProperty( clientConfig.getClientName()  + "." + clientConfig.getNameSpace()+ ".ServerListSubsetFilter.eliminationFailureThresold", 0);eliminationConnectionCountThreshold = new DynamicIntProperty(clientConfig.getClientName() + "." + clientConfig.getNameSpace()+ ".ServerListSubsetFilter.eliminationConnectionThresold", 0);}/*** Given all the servers, keep only a stable subset of servers to use. This method* keeps the current list of subset in use and keep returning the same list, with exceptions* to relatively unhealthy servers, which are defined as the following:* <p>* <ul>* <li>Servers with their concurrent connection count exceeding the client configuration for *  {@code <clientName>.<nameSpace>.ServerListSubsetFilter.eliminationConnectionThresold} (default is 0)* <li>Servers with their failure count exceeding the client configuration for *  {@code <clientName>.<nameSpace>.ServerListSubsetFilter.eliminationFailureThresold}  (default is 0)*  <li>If the servers evicted above is less than the forced eviction percentage as defined by client configuration*   {@code <clientName>.<nameSpace>.ServerListSubsetFilter.forceEliminatePercent} (default is 10%, or 0.1), the*   remaining servers will be sorted by their health status and servers will worst health status will be*   forced evicted.* </ul>* <p>* After the elimination, new servers will be randomly chosen from all servers pool to keep the* number of the subset unchanged. * */@Overridepublic List<T> getFilteredListOfServers(List<T> servers) {List<T> zoneAffinityFiltered = super.getFilteredListOfServers(servers);Set<T> candidates = Sets.newHashSet(zoneAffinityFiltered);Set<T> newSubSet = Sets.newHashSet(currentSubset);LoadBalancerStats lbStats = getLoadBalancerStats();for (T server: currentSubset) {// this server is either down or out of serviceif (!candidates.contains(server)) {newSubSet.remove(server);} else {ServerStats stats = lbStats.getSingleServerStat(server);// remove the servers that do not meet health criteriaif (stats.getActiveRequestsCount() > eliminationConnectionCountThreshold.get()|| stats.getFailureCount() > eliminationFailureCountThreshold.get()) {newSubSet.remove(server);// also remove from the general pool to avoid selecting them againcandidates.remove(server);}}}int targetedListSize = sizeProp.get();int numEliminated = currentSubset.size() - newSubSet.size();int minElimination = (int) (targetedListSize * eliminationPercent.get());int numToForceEliminate = 0;if (targetedListSize < newSubSet.size()) {// size is shrinkingnumToForceEliminate = newSubSet.size() - targetedListSize;} else if (minElimination > numEliminated) {numToForceEliminate = minElimination - numEliminated; }if (numToForceEliminate > newSubSet.size()) {numToForceEliminate = newSubSet.size();}if (numToForceEliminate > 0) {List<T> sortedSubSet = Lists.newArrayList(newSubSet);           Collections.sort(sortedSubSet, this);List<T> forceEliminated = sortedSubSet.subList(0, numToForceEliminate);newSubSet.removeAll(forceEliminated);candidates.removeAll(forceEliminated);}// after forced elimination or elimination of unhealthy instances,// the size of the set may be less than the targeted size,// then we just randomly add servers from the big poolif (newSubSet.size() < targetedListSize) {int numToChoose = targetedListSize - newSubSet.size();candidates.removeAll(newSubSet);if (numToChoose > candidates.size()) {// Not enough healthy instances to choose, fallback to use the// total server poolcandidates = Sets.newHashSet(zoneAffinityFiltered);candidates.removeAll(newSubSet);}List<T> chosen = randomChoose(Lists.newArrayList(candidates), numToChoose);for (T server: chosen) {newSubSet.add(server);}}currentSubset = newSubSet;       return Lists.newArrayList(newSubSet);            }/*** Randomly shuffle the beginning portion of server list (according to the number passed into the method) * and return them.*  * @param servers* @param toChoose* @return*/private List<T> randomChoose(List<T> servers, int toChoose) {int size = servers.size();if (toChoose >= size || toChoose < 0) {return servers;} for (int i = 0; i < toChoose; i++) {int index = random.nextInt(size);T tmp = servers.get(index);servers.set(index, servers.get(i));servers.set(i, tmp);}return servers.subList(0, toChoose);        }/*** Function to sort the list by server health condition, with* unhealthy servers before healthy servers. The servers are first sorted by* failures count, and then concurrent connection count.*/@Overridepublic int compare(T server1, T server2) {LoadBalancerStats lbStats = getLoadBalancerStats();ServerStats stats1 = lbStats.getSingleServerStat(server1);ServerStats stats2 = lbStats.getSingleServerStat(server2);int failuresDiff = (int) (stats2.getFailureCount() - stats1.getFailureCount());if (failuresDiff != 0) {return failuresDiff;} else {return (stats2.getActiveRequestsCount() - stats1.getActiveRequestsCount());}}
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/322065.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

听说你开发.NET还在用VS,小哥哥给你推荐全平台的Rider

前言.NET平台的开发一直都只能使用Visual Studio来开发&#xff0c;自从dotnet core 发布后不久&#xff0c;jetbrains 发布了Rider预览版&#xff0c;到目前为止的正式版2017.3.1。博主都使用过&#xff0c;因为博主的主力开发语言是C#&#xff0c;所以一直以来被捆绑到Window…

给Ocelot做一个Docker 镜像

写在前面在微服务架构中&#xff0c;ApiGateway起到了承前启后&#xff0c;不仅可以根据客户端进行分类&#xff0c;也可以根据功能业务进行分类&#xff0c;而且对于服务调用服务也起到了很好的接口作用。目前在各个云端中&#xff0c;基本上都提供了ApiGateway的功能&#xf…

.NET Core UI框架Avalonia

.NET Core UI框架Avalonia&#xff0c;Avalonia是一个基于WPF XAML的跨平台UI框架&#xff0c;并支持多种操作系统&#xff1a;Windows&#xff08;.NET Framework&#xff0c;.NET Core&#xff09;&#xff0c;Linux&#xff08;GTK&#xff09;&#xff0c;MacOS&#xff0c…

揽货最短路径解决方案算法 - C# 蚁群优化算法实现

需求为&#xff08;自己编的&#xff0c;非实际项目&#xff09;&#xff1a;某配送中心进行揽货&#xff0c;目标客户数为50个客户&#xff0c;配送中心目前的运力资源如下&#xff1a;现有车辆5台单台运力最大行驶距离200千米单台运力最大载重公斤1吨问&#xff1a;运力怎样走…

OIDC在 ASP.NET Core中的应用

我们在《ASP.NET Core项目实战的课程》第一章里面给identity server4做了一个全面的介绍和示例的练习 。如果想完全理解本文所涉及到的话题&#xff0c;你需要了解的背景知识有&#xff1a;什么是OpenId Connect (OIDC)OIDC 对oAuth进行了哪些扩展&#xff1f;Identity Server4…

论文阅读:Blind Super-Resolution Kernel Estimation using an Internal-GAN

这是发表在 2019 年 NIPS 上的一篇文章&#xff0c;那个时候还叫 NIPS&#xff0c;现在已经改名为 NeurIPS 了。文章中的其中一个作者 Michal Irani 是以色 Weizmann Institute of Science (魏茨曼科学研究学院) 的一名教授&#xff0c;对图像纹理的内在统计规律有着很深入的研…

【ASP.NET Core】处理异常

依照老周的良好作风&#xff0c;开始之前先说点题外话。前面的博文中&#xff0c;老周介绍过自定义 MVC 视图的搜索路径&#xff0c;即向 ViewLocationFormats 列表添加相应的内容&#xff0c;其实&#xff0c;对 Razor Page 模型&#xff0c;也可以向 PageViewLocationFormats…

树莓派3B上部署运行.net core 2程序

针对Linxu arm处理器如何部署.net core 2的资料很少&#xff0c;网上找到几篇但都写得不够详细&#xff0c;按照他们教程来撞墙了&#xff0c;折磨了几天终于部署成功了&#xff0c;先上一张运行成功的图1.windows系统中&#xff0c;在项目的目录下使用CMD命令运行进行发布dotn…

拥抱.NET Core系列:MemoryCache 初识

MSCache能做什么&#xff1f;绝对过期支持滑动过期支持&#xff08;指定一个时间&#xff0c;TimeSpan&#xff0c;指定时间内有被Get缓存时间则顺延&#xff0c;否则过期&#xff09;过期回调自定义过期MSCache目前最新的正式版是 2.0.0&#xff0c;预览版是2.1.0&#xff0c;…

Spark Structure Streaming(一)之简介

一、Structure Streaming 结构化流是基于Spark SQL引擎构建的可伸缩且容错的流处理引擎。可以像对静态数据进行批处理计算一样&#xff0c;来表示流计算。 当流数据继续到达时&#xff0c;Spark SQL引擎将负责递增地&#xff0c;连续地运行它并更新最终结果。可以在Scala&…

Ocelot中使用Butterfly实践

Ocelot(https://github.com/TomPallister/Ocelot)是一个用.net core实现的API网关&#xff0c;Butterfly(https://github.com/ButterflyAPM/butterfly)是用.net core实现的全程序跟踪&#xff0c;现在&#xff0c;Ocelot中可以使用Butterfly了&#xff0c;关于Ocelot和Butterfl…

jzoj6290-倾斜的线【计算几何,贪心】

正题 题目大意 有nnn个点&#xff0c;将两个点连成线&#xff0c;求斜率最接近PQ\frac{P}{Q}QP​的线。 解题思路 我们有一个结论&#xff1a;若我们对于每一个点做一条斜率为PQ\frac{P}{Q}QP​的线&#xff0c;然后按截距排序&#xff0c;然后答案必定是相邻的点。 证明: 我…

Java 平台调试架构JPDA

转载自 Java-JPDA 概述 JPDA&#xff1a;Java 平台调试架构&#xff08;Java Platform Debugger Architecture&#xff09; 它是 Java 虚拟机为调试和监控虚拟机专门提供的一套接口。 一、JPDA https://docs.oracle.com/javase/8/docs/technotes/guides/jpda/ JPDA 由三个…

Ocelot + Consul实践

关于Consul(https://www.consul.io)是一个分布式&#xff0c;高可用,支持多数据中心的服务发现和配置共享的服务软件,由 HashiCorp 公司用 Go 语言开发, 基于 Mozilla Public License 2.0 的协议进行开源。 在Consul的文档上&#xff0c;Consul 支持Service Discovery, Health …

Arthas - 开源 Java 诊断工具

转载自 Arthas使用 Authas — 开源的java诊断工具 下载安装 authas是一个jar包&#xff0c;可以直接下载后运行 wget https://alibaba.github.io/arthas/arthas-boot.jarjava -jar arthas-boot.jar就可以启动起来。启动后&#xff0c;authas会自动检测存在的java进程&…

jzoj6307-安排【归并排序】

正题 题目大意 一个目前序列&#xff0c;一个目标序列&#xff0c;每次可以选择一个区间交换区间最大值和最小值。 询问在345678345678345678步内将目前序列转换回目标序列的方案(输出该方案)。 解题思路 我们考虑归并排序&#xff0c;对于两个升序的序列&#xff0c;我们考…

.NET Core 2.1 Preview 1发布:更快的构建性能

今天&#xff0c;我们宣布发布 .NET Core 2.1 Preview 1。这是 .NET Core 2.1 的第一个公开发布。我们有很大的改进希望分享出来&#xff0c;并且渴望得到您的反馈意见&#xff0c;无论是在评论中还是在github中dotnet/core #1297ASP.NET Core 2.1 Preview 1 和 Entity Framewo…

Spark SQL(七)之基于用户的相似度公式

一、基于用户的Jaccard相似度公式 其中&#xff0c;u、v表示任意两个用户&#xff0c;N(u)表示用户u喜欢的物品集合,N(v)表示用户v喜欢物品的集合。 代码 public class UserCFApp {public static void main(String[]args){SparkConf sparkConf new SparkConf();sparkConf.se…

欢乐纪中A组赛【2019.8.17】

前言 前几天题目没改完(好难QvQQvQQvQ)&#xff0c;然后这几天ZZYZZYZZY和WHFWHFWHF去广州二中了 然后我是菜鸡&#xff0c;今天暴力写挂了QAQQAQQAQ&#xff0c;T2T2T2少判断了个东西少了808080 成绩 懒得写只放自己的了(反正垫底) Rank51,20ptsRank51,20ptsRank51,20pts 正…

BXUG第11期活动

分享主题&#xff1a;Xamarin Azure 微软云加端移动应用技术架构分享者&#xff1a;周岳 微软MVP分享主题&#xff1a;从设计图到最终界面- Xamarin跨平台界面最佳实践分享者: 程文锋 视高盛景分享主题&#xff1a;基于VSTS的App DevOps分享者&#xff1a; 安庭庭 张浩 视高…