我正在使用Spring云断路器抽象来实现断路器。https:/spring.ioprojectsspring-cloud-circuitbreaker。 与hystrix.I按照这里的例子。https:/github.comspring-cloud-samplesspring-cloud-circuitbreaker-demotreemasterspring-cloud-circuitbreaker-demo-hystrix。
默认情况下,从端点返回的HTTP Statuse组5.x.x和4.x.x都是打开电路的信号,我想只限制服务器错误5.x.x,排除4.x.x,如Bad requst。在我的例子中,服务的客户端应该被告知他的请求是不正确的,并且不应该从回退中得到响应。
我不知道如何实现它。对我来说,使用Spring Cloud Circuit Breaker抽象是很重要的,所以使用@HystrixCommand(ignoreExceptions={...})不是一个选项。我希望以更多的声明方式像配置一样配置它。
你可以试试这里提到的堆栈溢出本身的一个答案->。在Spring-Boot应用程序中使用application.yaml配置hystrix命令属性。
RestTemplate会抛出这些运行时异常-。
HttpClientErrorException
- 对于客户端错误,即HTTP 4XX。HttpServerErrorException
- 对于服务器端错误,即HTTP 5XX。Hystrix除了关心底层资源的故障外,不关心其他任何事情。而且,它使用异常来推断底层资源发生了故障。
如果你不想让你的电路在HTTP 4XX上打开,只需要 catch
的 HttpClientErrorException
在你 HttpBinService.java
类。您修改后的方法将是
public Map get() {
try{
return rest.getForObject("https://httpbin.org/get", Map.class);
}catch(HttpClientErrorException e){
// Log something
// return null or something
}
}