【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 {
...... // 信号量策略
}
}
命令的执行大体如此,后续逐个分析各个功能