I am implementing circuit breaker by using Spring cloud circuit breaker abstraction https://spring.io/projects/spring-cloud-circuitbreaker with hystrix. I followed examples from here 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. I would like to limit it only to server errors 5.xx and exclude 4.xx like Bad requst. 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. 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
RestTemplate throws these runtime exceptions-
HttpClientErrorException
- For client side errors ie HTTP 4XX HttpServerErrorException
- for server side errors ie HTTP 5XX Hystrix does not care about anything other than the malfunctioning of the underlying resource. 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. 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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.