hystrix 源码 线程池隔离_Hystrix源码学习--线程池隔离

分析你的系统

你所认识的分布式系统,哪些是可以进行垂直拆分的?拆分之后系统之间的依赖如何梳理?系统异构之后的稳定性调用如何保证?这些都是可能在分布式场景中面临的问题。

说个比较常见的问题,大家都知道秒杀系统,秒杀流程在分布式的环境中,就需要依赖:订单管理,会员管理,支付等,假如在整个系统中会员系统不稳定,导致系统请求挤压,慢慢的就会导致对其他外部系统的依赖也不可用,所以就要使用业务的隔离,这样会避免服务的局部不可用导致的全部不可用。

Hystrix 源码分析

找到HystrixCommand的父类AbstractCommand, 里面有个构造方法,从构造方法可以看出里这里定义了 threadPool对象。代码如下,关键代码都有做相应的注释。

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对象,用于组织一类业务相关的对象

this.commandGroup = initGroupKey(group);

// commandKey默认是以类为为名称的

this.commandKey = initCommandKey(key, getClass());

this.properties = initCommandProperties(this.commandKey, propertiesStrategy, commandPropertiesDefaults);

//这个方法里定义了TheradPool里的关键字,默认以传入的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);

//这里就是线程池对象啦。

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;

}

/**

这个方法用于得到HystrixThreadPoolKey 对象, Hystrix内部有大量的Key对象,可以简单理解这些 Key都是相应对象的唯一标识。从代码里可以看出,默认情况下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);

}

}

/**

在这里将调用具体的构造线程池的方法。

**/

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;

}

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/393914.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

P2341 [HAOI2006]受欢迎的牛 强连通

题目背景 本题测试数据已修复。 题目描述 每头奶牛都梦想成为牛棚里的明星。被所有奶牛喜欢的奶牛就是一头明星奶牛。所有奶 牛都是自恋狂,每头奶牛总是喜欢自己的。奶牛之间的“喜欢”是可以传递的——如果A喜 欢B,B喜欢C,那么A也喜欢C。牛栏…

oracle em agent,ORACLE 11G EM 配置命令及问题处理

11g装好以后,一直未用EM,昨天晚上和今天晚上终于抽时间把EM启动起来了,还遇到一点小问题,1.EM配置的一些命令创建一个EM资料库emca -repos create重建一个EM资料库emca -reposrecreate--------这个很主要,一般第一次不成功创建的时…

leetcode89. 格雷编码

格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个位数的差异。 给定一个代表编码总位数的非负整数 n,打印其格雷编码序列。即使有多个不同答案,你也只需要返回其中一种。 格雷编码序列必须以 0 开头。 示例 1:…

注重代码效率_如何提升质量:注重态度

注重代码效率by Harshdeep S Jawanda通过Harshdeep S Jawanda 如何提升质量:注重态度 (How to skyrocket quality: focus on attitude) When it comes to discussing quality and how we can improve, the most common things that come to peoples minds are test…

spark mllib推荐算法使用

2019独角兽企业重金招聘Python工程师标准>>> 一、pom.xml <!-- 机器学习包 --><dependency><groupId>org.apache.spark</groupId><artifactId>spark-mllib_2.10</artifactId><version>${spark.version}</version>&…

Android仿QQ复制昵称效果2

本文同步自http://javaexception.com/archives/77 背景: 在上一篇文章中&#xff0c;给出了一种复制QQ效果的方案&#xff0c;今天就来讲讲换一种方式实现。主要依赖的是一个开源项目https://github.com/shangmingchao/PopupList。 解决办法: PopupList.java的代码封装的比较完…

R语言的自定义函数—字符组合

前两天写了几个函数&#xff0c;对里面收获到的一些东西做一些记录。 函数str_comb&#xff0c;用于输入一个字符串或数值向量&#xff0c;返回由向量中元素组成的不重复的长度小于向量长度的所有组合&#xff0c;结果用矩阵形式输出。 函数使用结果如下&#xff1a; 思路很简单…

oracle group by 两项,Oracle中group by 的扩展函数rollup、cube、grouping sets

Oracle的group by除了基本使用方法以外&#xff0c;还有3种扩展使用方法&#xff0c;各自是rollup、cube、grouping sets。分别介绍例如以下&#xff1a;1、rollup对数据库表emp。如果当中两个字段名为a&#xff0c;b,c。假设使用group by rollup(a,b)&#xff0c;首先会对(a,b…

leetcode1079. 活字印刷(回溯)

你有一套活字字模 tiles&#xff0c;其中每个字模上都刻有一个字母 tiles[i]。返回你可以印出的非空字母序列的数目。 注意&#xff1a;本题中&#xff0c;每个活字字模只能使用一次。 示例 1&#xff1a; 输入&#xff1a;“AAB” 输出&#xff1a;8 解释&#xff1a;可能的…

什么从什么写短句_从什么到从什么造句

从什么到从什么造句从什么到从什么怎么来造句呢?以下是小编为大家收集整理的从什么到从什么造句&#xff0c;希望对你有所帮助&#xff01;从什么到从什么造句&#xff1a;从闻到花香到看到花朵,从看到花朵到触摸到花瓣,真是一种美妙的感觉.从今天到明天&#xff0c;从明天到后…

如何开发一个hexo主题_如何确定一个强烈的主题可以使产品开发更有效

如何开发一个hexo主题by Cameron Jenkinson卡梅伦詹金森(Cameron Jenkinson) 如何确定一个强烈的主题可以使产品开发更有效 (How identifying a strong theme can make product development more effective) MVPs always seem easy to execute and build. The first version i…

机器学习基石13-Hazard of Overfitting

注&#xff1a; 文章中所有的图片均来自台湾大学林轩田《机器学习基石》课程。 笔记原作者&#xff1a;红色石头 微信公众号&#xff1a;AI有道 上节课主要介绍了非线性分类模型&#xff0c;通过非线性变换&#xff0c;将非线性模型映射到另一个空间&#xff0c;转换为线性模型…

容器为何物,为什么它对OpenStack很重要?

本文讲的是容器为何物&#xff0c;为什么它对OpenStack很重要&#xff0c;【编者的话】本文主要介绍了容器的发展、容器技术、容器类型、Docker、Open Container Initiative、微服务以及OpenStack中容器的应用。 容器现在正经历着一次重生&#xff0c;部分原因是由于云计算的发…

oracle执行计划的rows不对,Oracle执行计划——all_rows和first_rows(n)优化器模式

Oracle执行计划——all_rows和first_rows(n)优化器模式0. 环境创建[sql]SQL> create usertest identified by test2 default tablespace users3 temporary tablespace temp4 quota unlimited on users;User created.SQL> grant createsession, resource, alter session t…

从 MVC 到前后端分离

转载自&#xff1a;https://my.oschina.net/huangyong/blog/521891 从MVC到前后端分离 1.理解 MVC MVC是一种经典的设计模式&#xff0c;全名为Model-View-Controller&#xff0c;即模型-视图-控制器。其中&#xff0c;模型是用于封装数据的载体&#xff0c;例如&#xff0c;在…

leetcode93. 复原IP地址(回溯)

给定一个只包含数字的字符串&#xff0c;复原它并返回所有可能的 IP 地址格式。 有效的 IP 地址正好由四个整数&#xff08;每个整数位于 0 到 255 之间组成&#xff09;&#xff0c;整数之间用 ‘.’ 分隔。 示例: 输入: “25525511135” 输出: [“255.255.11.135”, “255…

vj节点_创意编码—如何在JavaScript中创建VJ引擎

vj节点by George Gally通过乔治加利 创意编码—如何在JavaScript中创建VJ引擎 (Creative Coding — How to create a VJ engine in JavaScript) 了解如何将JavaScript动态注入网页 (Learn how to dynamically inject JavaScript into webpages) For years I’ve been using th…

上传下载

# 默写 TCP UDP 文件夹中的代码# 完成一个上传和下载文件的小程序 # server端 :根据客户端需求自定义 # client端 # 客户端启动之后 # 选择 上传操作 还是 下载操作 # 如果是上传操作 : 输入要上传的文件路径 # 基础需求 :直接将文件上传到默认目录 # 进阶需求 :将…

qt 串口 环形缓存_qt linux串口 缓冲区多大

满意答案Zc的爱丶很美2016.09.11采纳率&#xff1a;51% 等级&#xff1a;9已帮助&#xff1a;515人一、程序设计的基础&#xff0c;例如&#xff1a;基本的编程语言基础&#xff0c;至少对数据类型、程序的结构及流程控制等最基本的内容要相当清楚&#xff01;另外有不少同学…

在.NET中使用SMTP发送邮件

这是一篇转载&#xff0c;可能对大家很有用啊&#xff0c;放首页看看是否有参考价值。本文提到的方案仍然不能算是完全解决所有问题&#xff0c;最佳的dotNET下通过SMTP&#xff08;带验证&#xff09;发送邮件的机制是什么&#xff0c;不知道大家有什么好的看法&#xff01; …