Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use BestAvailableRule and RetryRule in Ribbon

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "the use of BestAvailableRule and RetryRule in Ribbon". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the use of BestAvailableRule and RetryRule in Ribbon".

The version of Ribbon is 2.3.0.release.

1.BestAvailableRule

Figure 1

ClientConfigEnabledRoundRobinRule, as shown below, defines a class attribute called RoundRobinRule in the RoundRobinRule,choose method for selection, so this is the round robin algorithm.

List-1.1

Public class ClientConfigEnabledRoundRobinRule extends AbstractLoadBalancerRule {RoundRobinRule roundRobinRule = new RoundRobinRule (); public ClientConfigEnabledRoundRobinRule () {} public void initWithNiwsConfig (IClientConfig clientConfig) {this.roundRobinRule = new RoundRobinRule ();} public void setLoadBalancer (ILoadBalancer lb) {super.setLoadBalancer (lb); this.roundRobinRule.setLoadBalancer (lb) } public Server choose (Object key) {if (this.roundRobinRule! = null) {return this.roundRobinRule.choose (key);} else {throw new IllegalArgumentException ("This class has not been initialized with the RoundRobinRule class");}

BestAvailableRule inherits ClientConfigEnabledRoundRobinRule and internally implements the following List-1.2, traversing all service providers and choosing the service with the least concurrency.

List-1.2

Private LoadBalancerStats loadBalancerStats;public Server choose (Object key) {if (this.loadBalancerStats = = null) {return super.choose (key);} else {List serverList = this.getLoadBalancer (). GetAllServers (); int minimalConcurrentConnections = 2147483647; long currentTime = System.currentTimeMillis (); Server chosen = null; Iterator var7 = serverList.iterator () While (var7.hasNext ()) {Server server = (Server) var7.next (); ServerStats serverStats = this.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; } }}public void setLoadBalancer(ILoadBalancer lb) { super.setLoadBalancer(lb); if (lb instanceof AbstractLoadBalancer) { this.loadBalancerStats = ((AbstractLoadBalancer)lb).getLoadBalancerStats(); }} choose方法重新了父类中的choose方法, 获取服务列表,遍历服务 通过ServerStats获取当前服务实例的并发连接数,如下List-3所示,并发连接数不是0,且当前时间与上次有效更改时间间隔在范围内,则返回当前并发连接数。 遍历所有的服务提供者后,如果得到的server是null,则调用父类的choose方法,用RoundRobin算法进行选择。 List-1.3 public int getActiveRequestsCount(long currentTime) { int count = this.activeRequestsCount.get(); if (count == 0) { return 0; } else if (currentTime - this.lastActiveRequestsCountChangeTimestamp = 0) { return count; } else { this.activeRequestsCount.set(0); return 0; }}2.RetryRule

Figure 2

The implementation of RetryRule is relatively simple, based on RoundRobinRule, as shown in the following List-2.1

List-2.1

Public class RetryRule extends AbstractLoadBalancerRule {IRule subRule = new RoundRobinRule (); long maxRetryMillis = 500L;... Public Server choose (ILoadBalancer lb, Object key) {long requestTime = System.currentTimeMillis (); long deadline = requestTime + this.maxRetryMillis; Server answer = null; answer = this.subRule.choose (key); if ((answer = = null | |! answer.isAlive ()) & & System.currentTimeMillis ()

< deadline) { InterruptTask task = new InterruptTask(deadline - System.currentTimeMillis()); while(!Thread.interrupted()) { answer = this.subRule.choose(key); if (answer != null && answer.isAlive() || System.currentTimeMillis() >

= deadline) {break;} Thread.yield ();} task.cancel ();} return answer! = null & & answer.isAlive ()? Answer: null;}...

Choose (Object key) in RetryRule calls choose (ILoadBalancer lb, Object key)

Current time plus maxRetryMillis to get deadline, that is, deadline

Use subRule to get the service server, and return the service directly if the service is valid

Construct InterruptTask, which has a Timer scheduled task, such as List-2.2. As long as the current thread is not interrupt, use subRule's RoundRobin algorithm to select a service instance. If the service is valid or the current time has passed the deadline, it will jump out of the default.

If the service instance obtained in step 3 is invalid and the current time is within the deadline, Thread.yield () is called to release thread resources to other threads

As can be seen from the source code, after RetryRule gets an invalid service instance in subRule.choose, it just uses subRule to get the service instance again, and will not try it all the time, that is, once.

List-2.2

Public class InterruptTask extends TimerTask {static Timer timer = new Timer ("InterruptTimer", true); protected Thread target = null; public InterruptTask (long millis) {this.target = Thread.currentThread (); timer.schedule (this, millis);} public boolean cancel () {try {return super.cancel ();} catch (Exception var2) {return false }} public void run () {if (this.target! = null & & this.target.isAlive ()) {this.target.interrupt ();}} Thank you for your reading, the above is the content of "the use of BestAvailableRule and RetryRule in Ribbon". After the study of this article, I believe you have a deeper understanding of the use of BestAvailableRule and RetryRule in Ribbon, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report