随机分配算法
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()); } } }
最少并发数优先策略
调用实例远程实例模拟类
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()); } } }