In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
IRule is a strategy for selecting services.
IRule
Public interface IRule {/ * choose one alive server from lb.allServers or * lb.upServers according to key * * @ return choosen Server object. NULL is returned if none * server is available * / public Server choose (Object key); public void setLoadBalancer (ILoadBalancer lb); public ILoadBalancer getLoadBalancer ();}
Choose selects the available services.
RandomRule
Randomly select a UP service.
Random rand; / / Random counter public RandomRule () {rand = new Random ();} public Server choose (ILoadBalancer lb, Object key) {. List upList = lb.getReachableServers (); List allList = lb.getAllServers (); int index = rand.nextInt (serverCount); / / randomly select a server = upList.get (index);...}
RoundRobinRule
Poll for services.
Public RoundRobinRule () {nextServerCyclicCounter = new AtomicInteger (0); / / int thread safety counter} public Server choose (ILoadBalancer lb, Object key) {... Int nextServerIndex = incrementAndGetModulo (serverCount); / / nextServerCyclicCounter gets services backward in turn. Server = allServers.get (nextServerIndex);...} / / polling method private int incrementAndGetModulo (int modulo) {for (;;) {int current = nextServerCyclicCounter.get (); int next = (current + 1)% modulo; if (nextServerCyclicCounter.compareAndSet (current, next)) return next;}
BestAvailableRule
Skip the service of the circuit breaker and get the service with the least number of requests. It is usually used with ServerListSubsetFilter.
Public Server choose (Object key) {if (loadBalancerStats = = null) {return super.choose (key); / / if there is no loadBalancerStats, RoundRibonRule. } List serverList = getLoadBalancer (). GetAllServers (); int minimalConcurrentConnections = Integer.MAX_VALUE; long currentTime = System.currentTimeMillis (); Server chosen = null; for (Server server: serverList) {ServerStats serverStats = loadBalancerStats.getSingleServerStat (server); if (! serverStats.isCircuitBreakerTripped (currentTime)) {int concurrentConnections = serverStats.getActiveRequestsCount (currentTime); if (concurrentConnections
< minimalConcurrentConnections) { minimalConcurrentConnections = concurrentConnections; chosen = server; } } } if (chosen == null) { return super.choose(key); } else { return chosen; }} WeightedResponseTimeRule 权重的方式挑选服务.服务实例响应时间越小的服务,则更容易被选中.如果服务实例响应的时间相差不大的,排在前面的服务实例更容易被选中. // 继承了RoundRobinRule,也就是当WeightedResponseTimeRule不满足条件的时候,则采用RoundRobinRule的方式.public class WeightedResponseTimeRule extends RoundRobinRule {// 这个方式很重要,就是定时的计算每个服务实例的响应时间,并以此作为每个服务实例的权重.void initialize(ILoadBalancer lb) { if (serverWeightTimer != null) { serverWeightTimer.cancel(); } serverWeightTimer = new Timer("NFLoadBalancer-serverWeightTimer-" + name, true); serverWeightTimer.schedule(new DynamicServerWeightTask(), 0,serverWeightTaskTimerInterval); // do a initial run ServerWeight sw = new ServerWeight(); sw.maintainWeights(); Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { public void run() { logger.info("Stopping NFLoadBalancer-serverWeightTimer-"+ name); serverWeightTimer.cancel(); } }));}// 定时任务内部类class DynamicServerWeightTask extends TimerTask { public void run() { ServerWeight serverWeight = new ServerWeight(); try { serverWeight.maintainWeights(); } catch (Exception e) { logger.error("Error running DynamicServerWeightTask for {}", name, e); } }}// 计算服务实例权重的核心方法.class ServerWeight { public void maintainWeights() { ILoadBalancer lb = getLoadBalancer(); if (lb == null) { return; } if (!serverWeightAssignmentInProgress.compareAndSet(false, true)) { return; } try { logger.info("Weight adjusting job started"); AbstractLoadBalancer nlb = (AbstractLoadBalancer) lb; LoadBalancerStats stats = nlb.getLoadBalancerStats(); if (stats == null) { // no statistics, nothing to do return; } double totalResponseTime = 0; // find maximal 95% response time for (Server server : nlb.getAllServers()) { // this will automatically load the stats if not in cache ServerStats ss = stats.getSingleServerStat(server); totalResponseTime += ss.getResponseTimeAvg(); } // weight for each server is (sum of responseTime of all servers - responseTime) // so that the longer the response time, the less the weight and the less likely to be chosen Double weightSoFar = 0.0; // create new list and hot swap the reference List finalWeights = new ArrayList(); for (Server server : nlb.getAllServers()) { ServerStats ss = stats.getSingleServerStat(server); double weight = totalResponseTime - ss.getResponseTimeAvg(); // 平均响应时间越短,则权重越大,就越容易被选中. weightSoFar += weight; finalWeights.add(weightSoFar); } setWeights(finalWeights); } catch (Exception e) { logger.error("Error calculating server weights", e); } finally { serverWeightAssignmentInProgress.set(false); } }}public Server choose(ILoadBalancer lb, Object key) { ... // 根据权重选择服务的核心代码 double randomWeight = random.nextDouble() * maxTotalWeight; // pick the server index based on the randomIndex int n = 0; for (Double d : currentWeights) { if (d >= randomWeight) {serverIndex = n; break;} else {server = allList.get (serverIndex);.}}
RetryRule
On the basis of RoundRobinRule, the mechanism of retry is added.
ZoneAvoidanceRule
Use ZoneAvoidancePredicate and AvailabilityPredicate to determine whether to select a server, the former to determine whether the performance of a zone is available, eliminate unavailable zone (all server), and AvailabilityPredicate is used to filter out Server with too many connections.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.