这可以通过定义ErrorDecoder 并手动控制Hystrix 断路器来实现。您可以检查异常的响应代码并提供您自己的回退。此外,如果您希望重试请求,请将您的异常包装并抛出RetryException。
为了满足您的重试要求,还要注册一个具有适当配置的Retryer bean。请记住,使用Retryer 将在持续时间内占用一个线程。 Retryer 的默认实现也确实使用了指数退避策略。
这是一个来自OpenFeign 文档的示例 ErrorDecoder:
public class StashErrorDecoder implements ErrorDecoder {
@Override
public Exception decode(String methodKey, Response response) {
if (response.status() >= 400 && response.status() <= 499) {
return new StashClientException(
response.status(),
response.reason()
);
}
if (response.status() >= 500 && response.status() <= 599) {
return new StashServerException(
response.status(),
response.reason()
);
}
return errorStatus(methodKey, response);
}
}
在您的情况下,您可以根据需要对419 做出反应。
您可以在运行时强制打开断路器设置此属性
hystrix.command.HystrixCommandKey.circuitBreaker.forceOpen
ConfigurationManager.getConfigInstance()
.setProperty(
"hystrix.command.HystrixCommandKey.circuitBreaker.forceOpen", true);
用您自己的命令替换HystrixCommandKey。您需要在所需时间后将此断路器恢复为闭合状态。