Dubbo集群容错(cluster)

本文将简单介绍Dubbo中如何配置集群容错

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

Failover Cluster 失败自动切换

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

<dubbo:service retries="2" />

<dubbo:reference retries="2" />

<dubbo:reference>
    <dubbo:method name="findFoo" retries="2" />
</dubbo:reference>

Failfast Cluster 一次定胜负

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

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

Failsafe Cluster 失败安全,直接忽略

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

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

Failback Cluster 失败恢复,定时重发

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

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

Forking Cluster 并行调用,只要一个成功

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

<dubbo:reference id="helloService" interface="com.huangx.dubbo.service.HelloService" cluster="forking">
    <dubbo:parameter key="forks" value="3" />
</dubbo:reference>

Broadcast Cluster 广播,不允许任何失败

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

<!-- broadcast 广播调用所有提供者,逐个调用,任意一台报错则报错 -->
<dubbo:reference id="helloService" interface="com.huangx.dubbo.service.HelloService" cluster="broadcast" />

实例:创建两个Maven模块,一个客户端和一个服务提供者(其中通过三个类启动三个服务,监听不同的端口)。然后客户端配置不同的容错机制,然后观察输出。

客户端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服务 -->

   <!-- failover 调用失败,根据设置进行重试 -->
   <!-- retries="6" 将重试5次,加上第一次,共计6次 -->
   <!--<dubbo:reference id="helloService" interface="com.huangx.dubbo.service.HelloService" cluster="failover" retries="5" />-->


   <!-- failfast 快速失败,只调用一次 -->
   <!--<dubbo:reference id="helloService" interface="com.huangx.dubbo.service.HelloService" cluster="failfast" />-->


   <!-- failsafe 安全失败,出现异常直接忽略 -->
   <!--<dubbo:reference id="helloService" interface="com.huangx.dubbo.service.HelloService" cluster="failsafe" />-->


   <!-- failback 失败自动恢复,后台记录失败请求,定时重发 -->
   <!--<dubbo:reference id="helloService" interface="com.huangx.dubbo.service.HelloService" cluster="failback" />-->


   <!-- forking 并行调用多个服务器,只要一个成功即返回,通过 forks 设置并发的个数 -->
   <!--<dubbo:reference id="helloService" interface="com.huangx.dubbo.service.HelloService" cluster="forking">
      <dubbo:parameter key="forks" value="3" />
   </dubbo:reference>-->


   <!-- broadcast 广播调用所有提供者,逐个调用,任意一台报错则报错 -->
   <dubbo:reference id="helloService" interface="com.huangx.dubbo.service.HelloService" cluster="broadcast" />

</beans>

客户端Java代码:

package com.huangx.dubbo.consumer;

import com.huangx.dubbo.service.HelloService;
import org.springframework.context.support.ClassPathXmlApplicationContext;


/**
 * 加载 Spring 配置,启动 Dubbo 服务消费者
 */
public class Consumer {

   public static void main(String[] args) throws Exception {
      ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"dubbo-consumer.xml"});
      context.start();

      // 获取远程服务
      HelloService demoService = (HelloService) context.getBean("helloService");
      System.out.println(demoService.sayHello("Bill"));

      // 按任意键退出
      System.in.read();
   }

}

注意:项目完整的源代码请查看附件。

「资源下载」

天下之事常成于困约,而败于奢靡。——陆游
0 不喜欢
说说我的看法 -
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号