Skip to content

容错机制 ​

深入理解Dubbo容错机制的实现原理,掌握失败重试、熔断降级等机制,理解超时控制、异常处理的实现方式。

📋 容错机制概览 ​

容错机制是分布式系统中的关键功能,Dubbo提供了多种容错策略保证系统的稳定性。

🎯 一、失败重试 ​

1.1 FailoverClusterInvoker ​

失败自动切换是默认的容错策略,调用失败后会自动切换到其他提供者并重试:

java
public class FailoverClusterInvoker<T> extends AbstractClusterInvoker<T> {
    @Override
    @SuppressWarnings({"unchecked", "rawtypes"})
    public Result doInvoke(Invocation invocation, final List<Invoker<T>> invokers, 
            LoadBalance loadbalance) throws RpcException {
        List<Invoker<T>> copyinvokers = invokers;
        checkInvokers(copyinvokers, invocation);
        String methodName = RpcUtils.getMethodName(invocation);
        int len = getUrl().getMethodParameter(methodName, RETRIES_KEY, DEFAULT_RETRIES) + 1;
        if (len <= 0) {
            len = 1;
        }
        RpcException le = null;
        List<Invoker<T>> invoked = new ArrayList<Invoker<T>>(copyinvokers.size());
        Set<String> providers = new HashSet<String>(len);
        for (int i = 0; i < len; i++) {
            if (i > 0) {
                checkWhetherDestroyed();
                copyinvokers = list(invocation);
                checkInvokers(copyinvokers, invocation);
            }
            Invoker<T> invoker = select(loadbalance, invocation, copyinvokers, invoked);
            invoked.add(invoker);
            RpcContext.getContext().setInvokers((List) invoked);
            try {
                Result result = invoker.invoke(invocation);
                if (le != null && logger.isWarnEnabled()) {
                    logger.warn("Although retry the method " + methodName
                            + " in the service " + getInterface().getName()
                            + " was successful by the provider " + invoker.getUrl().getAddress()
                            + ", but there have been failed providers " + providers
                            + " (" + providers.size() + "/" + copyinvokers.size()
                            + ") in the previous attempts. Last error is: "
                            + le.getMessage(), le);
                }
                return result;
            } catch (RpcException e) {
                if (e.isBiz()) {
                    throw e;
                }
                le = e;
            } catch (Throwable e) {
                le = new RpcException(e.getMessage(), e);
            } finally {
                providers.add(invoker.getUrl().getAddress());
            }
        }
        throw new RpcException(...);
    }
}

重试策略:

  • 重试次数:默认重试2次(总共3次调用)
  • 重试条件:只有非业务异常才重试
  • 重试提供者:不重复调用已失败的提供者

🎯 二、熔断降级 ​

2.1 熔断器 ​

熔断器用于在服务异常时快速失败,避免大量请求堆积:

CircuitBreaker接口:

java
public interface CircuitBreaker {
    boolean isOpen();
    void onSuccess();
    void onFailure();
}

2.2 降级策略 ​

降级策略在服务不可用时返回默认值或调用备用服务。

🎯 三、超时控制 ​

3.1 超时配置 ​

Dubbo支持两种超时配置:

  • timeout:调用超时时间(默认1000ms)
  • connect.timeout:连接超时时间(默认3000ms)

3.2 超时处理 ​

超时异常:

java
if (System.currentTimeMillis() - start > timeout) {
    throw new RpcException("Invoke timeout");
}

📖 学习资源 ​

视频教程 ​

书籍推荐 ​

  • 《深度剖析Apache Dubbo核心技术内幕》 - 华仔
  • 《分布式系统常用技术及案例分析》 - 柳伟卫

网站资源 ​

✅ 学习检查清单 ​

  • [ ] 已理解失败重试机制
  • [ ] 已理解熔断降级机制
  • [ ] 已理解超时控制
  • [ ] 已理解异常处理
  • [ ] 已绘制容错流程图
  • [ ] 已总结核心知识点

上一章节:负载均衡机制
下一章节:序列化协议
返回目录:Dubbo源码深度解析