這篇文章將為大家詳細(xì)講解有關(guān)ribbon中ServerListSubsetFilter的作用是什么,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
創(chuàng)新互聯(lián)公司是專(zhuān)業(yè)的中牟網(wǎng)站建設(shè)公司,中牟接單;提供成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站,網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專(zhuān)業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行中牟網(wǎng)站開(kāi)發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專(zhuān)業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,專(zhuān)業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!
ribbon-loadbalancer-2.3.0-sources.jar!/com/netflix/loadbalancer/ServerListSubsetFilter.java
public class ServerListSubsetFilterextends ZoneAffinityServerListFilter implements IClientConfigAware, Comparator { private Random random = new Random(); private volatile Set 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); @Override public 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); } @Override public List getFilteredListOfServers(List servers) { List zoneAffinityFiltered = super.getFilteredListOfServers(servers); Set candidates = Sets.newHashSet(zoneAffinityFiltered); Set newSubSet = Sets.newHashSet(currentSubset); LoadBalancerStats lbStats = getLoadBalancerStats(); for (T server: currentSubset) { // this server is either down or out of service if (!candidates.contains(server)) { newSubSet.remove(server); } else { ServerStats stats = lbStats.getSingleServerStat(server); // remove the servers that do not meet health criteria if (stats.getActiveRequestsCount() > eliminationConnectionCountThreshold.get() || stats.getFailureCount() > eliminationFailureCountThreshold.get()) { newSubSet.remove(server); // also remove from the general pool to avoid selecting them again candidates.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 shrinking numToForceEliminate = newSubSet.size() - targetedListSize; } else if (minElimination > numEliminated) { numToForceEliminate = minElimination - numEliminated; } if (numToForceEliminate > newSubSet.size()) { numToForceEliminate = newSubSet.size(); } if (numToForceEliminate > 0) { List sortedSubSet = Lists.newArrayList(newSubSet); Collections.sort(sortedSubSet, this); List 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 pool if (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 pool candidates = Sets.newHashSet(zoneAffinityFiltered); candidates.removeAll(newSubSet); } List 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 randomChoose(List 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. */ @Override public 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()); } } }
ServerListSubsetFilter繼承了ZoneAffinityServerListFilter,實(shí)現(xiàn)了IClientConfigAware、Comparator接口
initWithNiwsConfig方法從IClientConfig讀取了ServerListSubsetFilter.size、ServerListSubsetFilter.forceEliminatePercent、ServerListSubsetFilter.eliminationFailureThresold、ServerListSubsetFilter.eliminationConnectionThresold配置
getFilteredListOfServers方法會(huì)先調(diào)用父類(lèi)ZoneAffinityServerListFilter的getFilteredListOfServers先過(guò)濾出zoneAffinityFiltered作為candidates,然后遍歷currentSubset根據(jù)ServerStats剔除activeRequestsCount及failureCount超出閾值的server;然后根據(jù)numToForceEliminate,以及failureCount排序,使用subList方法得出forceEliminated,然后移除掉,最后randomChoose出來(lái)新的newSubSet,然后重置currentSubset
在server list非常多的場(chǎng)景下,沒(méi)有必要在連接池的保持這么多的連接,ServerListSubsetFilter可以在這種場(chǎng)景下對(duì)server list進(jìn)行精簡(jiǎn),通過(guò)剔除相對(duì)不健康(failureCount、activeRequestCount
)的server來(lái)達(dá)到此目標(biāo)
關(guān)于ribbon中ServerListSubsetFilter的作用是什么就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。