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
| package edu.algorithm.loadbalance;
import edu.algorithm.entity.Server;
import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.atomic.AtomicLong;
public class RoundRobinLoadBalance {
protected static class WeightedRoundRobin { private int weight; private AtomicLong current = new AtomicLong(0L); private long lastUpdate; protected WeightedRoundRobin() { } public int getWeight() { return this.weight; } public void setWeight(int weight) { this.weight = weight; this.current.set(0L); } public long increaseCurrent() { return this.current.addAndGet((long)this.weight); } public void sel(int total) { this.current.addAndGet((long)(-1 * total)); } public long getLastUpdate() { return this.lastUpdate; } public void setLastUpdate(long lastUpdate) { this.lastUpdate = lastUpdate; } } private ConcurrentMap<String, ConcurrentMap<String, WeightedRoundRobin>> methodWeightMap = new ConcurrentHashMap();
protected Server select(List<Server> serverList){ String key = "save key" + "edu.algorithm.loadbalance.RoundRobinLoadBalance.select"; ConcurrentMap<String, WeightedRoundRobin> map = (ConcurrentMap)this.methodWeightMap.computeIfAbsent(key, (k) -> { return new ConcurrentHashMap(); });
int totalWeight = 0; long maxCurrent = Long.MIN_VALUE; long now = System.currentTimeMillis(); int weight; Server selectedSever = null; WeightedRoundRobin selectedWRR = null;
for (Iterator var12= serverList.iterator(); var12.hasNext(); totalWeight += weight){ Server server = (Server) var12.next(); weight = server.getWeight(); int finalWeight = weight; WeightedRoundRobin weightedRoundRobin = (WeightedRoundRobin)map.computeIfAbsent(server.getIp(), (k) -> { WeightedRoundRobin wrr = new WeightedRoundRobin(); wrr.setWeight(finalWeight); return wrr; });
if (weight != weightedRoundRobin.getWeight()) { weightedRoundRobin.setWeight(weight); } long cur = weightedRoundRobin.increaseCurrent(); weightedRoundRobin.setLastUpdate(now); if (cur > maxCurrent) { maxCurrent = cur; selectedSever = server; selectedWRR = weightedRoundRobin; } }
if(selectedSever != null){ selectedWRR.sel(totalWeight); return selectedSever; }else { return serverList.get(0); }
}
public static void main(String[] args) throws Exception { List<Server> serverList = new ArrayList<>(); serverList.add(new Server(1,"服务器1","8080","192.168.2.2",80)); serverList.add(new Server(2,"服务器2","8080","192.168.2.5",30)); serverList.add(new Server(3,"服务器3","8080","192.168.2.8",40)); serverList.add(new Server(4,"服务器4","8080","192.168.3.2",20)); serverList.add(new Server(5,"服务器5","8080","192.168.3.5",99)); serverList.add(new Server(6,"服务器6","8080","192.168.3.8",60));
RoundRobinLoadBalance robinLoadBalance = new RoundRobinLoadBalance();
for (int i = 0; i < 10; i++) { Server server = robinLoadBalance.select(serverList); System.out.println(server.toString()); } } }
|