Dubbo 的集群容錯模式:Broadcast Cluster(廣播模式)


簡介

廣播調用所有提供者,逐個調用,任意一台報錯則報錯。通常用於通知所有提供者更新緩存或日志等本地資源信息。

如何使用

<dubbo:service cluster="broadcast" />

<dubbo:reference cluster="broadcast" />

實現邏輯

  1. 循環調用所有的實例
  2. 如果有發生異常則記錄異常保存
  3. 只要有異常,則拋出異常,如果沒有則返回執行結果

源代碼

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;
    }

}

  

原文:

https://blog.csdn.net/u011642663/article/details/81950410


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM