Dubbo 的集群容错模式:Failfast Cluster

本文简单介绍 Dubbo 中的 Failfast Cluster;都在dubbo-cluster 子目录里。

1 简介

调用实例失败后,如果有报错,则直接抛出异常。

2 如何使用

<dubbo:service cluster="failfast" />

<dubbo:reference cluster="failfast" />

3 实现逻辑

  1. 根据负载均衡算法选中被调用实例
  2. 执行选中的实例
  3. 执行成功则返回;执行有异常则直接抛出异常,不进行重试等操作

4 实现代码

public class FailfastClusterInvoker<T> extends AbstractClusterInvoker<T> {

    public FailfastClusterInvoker(Directory<T> directory) {
        super(directory);
    }

    @Override
    public Result doInvoke(Invocation invocation, List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException {
        checkInvokers(invokers, invocation);
        // 选择调用实例
        Invoker<T> invoker = select(loadbalance, invocation, invokers, null);
        try {
            // 执行调用
            return invoker.invoke(invocation);
        } catch (Throwable e) {
            // 如果有异常则直接抛出异常
            if (e instanceof RpcException && ((RpcException) e).isBiz()) { // biz exception.
                throw (RpcException) e;
            }
            throw new RpcException(e instanceof RpcException ? ((RpcException) e).getCode() : 0, "Failfast invoke providers " + invoker.getUrl() + " " + loadbalance.getClass().getSimpleName() + " select from all providers " + invokers + " for service " + getInterface().getName() + " method " + invocation.getMethodName() + " on consumer " + NetUtils.getLocalHost() + " use dubbo version " + Version.getVersion() + ", but no luck to perform the invocation. Last error is: " + e.getMessage(), e.getCause() != null ? e.getCause() : e);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值