如何解决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 来规避该问题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。