微服務組件--限流框架Spring Cloud Hystrix分析

Hystrix的介紹【1】Hystrix是springCloud的組件之一 , Hystrix 可以讓我們在分布式系統中對服務間的調用進行控制加入一些調用延遲或者依賴故障的容錯機制 。
【2】Hystrix 通過將依賴服務進行資源隔離進而阻止某個依賴服務出現故障時在整個系統所有的依賴服務調用中進行蔓延;【防止服務雪崩】
【3】其核心功能:
1)服務隔離(服務限流)
通過線程池或者信號量判斷是否已滿,超出容量的請求直接降級,以達到限流的作用 。
2)服務熔斷
當失敗率達到閾值自動觸發降級,熔斷器觸發的快速失敗會有助于系統防止崩潰 ?!究梢哉f熔斷是特定條件的降級】
3)服務降級
服務降級是當服務器壓力劇增的情況下,根據當前業務情況及流量對一些服務和頁面有策略的降級,以此釋放服務器資源以保證核心任務的正常運行 。
Hystrix的簡單使用【1】引入依賴
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency>【2】啟動類開啟hystrix功能
@SpringBootApplication//注冊到eureka@EnableEurekaClient//開啟斷路器功能@EnableCircuitBreakerpublic class WebApplication {【微服務組件--限流框架Spring Cloud Hystrix分析】【3】注解@HystrixCommand參數分析
@Target({ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)@Inherited@Documentedpublic @interface HystrixCommand {// HystrixCommand 命令所屬的組的名稱:默認注解方法類的名稱String groupKey() default "";// HystrixCommand 命令的key值 , 默認值為注解方法的名稱String commandKey() default "";// 線程池名稱,默認定義為groupKeyString threadPoolKey() default "";// 定義回退方法的名稱, 此方法必須和hystrix的執行方法在相同類中String fallbackMethod() default "";// 配置hystrix命令的參數HystrixProperty[] commandProperties() default {};// 配置hystrix依賴的線程池的參數HystrixProperty[] threadPoolProperties() default {};// 如果hystrix方法拋出的異常包括RUNTIME_EXCEPTION,則會被封裝HystrixRuntimeException異常 。我們也可以通過此方法定義哪些需要忽略的異常Class<? extends Throwable>[] ignoreExceptions() default {};// 定義執行hystrix observable的命令的模式,類型詳細見ObservableExecutionModeObservableExecutionMode observableExecutionMode() default ObservableExecutionMode.EAGER;// 如果hystrix方法拋出的異常包括RUNTIME_EXCEPTION , 則會被封裝HystrixRuntimeException異常 。此方法定義需要拋出的異常HystrixException[] raiseHystrixExceptions() default {};// 定義回調方法:但是defaultFallback不能傳入參數,返回參數和hystrix的命令兼容String defaultFallback() default "";}【4】使用示例
//線程池隔離的設置,線程池隔離與信號量隔離的最大區別在于發送請求的線程,信號量是采用調用方法的線程 , 而線程池則是用池內的線程去發送請求@HystrixCommand(groupKey="test-provider",threadPoolKey="test-provider",threadPoolProperties = {@HystrixProperty(name = "coreSize", value = "https://www.huyubaike.com/biancheng/20"),//線程池大小@HystrixProperty(name = "maximumSize", value = "https://www.huyubaike.com/biancheng/30"),//最大線程池大小@HystrixProperty(name = "maxQueueSize", value = "https://www.huyubaike.com/biancheng/20"),//最大隊列長度@HystrixProperty(name ="keepAliveTimeMinutes", value = "https://www.huyubaike.com/biancheng/2")//線程存活時間},commandProperties = {@HystrixProperty(name = "execution.isolation.strategy",value = "https://www.huyubaike.com/biancheng/THREAD")}//信號量隔離的設置@HystrixCommand(//用來設置降級方法fallbackMethod = "myTestFallbackMethod",commandProperties = {//進行熔斷配置//條件1,設置在滾動時間窗口中,斷路器的最小請求數(沒有達到不會熔斷) 。默認20 。@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold" ,value = "https://www.huyubaike.com/biancheng/10"),//條件2 , 設置斷路器打開的錯誤百分比 。在滾動時間內,在請求數量超過requestVolumeThreshold的值,且錯誤請求數的百分比超過這個比例,斷路器就為打開狀態 。@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage" ,value = "https://www.huyubaike.com/biancheng/30"),//條件3,設置滾動時間窗的長度,單位毫秒 。這個時間窗口就是斷路器收集信息的持續時間 。斷路器在收集指標信息的時會根據這個時間窗口把這個窗口拆分成多個桶,每個桶代表一段時間的指標,默認10000.@HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds" ,value = "https://www.huyubaike.com/biancheng/10000"),//設置當斷路器打開之后的休眠時間,休眠時間結束后斷路器為半開狀態 , 斷路器能接受請求,如果請求失敗又重新回到打開狀態 , 如果請求成功又回到關閉狀態//單位是毫秒@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds" ,value = "https://www.huyubaike.com/biancheng/3000"),//配置信號量隔離//配置信號量的數值@HystrixProperty(name = "execution.isolation.semaphore.maxConcurrentRequests",value = "https://www.huyubaike.com/biancheng/100"),//選擇策略為信號量隔離@HystrixProperty(name = "execution.isolation.strategy", value = "https://www.huyubaike.com/biancheng/SEMAPHORE"),//設置HystrixCommand執行的超時時間,單位毫秒@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "https://www.huyubaike.com/biancheng/1000000000")})public String Test(){....}public String myTestFallbackMethod() {log.info("========myTestFallbackMethod=========");return "myTestFallbackMethod";}

推薦閱讀