上一篇讲了普通轮询、加权轮询的两种实现方式,重点讲了平滑加权轮询算法,并在文末留下了悬念:节点出现分配失败时降低有效权重值;成功时提高有效权重值(但不能大于weight值)。
本文在平滑加权轮询算法的基础上讲,还没弄懂的可以看上一篇文章。
现在来模拟实现:平滑加权轮询算法的降权和提权
节点宕机时,降低有效权重值;
节点正常时,提高有效权重值(但不能大于weight值);
注意:降低或提高权重都是针对有效权重。
package com.yty.loadbalancingalgorithm.wrr;
/**
* String ip:负载IP
* final Integer weight:权重,保存配置的权重
* Integer effectiveWeight:有效权重,轮询的过程权重可能变化
* Integer currentWeight:当前权重,比对该值大小获取节点
* 第一次加权轮询时:currentWeight = weight = effectiveWeight
* 后面每次加权轮询时:currentWeight 的值都会不断变化,其他权重不变
* Boolean isAvailable:是否存活
*/
public class ServerNode implements Comparable<ServerNode>{
private String ip;
private final Integer weight;
private Integer effectiveWeight;
private Integer currentWeight;
private Boolean isAvailable;
public ServerNode(String ip, Integer weight){
this(ip,weight,true);
}
public ServerNode(String ip, Integer weight,Boolean isAvailable){
this.ip = ip;
this.weight = weight;
this.effectiveWeight = weight;
this.currentWeight = weight;
this.isAvailable = isAvailable;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public Integer getWeight() {
return weight;
}
public Integer getEffectiveWeight() {
return effectiveWeight;
}
public void setEffectiveWeight(Integer effectiveWeight) {
this.effectiveWeight = effectiveWeight;
}
public Integer getCurrentWeight() {
return currentWeight;
}
public void setCurrentWeight(Integer currentWeight) {
this.currentWeight = currentWeight;
}
public Boolean isAvailable() {
return isAvailable;
}
public void setIsAvailable(Boolean isAvailable){
this.isAvailable = isAvailable;
}
// 每成功一次,恢复有效权重1,不超过配置的起始权重
public void onInvokeSuccess(){
if(effectiveWeight < weight) effectiveWeight++;
}
// 每失败一次,有效权重减少1,无底线的减少
public void onInvokeFault(){
effectiveWeight--;
}
@Override
public int compareTo(ServerNode node) {
return currentWeight > node.currentWeight ? 1 : (currentWeight.equals(node.currentWeight) ? 0 : -1);
}
@Override
public String toString() {
return "{ip='" + ip + "', weight=" + weight + ", effectiveWeight=" + effectiveWeight
+ ", currentWeight=" + currentWeight + ", isAvailable=" + isAvailable + "}";
}
}
package com.yty.loadbalancingalgorithm.wrr;
import java.util.ArrayList;
import java.util.List;
/**
* 加权轮询算法:加入存活状态,降权使宕机权重降低,从而不会被选中
*/
public class WeightedRoundRobinAvailable {
private static List<ServerNode> serverNodes = new ArrayList<>();
// 准备模拟数据
static {
serverNodes.add(new ServerNode("192.168.1.101",1));// 默认为true
serverNodes.add(new ServerNode("192.168.1.102",3,false));
serverNodes.add(new ServerNode("192.168.1.103",2));
}
/**
* 按照当前权重(currentWeight)最大值获取IP
* @return ServerNode
*/
public ServerNode selectNode(){
if (serverNodes.size() <= 0) return null;
if (serverNodes.size() == 1)
return (serverNodes.get(0).isAvailable()) ? serverNodes.get(0) : null;
// 权重之和
Integer totalWeight = 0;
ServerNode nodeOfMaxWeight = null; // 保存轮询选中的节点信息
synchronized (WeightedRoundRobinAvailable.class){
StringBuffer sb1 = new StringBuffer();
StringBuffer sb2 = new StringBuffer();
sb1.append(Thread.currentThread().getName()+"==加权轮询--[当前权重]值的变化:"+printCurrentWeight(serverNodes));
// 有限权重总和可能发生变化
for(ServerNode serverNode : serverNodes){
totalWeight += serverNode.getEffectiveWeight();
}
// 选出当前权重最大的节点
ServerNode tempNodeOfMaxWeight = serverNodes.get(0);
for (ServerNode serverNode : serverNodes) {
if (serverNode.isAvailable()) {
serverNode.onInvokeSuccess();//提权
sb2.append(Thread.currentThread().getName()+"==[正常节点]:"+serverNode+"\n");
} else {
serverNode.onInvokeFault();//降权
sb2.append(Thread.currentThread().getName()+"==[宕机节点]:"+serverNode+"\n");
}
tempNodeOfMaxWeight = tempNodeOfMaxWeight.compareTo(serverNode) > 0 ? tempNodeOfMaxWeight : serverNode;
}
// 必须new个新的节点实例来保存信息,否则引用指向同一个堆实例,后面的set操作将会修改节点信息
nodeOfMaxWeight = new ServerNode(tempNodeOfMaxWeight.getIp(),tempNodeOfMaxWeight.getWeight(),tempNodeOfMaxWeight.isAvailable());
nodeOfMaxWeight.setEffectiveWeight(tempNodeOfMaxWeight.getEffectiveWeight());
nodeOfMaxWeight.setCurrentWeight(tempNodeOfMaxWeight.getCurrentWeight());
// 调整当前权重比:按权重(effectiveWeight)的比例进行调整,确保请求分发合理。
tempNodeOfMaxWeight.setCurrentWeight(tempNodeOfMaxWeight.getCurrentWeight() - totalWeight);
sb1.append(" -> "+printCurrentWeight(serverNodes));
serverNodes.forEach(serverNode -> serverNode.setCurrentWeight(serverNode.getCurrentWeight()+serverNode.getEffectiveWeight()));
sb1.append(" -> "+printCurrentWeight(serverNodes));
System.out.print(sb2); //所有节点的当前信息
System.out.println(sb1); //打印当前权重变化过程
}
return nodeOfMaxWeight;
}
// 格式化打印信息
private String printCurrentWeight(List<ServerNode> serverNodes){
StringBuffer stringBuffer = new StringBuffer("[");
serverNodes.forEach(node -> stringBuffer.append(node.getCurrentWeight()+",") );
return stringBuffer.substring(0, stringBuffer.length() - 1) + "]";
}
// 并发测试:两个线程循环获取节点
public static void main(String[] args) throws InterruptedException {
// 循环次数
int loop = 18;
new Thread(() -> {
WeightedRoundRobinAvailable weightedRoundRobin1 = new WeightedRoundRobinAvailable();
for(int i=1;i<=loop;i++){
ServerNode serverNode = weightedRoundRobin1.selectNode();
System.out.println(Thread.currentThread().getName()+"==第"+i+"次轮询选中[当前权重最大]的节点:" + serverNode + "\n");
}
}).start();
//
new Thread(() -> {
WeightedRoundRobinAvailable weightedRoundRobin2 = new WeightedRoundRobinAvailable();
for(int i=1;i<=loop;i++){
ServerNode serverNode = weightedRoundRobin2.selectNode();
System.out.println(Thread.currentThread().getName()+"==第"+i+"次轮询选中[当前权重最大]的节点:" + serverNode + "\n");
}
}).start();
//main 线程睡了一下,再偷偷把 所有宕机 拉起来:模拟服务器恢复正常
Thread.sleep(5);
for (ServerNode serverNode:serverNodes){
if(!serverNode.isAvailable())
serverNode.setIsAvailable(true);
}
}
}
执行结果:将执行结果的前中后四次抽出来分析
Thread-0==[正常节点]:{ip='192.168.1.101', weight=1, effectiveWeight=1, currentWeight=1, isAvailable=true}
Thread-0==[宕机节点]:{ip='192.168.1.102', weight=3, effectiveWeight=2, currentWeight=3, isAvailable=false}
Thread-0==[正常节点]:{ip='192.168.1.103', weight=2, effectiveWeight=2, currentWeight=2, isAvailable=true}
Thread-0==加权轮询--[当前权重]值的变化:[1,3,2] -> [1,-3,2] -> [2,-1,4]
Thread-0==第1次轮询选中[当前权重最大]的节点:{ip='192.168.1.102', weight=3, effectiveWeight=2, currentWeight=3, isAvailable=false}
……
Thread-1==[正常节点]:{ip='192.168.1.101', weight=1, effectiveWeight=1, currentWeight=6, isAvailable=true}
Thread-1==[宕机节点]:{ip='192.168.1.102', weight=3, effectiveWeight=-7, currentWeight=-21, isAvailable=false}
Thread-1==[正常节点]:{ip='192.168.1.103', weight=2, effectiveWeight=2, currentWeight=12, isAvailable=true}
Thread-1==加权轮询--[当前权重]值的变化:[6,-21,12] -> [6,-21,15] -> [7,-28,17]
Thread-1==第5次轮询选中[当前权重最大]的节点:{ip='192.168.1.103', weight=2, effectiveWeight=2, currentWeight=12, isAvailable=true}
……
Thread-0==[正常节点]:{ip='192.168.1.101', weight=1, effectiveWeight=1, currentWeight=13, isAvailable=true}
Thread-0==[正常节点]:{ip='192.168.1.102', weight=3, effectiveWeight=3, currentWeight=-19, isAvailable=true}
Thread-0==[正常节点]:{ip='192.168.1.103', weight=2, effectiveWeight=2, currentWeight=12, isAvailable=true}
Thread-0==加权轮询--[当前权重]值的变化:[13,-19,12] -> [7,-19,12] -> [8,-16,14]
Thread-0==第15次轮询选中[当前权重最大]的节点:{ip='192.168.1.101', weight=1, effectiveWeight=1, currentWeight=13, isAvailable=true}
……
Thread-1==[正常节点]:{ip='192.168.1.101', weight=1, effectiveWeight=1, currentWeight=2, isAvailable=true}
Thread-1==[正常节点]:{ip='192.168.1.102', weight=3, effectiveWeight=3, currentWeight=2, isAvailable=true}
Thread-1==[正常节点]:{ip='192.168.1.103', weight=2, effectiveWeight=2, currentWeight=2, isAvailable=true}
Thread-1==加权轮询--[当前权重]值的变化:[2,2,2] -> [2,2,-4] -> [3,5,-2]
Thread-1==第18次轮询选中[当前权重最大]的节点:{ip='192.168.1.103', weight=2, effectiveWeight=2, currentWeight=2, isAvailable=true}
分析
一开始权重最高的节点虽然是宕机了,但是还是会被选中并返回;
“有效权重总和” 和 “当前权重总和”都减少了1,因为设置轮询到失败节点,都会自减1;
到第5次轮询时,当前权重已经变成了[7,-28,17],可以看出宕机节点越往后当前权重越小,所以后面根本不会再选中宕机节点,虽然没剔除故障节点,但却起到不分配宕机节点;
到第15次轮询时,有效权重已经恢复起始值,当前权重变为[8,-16,14],当前权重只能慢慢恢复,并不是节点一正常就立即恢复宕机过的节点,起到对故障节点的缓冲恢复(故障过的节点可能还存在问题);
最后1次轮询时,因为没有宕机节点,所以有效权重不变,当前权重已经恢复[3,5,-2],如果再轮询一次,那就会访问到一开始故障的节点了。
降权起到缓慢“剔除”宕机节点的效果;提权起到缓冲恢复宕机节点的效果。
对比上一篇文章可以看到:
当前权重(currentWeight):针对的是节点的选择,受有效权重影响,起到缓慢“剔除”宕机节点和缓冲恢复宕机节点的效果,当前权重最高就会被选择;
有效权重(effectiveWeight):针对的是权重的变化,也即是降权和提权,降权/提权只会直接操作有效权重;
权重(weight):针对的是存储起始配置,限定有效权重的提权。


我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我有一个用户工厂。我希望默认情况下确认用户。但是鉴于unconfirmed特征,我不希望它们被确认。虽然我有一个基于实现细节而不是抽象的工作实现,但我想知道如何正确地做到这一点。factory:userdoafter(:create)do|user,evaluator|#unwantedimplementationdetailshereunlessFactoryGirl.factories[:user].defined_traits.map(&:name).include?(:unconfirmed)user.confirm!endendtrait:unconfirmeddoenden
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht
目录一.加解密算法数字签名对称加密DES(DataEncryptionStandard)3DES(TripleDES)AES(AdvancedEncryptionStandard)RSA加密法DSA(DigitalSignatureAlgorithm)ECC(EllipticCurvesCryptography)非对称加密签名与加密过程非对称加密的应用对称加密与非对称加密的结合二.数字证书图解一.加解密算法加密简单而言就是通过一种算法将明文信息转换成密文信息,信息的的接收方能够通过密钥对密文信息进行解密获得明文信息的过程。根据加解密的密钥是否相同,算法可以分为对称加密、非对称加密、对称加密和非
华为OD机试题本篇题目:明明的随机数题目输入描述输出描述:示例1输入输出说明代码编写思路最近更新的博客华为od2023|什么是华为od,od薪资待遇,od机试题清单华为OD机试真题大全,用Python解华为机试题|机试宝典【华为OD机试】全流程解析+经验分享,题型分享,防作弊指南华为o
这篇文章是继上一篇文章“Observability:从零开始创建Java微服务并监控它(一)”的续篇。在上一篇文章中,我们讲述了如何创建一个Javaweb应用,并使用Filebeat来收集应用所生成的日志。在今天的文章中,我来详述如何收集应用的指标,使用APM来监控应用并监督web服务的在线情况。源码可以在地址 https://github.com/liu-xiao-guo/java_observability 进行下载。摄入指标指标被视为可以随时更改的时间点值。当前请求的数量可以改变任何毫秒。你可能有1000个请求的峰值,然后一切都回到一个请求。这也意味着这些指标可能不准确,你还想提取最小/
HashMap中为什么引入红黑树,而不是AVL树呢1.概述开始学习这个知识点之前我们需要知道,在JDK1.8以及之前,针对HashMap有什么不同。JDK1.7的时候,HashMap的底层实现是数组+链表JDK1.8的时候,HashMap的底层实现是数组+链表+红黑树我们要思考一个问题,为什么要从链表转为红黑树呢。首先先让我们了解下链表有什么不好???2.链表上述的截图其实就是链表的结构,我们来看下链表的增删改查的时间复杂度增:因为链表不是线性结构,所以每次添加的时候,只需要移动一个节点,所以可以理解为复杂度是N(1)删:算法时间复杂度跟增保持一致查:既然是非线性结构,所以查询某一个节点的时候
C#实现简易绘图工具一.引言实验目的:通过制作窗体应用程序(C#画图软件),熟悉基本的窗体设计过程以及控件设计,事件处理等,熟悉使用C#的winform窗体进行绘图的基本步骤,对于面向对象编程有更加深刻的体会.Tutorial任务设计一个具有基本功能的画图软件**·包括简单的新建文件,保存,重新绘图等功能**·实现一些基本图形的绘制,包括铅笔和基本形状等,学习橡皮工具的创建**·设计一个合理舒适的UI界面**注明:你可能需要先了解一些关于winform窗体应用程序绘图的基本知识,以及关于GDI+类和结构的知识二.实验环境Windows系统下的visualstudio2017C#窗体应用程序三.