Hystrix Feign重试超时不起作用

问题描述

我的项目中有一个Feign配置和Hystrix命令。 下面是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;
}
}

以下是我的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;
   }
 }

下面是我的客户:

@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();


}
从TEST-CONFIG中的

SO我抛出了IOException(500 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,"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)

有人可以帮忙吗,我想念什么?

解决方法

我猜你启用了 hystrix。尝试设置

feign.hystrix.enabled: false

然后看看它是否有效;如果是这样,它将证明您的配置没问题。 hystrix and retrying 上有一个帖子表明这不能很好地结合在一起。如果您想保持 hystrix 启用(为什么不应该),也许值得查看 spring-retry 来规避该问题。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...