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

这是一个Java实现的权重轮询调度算法(Weighted Round-Robin Scheduling)。通过计算服务器权重的最大公约数(GCD)和最大权重,该算法能根据服务器的权重分配请求。在循环中,算法不断减少当前权值,直到找到权值匹配的服务器进行分配。在示例中,创建了5个具有不同权重的服务器,并模拟了100次请求的分配情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值