Hystrix(2)— 相關(guān)配置

SpringBoot整合Hystrix(無SpringCloud版)

1. SpringBoot如何整合Hystrix

1.1 導(dǎo)入maven依賴

        <!--hystrix官網(wǎng)-->
        <dependency>
            <groupId>com.netflix.hystrix</groupId>
            <artifactId>hystrix-core</artifactId>
            <version>1.5.18</version>
        </dependency>

        <dependency>
            <groupId>com.netflix.hystrix</groupId>
            <artifactId>hystrix-metrics-event-stream</artifactId>
            <version>1.5.18</version>
        </dependency>

        <dependency>
            <groupId>com.netflix.hystrix</groupId>
            <artifactId>hystrix-javanica</artifactId>
            <version>1.5.18</version>
        </dependency>

1.2 配置文件

在application.properties配置文件中加入如下配置:

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=3000

將全局的默認超時時間由1s修改為3s廊镜,但是配置未起作用。

配置未生效的原因:

Archaius 默認支持兩種方式來加載本地的配置文件:

  1. 默認情況下唉俗,Archaius 默認會加載classpath的config.properties文件期升。
  2. 在程序啟動后,加入如下參數(shù)
    -Darchaius.configurationSource.additionalUrls=file:///apps/myapp/application.properties互躬。

1.3 如何使用

若單純使用SpringBoot整合Hystrix播赁,若想配置文件或者注解可以生效,必須將切面聲明到Spring容器中吼渡。

    @Bean
    public HystrixCommandAspect hystrixCommandAspect(){
        return new HystrixCommandAspect();
    }

    @Bean
    public HystrixCacheAspect hystrixCacheAspect(){
        return  new HystrixCacheAspect();
    }

使用注解的方式容为,為方法添加hystrix斷路器。

    @HystrixCommand(
            fallbackMethod = "fallConfigOfRemote"
            ,commandProperties = {
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")
            }
            )
    public String getConfigOfRemote(String id) {
        String url = "http://localhost:8001/getInfo";
        String doPost = "S";
        try {
            //調(diào)用遠程服務(wù)寺酪,url坎背,參數(shù),socket超時時間
            doPost = HttpClientUtils.doPost(url, id, 10000);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return doPost;
    }

    private String fallConfigOfRemote(String id) {
        log.info("備選方案...");
        return "F";
    }

在resources\config.properties中進行配置:

#配置默認的超時時間
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=1000
#配置特定commandKey的超時時間(優(yōu)先級最高)
hystrix.command.getConfigOfRemote.execution.isolation.thread.timeoutInMilliseconds=4000

使用Hystrix后寄雀,配置均會在com.netflix.hystrix.AbstractCommand#AbstractCommand類中組裝得滤。

配置生效.png

注:需要注意的是,雖然在3個地方設(shè)置了 超時時間盒犹。但是只有4000ms的超時時間生效懂更。

引申出兩個問題眨业?

1. hystrix配置的優(yōu)先級是怎么樣的呢?
2. 如何為某個方法配置不同的hystrix策略沮协?

2. Hystrix配置

2.1 Hystrix配置的優(yōu)先級

優(yōu)先級從低到高的配置:

  1. 內(nèi)置全局屬性默認值:寫死在代碼中的值荠雕;
  2. 動態(tài)全局默認屬性:通過配置文件配置全局的值趁矾;
  3. 內(nèi)置實例默認值:寫死在代碼中的實例的值艳狐;
  4. 動態(tài)配置實例屬性:通過配置文件配置特定實例的值芹关;

注:全局配置是default的配置,而實例配置為commandKey配置行瑞。

2.2 CommandKey和CommandGroup

如何監(jiān)控或者個性化的配置Hystrix參數(shù)奸腺?

默認情況下,Hystrix會使用類名作為CommandGroup血久,會使用方法名作為CommandKey突照。可以使用commandKey進行個性化的配置。參考【1.3 如何使用】

配置.png

2.3 詳細配置

2.3.1 Execution

1. 設(shè)置Hystrix的隔離策略

配置 默認值
execution.isolation.strategy THREAD

表示HystrixCommand.run()的執(zhí)行時的策略洋魂,有以下兩種策略(色魔佛)

配置 屬性
THREAD(默認值) 在單獨的線程上執(zhí)行,并發(fā)請求受線程池中的線程數(shù)限制
SEMAPHORE 在調(diào)用線程上執(zhí)行喜鼓,并發(fā)請求量受信號量計數(shù)限制
// 設(shè)置所有實例的默認值
hystrix.command.default.execution.isolation.strategy=…
// 設(shè)置實例HystrixCommandKey的此屬性值
hystrix.command.HystrixCommandKey.execution.isolation.strategy=…

2. 設(shè)置調(diào)用者執(zhí)行的超時時間

配置 默認值
execution.isolation.thread.timeoutInMilliseconds 1000ms
// 設(shè)置所有實例的默認值
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=…
// 設(shè)置實例HystrixCommandKey的此屬性值
hystrix.command.HystrixCommandKey.execution.isolation.thread.timeoutInMilliseconds=…

3. 設(shè)置是否開啟超時設(shè)置

配置 默認值
execution.timeout.enabled true
// 設(shè)置所有實例的默認值
hystrix.command.default.execution.timeout.enabled=…
// 設(shè)置實例HystrixCommandKey的此屬性值
hystrix.command.HystrixCommandKey.execution.timeout.enabled=…

4. 設(shè)置是否超時時副砍,中斷HystrixCommand.run()的執(zhí)行

配置 默認值
execution.isolation.thread.interruptOnTimeout true
// 設(shè)置所有實例的默認值
hystrix.command.default.execution.isolation.thread.interruptOnTimeout=…
// 設(shè)置實例HystrixCommandKey的此屬性值
hystrix.command.HystrixCommandKey.execution.isolation.thread.interruptOnTimeout=…

5. 設(shè)置是否在取消任務(wù)執(zhí)行時,中斷HystrixCommand.run()的執(zhí)行

配置 默認值
execution.isolation.thread.interruptOnCancel false
// 設(shè)置所有實例的默認值
hystrix.command.default.execution.isolation.thread.interruptOnCancel=…
// 設(shè)置實例HystrixCommandKey的此屬性值
hystrix.command.HystrixCommandKey.execution.isolation.thread.interruptOnCancel

6. 使用SEMAPHORE策略時庄岖,設(shè)置最大的并發(fā)量

配置 默認值
execution.isolation.semaphore.maxConcurrentRequests 10
// 設(shè)置所有實例的默認值
hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests=…
// 設(shè)置實例HystrixCommandKey的此屬性值
hystrix.command.HystrixCommandKey.execution.isolation.semaphore.maxConcurrentRequests=…

2.3.2 Fallback

以下屬性控制HystrixCommand.getFallback()如何執(zhí)行豁翎,這些屬性對隔離策略THREADSEMAPHORE都起作用。

1. 設(shè)置從調(diào)用線程運行getFallback()方法允許的最大并發(fā)請求數(shù)

此屬性設(shè)置從調(diào)用線程允許Hystrix.getFallback()方法允許的最大并發(fā)請求數(shù)隅忿,如果達到最大的并發(fā)量心剥,則接下來的請求都會被拒絕并且拋出異常。

配置 默認值
fallback.isolation.semaphore.maxConcurrentRequests 10
// 設(shè)置所有實例的默認值
hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests
// 設(shè)置實例HystrixCommandKey的此屬性值
hystrix.command.HystrixCommandKey.fallback.isolation.semaphore.maxConcurrentRequests

2. 是否開啟fallback功能

配置 默認值
hystrix.command.default.fallback.enabled true
// 設(shè)置所有實例的默認值
hystrix.command.default.fallback.enabled=…
// 設(shè)置實例HystrixCommandKey的此屬性值
hystrix.command.HystrixCommandKey.fallback.enabled=…

2.3.3 斷路器

控制斷路器的行為背桐。

1. 是否開啟斷路器功能

配置 默認值
circuitBreaker.enabled true
// 設(shè)置所有實例的默認值
hystrix.command.default.circuitBreaker.enabled=…
// 設(shè)置實例HystrixCommandKey的此屬性值
hystrix.command.HystrixCommandKey.circuitBreaker.enabled=…

2. 該屬性設(shè)置滾動窗口中使斷路器跳閘的最小請求數(shù)量

默認值20优烧。若是在10s(窗口時間)內(nèi),只收到19個請求且都失敗了链峭,則斷路器也不會開啟畦娄。

// 設(shè)置所有實例的默認值
hystrix.command.default.circuitBreaker.requestVolumeThreshold=…
// 設(shè)置實例HystrixCommandKey的此屬性值
hystrix.command.HystrixCommandKey.circuitBreaker.requestVolumeThreshold=…

3. 設(shè)置斷路時間

斷路器跳閘后,在此值的時間內(nèi)弊仪,hystrix會拒絕新的請求熙卡,只有過了這個時間,斷路器才會打開閘門励饵。默認值5000ms

// 設(shè)置所有實例的默認值
hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds=…
// 設(shè)置實例HystrixCommandKey的此屬性值
hystrix.command.HystrixCommandKey.circuitBreaker.sleepWindowInMilliseconds=…

4. 設(shè)置跳閘百分比

設(shè)置失敗百分比的閾值驳癌,如果失敗比率超過這個值,則斷路器跳閘并且進入fallback狀態(tài)役听。默認值:50

// 設(shè)置所有實例的默認值
hystrix.command.default.circuitBreaker=…
// 設(shè)置實例HystrixCommandKey的此屬性值
hystrix.command.HystrixCommandKey.circuitBreaker.errorThresholdPercentage=…

5. 斷路器強制開啟

如果這個屬性true強制斷路器進入開路(跳閘)狀態(tài)颓鲜,它將拒絕所有請求表窘。
此屬性優(yōu)先于circuitBreaker.forceClosed。默認值false

// 設(shè)置所有實例的默認值
hystrix.command.default.circuitBreaker.forceOpen=…
// 設(shè)置實例HystrixCommandKey的此屬性值
hystrix.command.HystrixCommandKey.circuitBreaker.forceOpen=…

6. 斷路器強制關(guān)閉

如果設(shè)置true灾杰,則強制使斷路器進行關(guān)閉狀態(tài)蚊丐,此時會允許執(zhí)行所有請求,無論是否失敗的次數(shù)達到circuitBreaker.errorThresholdPercentage值艳吠。默認值:false麦备。

// 設(shè)置所有實例的默認值
hystrix.command.default.circuitBreaker.forceClosed=…
// 設(shè)置實例HystrixCommandKey的此屬性值
hystrix.command.HystrixCommandKey.circuitBreaker.forceClosed=…

2.3.4 Metrics (度量)

[麥錘死]捕獲和HystrixCommand以及HystrixObservableCommand執(zhí)行信息相關(guān)的配置屬性。

1. 設(shè)置統(tǒng)計窗口的持續(xù)時間(ms)

Hystrix保留斷路器使用和發(fā)布指標的時間昭娩。默認值:10000

// 設(shè)置所有實例的默認值
hystrix.command.default.metrics.rollingStats.timeInMilliseconds=10000
// 設(shè)置實例HystrixCommandKey的此屬性值
hystrix.command.HystrixCommandKey.metrics.rollingStats.timeInMilliseconds=10000

2. 設(shè)置滾動桶的數(shù)量

metrics.rollingStats.timeInMilliseconds % metrics.rollingStats.numBuckets == 01
如:10000/10凛篙、10000/20是正確的配置,但是10000/7錯誤的。默認值:10
注:在高并發(fā)的環(huán)境里栏渺,每個桶的時間長度建議大于100ms呛梆。

// 設(shè)置所有實例的默認值
hystrix.command.default.metrics.rollingStats.numBuckets=…
// 設(shè)置實例HystrixCommandKey的此屬性值
hystrix.command.HystrixCommandKey.metrics.rollingStats.numBuckets=…

2.3.5 Request Context

該屬性控制HystrixCommand使用到的Hystrix的上下文。

1. 是否開啟請求緩存功能

默認值:true

// 設(shè)置所有實例的默認值
hystrix.command.default.requestCache.enabled=…
// 設(shè)置實例HystrixCommandKey的此屬性值
hystrix.command.HystrixCommandKey.requestCache.enabled=…

2. 表示是否開啟日志

表示是否開啟日志磕诊,打印執(zhí)行HystrixCommand的情況和事件填物。默認值true

// 設(shè)置所有實例的默認值
hystrix.command.default.requestLog.enabled=…
// 設(shè)置實例HystrixCommandKey的此屬性值
hystrix.command.HystrixCommandKey.requestLog.enabled=…

2.3.6 Collapser Properties

設(shè)置請求合并請求。

1. 設(shè)置同時批量執(zhí)行的請求的最大數(shù)量

默認值:Integer.MAX_VALUE

// 設(shè)置所有實例的默認值
hystrix.collapser.default.maxRequestsInBatch=…
// 設(shè)置實例HystrixCommandKey的此屬性值
hystrix.collapser.HystrixCollapserKey.maxRequestsInBatch=…

2. 批量執(zhí)行創(chuàng)建多久之后霎终,再觸發(fā)真正的請求

默認值:10

// 設(shè)置所有實例的默認值
hystrix.collapser.default.timerDelayInMilliseconds=…
// 設(shè)置實例HystrixCommandKey的此屬性值
hystrix.collapser.HystrixCollapserKey.timerDelayInMilliseconds=…

3. 請求緩存

是否對HystrixCollapser.execute() 和 HystrixCollapser.queue()開啟請求緩存
默認值:true

// 設(shè)置所有實例的默認值
hystrix.collapser.default.requestCache.enabled=…
// 設(shè)置實例HystrixCommandKey的此屬性值
hystrix.collapser.HystrixCollapserKey.requestCache.enabled=…

2.3.7 ThreadPool配置

1. 核心線程數(shù)量

默認值:10

// 設(shè)置所有實例的默認值
hystrix.threadpool.default.coreSize=…
// 設(shè)置實例HystrixCommandKey的此屬性值
hystrix.threadpool.HystrixThreadPoolKey.coreSize=…

2. 最大線程數(shù)量

在1.5.9中添加滞磺。此屬性設(shè)置最大線程池大小。這是在不開始拒絕HystrixCommands的情況下可以支持的最大并發(fā)數(shù)量莱褒。請注意击困,此設(shè)置僅在您設(shè)置時生效allowMaximumSizeToDivergeFromCoreSize。在1.5.9之前广凸,核心和最大尺寸始終相等阅茶。
默認值:10

// 設(shè)置所有實例的默認值
hystrix.threadpool.default.maximumSize=…
// 設(shè)置實例HystrixCommandKey的此屬性值
hystrix.threadpool.HystrixThreadPoolKey.maximumSize=…

3. 隊列大小

設(shè)置最大的BlockingQueue隊列的值。如果設(shè)置-1谅海,則使用SynchronousQueue隊列(無限隊列)脸哀。如果設(shè)置正數(shù),則使用LinkedBlockingQueue隊列扭吁。默認值:-1

// 設(shè)置所有實例的默認值
hystrix.threadpool.default.maxQueueSize=…
// 設(shè)置實例HystrixCommandKey的此屬性值
hystrix.threadpool.HystrixThreadPoolKey.maxQueueSize=…

4. 拒絕閾值(動態(tài)設(shè)置隊列大衅蟛洹)

因為maxQueueSize值不能被動態(tài)修改,所有通過設(shè)置此值可以實現(xiàn)動態(tài)修改等待隊列長度智末。即等待的隊列的數(shù)量大于queueSizeRejectionThreshold時(但是沒有達到maxQueueSize值)谅摄,則開始拒絕后續(xù)的請求進入隊列。
默認值:5
注:如果設(shè)置-1系馆,則屬性不啟作用送漠。

// 設(shè)置所有實例的默認值
hystrix.threadpool.default.queueSizeRejectionThreshold=…
// 設(shè)置實例HystrixCommandKey的此屬性值
hystrix.threadpool.HystrixThreadPoolKey.queueSizeRejectionThreshold=…

5. 設(shè)置空閑時間

設(shè)置線程空閑多長時間后,釋放(maximumSize-coreSize )個線程由蘑。默認值1(分鐘)

// 設(shè)置所有實例的默認值
hystrix.threadpool.default.keepAliveTimeMinutes=…
// 設(shè)置實例HystrixCommandKey的此屬性值
hystrix.threadpool.HystrixThreadPoolKey.keepAliveTimeMinutes=…

6. 是否允許設(shè)置最大的線程數(shù)量

設(shè)置allowMaximumSizeToDivergeFromCoreSize值為true時闽寡,maximumSize才有作用
默認值:false

// 設(shè)置所有實例的默認值
hystrix.threadpool.default.allowMaximumSizeToDivergeFromCoreSize=…
// 設(shè)置實例HystrixCommandKey的此屬性值
hystrix.threadpool.HystrixThreadPoolKey.allowMaximumSizeToDivergeFromCoreSize=…

推薦閱讀

官網(wǎng)——Hystrix的參數(shù)配置
Hystrix常用功能介紹
Hystrix源碼解析--從原生的lib開始使用hystrix(一)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末代兵,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子爷狈,更是在濱河造成了極大的恐慌植影,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,695評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件涎永,死亡現(xiàn)場離奇詭異思币,居然都是意外死亡,警方通過查閱死者的電腦和手機羡微,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評論 3 399
  • 文/潘曉璐 我一進店門谷饿,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人妈倔,你說我怎么就攤上這事博投。” “怎么了盯蝴?”我有些...
    開封第一講書人閱讀 168,130評論 0 360
  • 文/不壞的土叔 我叫張陵毅哗,是天一觀的道長。 經(jīng)常有香客問我捧挺,道長虑绵,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,648評論 1 297
  • 正文 為了忘掉前任松忍,我火速辦了婚禮蒸殿,結(jié)果婚禮上筷厘,老公的妹妹穿的比我還像新娘鸣峭。我一直安慰自己,他們只是感情好酥艳,可當(dāng)我...
    茶點故事閱讀 68,655評論 6 397
  • 文/花漫 我一把揭開白布摊溶。 她就那樣靜靜地躺著,像睡著了一般充石。 火紅的嫁衣襯著肌膚如雪莫换。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,268評論 1 309
  • 那天骤铃,我揣著相機與錄音拉岁,去河邊找鬼。 笑死惰爬,一個胖子當(dāng)著我的面吹牛喊暖,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播撕瞧,決...
    沈念sama閱讀 40,835評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼陵叽,長吁一口氣:“原來是場噩夢啊……” “哼狞尔!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起巩掺,我...
    開封第一講書人閱讀 39,740評論 0 276
  • 序言:老撾萬榮一對情侶失蹤偏序,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后胖替,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體研儒,經(jīng)...
    沈念sama閱讀 46,286評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,375評論 3 340
  • 正文 我和宋清朗相戀三年刊殉,在試婚紗的時候發(fā)現(xiàn)自己被綠了殉摔。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,505評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡记焊,死狀恐怖逸月,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情遍膜,我是刑警寧澤碗硬,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站瓢颅,受9級特大地震影響恩尾,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜挽懦,卻給世界環(huán)境...
    茶點故事閱讀 41,873評論 3 333
  • 文/蒙蒙 一翰意、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧信柿,春花似錦冀偶、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至形病,卻和暖如春客年,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背漠吻。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評論 1 272
  • 我被黑心中介騙來泰國打工量瓜, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人途乃。 一個月前我還...
    沈念sama閱讀 48,921評論 3 376
  • 正文 我出身青樓绍傲,卻偏偏與公主長得像,于是被迫代替她去往敵國和親欺劳。 傳聞我的和親對象是個殘疾皇子唧取,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,515評論 2 359