繁体   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