1. 程式人生 > >SpringCloud 自定義負載均衡演算法

SpringCloud 自定義負載均衡演算法

官方說明:自定義配置類不能放在@ComponentScan所掃描的當前包以及子包下面,否則我們自定義的配置類就會被所有的Ribbon客戶端共享,也就是說我們達不到特殊化定製的目的了。

@SpringBootApplication註解中有@CompontentScan註解,這個加上SpringBootApplication所在的類所在的包,不能有自定義的負載配置類

我們定義成每5次換一個服務提供,在不同包下建立mySelfRule配置類,建立自定義配置類RandomRule_ZY

照貓畫虎,複製一份隨機負載的服務,修改裡面的原始碼

package com.gcxzflgl.myRule;

import java.util.List;

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server;

public class RandomRule_ZY extends AbstractLoadBalancerRule
{

	// total = 0 // 當total==5以後,我們指標才能往下走,
	// index = 0 // 當前對外提供服務的伺服器地址,
	// total需要重新置為零,但是已經達到過一個5次,我們的index = 1
	// 分析:我們5次,但是微服務只有8001 8002 8003 三臺,OK?
	// 
	
	
	private int total = 0; 			// 總共被呼叫的次數,目前要求每臺被呼叫5次
	private int currentIndex = 0;	// 當前提供服務的機器號

	public Server choose(ILoadBalancer lb, Object key)
	{
		if (lb == null) {
			return null;
		}
		Server server = null;

		while (server == null) {
			if (Thread.interrupted()) {
				return null;
			}
			List<Server> upList = lb.getReachableServers();
			List<Server> allList = lb.getAllServers();

			int serverCount = allList.size();
			if (serverCount == 0) {
				/*
				 * No servers. End regardless of pass, because subsequent passes only get more
				 * restrictive.
				 */
				return null;
			}

//			int index = rand.nextInt(serverCount);// java.util.Random().nextInt(3);
//			server = upList.get(index);

			
//			private int total = 0; 			// 總共被呼叫的次數,目前要求每臺被呼叫5次
//			private int currentIndex = 0;	// 當前提供服務的機器號
            if(total < 5)
            {
	            server = upList.get(currentIndex);
	            total++;
            }else {
	            total = 0;
	            currentIndex++;
	            if(currentIndex >= upList.size())
	            {
	              currentIndex = 0;
	            }
            }			
			
			
			if (server == null) {
				/*
				 * The only time this should happen is if the server list were somehow trimmed.
				 * This is a transient condition. Retry after yielding.
				 */
				Thread.yield();
				continue;
			}

			if (server.isAlive()) {
				return (server);
			}

			// Shouldn't actually happen.. but must be transient or a bug.
			server = null;
			Thread.yield();
		}

		return server;

	}

	@Override
	public Server choose(Object key)
	{
		return choose(getLoadBalancer(), key);
	}

	@Override
	public void initWithNiwsConfig(IClientConfig clientConfig)
	{
		// TODO Auto-generated method stub

	}

}

啟動3個EurekaServer ,3個提供者,一個消費者,訪問介面localhost/consumer/dept/list每五次換一個服務提供。