微服务之间的调用(Ribbon与Feign)

本文探讨了 Spring Cloud 中 Feign 进行服务间调用时出现的超时错误及其解决方法,包括配置 Ribbon 和 Hystrix 的超时时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目前,在Spring cloud 中服务之间通过restful方式调用有两种方式
- restTemplate+Ribbon
- feign

https://blog.youkuaiyun.com/jrn1012/article/details/77837658

(1) feign.RetryableException: Read timed out executing POST http://**
at feign.FeignException.errorExecuting(FeignException.java:67)
at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:104)
at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:76)
at feign.ReflectiveFeign FeignInvocationHandler.invoke(ReflectiveFeign.java:103)atcom.sun.proxy. F e i g n I n v o c a t i o n H a n d l e r . i n v o k e ( R e f l e c t i v e F e i g n . j a v a : 103 ) a t c o m . s u n . p r o x y . 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)atfeign.Client D e f a u l t . c o n v e r t R e s p o n s e ( C l i e n t . j a v a : 152 ) a t f e i g n . C l i e n t Default.execute(Client.java:74)

feign的调用分两层,ribbon的调用和hystrix的调用,高版本的hystrix默认是关闭的,所以设置ribbon即可

请求处理的超时时间
ribbon.ReadTimeout: 120000
请求连接的超时时间
ribbon.ConnectTimeout: 30000

## feign.hystrix.enabled: true
##hystrix 熔断机制
##hystrix:
##shareSecurityContext: true
##command:
##default:
##circuitBreaker:
## sleepWindowInMilliseconds: 100000
##forceClosed: true
##execution:
##isolation:
##thread:
## timeoutInMilliseconds: 600000
如果开启hystrix,hystrix的超时报错如下,此时就得设置hystrix

com.netflix.hystrix.exception.HystrixRuntimeException: FeignDemo#demo() timed-out and no fallback available.
at com.netflix.hystrix.AbstractCommand 22.call(AbstractCommand.java:819)atcom.netflix.hystrix.AbstractCommand 22. c a l l ( A b s t r a c t C o m m a n d . j a v a : 819 ) a t c o m . n e t f l i x . h y s t r i x . A b s t r a c t C o m m a n d 22.call(AbstractCommand.java:804)
at rx.internal.operators.OperatorOnErrorResumeNextViaFunction 4.onError(OperatorOnErrorResumeNextViaFunction.java:140)atrx.internal.operators.OnSubscribeDoOnEach 4. o n E r r o r ( O p e r a t o r O n E r r o r R e s u m e N e x t V i a F u n c t i o n . j a v a : 140 ) a t r x . i n t e r n a l . o p e r a t o r s . O n S u b s c r i b e D o O n E a c h DoOnEachSubscriber.onError(OnSubscribeDoOnEach.java:87)
at rx.internal.operators.OnSubscribeDoOnEach DoOnEachSubscriber.onError(OnSubscribeDoOnEach.java:87)atcom.netflix.hystrix.AbstractCommand D o O n E a c h S u b s c r i b e r . o n E r r o r ( O n S u b s c r i b e D o O n E a c h . j a v a : 87 ) a t c o m . n e t f l i x . h y s t r i x . A b s t r a c t C o m m a n d DeprecatedOnFallbackHookApplication 1.onError(AbstractCommand.java:1472)atcom.netflix.hystrix.AbstractCommand 1. o n E r r o r ( A b s t r a c t C o m m a n d . j a v a : 1472 ) a t c o m . n e t f l i x . h y s t r i x . A b s t r a c t C o m m a n d FallbackHookApplication 1.onError(AbstractCommand.java:1397)atrx.internal.operators.OnSubscribeDoOnEach 1. o n E r r o r ( A b s t r a c t C o m m a n d . j a v a : 1397 ) a t r x . i n t e r n a l . o p e r a t o r s . O n S u b s c r i b e D o O n E a c h DoOnEachSubscriber.onError(OnSubscribeDoOnEach.java:87)
at rx.observers.Subscribers 5.onError(Subscribers.java:230)Causedby:java.util.concurrent.TimeoutExceptionatcom.netflix.hystrix.AbstractCommand.handleTimeoutViaFallback(AbstractCommand.java:997)atcom.netflix.hystrix.AbstractCommand.access 5. o n E r r o r ( S u b s c r i b e r s . j a v a : 230 ) C a u s e d b y : j a v a . u t i l . c o n c u r r e n t . T i m e o u t E x c e p t i o n a t c o m . n e t f l i x . h y s t r i x . A b s t r a c t C o m m a n d . h a n d l e T i m e o u t V i a F a l l b a c k ( A b s t r a c t C o m m a n d . j a v a : 997 ) a t c o m . n e t f l i x . h y s t r i x . A b s t r a c t C o m m a n d . a c c e s s 500(AbstractCommand.java:60)
at com.netflix.hystrix.AbstractCommand 12.call(AbstractCommand.java:610)atcom.netflix.hystrix.AbstractCommand 12. c a l l ( A b s t r a c t C o m m a n d . j a v a : 610 ) a t c o m . n e t f l i x . h y s t r i x . A b s t r a c t C o m m a n d 12.call(AbstractCommand.java:601)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值