【问题标题】:Hystrix Feign Retry for Timeout not workingHystrix假装重试超时不起作用
【发布时间】:2021-01-05 19:17:55
【问题描述】:

我的项目中有一个 Feign 配置和 Hystrix 命令。 下面是 Feign 配置

@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 我抛出 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,"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)

有人可以帮忙吗,我错过了什么?

【问题讨论】:

    标签: spring-boot hystrix feign


    【解决方案1】:

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

    feign.hystrix.enabled: false
    

    然后看看它是否有效;如果是这样,它将证明您的配置没问题。 hystrix and retrying 上有一个帖子表明这并不顺利。如果您想保持 hystrix 处于启用状态(为什么不应该启用),也许值得查看 spring-retry 来规避问题。

    【讨论】:

      猜你喜欢
      • 2018-12-21
      • 2017-08-02
      • 2020-11-30
      • 2018-03-07
      • 2019-08-11
      • 1970-01-01
      • 2021-05-02
      • 1970-01-01
      • 2017-02-27
      相关资源
      最近更新 更多