一旦下游服務C因某些緣由變得不可用,積壓了大量請求,服務B的請求線程也隨之阻塞。線程資源逐漸耗盡,使得服務B也變得不可用。緊接着,服務A也變爲不可用,整個調用鏈路被拖垮(雪崩)html
主要爲了解決雪崩效應java
服務降級git
服務熔斷github
在固定時間窗口內,接口調用超時比率達到一個閾值,會開啓熔斷。進入熔斷狀態後,後續對該服務接口的調用再也不通過網絡,直接執行本地的默認方法,達到服務降級的效果。spring
熔斷不多是永久的。當通過了規定時間以後,服務將從熔斷狀態回覆過來,再次接受調用方的遠程調用。網絡
導入pomapp
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency>
開啓註解@EnableCircuitBreaker
ide
或者替換爲@EnableSpringCloud
函數
對應方法上面使用註解(這個方法必須寫)post
@HystrixCommand(fallbackMethod = "fallback")
設置全局
@DefaultProperties(defaultFallback = "defaultFallBack")
@RestController @DefaultProperties(defaultFallback = "defaultFallBack") public class HystrixController { //@HystrixCommand(fallbackMethod = "fallback") @HystrixCommand(commandProperties = { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000") }) @GetMapping("/getProductInfoList") public String getProductInfo() { RestTemplate restTemplate = new RestTemplate(); return restTemplate.postForObject("http://127.0.0.1:9083/product/listForOrder", Collections.singletonList("157875196366160022"), String.class); } private String fallback() { return "太擁擠了,請稍後再試~"; } private String defaultFallBack() { return "默認提示:太擁擠了,請稍後重試~"; } }
會自動實現依賴隔離,進行容錯保護
參數參考:http://zyouwei.com/%E6%8A%80%...
circuitBreaker
斷路器
circuitBreaker.requestVolumeThreshold
circuitBreaker.sleepWindowInMilliseconds
circuitBreaker.errorThresholdPercentage
https://martinfowler.com/blik...
狀態機
容錯:重試機制,第一次不成功再重試一次就成功了
故障問題:使用斷路器模式,故障達到必定的值,斷路器斷閘
服務沒有故障時,熔斷器所處的狀態,對調用方的調用不作任何限制。
在固定時間窗口內(Hystrix默認是10秒),接口調用出錯比率達到一個閾值(Hystrix默認爲50%),會進入熔斷開啓狀態。進入熔斷狀態後,後續對該服務接口的調用再也不通過網絡,直接執行本地的fallback方法。
在進入熔斷開啓狀態一段時間以後(Hystrix默認是5秒),熔斷器會進入半熔斷狀態。所謂半熔斷就是嘗試恢復服務調用,容許有限的流量調用該服務,並監控調用成功率。若是成功率達到預期,則說明服務已恢復,進入熔斷關閉狀態;若是成功率仍舊很低,則從新進入熔斷關閉狀態
``
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"), //時間窗口,統計時間範圍 @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "1000"), //錯誤百分比 @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60")
必須在對應的方法上使用註解 @HystrixCommand
hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 3000 # circuitBreaker: # enabled: true
給特定方法設置:commandKey
配置
feign: hystrix: enabled: true
@Component
不要忘記了@FeignClient(name = "product",fallback =ProductClient.FallBackClass.class ) public interface ProductClient { @PostMapping("/product/listForOrder") List<ProductInfoOutPut> listForOrder(List<String> productIdList); @PostMapping("/product/decreaseStock") void decreaseStock(List<DecreaseStockInput> CartDTO); @Component class FallBackClass implements ProductClient{ @Override public List<ProductInfoOutPut> listForOrder(List<String> productIdList) { return null; } @Override public void decreaseStock(List<DecreaseStockInput> CartDTO) { } } }
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId> </dependency>
@EnableHystrixDashboard
訪問http://localhost:8899/hystrix
使用了zuul會讓類懶加載,因此第一次訪問會超時,這時候咱們須要把配置直接放到zuul裏面