1.负载均衡

   在集群负载均衡时,Dubbo 提供了多种均衡策略,缺省为 random 随机调用。

1.  负载均衡策略

Random LoadBalance

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

RoundRobin LoadBalance

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

LeastActive LoadBalance

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

ConsistentHash LoadBalance

一致性 Hash,相同参数的请求总是发到同一提供者。当某一台提供者挂时,原本发往该提供者的请求,基于虚拟节点,平摊到其它提供者,不会引起剧烈变动。算法参见:http://en.wikipedia.org/wiki/Consistent_hashing缺省只对第一个参数 Hash,如果要修改,请配置 缺省用 160 份虚拟节点,如果要修改,请配置

2.  配置

服务端服务级别

客户端服务级别

 

服务端方法级别

客户端方法级别

 

3.负载均衡测试

(1)服务接口

package dubbo;

public interface DubboService {

String sayHello(String name);

}

 

(2)两个实现类

package dubbo;

public class DubboServiceImpl implements DubboService {

public String sayHello(String name) {

return "Hello " + name;

}

}

 

package dubbo;

public class DubboServiceImpl2 implements DubboService {

public String sayHello(String name) {

return "Hello " + name + ",DubboServiceImpl2";

}

}

 

(3)dubbo服务生产者 provider.xml

xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns="http://www.springframework.org/schema/beans"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

 

生产者代码:

package dubbo;

import java.io.IOException;

import java.util.concurrent.CountDownLatch;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Provider {

public static void main(String[] args) throws IOException, InterruptedException {

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "provider.xml" });

context.start();

CountDownLatch countDownLatch = new CountDownLatch(1);

countDownLatch.await();

}

}

 

(4)消费者consumer.xml---测试默认是随机策略

xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns="http://www.springframework.org/schema/beans"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

 

测试代码:

package dubbo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Consumer {

public static void main(String[] args) throws InterruptedException {

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "consumer.xml" });

context.start();

for (int i = 0; i < 10; i++) {

DubboService service = (DubboService) context.getBean("dubboService");

System.out.println(service.sayHello(" china "));

}

}

}

结果:(4/6分,也就是随机策略的结果)

Hello china Hello china ,DubboServiceImpl2Hello china Hello china Hello china Hello china Hello china ,DubboServiceImpl2Hello china ,DubboServiceImpl2Hello china Hello china ,DubboServiceImpl2

 

(5)策略改为轮询策略进行测试---loadbalance="roundrobin"

xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns="http://www.springframework.org/schema/beans"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

loadbalance="roundrobin" />

结果:(轮询的效果如下)

Hello china ,DubboServiceImpl2Hello china Hello china ,DubboServiceImpl2Hello china Hello china ,DubboServiceImpl2Hello china Hello china ,DubboServiceImpl2Hello china Hello china ,DubboServiceImpl2Hello china

 

从dubbo-admin项目查看权重信息如下:

(6)修改权重为300:100后再次测试以及查看

 修改provider.xml增加权重信息,consumer.xml同上

xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns="http://www.springframework.org/schema/beans"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

weight="300" />

weight="100" />

 测试结果如下:

Hello china Hello china Hello china ,DubboServiceImpl2Hello china Hello china Hello china Hello china ,DubboServiceImpl2Hello china Hello china Hello china

 

查看权重信息如下:

2.    集群容错

  在集群调用失败时,Dubbo 提供了多种容错方案,缺省为 failover 重试。

各节点关系:

  这里的 Invoker 是 Provider 的一个可调用 Service 的抽象,Invoker 封装了 Provider 地址及 Service 接口信息  Directory 代表多个 Invoker,可以把它看成 List ,但与 List 不同的是,它的值可能是动态变化的,比如注册中心推送变更  Cluster 将 Directory 中的多个 Invoker 伪装成一个 Invoker,对上层透明,伪装过程包含了容错逻辑,调用失败后,重试另一个  Router 负责从多个 Invoker 中按路由规则选出子集,比如读写分离,应用隔离等  LoadBalance 负责从多个 Invoker 中选出具体的一个用于本次调用,选的过程包含了负载均衡算法,调用失败后,需要重选

1.集群容错模式

Failover Cluster (默认值)

    失败自动切换,当出现失败,重试其它服务器 。通常用于读操作,但重试会带来更长延迟。可通过 retries="2" 来设置重试次数(不含第一次)。

重试次数配置如下:

 

Failfast Cluster

    快速失败,只发起一次调用,失败立即报错。通常用于非幂等性的写操作,比如新增记录。

Failsafe Cluster

    失败安全,出现异常时,直接忽略。通常用于写入审计日志等操作。

Failback Cluster

    失败自动恢复,后台记录失败请求,定时重发。通常用于消息通知操作。

Forking Cluster

    并行调用多个服务器,只要一个成功即返回。通常用于实时性要求较高的读操作,但需要浪费更多服务资源。可通过 forks="2" 来设置最大并行数。

Broadcast Cluster

    广播调用所有提供者,逐个调用,任意一台报错则报错 。通常用于通知所有提供者更新缓存或日志等本地资源信息。

2.    集群模式配置

按照以下示例在服务提供方和消费方配置集群模式

 

3.测试

  服务实现类将其中一个休眠5秒钟。

package dubbo;

public class DubboServiceImpl2 implements DubboService {

public String sayHello(String name) {

try {

Thread.sleep(5 * 1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

return "Hello " + name + ",DubboServiceImpl2";

}

}

 

 provider.xml如下:

xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns="http://www.springframework.org/schema/beans"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

weight="300" />

timeout="50000" weight="100" />

(1) 缺省Failover Cluster

  在服务消费者执行过程中,通过监控中心禁用某个服务提供者,执行结果显示能够正确切换到可用服务提供者,不会报错或者丢失调用。

 

(2)设置为Failfast Cluster的时候

  当服务消费者调用某个服务提供者时,如果停掉该服务提供者,将立即报错并不继续执行。

(3)设置为Failsafe Cluster的时候

  当服务消费者调用某个服务提供者时,如果停掉该服务提供者,直接忽略,继续往下走。

 

查看原文