一、问题描述
Spring Cloud项目,两个微服务之间调用报错超时,控制台抛出异常,截取部分报错:
feign.RetryableException: Read timed out executing POST http://back-control-engine/client/v1.0/robotClient/robotTest
Caused by: java.net.SocketTimeoutException: Read timed out
feign.RetryableException: Read timed out executing POST http://back-control-engine/client/v1.0/robotClient/robotTest at feign.FeignException.errorExecuting(FeignException.java:84) at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:113) at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:78) at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:103) at com.sun.proxy.$Proxy234.robotTest(Unknown Source) at com.uiotsoft.back.brain.platform.business.service.impl.RobotClientServiceImpl.robotTest(RobotClientServiceImpl.java:28) at com.uiotsoft.back.brain.platform.controller.api.RobotClientController.voiceChatClient(RobotClientController.java:33) at com.uiotsoft.back.brain.platform.controller.api.RobotClientController$$FastClassBySpringCGLIB$$61a13ae5.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749) at com.uiotsoft.back.brain.platform.config.AopConfig.doAround(AopConfig.java:55) at sun.reflect.GeneratedMethodAccessor245.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:644) at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:633) at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1747) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:748) 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:171) 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:735) at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480) at feign.Client$Default.convertResponse(Client.java:143) at feign.Client$Default.execute(Client.java:68) at org.springframework.cloud.openfeign.ribbon.RetryableFeignLoadBalancer$1.doWithRetry(RetryableFeignLoadBalancer.java:114) at org.springframework.cloud.openfeign.ribbon.RetryableFeignLoadBalancer$1.doWithRetry(RetryableFeignLoadBalancer.java:94) at rx.Observable.unsafeSubscribe(Observable.java:10327) at rx.internal.operators.OnSubscribeConcatMap.call(OnSubscribeConcatMap.java:94) at rx.internal.operators.OnSubscribeConcatMap.call(OnSubscribeConcatMap.java:42) at rx.Observable.unsafeSubscribe(Observable.java:10327) at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) at rx.Observable.subscribe(Observable.java:10423) at rx.Observable.subscribe(Observable.java:10390) at rx.observables.BlockingObservable.blockForSingle(BlockingObservable.java:443) at rx.observables.BlockingObservable.single(BlockingObservable.java:340) at com.netflix.client.AbstractLoadBalancerAwareClient.executeWithLoadBalancer(AbstractLoadBalancerAwareClient.java:112) at org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient.execute(LoadBalancerFeignClient.java:83) ... 132 common frames omitted
二、问题原因
这是一个http请求报错超时,因为feign的调用分为两层:Ribbon的调用和Hystrix的调用,所以我们在配置文件中添加超时配置即可。高版本的Hystrix默认是关闭的,可以不设置。
三、解决方案
所以在 application.yml 配置文件中设置 ribbon 即可:
ribbon: # 请求处理的超时时间 ReadTimeout: 60000 # 请求连接的超时时间 ConnectTimeout: 60000
或者 在 application.properties 配置文件中设置 ribbon
# 请求处理的超时时间 ribbon.ReadTimeout: 60000 # 请求连接的超时时间 ribbon.ConnectTimeout: 60000
完结!