当前位置:   article > 正文

roundrobin来历_权重轮询调度算法(WeightedRound-RobinScheduling)-Java实现

java smooth weighted round-robin balancing

1 importjava.math.BigInteger;2 importjava.util.ArrayList;3 importjava.util.HashMap;4 importjava.util.List;5 importjava.util.Map;6 importjava.util.Map.Entry;7

8 /**

9 * 权重轮询调度算法(WeightedRound-RobinScheduling)-Java实现10 *@authorhuligong11 **/

12 public classWeightedRoundRobinScheduling {13

14 private int currentIndex = -1;//上一次选择的服务器

15 private int currentWeight = 0;//当前调度的权值

16 private int maxWeight = 0; //最大权重

17 private int gcdWeight = 0; //所有服务器权重的最大公约数

18 private int serverCount = 0; //服务器数量

19 private List serverList; //服务器集合

20

21 /**

22 * 返回最大公约数23 *@parama24 *@paramb25 *@return

26 */

27 private static int gcd(int a, intb) {28 BigInteger b1 = newBigInteger(String.valueOf(a));29 BigInteger b2 = newBigInteger(String.valueOf(b));30 BigInteger gcd =b1.gcd(b2);31 returngcd.intValue();32 }33

34

35 /**

36 * 返回所有服务器权重的最大公约数37 *@paramserverList38 *@return

39 */

40 private static int getGCDForServers(ListserverList ) {41 int w = 0;42 for (int i = 0, len = serverList.size(); i < len - 1; i++) {43 if (w == 0) {44 w = gcd(serverList.get(i).weight, serverList.get(i + 1).weight);45 } else{46 w = gcd(w, serverList.get(i + 1).weight);47 }48 }49 returnw;50 }51

52

53 /**

54 * 返回所有服务器中的最大权重55 *@paramserverList56 *@return

57 */

58 public static int getMaxWeightForServers(ListserverList) {59 int w = 0;60 for (int i = 0, len = serverList.size(); i < len - 1; i++) {61 if (w == 0) {62 w = Math.max(serverList.get(i).weight, serverList.get(i + 1).weight);63 } else{64 w = Math.max(w, serverList.get(i + 1).weight);65 }66 }67 returnw;68 }69

70 /**

71 * 算法流程:72 * 假设有一组服务器 S = {S0, S1, …, Sn-1}73 * 有相应的权重,变量currentIndex表示上次选择的服务器74 * 权值currentWeight初始化为0,currentIndex初始化为-1 ,当第一次的时候返回 权值取最大的那个服务器,75 * 通过权重的不断递减 寻找 适合的服务器返回,直到轮询结束,权值返回为076 */

77 publicServer GetServer() {78 while (true) {79 currentIndex = (currentIndex + 1) %serverCount;80 if (currentIndex == 0) {81 currentWeight = currentWeight -gcdWeight;82 if (currentWeight <= 0) {83 currentWeight =maxWeight;84 if (currentWeight == 0)85 return null;86 }87 }88 if (serverList.get(currentIndex).weight >=currentWeight) {89 returnserverList.get(currentIndex);90 }91 }92 }93

94

95 classServer {96 publicString ip;97 public intweight;98 public Server(String ip, intweight) {99 super();100 this.ip =ip;101 this.weight =weight;102 }103 publicString getIp() {104 returnip;105 }106 public voidsetIp(String ip) {107 this.ip =ip;108 }109 public intgetWeight() {110 returnweight;111 }112 public void setWeight(intweight) {113 this.weight =weight;114 }115 }116

117

118 public voidinit() {119 Server s1 = new Server("192.168.0.100", 3);//3

120 Server s2 = new Server("192.168.0.101", 2);//2

121 Server s3 = new Server("192.168.0.102", 6);//6

122 Server s4 = new Server("192.168.0.103", 4);//4

123 Server s5 = new Server("192.168.0.104", 1);//1

124 serverList = new ArrayList();125 serverList.add(s1);126 serverList.add(s2);127 serverList.add(s3);128 serverList.add(s4);129 serverList.add(s5);130

131 currentIndex = -1;132 currentWeight = 0;133 serverCount =serverList.size();134 maxWeight =getMaxWeightForServers(serverList);135 gcdWeight =getGCDForServers(serverList);136 }137

138

139 public static voidmain(String[] args) {140 WeightedRoundRobinScheduling obj = newWeightedRoundRobinScheduling();141 obj.init();142

143 Map countResult = new HashMap();144

145 for (int i = 0; i < 100; i++) {146 Server s =obj.GetServer();147 String log = "ip:"+s.ip+";weight:"+s.weight;148 if(countResult.containsKey(log)){149 countResult.put(log,countResult.get(log)+1);150 }else{151 countResult.put(log,1);152 }153 System.out.println(log);154 }155

156 for(Entrymap : countResult.entrySet()){157 System.out.println("服务器 "+map.getKey()+" 请求次数: "+map.getValue());158 }159 }160

161 }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/289110
推荐阅读
相关标签
  

闽ICP备14008679号