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.pswp.cn/news/393914.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/393914.shtml
英文地址,請注明出處:http://en.pswp.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; …