聊聊dubbo-go的roundRobinLoadBalance
时间:2022-07-23
本文章向大家介绍聊聊dubbo-go的roundRobinLoadBalance,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
序
本文主要研究一下dubbo-go的roundRobinLoadBalance
roundRobinLoadBalance
dubbo-go-v1.4.2/cluster/loadbalance/round_robin.go
const (
// RoundRobin ...
RoundRobin = "roundrobin"
// COMPLETE ...
COMPLETE = 0
// UPDATING ...
UPDATING = 1
)
var (
methodWeightMap sync.Map // [string]invokers
state = int32(COMPLETE) // update lock acquired ?
recyclePeriod = 60 * time.Second.Nanoseconds()
)
func init() {
extension.SetLoadbalance(RoundRobin, NewRoundRobinLoadBalance)
}
type roundRobinLoadBalance struct{}
// NewRoundRobinLoadBalance ...
func NewRoundRobinLoadBalance() cluster.LoadBalance {
return &roundRobinLoadBalance{}
}
- roundRobinLoadBalance的NewRoundRobinLoadBalance方法创建了roundRobinLoadBalance
Select
dubbo-go-v1.4.2/cluster/loadbalance/round_robin.go
func (lb *roundRobinLoadBalance) Select(invokers []protocol.Invoker, invocation protocol.Invocation) protocol.Invoker {
count := len(invokers)
if count == 0 {
return nil
}
if count == 1 {
return invokers[0]
}
key := invokers[0].GetUrl().Path + "." + invocation.MethodName()
cache, _ := methodWeightMap.LoadOrStore(key, &cachedInvokers{})
cachedInvokers := cache.(*cachedInvokers)
var (
clean = false
totalWeight = int64(0)
maxCurrentWeight = int64(math.MinInt64)
now = time.Now()
selectedInvoker protocol.Invoker
selectedWeightRobin *weightedRoundRobin
)
for _, invoker := range invokers {
var weight = GetWeight(invoker, invocation)
if weight < 0 {
weight = 0
}
identifier := invoker.GetUrl().Key()
loaded, found := cachedInvokers.LoadOrStore(identifier, &weightedRoundRobin{weight: weight})
weightRobin := loaded.(*weightedRoundRobin)
if !found {
clean = true
}
if weightRobin.Weight() != weight {
weightRobin.setWeight(weight)
}
currentWeight := weightRobin.increaseCurrent()
weightRobin.lastUpdate = &now
if currentWeight > maxCurrentWeight {
maxCurrentWeight = currentWeight
selectedInvoker = invoker
selectedWeightRobin = weightRobin
}
totalWeight += weight
}
cleanIfRequired(clean, cachedInvokers, &now)
if selectedWeightRobin != nil {
selectedWeightRobin.Current(totalWeight)
return selectedInvoker
}
// should never happen
return invokers[0]
}
- Select方法遍历invokers,通过weightRobin.increaseCurrent()作为currentWeight,若currentWeight大于maxCurrentWeight则更新maxCurrentWeight,设置selectedInvoker为当前invoker,设置selectedWeightRobin为当前weightRobin;之后对于selectedWeightRobin不为nil的执行selectedWeightRobin.Current(totalWeight),返回selectedInvoker
小结
roundRobinLoadBalance的NewRoundRobinLoadBalance方法创建了roundRobinLoadBalance;其Select方法遍历invokers,通过weightRobin.increaseCurrent()作为currentWeight,若currentWeight大于maxCurrentWeight则更新maxCurrentWeight,设置selectedInvoker为当前invoker,设置selectedWeightRobin为当前weightRobin
doc
- GAN 的 keras 实现
- 双向 LSTM
- scrapy回调函数传递参数
- python更新数据库脚本两种方法
- 使聊天机器人具有个性
- 遇到502错误,invalid request block size 解决方法
- python中json.loads,dumps,jsonify使用
- sqlalchemy和flask-sqlalchemy几种分页操作
- 一个 tflearn 情感分析小例子
- 前端js,后台python实现RSA非对称加密
- 运行mysql时,提示Table ‘performance_schema.session_variables’ doesn’t exist
- ODL应用开发之MD-SAL中级教程
- Sql参数是一个list的最佳实践
- MyBatis 配置输出日志,不输出SQL问题解决
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- c++实例之通讯录管理系统之查找联系人功能(五)
- 【python-leetcode424-滑动窗口法】替换后的最长重复字符
- c++实例之通讯录管理系统之修改联系人功能(六)
- 解决arxiv.org打开pdf缓慢问题
- c++实例之通讯录管理系统之清空联系人功能(七)
- springboot任务之定时任务
- springboot检索之整合elasticsearch并使用jest操作
- springboot任务之邮件任务
- springboot安全之整合spring security实现(只有登录才有权限、不同用户显示不同内容、记住我)
- springboot任务之异步任务
- springboot缓存之@Cacheable中常用参数
- springboot开发之添加员工
- springboot分布式之整合zookeeper和dubbo
- springboot之热部署
- c++之内存模型