ribbon的重试机制
Posted dalianpai
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ribbon的重试机制相关的知识,希望对你有一定的参考价值。
1.在调用端的pom.xml加入重试jar
<dependency> <groupId>org.springframework.retry</groupId> <artifactId>spring-retry</artifactId> </dependency>
2.修改yml文件
service-product:
ribbon:
ConnectTimeout: 250 # Ribbon的连接超时时间
ReadTimeout: 1000 # Ribbon的数据读取超时时间
OkToRetryOnAllOperations: true # 是否对所有操作都进行重试
MaxAutoRetriesNextServer: 1 # 切换实例的重试次数
MaxAutoRetries: 1 # 对当前实例的重试次数
logging:
level:
root: debug
3.关闭一个服务,开始调用,然后查看日志
2020-02-05 15:31:29.914 DEBUG 24644 --- [nio-9012-exec-4] o.s.web.client.RestTemplate : HTTP GET http://service-product/product/1
2020-02-05 15:31:29.914 DEBUG 24644 --- [nio-9012-exec-4] o.s.web.client.RestTemplate : Accept=[application/json, application/*+json]
2020-02-05 15:31:29.914 DEBUG 24644 --- [nio-9012-exec-4] c.n.loadbalancer.ZoneAwareLoadBalancer : Zone aware logic disabled or there is only one zone
2020-02-05 15:31:29.915 DEBUG 24644 --- [nio-9012-exec-4] o.s.retry.support.RetryTemplate : Retry: count=0
2020-02-05 15:31:29.915 DEBUG 24644 --- [nio-9012-exec-4] s.n.www.protocol.http.HttpURLConnection : sun.net.www.MessageHeader@357d23b55 pairs: {GET /product/1 HTTP/1.1: null}{Accept: application/json, application/*+json}{User-Agent: Java/1.8.0_121}{Host: 10.9.9.139:9011}{Connection: keep-alive}
2020-02-05 15:31:31.920 DEBUG 24644 --- [nio-9012-exec-4] o.s.c.n.r.RibbonLoadBalancedRetryPolicy : 10.9.9.139:9011 RetryCount: 1 Successive Failures: 2 CircuitBreakerTripped:false
2020-02-05 15:31:31.920 DEBUG 24644 --- [nio-9012-exec-4] o.s.retry.support.RetryTemplate : Checking for rethrow: count=1
2020-02-05 15:31:31.920 DEBUG 24644 --- [nio-9012-exec-4] o.s.retry.support.RetryTemplate : Retry: count=1
2020-02-05 15:31:33.923 DEBUG 24644 --- [nio-9012-exec-4] o.s.c.n.r.RibbonLoadBalancedRetryPolicy : 10.9.9.139:9011 RetryCount: 2 Successive Failures: 4 CircuitBreakerTripped:true
2020-02-05 15:31:33.923 DEBUG 24644 --- [nio-9012-exec-4] c.n.loadbalancer.ZoneAwareLoadBalancer : Zone aware logic disabled or there is only one zone
2020-02-05 15:31:33.923 DEBUG 24644 --- [nio-9012-exec-4] o.s.retry.support.RetryTemplate : Checking for rethrow: count=2
2020-02-05 15:31:33.923 DEBUG 24644 --- [nio-9012-exec-4] o.s.retry.support.RetryTemplate : Retry: count=2
2020-02-05 15:31:33.924 DEBUG 24644 --- [nio-9012-exec-4] s.n.www.protocol.http.HttpURLConnection : sun.net.www.MessageHeader@4145dbbd5 pairs: {GET /product/1 HTTP/1.1: null}{Accept: application/json, application/*+json}{User-Agent: Java/1.8.0_121}{Host: 10.9.9.139:9001}{Connection: keep-alive}
2020-02-05 15:31:33.932 DEBUG 24644 --- [nio-9012-exec-4] s.n.www.protocol.http.HttpURLConnection : sun.net.www.MessageHeader@1df928ab6 pairs: {null: HTTP/1.1 200}{Content-Type: application/json;charset=UTF-8}{Transfer-Encoding: chunked}{Date: Wed, 05 Feb 2020 07:31:33 GMT}{Keep-Alive: timeout=60}{Connection: keep-alive}
2020-02-05 15:31:33.932 DEBUG 24644 --- [nio-9012-exec-4] o.s.web.client.RestTemplate : Response 200 OK
2020-02-05 15:31:33.932 DEBUG 24644 --- [nio-9012-exec-4] o.s.web.client.RestTemplate : Reading to [com.topcheer.order.entity.Product]
2020-02-05 15:31:33.933 DEBUG 24644 --- [nio-9012-exec-4] m.m.a.RequestResponseBodyMethodProcessor : Using ‘application/json;q=0.8‘, given [text/html, application/xhtml+xml, image/webp, image/apng, application/signed-exchange;v=b3, application/xml;q=0.9, */*;q=0.8] and supported [application/json, application/*+json, application/json, application/*+json]
2020-02-05 15:31:33.933 DEBUG 24644 --- [nio-9012-exec-4] m.m.a.RequestResponseBodyMethodProcessor : Writing [Product(id=1, productName=访问的服务地址:10.9.9.139:9001, status=1, price=1000.00, productDesc=抗曹, caption= (truncated)...]
2020-02-05 15:31:33.934 DEBUG 24644 --- [nio-9012-exec-4] o.j.s.OpenEntityManagerInViewInterceptor : Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
2020-02-05 15:31:33.934 DEBUG 24644 --- [nio-9012-exec-4] o.s.web.servlet.DispatcherServlet : Completed 200 OK
发现在尝试一次以后,又切换到9001了,不过ribbon用的不多,用的都是feign。
以上是关于ribbon的重试机制的主要内容,如果未能解决你的问题,请参考以下文章