Hystrix實現斷路器
Hystrix實現斷路器
核心依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <!-- hystrix-javanica 在starter-hystrix中預設會包含一個,可以不引入 --> <dependency> <groupId>com.netflix.hystrix</groupId> <artifactId>hystrix-javanica</artifactId> <version>1.5.9</version> </dependency>
使用
-
在服務的啟動類中添加註解
@SpringBootApplication @EnableCircuitBreaker public class Application { ...... }
如果不使用註解@EnableCircuitBreaker,斷路器不會啟用.
-
在需要呼叫服務的地方添加註解
@HystrixCommand public List<Object> getSomeInfo(String infoId) { ....... }
只要添加了註解@HystrixCommand,在呼叫方法的時候,如果呼叫超時,斷路器將會中斷對此方法的呼叫.
HystrixCommand註解的引數
@Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented public @interface HystrixCommand { String groupKey() default ""; String commandKey() default ""; String threadPoolKey() default ""; String fallbackMethod() default ""; HystrixProperty[] commandProperties() default {}; HystrixProperty[] threadPoolProperties() default {}; Class<? extends Throwable>[] ignoreExceptions() default {}; ObservableExecutionMode observableExecutionMode() default ObservableExecutionMode.EAGER; HystrixException[] raiseHystrixExceptions() default {}; }
-
groupKey
一組 Hystrix 命令的集合, 用來統計、報告,預設取類名.
-
commandKey
HystrixCommand 命令的key值,預設值為註解方法的名稱*
-
fallbackMethod
如果遠端呼叫失敗,會呼叫此方法.
回撥方法必須和@HystrixCommand在同一個類中,並且此屬性值與呼叫類有相同的方法簽名.
如果值不存在,會丟擲異常.
-
threadPoolKey
讓@HystrixCommand擁有一個唯一的名稱,並且建立一個獨立於預設執行緒池的執行緒池.
如果沒有定義任何值,則使用預設執行緒.
-
threadPoolProperties
用於配置執行緒池的行為
-
coreSize
設定執行緒池的大小.
預設10.
-
maxQueueSize
設定執行緒池最大佇列大小.
如果值為-1,不適用佇列.Hystrix會阻塞請求,直到有一個執行緒可用來處理.
預設-1.
-
circuitBreaker.requestVolumeThreshold
設定Hystrix開始檢查斷路器是否跳閘前,必須處理的最小請求數.
只能夠在commandPoolProperties屬性中設定.
預設20.
-
circuitBreaker.errorThresholdPercentage
斷路器跳閘前,必須達到的故障率百分比.
只能夠在commandPoolProperties中設定.
預設50.
-
circuitBreaker.sleepWindowInMilliseconds
斷路器跳閘後,Hystrix嘗試進行服務呼叫之前需要等待的時間.時間單位:毫秒.
只能夠在commandPoolProperties中設定.
預設5000.
-
metricsRollingStats.timeInMilliseconds
Hystrix收集和監控服務呼叫的統計資訊滾動視窗.時間單位:毫秒.
預設值1000.
-
metricsRollingStats.numBuckets
Hystrix在監控視窗中維護的度量桶的數量.
監視視窗內桶數量越多,Hystrix在視窗內監控故障的時間越低.
”metrics.rollingStats.timeInMilliseconds % metrics.rollingStats.numBuckets == 0"必須為true,否則會丟擲異常。
預設10.
-
execution.isolation.thread.timeoutInMilliseconds
指定Hystrix在超時呼叫之前等待的時間.
關於所有的HystrixProperty,可以在相關jar包的以下路徑中檢視:
hystrix-javanica-1.5.9.jar!\com\netflix\hystrix\contrib\javanica\conf\HystrixPropertiesManager.class
後備模式
根據上面的註解,後備模式只需要在註解中聲明後備方法,並且在當前類中定義好後備方法即可.
艙壁模式
為什麼使用艙壁模式
如果在使用多個微服務協同完成多個任務,這些服務呼叫都使用同一批執行緒.
但是這個執行緒時為了處理整個Java容器而預留的.
所以如果存在大量請求的情況下,如果某些伺服器出現了問題,會導致執行緒池滿並且其它請求會等待處理,阻塞新請求.
使用艙壁模式能夠讓遠端資源呼叫被隔離到一個新的執行緒池中.如果一個服務.調用出現了鼓掌,也不會影響其它的服務呼叫.
如何配置
- 配置獨立的執行緒池
- 設定執行緒池中的執行緒數量
- 設定執行緒繁忙時的佇列大小
舉例:
@HystrixCommand(
fallbackMethod = "fallbackMethod"
threadPoolKey = "newThreadPool"
threadPollProperties = {
@HystrixProperty(name = "coreSize", value = "15")
@HystrixProperty(name = "maxQueueSize", value = "12")
}
)
public Object getValue()
{
......
}
對於執行緒池大小的設定,Netflix給出公式:
(requests per second at peak when the service is healthy * 99th percentile latency in
seconds) + small amount of extra threads for overhead
每秒最大支撐的請求數 × 99%的請求的平均響應時間 + 快取值
maxQueueSize只能夠線上程池首次初始化時設定.
但是也可以使用queueSizeRejectionThreshold屬性動態調整佇列大小.
不過只有首次初始化的 maxQueueSize >0 的時候有效.