hystrix 源码 线程池隔离_熔断器 Hystrix 源码解析 —— 命令执行(二)之执行隔离策略...

本文深入解析 Hystrix 的执行隔离策略,重点探讨了线程池隔离的实现,包括信号量和线程池两种方式,并对比了它们的优缺点。同时,文章介绍了 HystrixThreadPoolProperties、HystrixThreadPoolKey 和 HystrixConcurrencyStrategy 等关键组件,以及如何使用这些组件来管理和控制命令执行的线程池。
摘要由CSDN通过智能技术生成

原标题:熔断器 Hystrix 源码解析 —— 命令执行(二)之执行隔离策略

1. 概述

本文主要分享 Hystrix 命令执行(二)之执行隔离策略。

建议 :对 RxJava 已经有一定的了解的基础上阅读本文。

Hystrix 提供两种执行隔离策略( ExecutionIsolationStrategy ) :

SEMAPHORE

:信号量,命令在调用线程执行。在《Hystrix 源码解析 —— 命令执行(一)之正常执行逻辑》「3. TryableSemaphore」 已经详细解析。

THREAD

:线程池,命令在线程池执行。在《Hystrix 源码解析 —— 命令执行(一)之正常执行逻辑》「5. #executeCommandWithSpecifiedIsolation(...)」 的

#executeCommandWithSpecifiedIsolation(...)

方法中,调用

Observable#subscribeOn(Scheduler)

方法,指定在 RxJava Scheduler 执行。

如果你暂时不了解 Scheduler ,可以阅读 《RxJava 源码解析 —— Scheduler》 。

如果你暂时不了解

Observable#subscribeOn(Scheduler)

,可以阅读 《RxJava 源码解析 —— Observable#subscribeOn(Scheduler)》 。

两种方式的优缺点比较,推荐阅读 《【翻译】Hystrix文档-实现原理》「依赖隔离」。

推荐 Spring Cloud 书籍:

请支持正版。下载盗版,等于主动编写低级 BUG 。

程序猿DD —— 《Spring Cloud微服务实战》

周立 —— 《Spring Cloud与Docker微服务架构实战》

两书齐买,京东包邮。

2. HystrixThreadPoolProperties

com.netflix.hystrix.HystrixThreadPoolProperties

,Hystrix 线程池属性配置抽象类,点击 链接 查看,已添加中文注释说明。

com.netflix.hystrix.strategy.properties.HystrixPropertiesThreadPoolDefault

,Hystrix 线程池配置实现类,点击 链接 查看。实际上没什么内容,官方如是说 :

Default implementation of {@link HystrixThreadPoolProperties} using Archaius (https://github.com/Netflix/archaius)

3. HystrixThreadPoolKey

com.netflix.hystrix.HystrixThreadPoolKey

,Hystrix 线程池标识接口。

FROM HystrixThreadPoolKey 接口注释

A key to represent a {@link HystrixThreadPool} for monitoring, metrics publishing, caching and other such uses.

This interface is intended to work natively with Enums so that implementing code can be an enum that implements this interface.

直白的说 ,希望通过相同的

name

( 标识 ) 获得同 HystrixThreadPoolKey 对象。通过在内部维持一个

name

与 HystrixThreadPoolKey 对象的映射,以达到枚举的效果。

HystrixThreadPoolKey 代码如下 :

1: public interface HystrixThreadPoolKey extends HystrixKey {

2: class Factory {

3: private Factory() {

4: }

5:

6: // used to intern instances so we don't keep re-creating them millions of times for the same key

7: private static final InternMap intern

8: = new InternMap(

9: new InternMap.ValueConstructor() {

10: @Override

11: public HystrixThreadPoolKey create(String key) {

12: return new HystrixThreadPoolKeyDefault(key);

13: }

14: });

15:

16: public static HystrixThreadPoolKey asKey(String name) {

17: return intern.interned(name);

18: }

19:

20: private static class HystrixThreadPoolKeyDefault extends HystrixKeyDefault implements HystrixThreadPoolKey {

21: public HystrixThreadPoolKeyDefault(String name) {

22: super(name);

23: }

24: }

25:

26: /* package-private */ static int getThreadPoolCount() {

27: return intern.size();

28: }

29: }

30: }

HystrixThreadPoolKey 实现

com.netflix.hystrix.HystrixKey

接口,点击 链接 查看。该接口定义的

#name()

方法,即是上文我们所说的标识( Key )。

intern

属性,

name

与 HystrixThreadPoolKey 对象的映射,以达到枚举的效果。

com.netflix.hystrix.util.InternMap

,点击 链接 查看带中文注释的代码。

#asKey(name)

方法,从

intern

获得 HystrixThreadPoolKey 对象。

#getThreadPoolCount()

方法,获得 HystrixThreadPoolKey 数量。

在 AbstractCommand 构造方法里,初始化命令的threadPoolKey属性,代码如下 :

protected final HystrixThreadPoolKey threadPoolKey;

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);

// 初始化 threadPoolKey

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

}

调用

#initThreadPoolKey(...)

方法,创建最终的

threadPoolKey

属性。代码如下 :

优先级 :

threadPoolKeyOverride

>

threadPoolKey

>

groupKey

privatestaticHystrixThreadPoolKeyinitThreadPoolKey(HystrixThreadPoolKeythreadPoolKey,HystrixCommandGroupKeygroupKey,StringthreadPoolKeyOverride){

if(threadPoolKeyOverride==null){

// we don't have a property ov

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值