Dubbo 的叢集容錯模式:Broadcast Cluster
阿新 • • 發佈:2019-01-26
本文簡單介紹 Dubbo 中的 Broadcast Cluster(廣播呼叫所有提供者,逐個呼叫,任何一臺報錯則報錯)。
簡介
廣播呼叫所有提供者,逐個呼叫,任意一臺報錯則報錯。通常用於通知所有提供者更新快取或日誌等本地資源資訊。
如何使用
<dubbo:service cluster="broadcast" />
或
<dubbo:reference cluster="broadcast" />
實現邏輯
- 迴圈呼叫所有的例項
- 如果有發生異常則記錄異常儲存
- 只要有異常,則丟擲異常,如果沒有則返回執行結果
原始碼
public class BroadcastClusterInvoker<T> extends AbstractClusterInvoker<T> {
private static final Logger logger = LoggerFactory.getLogger(BroadcastClusterInvoker.class);
public BroadcastClusterInvoker(Directory<T> directory) {
super(directory);
}
@Override
@SuppressWarnings({"unchecked" , "rawtypes"})
public Result doInvoke(final Invocation invocation, List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException {
checkInvokers(invokers, invocation);
RpcContext.getContext().setInvokers((List) invokers);
RpcException exception = null ;
Result result = null;
// 廣播到所有的被呼叫例項
for (Invoker<T> invoker : invokers) {
try {
result = invoker.invoke(invocation);
} catch (RpcException e) {
// 記錄異常
exception = e;
logger.warn(e.getMessage(), e);
} catch (Throwable e) {
// 記錄異常
exception = new RpcException(e.getMessage(), e);
logger.warn(e.getMessage(), e);
}
}
// 只要有一個例項有異常則報錯
if (exception != null) {
throw exception;
}
return result;
}
}
做個有夢想的程式猿