权重轮询调度算法(Weighted Round-Robin Scheduling)--java版本号

因为每台server的配置、安装的业务应用等不同。其处理能力会不一样。所以,我们依据server的不同处理能力,给每一个server分配不同的权值。使其可以接受对应权值数的服务请求。

2个java源文件,例如以下所看到的:

public interface IProduceStrategy {

    public int getPartitionIdForTopic();

}
public class WeightFactorProduceStrategy implements IProduceStrategy {
private int i = -1; //表示上一次选择的server
private int cw = 0; //表示当前调度的权值
private int gcd = 0; //当前全部权重的最大公约数 比方 2,4。8 的最大公约数为:2
private int maxWeight; private List<Integer> weights = null; //作用计算最大公约数
private PartitionWeightRRParameter weightRRParametersDns[] = null; /**
* 依照轮询调研权重配置,格式例如以下:partition1:weight,partition2:weight
* @param partConfig
*/
public WeightFactorProduceStrategy(String partConfig) {
validate(partConfig);
this.initWeigthParam(Tools.parseCsvMap(partConfig));
} private Pattern pattern = Pattern.compile("([\\d+\\:\\d+],?){1,}"); private void validate(String partConfig) {
if (partConfig.length() <= 0)
throw new InvalidPartitonConfigException("partition config is incorrect :" + partConfig);
else if (partConfig.equals(".") || partConfig.equals(".."))
throw new InvalidPartitonConfigException("partition config is incorrect :" + partConfig); Matcher matcher = pattern.matcher(partConfig);
if(matcher.find()) {
String rexStr = matcher.group();
if (!rexStr.equals(partConfig))
throw new InvalidPartitonConfigException("partition config is incorrect :" + partConfig);
} else {
throw new InvalidPartitonConfigException("partition config is incorrect :" + partConfig);
}
} /**
* 格式例如以下:partition1:weight,partition2:weight
* @param csvMap
*/
private void initWeigthParam(Map<String, String> csvMap) {
weightRRParametersDns = new PartitionWeightRRParameter[csvMap.size()];
int numPart = 0;
weights = new ArrayList<Integer>(csvMap.size());
Set<Map.Entry<String, String>> entrySet = csvMap.entrySet();
for(Iterator<Map.Entry<String, String>> its = entrySet.iterator(); its.hasNext(); ) {
Map.Entry<String, String> entry = its.next();
weights.add(Integer.valueOf(entry.getValue()));
weightRRParametersDns[numPart++] = new PartitionWeightRRParameter(Integer.valueOf(entry.getKey()), Integer.valueOf(entry.getValue()));
} gcd = getGcdByList(weights);
maxWeight = getMaxWeight();
} /**
* 计算最大公约数
* @param weight_m 权重数
* @param weight_n 权重数
* @return
*/
private int GCD(int weight_m,int weight_n)
{
int temp;
while(weight_n != 0){
if(weight_m < weight_n){
temp = weight_m;
weight_m = weight_n;
weight_n = temp;
}
temp = weight_m - weight_n;
weight_m = weight_n;
weight_n = temp;
}
return weight_m;
} /**
*
* @param weights 权重列表
* @param startIndex list索引值,起始位置。 * @param nextGcd 传入最大公约数
* @return
*/
private int getGcdByList(List<Integer> weights, int startIndex, int nextGcd) {
if ( weights.size() < 2) {
throw new IllegalArgumentException("At least a number of parameters for 2");
}
if (weights.size() == 2 && startIndex == 0) {
return this.GCD(weights.get(startIndex), weights.get(startIndex + 1));
} if (startIndex + 1 > weights.size() -1 )
return nextGcd;
int curGcd = nextGcd > 0 ? nextGcd : weights.get(startIndex);
int nextIndex = startIndex + 1;
nextGcd = GCD(curGcd, weights.get(startIndex + 1)); //0,1 return getGcdByList(weights, nextIndex, nextGcd);
} private int getGcdByList(List<Integer> weights) {
return this.getGcdByList(weights, 0, 0);
} private int getWeightDns() {
for ( ; ; ) {
i = (i + 1) % weightRRParametersDns.length;
if (i == 0) {
cw = cw - gcd; //表示当前调度的权值
if (cw <= 0) {
cw = maxWeight;
if (cw == 0) {
return 0;
}
}
} if (weightRRParametersDns[i].getWeight() >= cw ) {
return weightRRParametersDns[i].getPartition();
}
}
} private int getMaxWeight() {
int max = 0;
for (int i = 0; i< weightRRParametersDns.length;i++) {
if (weightRRParametersDns[i].getWeight() >= max) {
max = weightRRParametersDns[i].getWeight();
}
} return max;
} public int getPartitionIdForTopic() {
return this.getWeightDns();
} /**
* 分区权重參数类
*/
static class PartitionWeightRRParameter {
private int partition;
private int weight; public PartitionWeightRRParameter(int partition, int weight) {
this.partition = partition;
this.weight = weight;
} public int getPartition() {
return partition;
} public int getWeight() {
return weight;
}
} }

单元測试类:

public class WeightFactorProduceStrategyTest {

    @Test
public void testGetPartitionIdForTopic() throws Exception {
IProduceStrategy weightFcProStrategy = new WeightFactorProduceStrategy("0:5,1:15,2:20"); for (int i = 0; i < 40; i++) {
// weightFcProStrategy.getPartitionIdForTopic();
System.out.println(weightFcProStrategy.getPartitionIdForTopic());
}
}
}

測试结果例如以下:

2
2
1
2
1
2
1
0
2
2
1
2
1
2
1
0
2
2
1
2
1
2
1
0
2
2
1
2
1
2
1
0
2
2
1
2
1
2
1
0

权重轮询调度算法 java版本号的更多相关文章

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

    权重轮询调度算法(WeightedRound-RobinScheduling)-Java实现 ----参考Nginx中负载均衡算法实现 与上一遍博客 http://www.cnblogs.com/hu ...

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

    权重轮询调度算法(WeightedRound-RobinScheduling)-Java实现3 之前两篇相关博文: 权重轮询调度算法(WeightedRound-RobinScheduling)-Ja ...

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

    权重轮询调度算法(WeightedRound-RobinScheduling)-Java实现 import java.math.BigInteger; import java.util.ArrayLi ...

  4. 权重轮询调度算法(WeightedRound-RobinScheduling)

    权重轮询调度算法(WeightedRound-RobinScheduling)-Java实现 ----参考Nginx中负载均衡算法实现 这里主要参考这篇文章的实现: Nginx 负载均衡-加权轮询策略 ...

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

    在多台机器实现负载均衡的时候,存在调度分配的问题. 如果服务器的配置的处理能力都一致的话,平均轮询分配可以直接解决问题,然而有些时候机器的处理能力是不一致的. 假如有2台机器 A和B , A的处理能力 ...

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

    package main import ( "fmt" "time" ) var slaveDns = map[int]map[string]interface ...

  7. 通过 PowerShell 支持 Azure Traffic Manager 外部端点和权重轮询机制

    Jonathan TulianiAzure网络 - DNS和 Traffic Manager高级项目经理 在北美 TechEd 大会上,我们宣布了 Azure Traffic Manager将支持 ...

  8. loadbalance轮询算法 java实现

    /** * <html> * <body> * <P> Copyright JasonInternational</p> * <p> All ...

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

    2013-09-25 <?php class WeightedRoundRobin { private static $_weightArray = array(); private stati ...

随机推荐

  1. Quartz任务调度 服务日志+log4net打印日志+制作windows服务

    引言 现在许多的项目都需要定时的服务进行支撑,而我们经常用到的定时服务就是Quartz任务调度了.不过我们在使用定时Job进行获取的时候,有时候我们就需要记录一下自定义的日志,甚至我们还会对执行定时J ...

  2. sed 替换 引用变量值,记录一个自己学习错误的地方。

    先上脚本,脚本的目的是虚拟机克隆-连接克隆,然后修改ip这个搞定,修改hostname就很简单了 declare oldipdeclare -i Anamedeclare newipoldip=`ca ...

  3. 记录python之递归函数

    函数move(n,a,b,c)的定义是将n个圆盘从a借助b移动到c. def move(n,a,b,c): if n==1: print a,'-->',c move (n-1,a,c,b) p ...

  4. 紫书 例题 11-3 UVa 1151 (有边集的最小生成树+二进制枚举子集)

    标题指的边集是说这道题的套餐, 是由几条边构成的. 思路是先做一遍最小生成树排除边, 因为如果第一次做没有加入的边, 到后来新加入了很多权值为0的边,这些边肯定排在最前面,然后这条边的前面的那些边肯定 ...

  5. 2016 10 27 考试 dp 向量 乱搞

    目录 20161027考试 T1: T2: T3: 20161027考试 考试时间 7:50 AM to 11:15 AM 题目 考试包 据说这是一套比较正常的考卷,,,嗯,,或许吧, 而且,,整个小 ...

  6. codevs1281 矩阵乘法 快速幂 !!!手写乘法取模!!! 练习struct的构造函数和成员函数

    对于这道题目以及我的快速幂以及我的一节半晚自习我表示无力吐槽,, 首先矩阵乘法和快速幂没必要太多说吧,,嗯没必要,,我相信没必要,,实在做不出来写两个矩阵手推一下也就能理解矩阵的顺序了,要格外注意一些 ...

  7. BZOJ 4236~4247 题解

    BZOJ 4236 JOIOJI f[i][0..2]表示前i个字符中′J′/′O′/′I′的个数 将二元组<f[i][0]−f[i][1],f[i][1]−f[i][2]>扔进map,记 ...

  8. zoj 1655 单源最短路 改为比例+最长路

    http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=655 没有理解清题意就硬套模板.所以WA了好几次. 解析看我的还有一篇http ...

  9. 主程的晋升攻略(4):TCP、消息分包和协议设计

    在<主程的晋升攻略(3):IP.DNS和CDN>中,一次网络请求经过DNS解析知道了目的IP,如今就要发出网络包,这里我们说一说TCP的相关话题. TCP是一种流式协议 讲网络编程的教科书 ...

  10. JAVA学习第四十六课 — 其它对象API(二)Date类 &amp; Calendar类(重点掌握)

    Date类(重点) 开发时,会时常遇见时间显示的情况,所以必须熟练Date的应用 <span style="font-family:KaiTi_GB2312;font-size:18p ...