按照权重负载的实现

news/2024/7/10 0:02:40 标签: 负载均衡, 权重, loadbanlance

常见的负载均衡的几种方式

  1. 随机
  2. 轮询
  3. hash
  4. 一致性hash
  5. 权重
  6. 最小链接
  7. 最短响应时间

今天先来看下dubbo怎么实现权重负载均衡的,其实各种组件实现基本也一致,权重简单介绍下就是常见的设置,比如

server1 weight=1;
server2 weight=2;
server3 weight=1;

就是说现在来了100个请求,怎么能把对应的请求按照权重进行分配,server1占1/4 也就是分25个,server2一半,50个,server3 1/4 也就是25个
先看下dubbo怎么实现的,关键代码如下

protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
    String key = invokers.get(0).getUrl().getServiceKey() + "." + RpcUtils.getMethodName(invocation);
    ConcurrentMap<String, WeightedRoundRobin> map = ConcurrentHashMapUtils.computeIfAbsent(methodWeightMap, key, k -> new ConcurrentHashMap<>());
    int totalWeight = 0;
    long maxCurrent = Long.MIN_VALUE;
    long now = System.currentTimeMillis();
    Invoker<T> selectedInvoker = null;
    WeightedRoundRobin selectedWRR = null;
    // 所有的实例列表
    for (Invoker<T> invoker : invokers) {
        // 根据url得到的标识
        String identifyString = invoker.getUrl().toIdentityString();
        // 获取对应的权重
        int weight = getWeight(invoker, invocation);
        // 对应的权重
        WeightedRoundRobin weightedRoundRobin = ConcurrentHashMapUtils.computeIfAbsent(map, identifyString, k -> {
            WeightedRoundRobin wrr = new WeightedRoundRobin();
            wrr.setWeight(weight);
            return wrr;
        });

        if (weight != weightedRoundRobin.getWeight()) {
            //weight changed
            weightedRoundRobin.setWeight(weight);
        }
        // 这里是每次遍历都要加上对应的权重,但是到后面选中的会减去,也就是大的都是需要被选中执行的
        long cur = weightedRoundRobin.increaseCurrent();
        weightedRoundRobin.setLastUpdate(now);
        if (cur > maxCurrent) {
            // maxCurrent就是在计算所有invoke里面的最大的cur,然后让其选中
            maxCurrent = cur;
            selectedInvoker = invoker;
            selectedWRR = weightedRoundRobin;
        }
        totalWeight += weight;
    }
    if (invokers.size() != map.size()) {
        map.entrySet().removeIf(item -> now - item.getValue().getLastUpdate() > RECYCLE_PERIOD);
    }
    if (selectedInvoker != null) {
        // 减去对应的值,下次如果不大于,totalWeight就是所有成员的总大小,
        selectedWRR.sel(totalWeight);
        return selectedInvoker;
    }
    // should not happen here
    return invokers.get(0);
}

大致总结下实现原理呢,就是把所有的服务提供者先列出来,放入到对应的map,有存储对应的服务者的权重信息,然后给其一个记录值 cur,然后每次遍历这些服务提供者,对其cur加上对应的权重值,然后找到cur最大的,选中让其执行,选中执行的,执行一次之后把对应的cur减去总的weight,举个例子,默认cur大家开始都是0,现在开始遍历
server1 cur = cur+weight;// cur = 1,
server2 cur = cur+weight;// cur=2
server3 cur = cur+weight; //cur=1
totalweight = server1的weight+server2的weight+server3的weight 就是4
这时候选中的就是服务2,然后服务2对应的cur 减去totalweight 变为了-2;
下一轮遍历
server1 cur = cur+weight;// cur = 2,
server2 cur = cur+weight;// cur=0
server3 cur = cur+weight; //cur=2
遍历完就是选中server1,然后server1的cur = cur-totalweight,cur变为-2;
就是这样一遍遍的选择,选择cur最大的,因为cur是根据权重增加的,所以,权重大的被选中的次数更高,实现的很巧妙,也不至于出现不均衡


http://www.niftyadmin.cn/n/5130260.html

相关文章

【计算机网络】TCP协议

文章目录 1. TCP报文的结构2. TCP的发送缓冲区和接收缓冲区3. 确保可靠性序列号和确认序列号确认应答超时重传连接管理1️⃣三次握手建立连接2️⃣四次挥手断开连接 4. 提高性能流量控制滑动窗口拥塞控制延迟应答捎带应答 5. 面向字节流6. TCP/UDP对比 概念&#xff1a;TCP&…

电脑如何激活windows

当我们电脑出现如下图&#xff1a; 显示需要激活windows时&#xff0c;操作如下。 1、桌面-新建-文本文档 2、将文档命名为&#xff08;激活windows.bat&#xff09;把原有文档中的后缀.txt去掉 3、点击右键&#xff0c;选择编辑输入代码 slmgr/skms kms.03k.org slmgr/ato4、…

mysql 计算两个坐标距离

方式一&#xff1a;st_distance_sphere 计算结果单位米 SELECT *, st_distance_sphere(point(lng,lat),point(lng,lat)) as distance FROM table mysql 版本5.7 以上 方式二&#xff1a;st_distance 计算结果单位是度 SELECT *, (st_distance(point(lng,lat),point(lng4,lat…

0042【Edabit ★☆☆☆☆☆】【是否被100整除】Multiple of 100

0042【Edabit ★☆☆☆☆☆】【是否被100整除】Multiple of 100 algebra conditions math validation Instructions Create a function that takes an integer and returns true if it’s divisible by 100, otherwise return false. Examples divisible(1) // false divisi…

借助 Pyroscope 对 Amazon EKS 容器服务进行 Continuous Profiling 诊断应用性能

Continuous Profiling 的现状 在可观测领域&#xff0c;Trace、Log、Metrics 作为“三大支柱”&#xff0c;帮助工程师更方便的洞察应用的内部问题。然而&#xff0c;对于开发人员而言&#xff0c;经常还需要深入应用程序&#xff0c;找出造成瓶颈的根本原因。在可观测的“三大…

[计算机提升] Windows系统各种开机启动方式介绍

1.14 开机启动 在Windows系统中&#xff0c;开机启动是指开启电脑后&#xff0c;自动运行指定的程序或服务的技术。一些程序或服务需要在开机后自动启动&#xff0c;以便及时响应用户操作&#xff0c;比如防安防软件、即时通信工具、文件同步软件等。 同时&#xff0c;一些系统…

Wpf 使用 Prism 实战开发Day02

一.设计首页导航条 导航条的样式&#xff0c;主要是从Material DesignThemes UI 拷贝过来修改的,项目用了这个UI组件库&#xff0c;就看自己需要什么&#xff0c;就去拷过来使用&#xff0c;界面布局或其他组件使用&#xff0c;不做介绍。 直接下载源码&#xff0c;编译运行就可…

前端第一阶段测试

前端第一阶段测试 选择问答 如果觉得有用请给我点个赞⑧~ 选择 1、【单选】下列哪个是子代选择器 A A、p>b B、p b C、pb D、p.b 2、【单选】下述有关css属性position的属性值的描述&#xff0c;说法错误的是&#xff1f;B A、static&#xff1a;没有定位&#xff0c;元素出…