Netflix透過Hystrix庫實現了斷路器模式,在微服務架構體系中存在多層呼叫的,低階的服務故障會導取級聯故障,斷路器可以有效保護微服務,防止出現雪崩效應。
斷路器開啟條件:
在定義時間視窗內預設10s(metrics。rollingStatus。timeInMilliSeconds)
某服務被請求次數超過閾值,預設20次(circuitBreaker。requestVolue Threshold)
呼叫失敗率大於閾值,預設>50%(circuitBreaker。errorThresholdPercentage)
基於@HystrixCommand註解的斷開例項
@EnableCircuitBreaker
@HystrixCommand(fallbackMethod=“。。service”)
commandProperties={@HystrixProperty(name=“”)}
也可以透過配置如下引數:Hystrix會自動將上下文傳遞給@HystirxCommand註解方法中
hystrix。shareSecurityContext =true
開啟
斷路器流量
依賴:spring-boot-stater-acturtor
配製:management。endpoints。web。exposure。include:hystrix。stream
訪問:/acturtor/hystrix。stream