负载均衡扩展

扩展说明

从多个服务提供方中选择一个进行调用

扩展接口

org.apache.dubbo.rpc.cluster.LoadBalance

扩展配置

  1. <dubbo:protocol loadbalance="xxx" />
  2. <!-- 缺省值设置,当<dubbo:protocol>没有配置loadbalance时,使用此配置 -->
  3. <dubbo:provider loadbalance="xxx" />

已知扩展

  • org.apache.dubbo.rpc.cluster.loadbalance.RandomLoadBalance
  • org.apache.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance
  • org.apache.dubbo.rpc.cluster.loadbalance.LeastActiveLoadBalance

扩展示例

Maven 项目结构:

  1. src
  2. |-main
  3. |-java
  4. |-com
  5. |-xxx
  6. |-XxxLoadBalance.java (实现LoadBalance接口)
  7. |-resources
  8. |-META-INF
  9. |-dubbo
  10. |-org.apache.dubbo.rpc.cluster.LoadBalance (纯文本文件,内容为:xxx=com.xxx.XxxLoadBalance)

XxxLoadBalance.java:

  1. package com.xxx;
  2. import org.apache.dubbo.rpc.cluster.LoadBalance;
  3. import org.apache.dubbo.rpc.Invoker;
  4. import org.apache.dubbo.rpc.Invocation;
  5. import org.apache.dubbo.rpc.RpcException;
  6. public class XxxLoadBalance implements LoadBalance {
  7. public <T> Invoker<T> select(List<Invoker<T>> invokers, Invocation invocation) throws RpcException {
  8. // ...
  9. }
  10. }

META-INF/dubbo/org.apache.dubbo.rpc.cluster.LoadBalance:

  1. xxx=com.xxx.XxxLoadBalance