【问题标题】:Spring cloud circuit breaker - how control on what http status circuit gets openedSpring Cloud 断路器 - 如何控制打开的 http 状态电路
【发布时间】:2020-08-29 05:50:08
【问题描述】:

我正在通过使用带有 hystrix 的 Spring 云断路器抽象 https://spring.io/projects/spring-cloud-circuitbreaker 来实现断路器。 我按照这里的例子https://github.com/spring-cloud-samples/spring-cloud-circuitbreaker-demo/tree/master/spring-cloud-circuitbreaker-demo-hystrix

默认情况下,从端点返回的 HTTP 状态组 5.x.x 和 4.x.x 都是断开电路的信号。 我想将其仅限于服务器错误 5.x.x 并排除 4.x.x 之类的错误请求。在我的情况下,服务的客户应该被告知他的请求是不正确的,不应该得到回退的响应。

我不知道如何实现它。使用 Spring Cloud Circuit Breaker 抽象对我来说很重要,因此使用 @HystrixCommand(ignoreExceptions={...}) 不是一种选择。我想以更具声明性的方式进行配置,例如配置。

【问题讨论】:

    标签: java spring-boot spring-cloud hystrix circuit-breaker


    【解决方案1】:

    您可以尝试此处提到的堆栈溢出本身的答案之一 -> Configuring hystrix command properties using application.yaml in Spring-Boot application

    【讨论】:

    • 谢谢。不幸的是,我的问题没有答案。
    【解决方案2】:

    RestTemplate 抛出这些运行时异常-

    1. HttpClientErrorException- 对于客户端错误,即 HTTP 4XX
    2. HttpServerErrorException- 用于服务器端错误,即 HTTP 5XX

    Hystrix 只关心底层资源的故障。此外,它使用异常来推断底层资源发生故障。

    如果您不希望您的电路在 HTTP 4XX 上打开,只需在您的 HttpBinService.java 类中 catch HttpClientErrorException。您修改后的方法将是-

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

    【讨论】:

      猜你喜欢
      • 2020-10-29
      • 2016-09-22
      • 2015-05-23
      • 2018-07-09
      • 2016-01-07
      • 2021-08-07
      • 2019-02-06
      • 2021-05-13
      • 2023-03-09
      相关资源
      最近更新 更多