T
- 对象类型public class SmoothWeightSelector<T> extends Object implements Selector<T>
来自:https://gitee.com/dromara/hutool/pulls/982/
介绍:https://cloud.tencent.com/developer/beta/article/1680928
思路: 比如 A : 5 , B : 3 , C : 2 (服务器 A,B,C 对应权重分别是 5,3,2) ip: A,B,C weight: 5,3,2 (计算得到 totalWeight = 10) currentWeight: 0,0,0 (当前ip的初始权重都为0)
请求次数: | currentWeight = currentWeight + weight | 最大权重为 | 返回的ip为 | 最大的权重 - totalWeight,其余不变 1 | 5,3,2 (0,0,0 + 5,3,2) | 5 | A | -5,3,2 2 | 0,6,4 (-5,3,2 + 5,3,2) | 6 | B | 0,-4,4 3 | 5,-1,6 (0,-4,4 + 5,3,2) | 6 | C | 5,-1,-4 4 | 10,2,-2 (5,-1,-4 + 5,3,2) | 10 | A | 0,2,-2 5 | 5,5,0 | 5 | A | -5,5,0 6 | 0,8,2 | 8 | B | 0,-2,2 7 | 5,1,4 | 5 | A | -5,1,4 8 | 0,4,6 | 6 | C | 0,4,-4 9 | 5,7,-2 | 7 | B | 5,-3,-2 10 | 10,0,0 | 10 | A | 0,0,0至此结束: 可以看到负载轮询的策略是: A,B,C,A,A,B,A,C,B,A,
Constructor and Description |
---|
SmoothWeightSelector()
构造
|
SmoothWeightSelector(Iterable<? extends WeightObj<T>> weightObjList)
构造
|
Modifier and Type | Method and Description |
---|---|
SmoothWeightSelector<T> |
add(T obj,
int weight)
增加对象
|
SmoothWeightSelector<T> |
add(WeightObj<T> weightObj)
增加权重对象
|
static <T> SmoothWeightSelector<T> |
of()
创建平滑加权获取器
|
T |
select()
通过平滑加权方法获取列表中的当前对象
|
public static <T> SmoothWeightSelector<T> of()
T
- 对象类型public SmoothWeightSelector<T> add(T obj, int weight)
obj
- 对象weight
- 权重public SmoothWeightSelector<T> add(WeightObj<T> weightObj)
weightObj
- 权重对象Copyright © 2025. All rights reserved.