内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

如何理解DependenciesBasedLoadBalancer

這篇文章給大家介紹如何理解DependenciesBasedLoadBalancer,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

坊子網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站開發(fā)等網(wǎng)站項目制作,到程序開發(fā),運營維護(hù)。創(chuàng)新互聯(lián)2013年開創(chuàng)至今到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。

本文主要研究一下DependenciesBasedLoadBalancer

DependenciesBasedLoadBalancer

spring-cloud-zookeeper-discovery-2.1.2.RELEASE-sources.jar!/org/springframework/cloud/zookeeper/discovery/dependency/DependenciesBasedLoadBalancer.java

public class DependenciesBasedLoadBalancer extends DynamicServerListLoadBalancer {

	private static final Log log = LogFactory.getLog(DependenciesBasedLoadBalancer.class);

	private final Map<String, IRule> 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繼承了com.netflix.loadbalancer.DynamicServerListLoadBalancer

  • 其chooseServer方法會使用zookeeperDependencies.getDependencyForAlias來跟進(jìn)key獲取ZookeeperDependency,如果dependency為null則直接使用rule.choose(key),不為null則進(jìn)行chahe,然后更新server列表,最后通過ruleCache.get(keyAsString).choose(key)返回

  • cacheEntryIfMissing方法會根據(jù)ZookeeperDependency.getLoadBalancerType()進(jìn)行chooseRuleForLoadBalancerType,這里分為了ROUND_ROBIN、RANDOM、STICKY三種

StickyRule

spring-cloud-zookeeper-discovery-2.1.2.RELEASE-sources.jar!/org/springframework/cloud/zookeeper/discovery/dependency/StickyRule.java

public class StickyRule extends AbstractLoadBalancerRule {

	private static final Log log = LogFactory.getLog(StickyRule.class);

	private final IRule masterStrategy;

	private final AtomicReference<Server> 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<Server> 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繼承了com.netflix.loadbalancer.AbstractLoadBalancerRule;其choose方法首先通過getLoadBalancer().getServerList(true)獲取server列表,對于該列表沒有l(wèi)ocalOurInstance的,則更新本地引用為null;然后判斷l(xiāng)ocalOurInstance是否為null,為null的話則使用masterStrategy.choose(key)進(jìn)行選擇然后更新;最后返回ourInstance.get()

小結(jié)

  • DependenciesBasedLoadBalancer繼承了com.netflix.loadbalancer.DynamicServerListLoadBalancer

  • 其chooseServer方法會使用zookeeperDependencies.getDependencyForAlias來跟進(jìn)key獲取ZookeeperDependency,如果dependency為null則直接使用rule.choose(key),不為null則進(jìn)行chahe,然后更新server列表,最后通過ruleCache.get(keyAsString).choose(key)返回

  • cacheEntryIfMissing方法會根據(jù)ZookeeperDependency.getLoadBalancerType()進(jìn)行chooseRuleForLoadBalancerType,這里分為了ROUND_ROBIN、RANDOM、STICKY三種

關(guān)于如何理解DependenciesBasedLoadBalancer就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

新聞標(biāo)題:如何理解DependenciesBasedLoadBalancer
文章網(wǎng)址:http://www.rwnh.cn/article22/ghcdjc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、面包屑導(dǎo)航營銷型網(wǎng)站建設(shè)、靜態(tài)網(wǎng)站企業(yè)網(wǎng)站制作、搜索引擎優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)
漳州市| 筠连县| 南川市| 长汀县| 上杭县| 苍山县| 长春市| 梅河口市| 怀宁县| 宾川县| 遂溪县| 胶州市| 虞城县| 通州区| 台东市| 营口市| 鹿邑县| 淳安县| 新营市| 闽清县| 嘉善县| 洛扎县| 泽库县| 砀山县| 二手房| 日土县| 柘荣县| 安多县| 枣阳市| 正定县| 昌图县| 红安县| 新巴尔虎右旗| 明水县| 稷山县| 象州县| 营山县| 深泽县| 内江市| 莎车县| 得荣县|