我正在使用 Spring-Cloud-netflix 库。
我想知道是否有一种方法可以获取此代码并对其进行配置,而不是立即执行回退方法以重试执行 N 次,如果 N 次则执行回退方法:
@HystrixCommand(fallbackMethod = "defaultInvokcation")
public String getRemoteBro(String name) {
return(executeRemoteService(name));
}
private String defaultInvokcation(String name) {
return "something";
}
谢谢, 射线。
最佳答案
来 self 的 comment :
在您的代码中处理此行为。了解您的“特殊”业务逻辑不是 hystrix 的工作。举个例子
private final static int MAX_RETRIES = 5;
@HystrixCommand(fallbackMethod = "defaultInvokcation")
public String getRemoteBro(String name) {
return(executeRemoteService(name));
}
private String executeRemoteService(String serviceName) {
for (int i = 0; i < MAX_RETRIES; i++) {
try {
return reallyExecuteRemoteService(serviceName);
} catch (ServiceException se) {
// handle or log execption
}
}
throw new RuntimeException("bam");
}
不知道您是否喜欢在循环内使用异常 ;) 您也可以将来自 reallyExecuteRemoteService
的答案包装在某种带有状态代码的 ServiceReturnMessage 中。
关于java - 我们可以使用 Spring-cloud-netflix 和 Hystrix 来重试失败的执行吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30482811/