尧图网络 高端网站定制 · 原创设计
免费咨询热线
400-888-6620
免费获取方案
dubbo 负载均衡之最小活跃度的实现原理
1.概括dubbo 2.7 中提供了4中负载均衡算法分别为随机、轮询、一致性哈希、最小活跃度分别对应于源码文件的 RandomLoadBalance.class、RoundRobinLoadBalance.class、ConsistentHashLoadBalance、LeastActiveLoadBalance.class。2.使用对于dubbo中负载均衡的配置使用也是很简单只需要在对应的服务提供方或消费端配置即可两边都配置优先取消费端配置方式如下服务提供端/** * 负载均衡 * roundrobin/random/leastactive/consistenthash * 轮询/加权随机(默认)/最少活跃度/一致性hash */ Service(loadbalance roundrobin) // 这里的 Service是dubbo的注解不是Spring的注解 public class PayServiceImpl implements IPayService { Override public String pay(String id) { System.out.println(request coming); return dubbo-springboot pay———— id; } }服务消费端/** * 服务调用 */ RestController RequestMapping(/pay) public class PayController { // roundrobin/random/leastactive/consistenthash Reference(loadbalance roundrobin) private IPayService payService; GetMapping(/{id}) public String pay(PathVariable String id) { return payService.pay(id); } }这样就可以了3.源码分析这里仅分析最小活跃度的源码实现其他的负载均衡相对简单。如果要使用最小活跃度的负载均衡算法可以配置最小活跃度对应的过滤器来使用如下Reference(loadbalance leastactive, filter actives) private IPayService payService;首先配置了 actives 过滤器服务会使得 ActiveLimitFilter.class 这个过滤器生效这个过滤器会在invoker被调用的时候进行活跃度的维护invoker是dubbo中对于处理的封装包括连接和调用处理只要拿到invoker调用就可以完成远程通信。接下来是重点部分 LeastActiveLoadBalance.class 的 doSelect方法源码如下protected T InvokerT doSelect(ListInvokerT invokers, URL url, Invocation invocation) { // Number of invokers invoker 数量 int length invokers.size(); // The least active value of all invokers int leastActive -1; // The number of invokers having the same least active value (leastActive) int leastCount 0; // The index of invokers having the same least active value (leastActive) int[] leastIndexes new int[length]; // the weight of every invokers int[] weights new int[length]; // The sum of the warmup weights of all the least active invokes int totalWeight 0; // The weight of the first least active invoke int firstWeight 0; // Every least active invoker has the same weight value? boolean sameWeight true; // Filter out all the least active invokers for (int i 0; i length; i) { InvokerT invoker invokers.get(i); // Get the active number of the invoke 获得活跃度通过 uti 和 方法名 int active RpcStatus.getStatus(invoker.getUrl(), invocation.getMethodName()).getActive(); // Get the weight of the invoke configuration. The default value is 100. 获得权重没设置默认100 int afterWarmup getWeight(invoker, invocation); // save for later use 记录活跃度 weights[i] afterWarmup; // If it is the first invoker or the active number of the invoker is less than the current least active number if (leastActive -1 || active leastActive) { // 第一次一定能走这里后边当拿到的invoker的活跃度小于最小活跃度则进来这里的逻辑 // Reset the active number of the current invoker to the least active number 记录获得的最小活跃度 leastActive active; // Reset the number of least active invokers 拥有最小活跃度的invoker数量 leastCount 1; // Put the first least active invoker first in leastIndexes 记录拥有最小活跃度的invoker的下标到leastIndexes[0] leastIndexes[0] i; // Reset totalWeight 总权重 totalWeight afterWarmup; // Record the weight the first least active invoker 记录最小活跃的的invoker的权重 firstWeight afterWarmup; 因为最小活跃度的invoker只有1个所以是等权的 // Each invoke has the same weight (only one invoker here) sameWeight true; // If current invokers active value equals with leaseActive, then accumulating. // 如果当前遍历的invoker的活跃度和当前记录的最小活跃度相等走这里 } else if (active leastActive) { // leastCount自增并且记录最小活跃度的invoker的下标到leastIndexes数组 // Record the index of the least active invoker in leastIndexes order leastIndexes[leastCount] i; // 统计总权重 // Accumulate the total weight of the least active invoker totalWeight afterWarmup; // 如果之前是等权并且现在获得的最小活跃度的invoker的权重和之前最小活跃度的invoker的权重不一样则不再等权 // If every invoker has the same weight? if (sameWeight i 0 afterWarmup ! firstWeight) { sameWeight false; } } } // 只有一个最小活跃度的invoker则就选择这个 // Choose an invoker from all the least active invokers if (leastCount 1) { // If we got exactly one invoker having the least active value, return this invoker directly. return invokers.get(leastIndexes[0]); } // 如果不等权并且总权重 0 if (!sameWeight totalWeight 0) { // If (not every invoker has the same weight at least one invokers weight0), select randomly based on // totalWeight. // 随机获得一个总权重之内的整数 int offsetWeight ThreadLocalRandom.current().nextInt(totalWeight); // Return a invoker based on the random value. // 对拥有最小活跃的的invoker进行加权随机 for (int i 0; i leastCount; i) { int leastIndex leastIndexes[i]; offsetWeight - weights[leastIndex]; if (offsetWeight 0) { return invokers.get(leastIndex); } } } // 等权的情况则在最小活跃度的invoker中随机获取 // If all invokers have the same weight value or totalWeight0, return evenly. return invokers.get(leastIndexes[ThreadLocalRandom.current().nextInt(leastCount)]); }梳理下就是1.拿到当前的所有 invoker因为是集群会有多个2.遍历所有invoker拿到其中 active活跃度在每次调用会被ActiveLimitFilter维护最小的一个或多个invoker并且记录并统计拥有最小活跃度的invoker的权重不设置默认为2003.最后如果拥有最小活跃度的invoker只有1个则就返回这个如果大于1个且他们的权重不是都一样的则加权随机(随机获得在总权重之内的非负整数落到谁的权重范围内则就是谁)如果是等权则直接等权随机(在拥有最小活跃度的invoker中随机获得一个)。具体的流程看上边源码注释即可
RELATED

相关推荐

Electron Node.js 依赖升级实战指南:从 e sync --3 补丁冲突修复到上游测试套件运行

Electron Node.js 依赖升级实战指南:从 e sync --3 补丁冲突修复到上游测试套件运行

Electron Node.js 依赖升级实战指南:从 e sync --3 补丁冲突修复到上游测试套件运行 【免费下载链接】electron :electron: Build cross-platform desktop apps with JavaScript, HTML, and CSS 项目地址: https://gitcode.com/GitHub_Trending/el/electron …

📅 2026/9/7 9:56:13
Win11Debloat:免费的 Windows 11 系统优化与去臃肿完整指南

Win11Debloat:免费的 Windows 11 系统优化与去臃肿完整指南

Win11Debloat:免费的 Windows 11 系统优化与去臃肿完整指南 【免费下载链接】Win11Debloat A simple, lightweight PowerShell script that allows you to remove pre-installed apps, disable telemetry, as well as perform various other changes to declutter a…

📅 2026/9/8 17:19:27
免费开源游戏引擎 GDevelop:从克隆仓库到跑起编辑器、写出第一个扩展

免费开源游戏引擎 GDevelop:从克隆仓库到跑起编辑器、写出第一个扩展

免费开源游戏引擎 GDevelop:从克隆仓库到跑起编辑器、写出第一个扩展 【免费下载链接】GDevelop 🎮 Open-source, cross-platform 2D/3D/multiplayer game engine designed for everyone. 项目地址: https://gitcode.com/GitHub_Trending/gd/GDevelop …

📅 2026/9/15 14:05:55
MORE NEWS

更多资讯

📰

启动报Port 8088 was already in use?先查是谁占的

启动报Port 8088 was already in use?先查是谁占的 项目启动报 Port 8088 was already in use,意思很直白:这个端口已经有人占了。 先说原理:端口是"门牌号",一间房只住一个进程 端口就是一台机器上的门牌号…

📰

担心文档上云?本地运行的办公AI才敢放心用

单位上周开了个会,主题是"AI 提效",散会时领导补了一句:涉及客户资料的文档,不许往云端传。这话一出,会议室里本来跃跃欲试的同事全蔫了——大家平时用的那些在线 AI,文档一上传就不知道存哪儿去…

📰

先能用,再好用:从烂尾复盘到两周上线,全栈项目的‘原型—快速迭代‘方法论

先能用,再好用:从烂尾复盘到两周上线,全栈项目的“原型—快速迭代”方法论 全栈学习路径的文章已经很多了:怎么选技术栈、怎么搭环境、怎么记文档,几乎每个方向都有人写。但一个更扎心的问题很少有人正面讨论——为什…

📰

深度思考模式把 ARC-AGI-2 从 17.6% 拉到 45.1%:对照 Gemini 3 Pro 的三份能力评测,看‘会思考‘改写了哪些边界

深度思考模式把 ARC-AGI-2 从 17.6% 拉到 45.1%:对照 Gemini 3 Pro 的三份能力评测,看“会思考”改写了哪些边界 一、先说结论:变强的不是“知识”,而是“解法” 每次新模型发布,评测榜单都会涨几个点,大…

📰

1.c++入门篇

1.c中的关键字asmdoifreturntrycontinueautodoubleinlineshorttypedefforbooldynamic_castintsignedtypeidpublicbreakelselongsizeoftypenamethrowcaseenummutablestaticunionwchar_tcatchexplicitnamespacestatic_castunsigneddefaultcharexportnewstructusingfriendclassext…

📰

《江灯合声》:有可行的分框版,也仍可以选择连续鸣谢

武汉江岸区栎坡合唱团在免费节目册《江灯合声》中,以表格集中鸣谢资助者。各家共同要求改为独立小框,团里仍想保留表格。现有全名与支持事项清楚可见,双方此前未承诺具体排版。我支持保留表格。补充的试排材料让选择更具体:分框能…

TODAY

今日更新

THIS WEEK

本周精选

THIS MONTH

本月热门

读完文章,想聊聊您的网站?

告诉我们您的行业与需求,资深顾问一对一梳理方案与报价,全程免费。

📞 💬