【SpringCloud】HystrixCommand的threadPoolKey默认值及线程池初始化 (3)

但执行HystrixCommand时是有默认threadPoolKey的,那么这个默认值从何而来,command又是怎么初始化线程池的呢??

通过metaHolder构造HystrixCommandBuilder //----------HystrixCommandFactory#create() public HystrixInvokable create(MetaHolder metaHolder) { HystrixInvokable executable; if (metaHolder.isCollapserAnnotationPresent()) { executable = new CommandCollapser(metaHolder); } else if (metaHolder.isObservable()) { executable = new GenericObservableCommand(HystrixCommandBuilderFactory.getInstance().create(metaHolder)); } else { //通过metaHolder构造HystrixCommandBuilder,再创建GenericCommand executable = new GenericCommand(HystrixCommandBuilderFactory.getInstance().create(metaHolder)); } return executable; } //----------HystrixCommandBuilderFactory#create() 创建HystrixCommandBuilder //在创建HystrixCommandBuilder时,createGenericSetterBuilder(metaHolder)构造了Setter,是用于设置groupKey、commandKey、threadPoolKey的 public <ResponseType> HystrixCommandBuilder create(MetaHolder metaHolder, Collection<HystrixCollapser.CollapsedRequest<ResponseType, Object>> collapsedRequests) { validateMetaHolder(metaHolder); return HystrixCommandBuilder.builder() .setterBuilder(createGenericSetterBuilder(metaHolder)) //重点:设置setterBuilder .commandActions(createCommandActions(metaHolder)) .collapsedRequests(collapsedRequests) .cacheResultInvocationContext(createCacheResultInvocationContext(metaHolder)) .cacheRemoveInvocationContext(createCacheRemoveInvocationContext(metaHolder)) .ignoreExceptions(metaHolder.getCommandIgnoreExceptions()) .executionType(metaHolder.getExecutionType()) .build(); } //----------createGenericSetterBuilder() 创建SetterBuilder private GenericSetterBuilder createGenericSetterBuilder(MetaHolder metaHolder) { GenericSetterBuilder.Builder setterBuilder = GenericSetterBuilder.builder() .groupKey(metaHolder.getCommandGroupKey()) .threadPoolKey(metaHolder.getThreadPoolKey()) //查看从metaHolder如何获取threadPoolKey .commandKey(metaHolder.getCommandKey()) .collapserKey(metaHolder.getCollapserKey()) .commandProperties(metaHolder.getCommandProperties()) .threadPoolProperties(metaHolder.getThreadPoolProperties()) .collapserProperties(metaHolder.getCollapserProperties()); if (metaHolder.isCollapserAnnotationPresent()) { setterBuilder.scope(metaHolder.getHystrixCollapser().scope()); } return setterBuilder.build(); } //如果使用了Command注解,从注解指定的threadPoolKey 和 defaultThreadPoolKey二选一,以前者为主 //本例中,既没有通过注解指定threadPoolKey,也没有defaultThreadPoolKey public String getThreadPoolKey() { return isCommandAnnotationPresent() ? get(hystrixCommand.threadPoolKey(), defaultThreadPoolKey) : ""; }

从上面看,HystrixCommandBuilder都构造完成了,还没有设置threadPoolKey

通过HystrixCommandBuilder创建HystrixCommand

下面是通过HystrixCommandBuilder作为参数创建GenericCommand(),即通过HystrixCommandBuilder创建HystrixCommand

GenericCommand的类图为:

【SpringCloud】HystrixCommand的threadPoolKey默认值及线程池初始化

可见GenericCommand集成关系,从AbstractHystrixCommand --> HystrixCommand --> AbstractCommand,最终他们都是HystrixInvokeable接口的实现了,即可被Hystrix调用的

向上进入父类构造,HystrixCommand(Setter setter)

protected HystrixCommand(Setter setter) { // use 'null' to specify use the default this(setter.groupKey, setter.commandKey, setter.threadPoolKey, null, null, setter.commandPropertiesDefaults, setter.threadPoolPropertiesDefaults, null, null, null, null, null); }

从setter中获取了groupKey、commandKey、threadPoolKey、commandPropertiesDefaults、threadPoolPropertiesDefaults,其它参数为null

AbstractCommand构造

进入到 AbstractCommand构造方法,封装了构造一个HystrixCommand的基本上所有元素的逻辑

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); //初始化commandGroupKey this.commandKey = initCommandKey(key, getClass()); //初始化commandKey this.properties = initCommandProperties(this.commandKey, propertiesStrategy, commandPropertiesDefaults); //初始化commandProperties this.threadPoolKey = initThreadPoolKey(threadPoolKey, this.commandGroup, this.properties.executionIsolationThreadPoolKeyOverride().get()); //初始化threadPoolKey this.metrics = initMetrics(metrics, this.commandGroup, this.threadPoolKey, this.commandKey, this.properties); //初始化metrics 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 plugins this.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; }

接下来主要看是如何初始化threadPoolKey,以及threadPool的

initThreadPoolKey()

initThreadPoolKey(threadPoolKey, this.commandGroup, this.properties.executionIsolationThreadPoolKeyOverride().get())

参数:

threadPoolKey -- 指定的 或 默认的threadPoolKey

this.commandGroup -- 当前的groupKey

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zygxjz.html