hystrix 源码 线程池隔离_Hystrix源码学习--线程池隔离
分析你的系統(tǒng)
你所認(rèn)識(shí)的分布式系統(tǒng),哪些是可以進(jìn)行垂直拆分的?拆分之后系統(tǒng)之間的依賴如何梳理?系統(tǒng)異構(gòu)之后的穩(wěn)定性調(diào)用如何保證?這些都是可能在分布式場(chǎng)景中面臨的問題。
說個(gè)比較常見的問題,大家都知道秒殺系統(tǒng),秒殺流程在分布式的環(huán)境中,就需要依賴:訂單管理,會(huì)員管理,支付等,假如在整個(gè)系統(tǒng)中會(huì)員系統(tǒng)不穩(wěn)定,導(dǎo)致系統(tǒng)請(qǐng)求擠壓,慢慢的就會(huì)導(dǎo)致對(duì)其他外部系統(tǒng)的依賴也不可用,所以就要使用業(yè)務(wù)的隔離,這樣會(huì)避免服務(wù)的局部不可用導(dǎo)致的全部不可用。
Hystrix 源碼分析
找到HystrixCommand的父類AbstractCommand, 里面有個(gè)構(gòu)造方法,從構(gòu)造方法可以看出里這里定義了 threadPool對(duì)象。代碼如下,關(guān)鍵代碼都有做相應(yīng)的注釋。
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) {
//commandGroup對(duì)象,用于組織一類業(yè)務(wù)相關(guān)的對(duì)象
this.commandGroup = initGroupKey(group);
// commandKey默認(rèn)是以類為為名稱的
this.commandKey = initCommandKey(key, getClass());
this.properties = initCommandProperties(this.commandKey, propertiesStrategy, commandPropertiesDefaults);
//這個(gè)方法里定義了TheradPool里的關(guān)鍵字,默認(rèn)以傳入的commandGroup 的name做為key的名稱
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);
//這里就是線程池對(duì)象啦。
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;
}
/**
這個(gè)方法用于得到HystrixThreadPoolKey 對(duì)象, Hystrix內(nèi)部有大量的Key對(duì)象,可以簡(jiǎn)單理解這些 Key都是相應(yīng)對(duì)象的唯一標(biāo)識(shí)。從代碼里可以看出,默認(rèn)情況下Hystrix采用的是commandGroup 的name做為Thread Pool的key值。
**/
private static HystrixThreadPoolKey initThreadPoolKey(HystrixThreadPoolKey threadPoolKey, HystrixCommandGroupKey groupKey, String threadPoolKeyOverride) {
if (threadPoolKeyOverride == null) {
// we don't have a property overriding the value so use either HystrixThreadPoolKey or HystrixCommandGroup
if (threadPoolKey == null) {
/* use HystrixCommandGroup if HystrixThreadPoolKey is null */
return HystrixThreadPoolKey.Factory.asKey(groupKey.name());
} else {
return threadPoolKey;
}
} else {
// we have a property defining the thread-pool so use it instead
return HystrixThreadPoolKey.Factory.asKey(threadPoolKeyOverride);
}
}
/**
在這里將調(diào)用具體的構(gòu)造線程池的方法。
**/
private static HystrixThreadPool initThreadPool(HystrixThreadPool fromConstructor, HystrixThreadPoolKey threadPoolKey, HystrixThreadPoolProperties.Setter threadPoolPropertiesDefaults) {
if (fromConstructor == null) {
// get the default implementation of HystrixThreadPool
return HystrixThreadPool.Factory.getInstance(threadPoolKey, threadPoolPropertiesDefaults);
} else {
return fromConstructor;
}
}
總結(jié)
以上是生活随笔為你收集整理的hystrix 源码 线程池隔离_Hystrix源码学习--线程池隔离的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: dda算法_计算机图形学中的DDA(数字
- 下一篇: Linux基础监控小工具nmon