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…

jzoj6293-迷宫【ddp,线段树,矩阵乘法】

正题 题目大意 一个n∗mn*mn∗m的迷宫&#xff0c;不能往左走&#xff0c;有墙&#xff0c;每次修改一个点或询问两个点之间的最短距离。 解题思路 考虑到nnn的值很小&#xff0c;所以我们可以用矩阵转移&#xff0c;然后要求支持修改和查询所以我们考虑ddpddpddp。 我们可以…

Scala与Java差异(六)之类定义

一、类定义 &#xff08;1&#xff09;定义类&#xff0c;包含field以及方法 class ScalaClass {private var field "one"def aMethod() { print("field : " field ) } def getField field } &#xff08;2&#xff09;调用对象方法 val scalaCla…

jzoj6297-世界第一的猛汉王【切比雪夫距离,扫描线】

正题 题目大意 有若干个红点和蓝点&#xff0c;对于每一对红点和蓝点&#xff0c;若距离大于DDD则蓝点压制红点&#xff0c;否则红点压制蓝点。然后红点和蓝点之间也有不定的压制关系。 求有多少个三角要求AAA压制BBB&#xff0c;BBB压制CCC&#xff0c;CCC压制AAA且至少包含…

给Ocelot做一个Docker 镜像

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

Spark SQL(一)之简介

Spark SQL是用于结构化数据处理的Spark模块。与基本的Spark RDD API不同&#xff0c;Spark SQL提供的接口为Spark提供了有关数据结构和正在执行的计算的更多信息。在内部&#xff0c;Spark SQL使用这些额外的信息来执行额外的优化。 与Spark SQL交互的方法有多种&#xff0c;包…

hdu4336-Card Collector【min-max容斥,期望概率】

正题 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid4336 题目大意 每次获得第iii张牌的概率为pip_ipi​(每次只能获得一张)&#xff0c;期望多少回合后获得所有牌。 解题思路 min−maxmin-maxmin−max容斥:max{S}∑T⊆S(−1)∣T∣−1min{T}max\{S\}\sum_{T\subseteq …

.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…

Spark SQL(二)之DataSet操作

一、创建DataSet 使用SparkSession&#xff0c;应用程序可以从现有的RDD&#xff0c;Hive表的或Spark数据源创建DataFrame 。 &#xff08;1&#xff09;基于JSON的内容创建一个DataFrame //hdfs Dataset<Row> df spark.read().json("hdfs://master:9000/test.j…

jzoj6296-投票【期望dp,贪心】

正题 题目大意 nnn个人&#xff0c;第iii投票的概率是pip_ipi​&#xff0c;选择kkk个求最大的平票概率。 解题思路 我们显然要让kkk人中一半投票的概率大&#xff0c;一半投票的概率小。 所以我们可以先进行排序&#xff0c;这样我们发现答案一定是选取一段前缀和一段后缀。…

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

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

Spark SQL(三)之视图与执行SQL

一、视图与sql执行 SparkSession能够以编程方式运行SQL查询并返回结果Dataset<Row> Dataset<Row> df spark.read().json("hdfs://master:9000/test.json"); df.createOrReplaceTempView("man");Dataset<Row> sqlDF spark.sql("…

hdu4965-Fast Matrix Calculation【矩阵乘法】

正题 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid4965 题目大意 给出矩阵A,BA,BA,B&#xff0c;求(AB)n(AB)^n(AB)n。然后对于每个元素%6\% 6%6后取和。 题目大意 我们发现如果直接让AB∗ABAB*ABAB∗AB这样的时间复杂度是n3n^3n3&#xff0c;显然不可过。但是我们…

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;对图像纹理的内在统计规律有着很深入的研…

Spark SQL(四)之DataSet与RDD转换

一、创建DataSet DataSet与RDD相似&#xff0c;但是&#xff0c;它们不使用Java序列化或Kryo&#xff0c;而是使用专用的Encoder对对象进行序列化以进行网络处理或传输。虽然编码器和标准序列化都负责将对象转换为字节&#xff0c;但是编码器是动态生成的代码&#xff0c;并使…

P3389-[模板]高斯消元法

正题 题目链接:https://www.luogu.org/problem/P3389 题目大意 给出一个nnn元一次方程组&#xff0c;求解。 解题思路 模板&#xff0c;有什么好说的吗 codecodecode #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> usi…

【ASP.NET Core】处理异常

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

Spark SQL(五)之数据加载与存储

一、数据加载 &#xff08;1&#xff09;默认数据源&#xff08;parquet&#xff09; 最简单加载数据的方式&#xff0c;所有操作都使用默认数据源&#xff08;parquet&#xff09;。如果指定默认数据源需要配置 spark.sql.sources.default参数。 Dataset<Row> manDF …

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

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