1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
| package edu.algorithm.loadbalance;
import edu.algorithm.entity.RpcStatus; import edu.algorithm.entity.Server;
import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ThreadLocalRandom;
public class LeastActiveLoadBalance {
public static final String NAME = "leastactive";
public Server select(List<Server> serverList){ int length = serverList.size(); int leastActive = -1; int leastCount = 0; int[] leastIndexes = new int[length]; int[] weights = new int[length]; int totalWeight = 0; int firstWeight = 0; boolean sameWeight = true;
int offsetWeight; int leastIndex;
for (offsetWeight = 0;offsetWeight < length; ++offsetWeight) { Server server = serverList.get(offsetWeight); leastIndex = RpcStatus.getStatus(server.getIp()).getActive(); int afterWarmup = server.getWeight(); weights[offsetWeight] = afterWarmup; if(leastActive != -1 && leastIndex >= leastActive){ if(leastIndex == leastActive){ leastIndexes[leastCount++] = offsetWeight; totalWeight += afterWarmup; if(sameWeight && afterWarmup != firstWeight){ sameWeight = false; } } }else { leastActive = leastIndex; leastCount = 1; leastIndexes[0] = offsetWeight; totalWeight = afterWarmup; firstWeight = afterWarmup; sameWeight = true; } } if(leastCount == 1){ return serverList.get(leastIndexes[0]); }else { if (!sameWeight && totalWeight > 0) { offsetWeight = ThreadLocalRandom.current().nextInt(totalWeight);
for(int i = 0; i < leastCount; ++i) { leastIndex = leastIndexes[i]; offsetWeight -= weights[leastIndex]; if (offsetWeight < 0) { return serverList.get(leastIndex); } } } return serverList.get(leastIndexes[ThreadLocalRandom.current().nextInt(leastCount)]); }
}
public static void main(String[] args) { List<Server> serverList = new ArrayList<>(); serverList.add(new Server(1,"服务器2","8080","127.0.0.1",90,100)); serverList.add(new Server(2,"服务器1","8080","127.0.0.2",60,110)); serverList.add(new Server(3,"服务器3","8080","127.0.0.3",50,120)); serverList.add(new Server(4,"服务器4","8080","127.0.0.4",40,130)); serverList.add(new Server(5,"服务器5","8080","127.0.0.5",30,140)); serverList.add(new Server(6,"服务器6","8080","127.0.0.6",20,150)); serverList.add(new Server(7,"服务器7","8080","127.0.0.7",10,160));
LeastActiveLoadBalance loadBalance = new LeastActiveLoadBalance();
for (int i = 0; i < 10; i++) { Server server = loadBalance.select(serverList); RpcStatus.beginCount(server.getIp()); System.out.println("被选中的服务器:" + server.toString()); } Iterator iterator = RpcStatus.SERVICE_STATISTICS.entrySet().iterator(); while (iterator.hasNext()){ Map.Entry<String,RpcStatus> entry = (Map.Entry<String, RpcStatus>) iterator.next(); System.out.println("Key: "+entry.getKey()+" Value: "+entry.getValue().toString() + " avg:" + entry.getValue().getAverageElapsed()); }
} }
|