【Hystrix】【源码+图解】【三】执行Hystrix命令

【Hystrix】【源码+图解】【二】创建Hystrix命令
[toc]

6. 执行命令

    @Around("hystrixCommandAnnotationPointcut() || hystrixCollapserAnnotationPointcut()")
    public Object methodsAnnotatedWithHystrixCommand(final ProceedingJoinPoint joinPoint) throws Throwable {
        ......
            if (!metaHolder.isObservable()) {
                result = CommandExecutor.execute(invokable, executionType, metaHolder);
            } else {
                result = executeObservable(invokable, executionType, metaHolder);
            }
        ......
    }

    private Observable executeObservable(HystrixInvokable invokable, ExecutionType executionType, final MetaHolder metaHolder) {
        return ((Observable) CommandExecutor.execute(invokable, executionType, metaHolder))
        ......
    }

不管是什么类型,最终都会调用CommandExecutor.execute()方法执行命令

// CommandExecutor
	public static Object execute(HystrixInvokable invokable, ExecutionType executionType, MetaHolder metaHolder) throws RuntimeException {
        ......
        // 通过【3.1】获知executionType
        // 通过Hystrix命令类型可知不管是同步执行execute()、还是异步执行queue()、热观察者模式observe(),最终都会先调用toObservable()获得被观察者,因此我们直接去分析AbstractCommand.toObservable()
        switch (executionType) {
            // 同步执行execute()
            case SYNCHRONOUS: {
                return castToExecutable(invokable, executionType).execute();
            }
            // 异步执行queue()
            case ASYNCHRONOUS: {
                HystrixExecutable executable = castToExecutable(invokable, executionType);
                ......
                return executable.queue();
            }
            // 热观察者模式observe() : 冷观察者模式toObservable()
            case OBSERVABLE: {
                HystrixObservable observable = castToObservable(invokable);
                return ObservableExecutionMode.EAGER == metaHolder.getObservableExecutionMode() ? observable.observe() : observable.toObservable();
            }
            default:
                throw new RuntimeException("unsupported execution type: " + executionType);
        }
    }

AbstractCommand.toObservable()

    public Observable<R> toObservable() {
		......
        return Observable.defer(new Func0<Observable<R>>() {
            @Override
            public Observable<R> call() {
                ......
                if (requestCacheEnabled) {
                    // 缓存功能【7】
                    HystrixCommandResponseFromCache<R> fromCache = (HystrixCommandResponseFromCache<R>) requestCache.get(cacheKey);
                    if (fromCache != null) {
                        isResponseFromCache = true;
                        return handleRequestCacheHitAndEmitValues(fromCache, _cmd);
                    }
                }
                // 缓存不可用,调用applyHystrixSemantics()创建执行命令的被观察者
                Observable<R> hystrixObservable =
                        Observable.defer(applyHystrixSemantics)
                                .map(wrapWithAllOnNextHooks);

                ......
            }
        });
    }

调用调用applyHystrixSemantics()

// AbstractCommand
	private Observable<R> applyHystrixSemantics(final AbstractCommand<R> _cmd) {
        // 断路器功能【8】
        if (circuitBreaker.allowRequest()) {
            final TryableSemaphore executionSemaphore = getExecutionSemaphore();
            ......
            // 限流功能【9】
            if (executionSemaphore.tryAcquire()) {
                ......
                    return executeCommandAndObserve(_cmd)
                            .doOnError(markExceptionThrown)
                            .doOnTerminate(singleSemaphoreRelease)
                            .doOnUnsubscribe(singleSemaphoreRelease);
               ......
            } else {
                // 降级处理【11】
                return handleSemaphoreRejectionViaFallback();
            }
        } else {
            // 降级处理
            return handleShortCircuitViaFallback();
        }
    }

调用executeCommandAndObserve

// AbstractCommand
	private Observable<R> executeCommandAndObserve(final AbstractCommand<R> _cmd) {
        ......
            executeCommandWithSpecifiedIsolation(_cmd);
        ......
    }

    private Observable<R> executeCommandWithSpecifiedIsolation(final AbstractCommand<R> _cmd) {
        // 隔离功能【9】
        if (properties.executionIsolationStrategy().get() == ExecutionIsolationStrategy.THREAD) {
            ...... // 线程池策略
        } else {
            ...... // 信号量策略
        }
    }

命令的执行大体如此,后续逐个分析各个功能

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值