隨機分配算法算法
public class RandomLoadBalance { public static List<Integer> list = new ArrayList<Integer>(); static { for (int i = 1; i < 11; i++) { list.add(i); } } public static Integer random() { int idx = (int)(ThreadLocalRandom.current().nextDouble() * list.size()); return idx % list.size(); } public static void main(String[] args) { for (int i = 0; i < 100; i++) { System.out.println(random()); } } }
輪詢算法併發
public class RoundRobinLoadBalance { private static AtomicInteger idx = new AtomicInteger(0); public static List<Integer> list = new ArrayList<Integer>(); static { for (int i = 1; i < 11; i++) { list.add(i); } } /** * 經過二進制位操做將originValue轉化爲非負數: * 0和正數返回自己 * 負數經過二進制首位取反轉化爲正數或0(Integer.MIN_VALUE將轉換爲0) * return non-negative int value of originValue * @param originValue * @return positive int */ public static int getNonNegative(int originValue){ return 0x7fffffff & originValue; } public static Integer roundRobin() { int index = getNonNegative(idx.incrementAndGet()); return index % list.size(); } public static void main(String[] args) { for (int i = 0; i < 100; i++) { System.out.println(roundRobin()); } } }
最少併發數優先策略dom
調用實例遠程實例模擬類ide
public class Refer { private int serverId; /** * 當前使用該referer的調用數 * * @return */ private int activeRefererCount; private boolean isAvailable; public int getActiveRefererCount() { return activeRefererCount; } public void setActiveRefererCount(int activeRefererCount) { this.activeRefererCount = activeRefererCount; } public boolean isAvailable() { return isAvailable; } public void setAvailable(boolean available) { isAvailable = available; } public int getServerId() { return serverId; } public void setServerId(int serverId) { this.serverId = serverId; } @Override public String toString() { return "Refer{" + "serverId=" + serverId + ", activeRefererCount=" + activeRefererCount + ", isAvailable=" + isAvailable + '}'; } }
測試最小可用併發數的實例類測試測試
public class ActiveWeightLoadBalance { private static AtomicInteger idx = new AtomicInteger(0); public static List<Refer> refers = new ArrayList<Refer>(); static { for (int i = 1; i < 10; i++) { Refer refer = new Refer(); refer.setServerId(i); refer.setActiveRefererCount(ThreadLocalRandom.current().nextInt(11)); refer.setAvailable(ThreadLocalRandom.current().nextInt(2) == 1 ? true : false); refers.add(refer); } } public static Refer activeWeight() { int refererSize = refers.size(); //隨機從list的一個位置,開始計算最小處理併發數 int startIndex = ThreadLocalRandom.current().nextInt(refererSize); //記錄獲取Refer次數,以及實現refers實例輪詢獲取 int currentCusor = 0; //記錄已經獲取可用Refer次數 int currentAvailableCusor = 0; Refer refer = null; //獲取可用refer次數不超過10次,以及獲取refer次數不能大於refers的大小 while(currentAvailableCusor < 10 && currentCusor < refererSize) { Refer temp = refers.get((startIndex + currentCusor) % refererSize); currentCusor++; if(!temp.isAvailable()) { continue; } currentAvailableCusor++; if(refer == null) { refer = temp; }else{ if(compare(refer, temp) > 0) { refer = temp; } } } return refer; } private static int compare(Refer refer1, Refer refer2) { return refer1.getActiveRefererCount() - refer2.getActiveRefererCount(); } public static void main(String[] args) { for (Refer refer : refers) { System.out.println(refer); } for (int i = 0; i < 10; i++) { System.out.println("獲取可用而且最小併發數Refer:" + activeWeight()); } } }