Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we use Spring-cloud-netflix and Hystrix to retry failed exectuion

I am using Spring-Cloud-netflix library.

I wonder if there is a way to take this code and add configure it instead of executing the fallback method right away to retry to execute it N times and in case of N times than execute the fallback method:

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

     private String defaultInvokcation(String name) {
   return "something";
}

Thanks, ray.

like image 844
rayman Avatar asked May 27 '15 12:05

rayman


People also ask

How do I retry with Hystrix?

I have a hystrix command that encapsulates a REST call. In case of failure(e.g. timeout), I want to make a single retry and return an appropriate error if it still fails. As I can see, Hystrix doesn't support retries. The only way to do it with Hystrix is to put the main logic into getFallback() method.

What is the use of Netflix Hystrix?

Hystrix helps by providing protection and control over latency and failure from dependencies, most commonly those accessed over network. It helps stop cascading failures and allows you to fail fast and rapidly recover, or fallback and gracefully degrade.

What has replaced Hystrix?

Akka, Envoy, Istio, Zuul, and Polly are the most popular alternatives and competitors to Hystrix.

Does Netflix still use Hystrix?

Hystrix Status. Hystrix is no longer in active development, and is currently in maintenance mode. Hystrix (at version 1.5. 18) is stable enough to meet the needs of Netflix for our existing applications.


1 Answers

From my comment:

Handle this behavior in your code. It's not the job of hystrix to know your "special" business logic. As an example

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");
}

Don't know if you prefer to use an exception inside the loop ;) You could also wrap your answer from reallyExecuteRemoteServicein some kind of ServiceReturnMessage with a status code.

like image 161
meistermeier Avatar answered Oct 02 '22 00:10

meistermeier