解决Spring Cloud中Feign第一次请求失败的问题

Posted 逆水行舟,不进则退

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了解决Spring Cloud中Feign第一次请求失败的问题相关的知识,希望对你有一定的参考价值。

在Spring Cloud中,Feign和Ribbon在整合了Hystrix后,可能会出现首次调用失败的问题

com.netflix.hystrix.exception.HystrixTimeoutException: null
at com.netflix.hystrix.AbstractCommand$HystrixObservableTimeoutOperator$1.run(AbstractCommand.java:1142) ~[hystrix-core-1.5.18.jar:1.5.18]
at com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable$1.call(HystrixContextRunnable.java:41) ~[hystrix-core-1.5.18.jar:1.5.18]
at com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable$1.call(HystrixContextRunnable.java:37) ~[hystrix-core-1.5.18.jar:1.5.18]
at com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable.run(HystrixContextRunnable.java:57) ~[hystrix-core-1.5.18.jar:1.5.18]
at com.netflix.hystrix.AbstractCommand$HystrixObservableTimeoutOperator$2.tick(AbstractCommand.java:1159) [hystrix-core-1.5.18.jar:1.5.18]
at com.netflix.hystrix.util.HystrixTimer$1.run(HystrixTimer.java:99) [hystrix-core-1.5.18.jar:1.5.18]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_131]
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) [na:1.8.0_131]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_131]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) [na:1.8.0_131]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_131]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_131]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_131]

产生的原因:

  Hystrix默认的超时时间是1秒,如果超过这个时间尚未响应,将会进入fallback

  第一次请求往往会比较慢,响应时间可能会大于1秒

解决的方法有3种:

  (1)Hystrix的超时时间设置为5秒

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=5000

  (2)禁用Hystrix的超时时间

hystrix.command.default.execution.timeout.enabled

  (3)禁用feign的hystrix

feign.hystrix.enabled=false

 

 

以上是关于解决Spring Cloud中Feign第一次请求失败的问题的主要内容,如果未能解决你的问题,请参考以下文章

spring Cloud中,解决Feign/Ribbon整合Hystrix第一次请求失败的问题?

Spring Cloud之Feign 转发请求头(header参数)

spring cloud feign 报错 feign.FeignException$MethodNotAllowed: status 405 reading

spring cloud feign 报错 feign.FeignException$MethodNotAllowed: status 405 reading

使用Spring Cloud Feign 日志查看请求响应

Spring Cloud Feign 拦截器