Dubbo集群容错模式之Failfast实现

注:dubbo的版本是2.6.2。java

                       

                                                  图1 Dubbo的FailfastClusterInvoker类继承图ide

1.Failfast的含义

    Failfast能够理解为只发起一次调用,若失败则当即报错。spa

2.Failfast的实现

    核心代码在FailfastClusterInvoker的doInvoke(Invocation,List<Invoker<T>>,LoadBalance)中,源码以下。code

@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);
    }
}
  • 方法select(loadbalance, invocation, invokers, null),根据loadbalance从候选服务提供者中选出Invoker<T> invoker,以后调用invoker的invoke方法。
  • 若是调用成功,则直接返回结果,若失败则抛出异常,再也不作其它任何处理。

    这个比较简单,好理解。重点在失败以后再也不进行重调等尝试。orm