聊聊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
- round_robin
- 【译】Spring 官方教程:使用 Restdocs 创建 API 文档
- c#中设置快捷键
- 程序员你为什么这么累【续】:编码习惯之工具类规范
- IntelliJ IDEA 复杂的重构技巧
- 打造属于自己的支持版本迭代的Asp.Net Web Api Route
- 分布式消息队列 RocketMQ 源码分析 —— Message 拉取与消费(上)
- 分布式消息队列 RocketMQ 源码分析 —— Message 拉取与消费(下)
- Spring Boot中使用RabbitMQ
- Spring Cloud构建微服务架构:消息驱动的微服务(入门)【Dalston版】
- 哪类人适合当产品经理?
- 产品经理·杂谈
- Python机器学习中的特征选择
- Android学习第八弹之改变状态栏的颜色使其与APP风格一体化
- 手把手教你 MongoDB 的安装与详细使用(二)
- 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 数组属性和方法
- 如何表示python中的相对路径
- 基于Tensorflow读取MNIST数据集时网络超时的解决方式
- python中的错误如何查看
- python实现斗地主分牌洗牌
- Python Mock模块原理及使用方法详解
- python实现猜数游戏(保存游戏记录)
- PHP实现简单计算器小程序
- PHP单例模式模拟Java Bean实现方法示例
- php实现每日签到功能
- laravel5实现微信第三方登录功能
- Laravel框架定时任务2种实现方式示例
- tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this T
- Python 基于jwt实现认证机制流程解析
- Keras – GPU ID 和显存占用设定步骤
- PHP中如何使用Redis接管文件存储Session详解