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

相關文章
相關標籤/搜索