Servlet.service() for servlet [dispatcherServlet] in context with path [/payment] threw exception [Request processing failed; nested exception is feign.RetryableException: connect timed out executing POST http://xx/xx/] with root cause
java.net.SocketTimeoutException: connect timed out
Spring Cloud中,Feign和Ribbon在整合了Hystrix後,可能會出現首次調用失敗的問題,要如何解決該問題呢?
造成該問題的原因
Hystrix默認的超時時間是1秒,如果超過這個時間尚未響應,將會進入fallback代碼。而首次請求往往會比較慢(因爲Spring的懶加載機制,要實例化一些類),這個響應時間可能就大於1秒了。知道原因後,我們來總結一下解決放你。解決方案有三種,以feign爲例。
方法一
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000
該配置是讓Hystrix的超時時間改爲5秒
方法二
hystrix.command.default.execution.timeout.enabled: false
該配置,用於禁用Hystrix的超時時間
方法三
feign.hystrix.enabled: false
該配置,用於索性禁用feign的hystrix。該做法除非一些特殊場景,不推薦使用。
作者:周立_itmuch
鏈接:https://www.jianshu.com/p/75dbcf3d7904
來源:簡書
著作權歸作者所有。商業轉載請聯繫作者獲得授權,非商業轉載請註明出處。