【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模式的
推荐阅读
- 王者荣耀9月27日微信每日一题答案分享
- 如何修改微信开机密码(微信原密码忘了如何重新设置密码)
- 11 微服务架构学习与思考:开源 API 网关02-以 Java 为基础的 API 网关详细介绍
- 如何修改微信密码(怎样修改微信密码教程)
- 如何重新设定微信密码(微信密码8-16位)
- 微信独立密码怎么改(微信登录新手机三天不能改密码)
- 怎么修改微信号,怎么修改微信密码(微信修改微信号的方法)
- 微信怎么修改密码,如何修改微信密码(微信手势密码忘了进不了微信)
- 我的微信密码怎么修改(微信为了账号安全暂不能修改密码)
- 怎样修改微信加密密码(微信如何加密隐藏某个人聊天记录)