微服务组件--限流框架Spring Cloud Hystrix分析( 三 )

【6】分析HystrixInvokable的创建【层层追溯,其实发现是生成一个包装过的HystrixCommand】
//分析HystrixInvokable的创建public HystrixInvokable create(MetaHolder metaHolder) {HystrixInvokable executable;//判断是不是HystrixCollapser注解if (metaHolder.isCollapserAnnotationPresent()) {executable = new CommandCollapser(metaHolder);} else if (metaHolder.isObservable()) {executable = new GenericObservableCommand(HystrixCommandBuilderFactory.getInstance().create(metaHolder));} else {//主要是这里executable = new GenericCommand(HystrixCommandBuilderFactory.getInstance().create(metaHolder));}return executable;}public GenericCommand(HystrixCommandBuilder builder) {super(builder);}protected AbstractHystrixCommand(HystrixCommandBuilder builder) {super(builder.getSetterBuilder().build());this.commandActions = builder.getCommandActions();this.collapsedRequests = builder.getCollapsedRequests();this.cacheResultInvocationContext = builder.getCacheResultInvocationContext();this.cacheRemoveInvocationContext = builder.getCacheRemoveInvocationContext();this.ignoreExceptions = builder.getIgnoreExceptions();this.executionType = builder.getExecutionType();}HystrixCommand(HystrixCommandGroupKey group, HystrixCommandKey key, HystrixThreadPoolKey threadPoolKey, HystrixCircuitBreaker circuitBreaker, HystrixThreadPool threadPool,HystrixCommandProperties.Setter commandPropertiesDefaults, HystrixThreadPoolProperties.Setter threadPoolPropertiesDefaults,HystrixCommandMetrics metrics, TryableSemaphore fallbackSemaphore, TryableSemaphore executionSemaphore,HystrixPropertiesStrategy propertiesStrategy, HystrixCommandExecutionHook executionHook) {super(group, key, threadPoolKey, circuitBreaker, threadPool, commandPropertiesDefaults, threadPoolPropertiesDefaults, metrics, fallbackSemaphore, executionSemaphore, propertiesStrategy, executionHook);}protected AbstractCommand(HystrixCommandGroupKey group, HystrixCommandKey key, HystrixThreadPoolKey threadPoolKey, HystrixCircuitBreaker circuitBreaker, HystrixThreadPool threadPool,HystrixCommandProperties.Setter commandPropertiesDefaults, HystrixThreadPoolProperties.Setter threadPoolPropertiesDefaults,HystrixCommandMetrics metrics, TryableSemaphore fallbackSemaphore, TryableSemaphore executionSemaphore,HystrixPropertiesStrategy propertiesStrategy, HystrixCommandExecutionHook executionHook) {this.commandGroup = initGroupKey(group);this.commandKey = initCommandKey(key, getClass());this.properties = initCommandProperties(this.commandKey, propertiesStrategy, commandPropertiesDefaults);this.threadPoolKey = initThreadPoolKey(threadPoolKey, this.commandGroup, this.properties.executionIsolationThreadPoolKeyOverride().get());this.metrics = initMetrics(metrics, this.commandGroup, this.threadPoolKey, this.commandKey, this.properties);this.circuitBreaker = initCircuitBreaker(this.properties.circuitBreakerEnabled().get(), circuitBreaker, this.commandGroup, this.commandKey, this.properties, this.metrics);this.threadPool = initThreadPool(threadPool, this.threadPoolKey, threadPoolPropertiesDefaults);//Strategies from pluginsthis.eventNotifier = HystrixPlugins.getInstance().getEventNotifier();this.concurrencyStrategy = HystrixPlugins.getInstance().getConcurrencyStrategy();HystrixMetricsPublisherFactory.createOrRetrievePublisherForCommand(this.commandKey, this.commandGroup, this.metrics, this.circuitBreaker, this.properties);this.executionHook = initExecutionHook(executionHook);this.requestCache = HystrixRequestCache.getInstance(this.commandKey, this.concurrencyStrategy);this.currentRequestLog = initRequestLog(this.properties.requestLogEnabled().get(), this.concurrencyStrategy);/* fallback semaphore override if applicable */this.fallbackSemaphoreOverride = fallbackSemaphore;/* execution semaphore override if applicable */this.executionSemaphoreOverride = executionSemaphore;}【7】那么接下来分析CommandExecutor.execute做了什么
public static Object execute(HystrixInvokable invokable, ExecutionType executionType, MetaHolder metaHolder) throws RuntimeException {Validate.notNull(invokable);Validate.notNull(metaHolder);switch (executionType) {//基于上面构成先分析同步方法case SYNCHRONOUS: {//调用HystrixCommandreturn castToExecutable(invokable, executionType).execute();}case ASYNCHRONOUS: {HystrixExecutable executable = castToExecutable(invokable, executionType);if (metaHolder.hasFallbackMethodCommand()&& ExecutionType.ASYNCHRONOUS == metaHolder.getFallbackExecutionType()) {return new FutureDecorator(executable.queue());}return executable.queue();}case OBSERVABLE: {HystrixObservable observable = castToObservable(invokable);return ObservableExecutionMode.EAGER == metaHolder.getObservableExecutionMode() ? observable.observe() : observable.toObservable();}default:throw new RuntimeException("unsupported execution type: " + executionType);}}//HystrixCommand类#execute方法public R execute() {try {//利用了Future模式return queue().get();} catch (Exception e) {throw Exceptions.sneakyThrow(decomposeException(e));}}【8】分析queue()方法怎么使用Future模式的

推荐阅读