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 默認支持兩種方式來加載本地的配置文件:
- 默認情況下唉俗,Archaius 默認會加載classpath的config.properties文件期升。
- 在程序啟動后,加入如下參數(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
類中組裝得滤。
注:需要注意的是,雖然在3個地方設(shè)置了 超時時間盒犹。但是只有4000ms的超時時間生效懂更。
引申出兩個問題眨业?
1. hystrix配置的優(yōu)先級是怎么樣的呢?
2. 如何為某個方法配置不同的hystrix策略沮协?
2. Hystrix配置
2.1 Hystrix配置的優(yōu)先級
優(yōu)先級從低到高的配置:
- 內(nèi)置全局屬性默認值:寫死在代碼中的值荠雕;
- 動態(tài)全局默認屬性:通過配置文件配置全局的值趁矾;
- 內(nèi)置實例默認值:寫死在代碼中的實例的值艳狐;
- 動態(tài)配置實例屬性:通過配置文件配置特定實例的值芹关;
注:全局配置是default的配置,而實例配置為commandKey配置行瑞。
2.2 CommandKey和CommandGroup
如何監(jiān)控或者個性化的配置Hystrix參數(shù)奸腺?
默認情況下,Hystrix會使用類名作為CommandGroup血久,會使用方法名作為CommandKey突照。可以使用commandKey進行個性化的配置。參考【1.3 如何使用】
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í)行豁翎,這些屬性對隔離策略THREAD
和SEMAPHORE
都起作用。
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(一)