簡體   English   中英

Spring 雲斷路器 - 如何控制 http 狀態電路打開

[英]Spring cloud circuit breaker - how control on what http status circuit gets opened

我正在通過使用 Spring 雲斷路器抽象https://spring.io/projects/spring-cloud-circuitbreaker和 hystrix 來實現斷路器。 我按照這里的例子https://github.com/spring-cloud-samples/spring-cloud-circuitbreaker-demo/tree/master/spring-cloud-circuitbreaker-demo-hystrix

默認情況下,從端點返回的 HTTP 狀態組 5.xx 和 4.xx 都是斷開電路的信號。 我想將其僅限制為服務器錯誤 5.xx 並排除 4.xx 之類的錯誤請求。 在我的情況下,服務的客戶應該被告知他的請求是不正確的,並且不應該得到回退的響應。

我不知道如何實現它。 使用 Spring 雲斷路器抽象對我來說很重要,因此使用 @HystrixCommand(ignoreExceptions={...}) 不是一種選擇。 我想以更具聲明性的方式配置它,例如配置。

您可以嘗試此處提到的堆棧溢出本身的答案之一-> 在 Spring-Boot 應用程序中使用 application.yaml 配置 hystrix 命令屬性

RestTemplate 拋出這些運行時異常-

  1. HttpClientErrorException - 對於客戶端錯誤,即 HTTP 4XX
  2. HttpServerErrorException - 用於服務器端錯誤,即 HTTP 5XX

除了底層資源的故障之外,Hystrix 不關心任何事情。 此外,它使用異常來推斷底層資源發生故障。

如果您不希望您的電路在 HTTP 4XX 上打開,只需在HttpBinService.java class 中catch HttpClientErrorException 您修改后的方法將是-

public Map get() {
    try{
        return rest.getForObject("https://httpbin.org/get", Map.class);
    }catch(HttpClientErrorException e){
        // Log something
        // return null or something
    }
}

看看這個項目: https://github.com/Romeh/spring-cloud-gateway-resilience4j

基本上,您需要編寫自己的過濾器/謂詞來為錯誤代碼創建錯誤 - 然后拋出異常。 異常必須在導致斷路器跳閘的異常列表中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM