Hystrix Feign Retry for Timeout not working

Viewed 1299

I have a Feign Configuration and Hystrix Commands in my project. below is Feign Config

@Configuration
public class FeignRetryConfig {

@Primary
@Bean
public Feign.Builder feignBuilder(Retryer nephosFeignRetryer) {
    return HystrixFeign.builder()
            .errorDecoder(new FeignErrorDecoder())
            .retryer(nephosFeignRetryer);
}

// retry set to 3 times
@Bean
public Retryer nephosFeignRetryer() {
    return new Retryer.Default(10, SECONDS.toMillis(5), 5);
}

@Bean
Logger.Level feignLoggerLevel() {
    return Logger.Level.FULL;
}
}

and below is my ErrorDecoder:

public class FeignErrorDecoder implements ErrorDecoder {

    private final ErrorDecoder defaultErrorDecoder = new Default();

    @Override
    public Exception decode(String methodKey, Response response) {
        Exception exception = defaultErrorDecoder.decode(methodKey, response);
        if (response.status() == 500) {
            log.error(String.format("##### Got %s response from %s #######", response.status(), 
        methodKey));
        return new RetryableException(
                  exception.getMessage(),
                  exception,
                  null
              );
       }
    return exception;
   }
 }

and below is my client:

@FeignClient(name = "TEST-CONFIG", configuration = FeignRetryConfig.class, fallbackFactory = 
      XYZClientFallbackFactory.class)
public interface TestClient {

    @RequestMapping(value = "/test", method = RequestMethod.GET, consumes = 
     MediaType.APPLICATION_JSON_VALUE)
     Observable<String> test();


}

SO from TEST-CONFIG I am throwing IOException ( 500 Error ) to Test, but it does not make any retry. below is my error:

com.netflix.hystrix.exception.HystrixRuntimeException: TestClient#test() failed and fallback failed.
at com.netflix.hystrix.AbstractCommand$22.call(AbstractCommand.java:815)
at com.netflix.hystrix.AbstractCommand$22.call(AbstractCommand.java:790)
at rx.internal.operators.OperatorOnErrorResumeNextViaFunction$4.onError(OperatorOnErrorResumeNextViaFunction.java:140)
at rx.internal.operators.OnSubscribeDoOnEach$DoOnEachSubscriber.onError(OnSubscribeDoOnEach.java:87)
at rx.internal.operators.OnSubscribeDoOnEach$DoOnEachSubscriber.onError(OnSubscribeDoOnEach.java:87)
at com.netflix.hystrix.AbstractCommand$DeprecatedOnFallbackHookApplication$1.onError(AbstractCommand.java:1451)
at com.netflix.hystrix.AbstractCommand$FallbackHookApplication$1.onError(AbstractCommand.java:1376)
at rx.internal.operators.OnSubscribeDoOnEach$DoOnEachSubscriber.onError(OnSubscribeDoOnEach.java:87)
at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: feign.RetryableException: status 500 reading TestClient#test(); content:
  {"status":500,"erroritems":[{"code":"RuntimeException","message":"org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection"}]}
at feign.hystrix.HystrixInvocationHandler$1.run(HystrixInvocationHandler.java:108)
at com.netflix.hystrix.HystrixCommand$2.call(HystrixCommand.java:301)
at com.netflix.hystrix.HystrixCommand$2.call(HystrixCommand.java:297)
at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:46)
... 30 common frames omitted
 Caused by: feign.FeignException: status 500 reading TestClient#test(); content:
  {"status":500,"erroritems":[{"code":"RuntimeException","message":"org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection"}]}
at feign.FeignException.errorStatus(FeignException.java:62)
at feign.codec.ErrorDecoder$Default.decode(ErrorDecoder.java:91)

Can Somebody Help Please, What am I Missing ?

1 Answers

I guess you have hystrix enabled. Try setting

feign.hystrix.enabled: false

and see if it works then; if so it would prove your configuration to be ok. There is a post on hystrix and retrying that suggests that this does not go well together. If you want to keep hystrix enabled (and why should you not), perhaps it is worth looking at spring-retry to circumvent the problem.

Related