Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to understand DependenciesBasedLoadBalancer

Shulou Source: shulou.com Published: 2022-06-02 02:09:43 09月14日 Update

This article introduces you how to understand DependenciesBasedLoadBalancer, the content is very detailed, interested friends can use for reference, hope to be helpful to you.

Order

This paper mainly studies DependenciesBasedLoadBalancer.

DependenciesBasedLoadBalancer

SpringMurray cloudlyzookeepercopyright discoveryMur2.1.2.RELEASEsarsources.jarbrands to orgSpringframeworkDependenciesBasedLoadBalancer.java

Public class DependenciesBasedLoadBalancer extends DynamicServerListLoadBalancer {private static final Log log = LogFactory.getLog (DependenciesBasedLoadBalancer.class); private final Map ruleCache = new ConcurrentHashMap (); private final ZookeeperDependencies zookeeperDependencies; public DependenciesBasedLoadBalancer (ZookeeperDependencies zookeeperDependencies, ServerList serverList, IClientConfig config, IPing iPing) {super (config); this.zookeeperDependencies = zookeeperDependencies; setServersList (serverList.getInitialListOfServers ()) SetPing (iPing); setServerListImpl (serverList);} @ Override public Server chooseServer (Object key) {String keyAsString; if ("default" .equals (key)) {/ / this is the default hint, use name instead keyAsString = getName () } else {keyAsString = (String) key;} ZookeeperDependency dependency = this.zookeeperDependencies .getDependencyForAlias (keyAsString); log.debug (String.format ("Current dependencies are [% s]", this.zookeeperDependencies)) If (dependency = = null) {log.debug (String.format ("No dependency found for alias [% s]-will use the default rule which is [% s]", keyAsString, this.rule)); return this.rule.choose (key) } cacheEntryIfMissing (keyAsString, dependency); log.debug (String.format ("Will try to retrieve dependency for key [% s]. Current cache contents [% s] ", keyAsString, this.ruleCache); updateListOfServers (); return this.ruleCache.get (keyAsString) .choose (key) } private void cacheEntryIfMissing (String keyAsString, ZookeeperDependency dependency) {if (! this.ruleCache.containsKey (keyAsString)) {log.debug (String.format ("Cache doesn't contain entry for [% s]", keyAsString)); this.ruleCache.put (keyAsString, chooseRuleForLoadBalancerType (dependency.getLoadBalancerType () }} private IRule chooseRuleForLoadBalancerType (LoadBalancerType type) {switch (type) {case ROUND_ROBIN: return getRoundRobinRule (); case RANDOM: return getRandomRule (); case STICKY: return getStickyRule () Default: throw new IllegalArgumentException ("Unknown load balancer type" + type);}} private RoundRobinRule getRoundRobinRule () {return new RoundRobinRule (this);} private IRule getRandomRule () {RandomRule randomRule = new RandomRule (); randomRule.setLoadBalancer (this) Return randomRule;} private IRule getStickyRule () {StickyRule stickyRule = new StickyRule (getRoundRobinRule ()); stickyRule.setLoadBalancer (this); return stickyRule;}}

DependenciesBasedLoadBalancer inherits com.netflix.loadbalancer.DynamicServerListLoadBalancer

Its chooseServer method uses zookeeperDependencies.getDependencyForAlias to follow up with key to get ZookeeperDependency, if dependency is null, use rule.choose (key) directly, if not null, chahe, then update the server list, and finally return via ruleCache.get (keyAsString) .choose (key).

The cacheEntryIfMissing method will chooseRuleForLoadBalancerType according to ZookeeperDependency.getLoadBalancerType (), which is divided into ROUND_ROBIN, RANDOM and STICKY.

StickyRule

SpringMurray cloudlyzookeepercopyright discoveryMur2.1.2.RELEASEsulces.jarframes to orgpickSpringframeworkMakeeperStickyRule.java

Public class StickyRule extends AbstractLoadBalancerRule {private static final Log log = LogFactory.getLog (StickyRule.class); private final IRule masterStrategy; private final AtomicReference ourInstance = new AtomicReference (null); private final AtomicInteger instanceNumber = new AtomicInteger (- 1); public StickyRule (IRule masterStrategy) {this.masterStrategy = masterStrategy } @ Override public void initWithNiwsConfig (IClientConfig iClientConfig) {} @ Override public Server choose (Object key) {final List instances = getLoadBalancer () .getServerList (true); log.debug (String.format ("Instances taken from load balancer [% s]", instances)); Server localOurInstance = this.ourInstance.get () Log.debug (String.format ("Current saved instance [% s]", localOurInstance)); if (! instances.contains (localOurInstance)) {this.ourInstance.compareAndSet (localOurInstance, null);} if (this.ourInstance.get () = = null) {Server instance = this.masterStrategy.choose (key) If (this.ourInstance.compareAndSet (null, instance)) {this.instanceNumber.incrementAndGet ();}} return this.ourInstance.get ();} / * Each time a new instance is picked, an internal counter is incremented. This way * you can track when/if the instance changes. The instance can change when the * selected instance is not in the current list of instances returned by the instance * provider * @ return instance number * / public int getInstanceNumber () {return this.instanceNumber.get ();}}

StickyRule inherits com.netflix.loadbalancer.AbstractLoadBalancerRule; 's choose method to first get the server list through getLoadBalancer (). GetServerList (true). If there is no localOurInstance in the list, update the local reference to null; and then determine whether localOurInstance is null; if it is null, use masterStrategy.choose (key) to select and update; finally return ourInstance.get ()

Summary

DependenciesBasedLoadBalancer inherits com.netflix.loadbalancer.DynamicServerListLoadBalancer

Its chooseServer method uses zookeeperDependencies.getDependencyForAlias to follow up with key to get ZookeeperDependency, if dependency is null, use rule.choose (key) directly, if not null, chahe, then update the server list, and finally return via ruleCache.get (keyAsString) .choose (key).

The cacheEntryIfMissing method will chooseRuleForLoadBalancerType according to ZookeeperDependency.getLoadBalancerType (), which is divided into ROUND_ROBIN, RANDOM and STICKY.

On how to understand DependenciesBasedLoadBalancer to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Methods updates content more help good interest guys buddies summary articles knowledge articles references research selection Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Shulou Information Redmi Xiaomi Shulou Technology