探索flutter框架开发的app在移动应用市场的潜力与挑战
1168
2022-11-08
记一次线上SpringCloud Feign请求服务超时异常排查问题
由于近期线上单量暴涨,第三方反馈部分工单业务存在查询处理失败现象,经排查是当前系统通过FeignClient调用下游系统出现部分超时失败(异常代码贴在下方)。
Caused by: feign.RetryableException: Read timed out executing POST http://xxxx at feign.FeignException.errorExecuting(FeignException.java:84) ~[feign-core-10.1.0.jar!/:na] at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:113) ~[feign-core-10.1.0.jar!/:na] at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:78) ~[feign-core-10.1.0.jar!/:na] at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:103) ~[feign-core-10.1.0.jar!/:na] at com.sun.proxy.$Proxy141.creditReportConvert(Unknown Source) ~[na:na]Caused by: java-.SocketTimeoutException: Read timed out at java-.SocketInputStream.socketRead0(Native Method) ~[na:1.8.0_121] at java-.SocketInputStream.socketRead(SocketInputStream.java:116) ~[na:1.8.0_121] at java-.SocketInputStream.read(SocketInputStream.java:171) ~[na:1.8.0_121] at java-.SocketInputStream.read(SocketInputStream.java:141) ~[na:1.8.0_121] at java.io.BufferedInputStream.fill(BufferedInputStream.java:246) ~[na:1.8.0_121] at java.io.BufferedInputStream.read1(BufferedInputStream.java:286) ~[na:1.8.0_121] at java.io.BufferedInputStream.read(BufferedInputStream.java:345) ~[na:1.8.0_121] at sun-.http.HttpClient.parseHTTPHeader(HttpClient.java:704) ~[na:1.8.0_121] at sun-.http.HttpClient.parseHTTP(HttpClient.java:647) ~[na:1.8.0_121] at sun-.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1569) ~[na:1.8.0_121] at sun-.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474) ~[na:1.8.0_121] at java-.HttpURLConnection.getResponseCode(HttpURLConnection.java:480) ~[na:1.8.0_121] at feign.Client$Default.convertResponse(Client.java:143) ~[feign-core-10.1.0.jar!/:na] at feign.Client$Default.execute(Client.java:68) ~[feign-core-10.1.0.jar!/:na] at org.springframework.cloud.openfeign.ribbon.FeignLoadBalancer.execute(FeignLoadBalancer.java:93) ~[spring-cloud-openfeign-core-2.1.1.RELEASE.jar!/:2.1.1.RELEASE] at org.springframework.cloud.openfeign.ribbon.FeignLoadBalancer.execute(FeignLoadBalancer.java:56) ~[spring-cloud-openfeign-core-2.1.1.RELEASE.jar!/:2.1.1.RELEASE] at com-flix.client.AbstractLoadBalancerAwareClient$1.call(AbstractLoadBalancerAwareClient.java:104) ~[ribbon-loadbalancer-2.3.0.jar!/:2.3.0] at com-flix.loadbalancer.reactive.LoadBalancerCommand$3$1.call(LoadBalancerCommand.java:303) ~[ribbon-loadbalancer-2.3.0.jar!/:2.3.0]
通过系统慢请求捕捉拦截,发现当前请求仅耗时1031毫秒,就触发Read timed out超时错误,本项目与下游项目均注册在Eureka上面,对这个1秒就超时感到很迷惑,于是开始查阅底层源码之旅。
通过跟踪代码可以在feign.Request内部类Options构造函数默认配置连接超时10秒,读超时60秒。然而本次调用请求仅耗时1秒左右就被掐断连接提示超时错误,初步判断默认这个超时配置并未生效导致。
写到这,咱们先回顾下Feign链路完整调用流程步骤。
可以看出F
nYUMD
eign调用分为Hystrix、Ribbon两层,一般高版本的Hystrix默认是关闭的(本项目Hystrix默认是关闭的,那么现在需要去分析Ribbon层调用配置信息)
可以看到RibbonClientConfiguration默认配置的读超时和连接超时时间1000毫秒=1秒,在没有配置超时情况下当前这个超时是符合本次调用超时错误触发。
我们可以在FeignLoadBalancer的execute方法中看到当IClientConfig为空的时候进行覆盖超时时间,默认是取Ribbon的超时时间,并不是Feign Options默认的超时时间。
public FeignLoadBalancer(ILoadBalancer lb, IClientConfig clientConfig,
ServerIntrospector serverIntrospector) {
super(lb, clientConfig);
this.setRetryHandler(RetryHandler.DEFAULT);
this.clientConfig = clientConfig;
this.ribbon = RibbonProperties.from(clientConfig);
RibbonPropertiesnYUMD ribbon = this.ribbon;
this.connectTimeout = ribbon.getConnectTimeout();
this.readTimeout = ribbon.getReadTimeout();
this.serverIntrospector = serverIntrospector;
}
如果application.properties文件中有配置超时时间,则使用配置的超时时间。否则是取Ribbon默认的超时时间,也就是网上所说的Feign调用服务的默认时长是1秒钟,也就是如果超过1秒没连接上或者超过1秒没响应,那么会相应的报错。
实际业务中服务如果响应时间超过1秒,咱们可以按照实际响应情况给予配置相对应的超时时间,下面分别贴出properties和yml版本的配置
#properties版本
feign.client.config.default.connectTimeout=60000
feign.client.config.default.readTimeout=60000
#yml版本
feign:
client:
config:
default:
connectTimeout: 60000
http:// readTimeout: 60000
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~