Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我们可以使用SpringCloudNetflix和Hystrix重试失败的执行吗_Java_Spring_Spring Boot_Spring Cloud_Hystrix - Fatal编程技术网

Java 我们可以使用SpringCloudNetflix和Hystrix重试失败的执行吗

Java 我们可以使用SpringCloudNetflix和Hystrix重试失败的执行吗,java,spring,spring-boot,spring-cloud,hystrix,Java,Spring,Spring Boot,Spring Cloud,Hystrix,我正在使用SpringCloudNetflix库 我想知道是否有一种方法可以将此代码添加并配置它,而不是立即执行回退方法,以重试执行N次,如果是N次,则执行回退方法: @HystrixCommand(fallbackMethod = "defaultInvokcation") public String getRemoteBro(String name) { return(executeRemoteService(name)); } private

我正在使用SpringCloudNetflix库

我想知道是否有一种方法可以将此代码添加并配置它,而不是立即执行回退方法,以重试执行N次,如果是N次,则执行回退方法:

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

     private String defaultInvokcation(String name) {
   return "something";
}
谢谢, 雷。

来自我的:

在代码中处理此行为。了解您的“特殊”业务逻辑不是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");
}
private final static int MAX_RETRIES=5;
@HystrixCommand(fallbackMethod=“DefaultInvokation”)
公共字符串getRemoteBro(字符串名称){
返回(ExecuteMoteService(名称));
}
私有字符串ExecuteMoteService(字符串服务名称){
对于(int i=0;i

不知道是否希望在循环中使用异常;)您还可以将
ReallyExecuteMoteService
中的答案封装在某种带有状态代码的ServiceReturnMessage中。

我认为这不是hystrix的工作。负责远程请求行为的更多是您的
executemoteservice
。将此方法扩展为“循环{成功?返回:重试}抛出RequestAmountExceedeException”啊。好主意。请用一个答案来说明这一点好吗?我将把它标记为已回答。谢谢。啊,我想我可以在Hystrix异常处理中处理这个问题。我的服务也不例外,因为我试图表达:Hystrix检查您的服务执行情况,如果某项服务失败,从Hystrix的角度来看,它就是失败了。因此,在被调用的服务中,逻辑必须是正确的。对于hystrix来说,确定服务(远程端)是否已死亡或只需再次询问它将非常复杂(只是一个想法:可能基于调用的异常类型)。如果hystrix在执行默认方法之前有一个重试机制(不管失败原因是什么),那将非常好如果有可能,您必须记住这将带来更多的问题/配置需求。例如重试次数或重试调用之间的超时。另一方面:如果您的调用在远程端执行繁重的工作(但以异常结束),那么即使您可以确保远程服务器在每次运行中都会失败,您也会增加远程服务器上的负载。在断路器启动之前,没有任何机制可以阻止它。我听到了。你认为你可以用rxjava模式和hystrix来“升级”你的答案吗?与订阅。。因为我想如果我可以在例外情况下订阅Oberservable,我可以决定做什么