简体   繁体   English

Spring 云断路器 - 如何控制 http 状态电路打开

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

I am implementing circuit breaker by using Spring cloud circuit breaker abstraction https://spring.io/projects/spring-cloud-circuitbreaker with hystrix.我正在通过使用 Spring 云断路器抽象https://spring.io/projects/spring-cloud-circuitbreaker和 hystrix 来实现断路器。 I followed examples from here https://github.com/spring-cloud-samples/spring-cloud-circuitbreaker-demo/tree/master/spring-cloud-circuitbreaker-demo-hystrix我按照这里的例子https://github.com/spring-cloud-samples/spring-cloud-circuitbreaker-demo/tree/master/spring-cloud-circuitbreaker-demo-hystrix

By default both HTTP Statuse groups 5.xx and 4.xx returned from endpoint are signals to open the circuit.默认情况下,从端点返回的 HTTP 状态组 5.xx 和 4.xx 都是断开电路的信号。 I would like to limit it only to server errors 5.xx and exclude 4.xx like Bad requst.我想将其仅限制为服务器错误 5.xx 并排除 4.xx 之类的错误请求。 In my case client of the service should be informed his request is incorrect and should not get response from fallback.在我的情况下,服务的客户应该被告知他的请求是不正确的,并且不应该得到回退的响应。

I do not know how to implement it.我不知道如何实现它。 It is importand to me to use Spring Cloud Circuit Breaker abstraction so using @HystrixCommand(ignoreExceptions={...}) is not an option.使用 Spring 云断路器抽象对我来说很重要,因此使用 @HystrixCommand(ignoreExceptions={...}) 不是一种选择。 I would like to configure it in more declarative manner like configuration.我想以更具声明性的方式配置它,例如配置。

you can try one of the answer from the stack overflow itself mentioned here -> Configuring hystrix command properties using application.yaml in Spring-Boot application您可以尝试此处提到的堆栈溢出本身的答案之一-> 在 Spring-Boot 应用程序中使用 application.yaml 配置 hystrix 命令属性

RestTemplate throws these runtime exceptions- RestTemplate 抛出这些运行时异常-

  1. HttpClientErrorException - For client side errors ie HTTP 4XX HttpClientErrorException - 对于客户端错误,即 HTTP 4XX
  2. HttpServerErrorException - for server side errors ie HTTP 5XX HttpServerErrorException - 用于服务器端错误,即 HTTP 5XX

Hystrix does not care about anything other than the malfunctioning of the underlying resource.除了底层资源的故障之外,Hystrix 不关心任何事情。 Moreover, it uses exceptions to deduce that the underlying resource has malfunctioned.此外,它使用异常来推断底层资源发生故障。

If you don't want your circuit to open on HTTP 4XX, simply catch the HttpClientErrorException in your HttpBinService.java class.如果您不希望您的电路在 HTTP 4XX 上打开,只需在HttpBinService.java class 中catch HttpClientErrorException Your modified method will be-您修改后的方法将是-

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

Take a look at this project: https://github.com/Romeh/spring-cloud-gateway-resilience4j看看这个项目: https://github.com/Romeh/spring-cloud-gateway-resilience4j

Basically you need to write your own filter/predicate that creates a fault for the error codes - and then throw an exception.基本上,您需要编写自己的过滤器/谓词来为错误代码创建错误 - 然后抛出异常。 The exception must be in list of exceptions that cause the circuit breaker to trip.异常必须在导致断路器跳闸的异常列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM