Dubbo负载均衡(Load Balancing)

本文章将介绍在Dubbo中进行负载均衡配置

在集群负载均衡时,Dubbo 提供了多种均衡策略,缺省为 random 随机调用。当然你也可以自行扩展负载均衡策略。

负载均衡策略

Random LoadBalance

随机,按权重设置随机概率。在一个截面上碰撞的概率高,但调用量越大分布越均匀,而且按概率使用权重后也比较均匀,有利于动态调整提供者权重。实例:

<dubbo:reference id="helloService" loadbalance="random"
                 interface="com.huangx.dubbo.service.HelloService" />

RoundRobin LoadBalance

轮循,按公约后的权重设置轮循比率。存在慢的提供者累积请求的问题,比如:第二台机器很慢,但没挂,当请求调到第二台时就卡在那,久而久之,所有请求都卡在调到第二台上。实例:

<dubbo:reference id="helloService" loadbalance="roundrobin"
                 interface="com.huangx.dubbo.service.HelloService" />

LeastActive LoadBalance

最少活跃调用数,相同活跃数的随机,活跃数指调用前后计数差。使慢的提供者收到更少请求,因为越慢的提供者的调用前后计数差会越大。实例:

<dubbo:reference id="helloService" loadbalance="leastactive" timeout="5000"
                 interface="com.huangx.dubbo.service.HelloService" />

ConsistentHash LoadBalance

一致性 Hash,相同参数的请求总是发到同一提供者。当某一台提供者挂时,原本发往该提供者的请求,基于虚拟节点,平摊到其它提供者,不会引起剧烈变动。

算法参见:https://en.wikipedia.org/wiki/Consistent_hashing 

缺省只对第一个参数 Hash,如果要修改,请配置 :

<!-- 下面将使用服务的第一个和第二个参数作为hash运算key -->
<dubbo:parameter key="hash.arguments" value="0,1" />

缺省用 160 份虚拟节点,如果要修改,请配置 :

<!-- 设置虚拟节点的数为320个 -->
<dubbo:parameter key="hash.nodes" value="320" />

实例:

<dubbo:reference id="helloExtService" loadbalance="consistenthash" timeout="5000"
                 interface="com.huangx.dubbo.service.HelloExtService" >
    <!-- 将第二个参数作为hash运算的key -->
    <!-- 0,1 将第一个和第二个参数作为hash运算的key -->
    <dubbo:parameter key="hash.arguments" value="2" />
    <dubbo:parameter key="hash.nodes" value="4" />
</dubbo:reference>

注意:更多关于负载均衡的算法实现请查看Dubbo源代码。

实例:客户端XML配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
      xmlns:dubbo="https://code.alibabatech.com/schema/dubbo"
      xmlns="https://www.springframework.org/schema/beans"
      xsi:schemaLocation="https://www.springframework.org/schema/beans
         https://www.springframework.org/schema/beans/spring-beans-4.3.xsd
         https://code.alibabatech.com/schema/dubbo
         https://code.alibabatech.com/schema/dubbo/dubbo.xsd">

   <!-- 应用名称 -->
   <dubbo:application name="demo-consumer"/>

   <!-- 注册中心 -->
   <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />

   <!-- 生成远程服务的代理,然后可以像使用本地接口一样去使用demoService服务 -->

   <!-- random 随机选择一个服务调用。默认 -->
   <!--<dubbo:reference id="helloService" interface="com.huangx.dubbo.service.HelloService" loadbalance="random" />-->


   <!-- roundrobin 轮询,可能存在阻塞问题,当一个节点很慢时,就会累计很多的请求在该节点 -->
   <!--<dubbo:reference id="helloService" interface="com.huangx.dubbo.service.HelloService" loadbalance="roundrobin" />-->


   <!-- leastactive 最少活跃数 -->
   <!--<dubbo:reference id="helloService" interface="com.huangx.dubbo.service.HelloService" loadbalance="leastactive" timeout="5000" />-->


   <!-- consistenthash 一致性哈希负载均衡配置 -->
   <!-- hash.arguments 缺省只对第一个参数Hash,如果要修改,请配置。
         当进行调用时候根据调用方法的哪几个参数生成key,并根据key来通过一致性hash算法来选择调用结点。
         例如调用方法invoke(String s1,String s2); 若hash.arguments为1(默认值),则仅取invoke的参
         数1(s1)来生成hashCode。
         注意:参数的下标从0开始,0表示第一个参数;1表示第二个参数 -->
   <!-- hash.nodes 缺省用160份虚拟节点,如果要修改,请配置。下面配置虚拟节点为 320 个 -->
   <dubbo:reference id="helloService" interface="com.huangx.dubbo.service.HelloService" loadbalance="consistenthash" timeout="5000">
      <dubbo:parameter key="hash.arguments" value="0,1" />
      <dubbo:parameter key="hash.nodes" value="320" />
   </dubbo:reference>

   <dubbo:reference id="helloExtService" interface="com.huangx.dubbo.service.HelloExtService" loadbalance="consistenthash" timeout="5000">
      <!-- 将第二个参数作为hash运算的key -->
      <!-- 0,1 将第一个和第二个参数作为hash运算的key -->
      <dubbo:parameter key="hash.arguments" value="2" />
      <dubbo:parameter key="hash.nodes" value="4" />
   </dubbo:reference>

</beans>

完整项目代码见附件

「资源下载」

生活总会给你答案的,但不会马上把一切都告诉你。只要你肯等一等,生活的美好,总在你不经意的时候,盛装莅临。
0 不喜欢
说说我的看法 -
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号