publicclassFailoverClusterInvoker<T> extendsAbstractClusterInvoker<T> { public Result doInvoke(Invocation invocation, final List<Invoker<T>> invokers, LoadBalance loadbalance)throws RpcException { List<Invoker<T>> copyInvokers = invokers; checkInvokers(copyInvokers, invocation); StringmethodName= RpcUtils.getMethodName(invocation); intlen= getUrl().getMethodParameter(methodName, RETRIES_KEY, DEFAULT_RETRIES) + 1; if (len <= 0) { len = 1; } // retry loop. RpcExceptionle=null; // last exception. List<Invoker<T>> invoked = newArrayList<Invoker<T>>(copyInvokers.size()); // invoked invokers. Set<String> providers = newHashSet<String>(len); for (inti=0; i < len; i++) { //Reselect before retry to avoid a change of candidate `invokers`. //NOTE: if `invokers` changed, then `invoked` also lose accuracy. if (i > 0) { checkWhetherDestroyed(); copyInvokers = list(invocation); // check again checkInvokers(copyInvokers, invocation); } Invoker<T> invoker = select(loadbalance, invocation, copyInvokers, invoked); invoked.add(invoker); RpcContext.getContext().setInvokers((List) invoked); try { Resultresult= invoker.invoke(invocation); return result; } catch (RpcException e) { // 只有业务异常不会重试 if (e.isBiz()) { // biz exception. throw e; } le = e; } catch (Throwable e) { le = newRpcException(e.getMessage(), e); } finally { providers.add(invoker.getUrl().getAddress()); } } thrownewRpcException(le.getCode(), "Failed to invoke"); } }
注意,Result result = invoker.invoke(invocation);会封装provider端返回的异常,最终InvokerInvocationHandler会进行recreate把异常在consumer端抛出。invoker.invoke(rpcInvocation).recreate();。