feign.RetryableException: Read timed out executing—SpringCloud组件Feign连接超时处理

一、问题描述

目前大部分公司都是使用微服务架构,自然免不了服务间的调用,在服务调用的过程中,可能会出现连接超时或者读取超时的问题。接下来针对SpringCloud组件Feign的超时问题从根源上看一下如何解决。

feign.RetryableException: Read timed out executing(请求方式)(请求地址)         at feign.FeignException.errorExecuting(FeignException.java:65)         at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:105)         at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:76)         at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:103)         at com.sun.proxy.$Proxy113.getBaseRow(Unknown Source) Caused by: java.net.SocketTimeoutException: Read timed out         at java.net.SocketInputStream.socketRead0(Native Method)         at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)         at java.net.SocketInputStream.read(SocketInputStream.java:170)         at java.net.SocketInputStream.read(SocketInputStream.java:141)         at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)         at java.io.BufferedInputStream.read1(BufferedInputStream.java:286)         at java.io.BufferedInputStream.read(BufferedInputStream.java:345)         at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:704)         at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:647)         at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1569)         at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)         at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)         at feign.Client$Default.convertResponse(Client.java:152)         at feign.Client$Default.execute(Client.java:74)

二、解决方案

在yml文件中配置connectTimeout和readTimeout
feign:     client:         config:default:                 connectTimeout:60000                 readTimeout:60000

三、源码追踪(知其然,知其所以然)

1、首先看下日志报错打印 FeignException.java:67、SynchronousMethodHandler.java:104

feign.RetryableException: Read timed out executing---SpringCloud组件Feign连接超时处理
feign.RetryableException: Read timed out executing---SpringCloud组件Feign连接超时处理

2、查看options(HTTP请求的配置)的初始化

feign.RetryableException: Read timed out executing---SpringCloud组件Feign连接超时处理
feign.RetryableException: Read timed out executing---SpringCloud组件Feign连接超时处理
feign.RetryableException: Read timed out executing---SpringCloud组件Feign连接超时处理

3、options的默认值和配置

源码追踪到这可以看出feign请求的默认连接超时时间为10秒 读取超时时间为60秒

feign.RetryableException: Read timed out executing---SpringCloud组件Feign连接超时处理
feign.RetryableException: Read timed out executing---SpringCloud组件Feign连接超时处理

3、FeignClientProperties的config属性

FeignClientProperties是Feign客户端的配置类,对应我们在yml文件中的feign.client。
我们可以通过配置connectTimeout和readTimeout的大戏

feign.RetryableException: Read timed out executing---SpringCloud组件Feign连接超时处理
feign.RetryableException: Read timed out executing---SpringCloud组件Feign连接超时处理