Dubb原始碼學習--FailfastCluster叢集容錯(五)
阿新 • • 發佈:2018-12-25
Failfast Cluster
快速失敗,只發起一次呼叫,失敗立即報錯。通常用於非冪等性的寫操作,比如新增記錄。
在FailfastCluster中是通過建立一個FailfastClusterInvoker來完成一次呼叫失敗報錯機制的。
public class FailfastCluster implements Cluster { public final static String NAME = "failfast"; public <T> Invoker<T> join(Directory<T> directory) throws RpcException { return new FailfastClusterInvoker<T>(directory); } }
FailfastClusterInvoker中簡單來說就是通過負載均衡查詢服務提供者,呼叫服務提供提供者,如果失敗則返回失敗結果
public class FailfastClusterInvoker<T> extends AbstractClusterInvoker<T> { public FailfastClusterInvoker(Directory<T> directory) { super(directory); } 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); } } }