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

权重轮询调度算法(WeightedRound-RobinScheduling)-Java实现文章来源地址https://www.yii666.com/article/754057.html文章地址https://www.yii666.com/article/754057.html网址:yii666.com<网址:yii666.com文章来源地址:https://www.yii666.com/article/754057.html

 import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry; /**
* 权重轮询调度算法(WeightedRound-RobinScheduling)-Java实现
* @author huligong
* */
public class WeightedRoundRobinScheduling { private int currentIndex = -1;// 上一次选择的服务器
private int currentWeight = 0;// 当前调度的权值
private int maxWeight = 0; // 最大权重
private int gcdWeight = 0; //所有服务器权重的最大公约数
private int serverCount = 0; //服务器数量
private List<Server> serverList; //服务器集合 /**
* 返回最大公约数
* @param a
* @param b
* @return
*/
private static int gcd(int a, int b) {
BigInteger b1 = new BigInteger(String.valueOf(a));
BigInteger b2 = new BigInteger(String.valueOf(b));
BigInteger gcd = b1.gcd(b2);
return gcd.intValue();
} /**
* 返回所有服务器权重的最大公约数
* @param serverList
* @return
*/
private static int getGCDForServers(List<Server> serverList ) {
int w = 0;
for (int i = 0, len = serverList.size(); i < len - 1; i++) {
if (w == 0) {
w = gcd(serverList.get(i).weight, serverList.get(i + 1).weight);
} else {
w = gcd(w, serverList.get(i + 1).weight);
}
}
return w;
} /**
* 返回所有服务器中的最大权重
* @param serverList
* @return
*/
public static int getMaxWeightForServers(List<Server> serverList) {
int w = 0;
for (int i = 0, len = serverList.size(); i < len - 1; i++) {
if (w == 0) {
w = Math.max(serverList.get(i).weight, serverList.get(i + 1).weight);
} else {
w = Math.max(w, serverList.get(i + 1).weight);
}
}
return w;
} /**
* 算法流程:
* 假设有一组服务器 S = {S0, S1, …, Sn-1}
* 有相应的权重,变量currentIndex表示上次选择的服务器
* 权值currentWeight初始化为0,currentIndex初始化为-1 ,当第一次的时候返回 权值取最大的那个服务器,
* 通过权重的不断递减 寻找 适合的服务器返回,直到轮询结束,权值返回为0
*/
public Server GetServer() {
while (true) {
currentIndex = (currentIndex + 1) % serverCount;
if (currentIndex == 0) {
currentWeight = currentWeight - gcdWeight;
if (currentWeight <= 0) {
currentWeight = maxWeight;
if (currentWeight == 0)
return null;
}
}
if (serverList.get(currentIndex).weight >= currentWeight) {
return serverList.get(currentIndex);
}
}
} class Server {
public String ip;
public int weight;
public Server(String ip, int weight) {
super();
this.ip = ip;
this.weight = weight;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
} public void init() {
Server s1 = new Server("192.168.0.100", 3);//
Server s2 = new Server("192.168.0.101", 2);//
Server s3 = new Server("192.168.0.102", 6);//
Server s4 = new Server("192.168.0.103", 4);//
Server s5 = new Server("192.168.0.104", 1);//
serverList = new ArrayList<Server>();
serverList.add(s1);
serverList.add(s2);
serverList.add(s3);
serverList.add(s4);
serverList.add(s5); currentIndex = -1;
currentWeight = 0;
serverCount = serverList.size();
maxWeight = getMaxWeightForServers(serverList);
gcdWeight = getGCDForServers(serverList);
} public static void main(String[] args) {
WeightedRoundRobinScheduling obj = new WeightedRoundRobinScheduling();
obj.init(); Map<String,Integer> countResult = new HashMap<String,Integer>(); for (int i = 0; i < 100; i++) {
Server s = obj.GetServer();
String log = "ip:"+s.ip+";weight:"+s.weight;
if(countResult.containsKey(log)){
countResult.put(log,countResult.get(log)+1);
}else{
countResult.put(log,1);
}
System.out.println(log);
} for(Entry<String, Integer> map : countResult.entrySet()){
System.out.println("服务器 "+map.getKey()+" 请求次数: "+map.getValue());
}
} }

版权声明:本文内容来源于网络,版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。文本页已经标记具体来源原文地址,请点击原文查看来源网址,站内文章以及资源内容站长不承诺其正确性,如侵犯了您的权益,请联系站长如有侵权请联系站长,将立刻删除

权重轮询调度算法(WeightedRound-RobinScheduling)-Java实现-相关文章

  1. 权重轮询调度算法(WeightedRound-RobinScheduling)-Java实现

  2. 权重轮询调度算法(WeightedRound-RobinScheduling)-Java实现3

  3. 权重轮询调度算法 java版本号

  4. 权重轮询调度算法(Weighted Round-Robin Scheduling)-C#实现

  5. golang实现权重轮询调度算法

  6. php版权重轮询调度算法

  7. loadbalance轮询算法 java实现

  8. 为什么JAVA要提供 wait/notify 机制?是为了避免轮询带来的性能损失

    wait/notify  机制是为了避免轮询带来的性能损失。为了说清道理,我们用“图书馆借书”这个经典例子来作解释。一本书同时只能借给一个人。现在有一本书,图书馆已经把这本书借了张三。在简单的synchrnozed 同步机制下,李四如果想借,先要去图书馆查看书有没有还回来。李

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信图片_20190322181744_03.jpg

微信扫一扫打赏

请作者喝杯咖啡吧~

支付宝扫一扫领取红包,优惠每天领

二维码1

zhifubaohongbao.png

二维码2

zhifubaohongbao2.png