In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "the use of RandomRule and RoundRobinRule in Ribbon". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The version of Ribbon is 2.3.0.release.
1.RandomRule
Figure 1
As shown in figure 1, RandomRule inherits AbstractLoadBalancerRule, and when choose (Object) is called, the inner method choose (ILoadBalancer lb, Object key) is called, as shown in List-1
List-1
Public Server choose (ILoadBalancer lb, Object key) {if (lb = = null) {return null;} else {Server server = null; while (server = = null) {if (Thread.interrupted ()) {return null;} List upList = lb.getReachableServers (); List allList = lb.getAllServers () Int serverCount = allList.size (); if (serverCount = = 0) {return null;} int index = this.chooseRandomInt (serverCount); server = (Server) upList.get (index); if (server = = null) {Thread.yield () } else {if (server.isAlive ()) {return server;} server = null; Thread.yield ();}} return server;}} protected int chooseRandomInt (int serverCount) {return ThreadLocalRandom.current () .nextInt (serverCount);}
Get all the services through ILoadBalancer. If the number of services is 0, return null directly.
Call the chooseRandomInt method, and the parameter is the number of services, so the random value returned is between 0 and the number of services. Interestingly, for the sake of multithread safety, java.util.concurrent.ThreadLocalRandom#current is used to obtain random values.
If the service is alive, return to change the service.
2.RoundRobinRule
Figure 2
RoundRobinRule is implemented by a round robin algorithm, and the choose (Object) method calls choose (ILoadBalancer lb, Object key), as shown in the following List-2
List-2
Private AtomicInteger nextServerCyclicCounter;public RoundRobinRule () {this.nextServerCyclicCounter = new AtomicInteger (0);} public RoundRobinRule (ILoadBalancer lb) {this (); this.setLoadBalancer (lb);} public Server choose (ILoadBalancer lb, Object key) {if (lb = = null) {log.warn ("no load balancer"); return null;} else {Server server = null; int count = 0 While (true) {if (server = = null & & count++)
< 10) { List reachableServers = lb.getReachableServers(); List allServers = lb.getAllServers(); int upCount = reachableServers.size(); int serverCount = allServers.size(); if (upCount != 0 && serverCount != 0) { int nextServerIndex = this.incrementAndGetModulo(serverCount); server = (Server)allServers.get(nextServerIndex); if (server == null) { Thread.yield(); } else { if (server.isAlive() && server.isReadyToServe()) { return server; } server = null; } continue; } log.warn("No up servers available from load balancer: " + lb); return null; } if (count >= 10) {log.warn ("No available alive servers after 10 tries from load balancer:" + lb);} return server;}} private int incrementAndGetModulo (int modulo) {int current; int next; do {current = this.nextServerCyclicCounter.get (); next = (current + 1)% modulo;} while (! this.nextServerCyclicCounter.compareAndSet (current, next)) Return next;}
A very important class attribute is AtomicInteger nextServerCyclicCounter, through which round robin is implemented.
ILoadBalancer gets a list of all services
After that, the incrementAndGetModulo method is called, and the parameter is the number of services. CAS is used in the incrementAndGetModulo method to achieve thread safety and obtain the subscript of the service.
After getting the service Server, it determines whether it is alive and ReadyToServe, and returns; if it has failed 10 times and has not been found, log prints the warn log prompt
This implementation is a simple round robin and does not implement a weighted RoundRibbon.
This is the end of the content of "how to use RandomRule and RoundRobinRule in Ribbon". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.