RPC之美团pigeon源码分析(四)熔断和降级

服务的熔断指的是当调用方对服务的请求出现大量的超时和失败的情况下,后续对该服务的请求将不再发起调用,直接返回。保证资源的快速释放。等到服务恢复后再正常请求。
服务的降级指的是为了缓解服务器的压力,对一些非核心业务的处理或者第三方服务的调用直接走降级逻辑,返回默认值,从而缓解服务器的压力,保证核心业务的正常处理。

关于服务的熔断,开源项目hystrix进行了非常优秀的实践,使用起来非常方便。简单了解下HystrixCommand.java:

    /**
     * Implement this method with code to be executed when {@link #execute()} or {@link #queue()} are invoked.
     * 
     * @return R response type
     * @throws Exception
     *             if command execution fails
     */
    protected abstract R run() throws Exception;

    /**
     * If {@link #execute()} or {@link #queue()} fails in any way then this method will be invoked to provide an opportunity to return a fallback response.
     * <p>
     * This should do work that does not require network transport to produce.
     * <p>
     * In other words, this should be a static or cached result that can immediately be returned upon failure.
     * <p>
     * If network traffic is wanted for fallback (such as going to MemCache) then the fallback implementation should invoke another {@link HystrixCommand} instance that protects against that network
     * access and possibly has another level of fallback that does not involve network access.
     * <p>
     * DEFAULT BEHAVIOR: It throws UnsupportedOperationException.
     * 
     * @return R or throw UnsupportedOperationException if not implemented
     */
    protected R getFallback() {
        throw new UnsupportedOperationException("No fallback available.");
    }

方法的注释非常清晰,使用中我们继承HystrixCommand,实现run()方法和getFallback()方法。run()是正常逻辑实现,当run()失败后执行getFallback()来返回一个默认值。
github地址:https://github.com/Netflix/Hystrix
这里只简单涉及下,后续会对Hystrix和RxJava源码做一个系列分析。

微服务架构中对服务的熔断和降级处理至关重要,避免依赖的某一个服务或依赖的第三方服务不可用时出现雪崩。

本篇我们来看看pigeon中对服务的熔断和降级的处理。
回顾一下调用方请求责任链:

        public static void init() {
        if (!isInitialized) {
            if (Constants.MONITOR_ENABLE) {
                registerBizProcessFilter(new RemoteCallMonitorInvokeFilter());
            }
            registerBizProcessFilter(new TraceFilter());
                        //熔断和降级
            registerBizProcessFilter(new DegradationFilter());
                        registerBizProcessFilter(new ClusterInvokeFilter());
            registerBizProcessFilter(new GatewayInvokeFilter());
            registerBizProcessFilter(new ContextPrepareInvokeFilter());
            registerBizProcessFilter(new SecurityFilter());
                        //远程调用
            registerBizProcessFilter(new RemoteCallInvokeFilter());
            bizInvocationHandler = createInvocationHandler(bizProcessFilters);
            isInitialized = true;
        }
    }

我们看看DegradationFilter.invoke的代码实现:

    @Override
    public InvocationResponse invoke(ServiceInvocationHandler handler, InvokerContext context) throws Throwable {
        context.getTimeline().add(new TimePoint(TimePhase.D));
        InvocationResponse response = null;
        //判断是否需要直接返回降级结果
        if (DegradationManager.INSTANCE.needDegrade(context)) {
            response = degradeCall(context, false);
        }
        //降级处理,直接返回
        if (response != null) {// 返回三种调用模式的降级结果
            return response;
        }

        boolean failed = false;
        try {
            response = handler.handle(context);
            Object responseReturn = response.getReturn();
            if (responseReturn != null) {
                int messageType = response.getMessageType();
                if (messageType == Constants.MESSAGE_TYPE_EXCEPTION) {
                    RpcException rpcException = InvokerUtils.toRpcException(response);
                    if (rpcException instanceof RemoteInvocationException
                            || rpcException instanceof RejectedException) {
                        failed = true;
                        //异常返回,统计请求数据
                        DegradationManager.INSTANCE.addFailedRequest(context, rpcException);
                    }
                }
            }

            return response;

        } catch (ServiceUnavailableException | RemoteInvocationException | RequestTimeoutException
                | RejectedException e) {
            failed = true;
            //是否需要返回失败降级结果
            if (DegradationManager.INSTANCE.needFailureDegrade(context)) {
                context.getDegradeInfo().setFailureDegrade(true);
                context.getDegradeInfo().setCause(e);
                response = degradeCall(context, false);
            }
            if (response != null) {// 返回同步调用模式的失败降级结果
                return response;
            }
            //异常返回,统计请求数据
            DegradationManager.INSTANCE.addFailedRequest(context, e);
            throw e;
        } finally {
            RequestQualityManager.INSTANCE.addClientRequest(context, failed);
        }
    }

首先会判断当前请求是否需要直接返回降级结果,判断逻辑是基于秒级窗口的总调用数、失败调用数、降级调用数。具体逻辑请看DegradationManager.needDegrade、DegradationManager.addRequest、DegradationManager.Checker。

本篇关于熔断和降级的介绍到此结束,希望能对大家有所帮助。

猜你喜欢

转载自blog.csdn.net/weixin_34409741/article/details/88312601