我们可以使用Spring-cloud-netflix和Hystrix来重试失败的exectuion

ray*_*man 5 java spring spring-boot hystrix spring-cloud

我正在使用Spring-Cloud-netflix库.

我想知道是否有办法获取此代码并添加配置它而不是立即执行回退方法重试执行N次,并且在N次执行回退方法的情况下:

 @HystrixCommand(fallbackMethod = "defaultInvokcation")
    public String getRemoteBro(String name) {
        return(executeRemoteService(name));
    }

     private String defaultInvokcation(String name) {
   return "something";
}
Run Code Online (Sandbox Code Playgroud)

谢谢,雷.

mei*_*ier 3

从我的评论来看:

在您的代码中处理此行为。了解你的“特殊”业务逻辑不是 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");
}
Run Code Online (Sandbox Code Playgroud)

不知道您是否更喜欢在循环内使用异常;)您还可以reallyExecuteRemoteService使用状态代码将您的答案包装在某种 ServiceReturnMessage 中。