Spring Cloud重试机制与各组件的重试总结
SpringCloud重试机制配置
首先声明一点,这里的重试并不是报错以后的重试,而是负载均衡客户端发现远程请求实例不可到达后,去重试其他实例。

|
1
2
3
4
5
6
7
8
|
@Bean@LoadBalancedRestTemplate restTemplate() { HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory(); httpRequestFactory.setReadTimeout(5000); httpRequestFactory.setConnectTimeout(5000); return new RestTemplate(httpRequestFactory);} |

feign重试机制
feign默认是通过自己包下的Retryer进行重试配置,默认是5次
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
package feign;import static java.util.concurrent.TimeUnit.SECONDS;/** * Cloned for each invocation to {@link Client#execute(Request, feign.Request.Options)}. * Implementations may keep state to determine if retry operations should continue or not. */public interface Retryer extends Cloneable { /** * if retry is permitted, return (possibly after sleeping). Otherwise propagate the exception. */ void continueOrPropagate(RetryableException e); Retryer clone(); public static class Default implements Retryer { private final int maxAttempts; private final long period; private final long maxPeriod; int attempt; long sleptForMillis; public Default() { this(100, SECONDS.toMillis(1), 5); } public Default(long period, long maxPeriod, int maxAttempts) { this.period = period; this.maxPeriod = maxPeriod; this.maxAttempts = maxAttempts; this.attempt = 1; } |
feign取消重试
|
1
2
3
4
|
@BeanRetryer feignRetryer() {return Retryer.NEVER_RETRY;} |
feign请求超时设置
|
1
2
3
4
5
6
7
|
@BeanRequest.Options requestOptions(ConfigurableEnvironment env){ int ribbonReadTimeout = env.getProperty("ribbon.ReadTimeout", int.class, 6000); int ribbonConnectionTimeout = env.getProperty("ribbon.ConnectTimeout", int.class, 3000); return new Request.Options(ribbonConnectionTimeout, ribbonReadTimeout);} |
Spring Cloud中各组件的重试
最近挺多童鞋问我如何配置Spring Cloud xxx组件的重试。本篇进行一个总结。
Spring Cloud中的重试机制应该说是比较混乱的,不同的版本有一定区别,实现也不大一样,好在Spring Cloud Camden之后已经基本稳定下来,Dalston中又进行了一些改进,详情暂且不表。
下面我们来详细探讨。
笔者使用的版本是 Spring Cloud Dalston SR4 ,同样适应于Edgware 以及更高版本,对于Dalston 此前的版本,本文不做讨论,大家可自行研究。
Ribbon+RestTemplate的重试
对于整合了Ribbon的RestTemplate,例如一个RestTemplate添加了@LoadBalanced 注解:
|
1
2
3
4
5
6
7
8
|
@Bean@LoadBalancedpublic RestTemplate restTemplate() { SimpleClientHttpRequestFactory simpleClientHttpRequestFactory = new SimpleClientHttpRequestFactory(); simpleClientHttpRequestFactory.setConnectTimeout(1000); simpleClientHttpRequestFactory.setReadTimeout(1000); return new RestTemplate(simpleClientHttpRequestFactory);} |
在此基础上,使用如下配置,即可实现重试:
|
1
2
3
4
5
6
7
8
9
10
11
12
|
spring: cloud: loadbalancer: retry: enabled: trueribbon: # 同一实例最大重试次数,不包括首次调用 MaxAutoRetries: 1 # 重试其他实例的最大重试次数,不包括首次所选的server MaxAutoRetriesNextServer: 2 # 是否所有操作都进行重试 OkToRetryOnAllOperations: false |
Feign的重试
Feign本身也具备重试能力,在早期的Spring Cloud中,Feign使用的是 feign.Retryer.Default#Default() ,重试5次。但Feign整合了Ribbon,Ribbon也有重试的能力,此时,就可能会导致行为的混乱。
Spring Cloud意识到了此问题,因此做了改进,将Feign的重试改为 feign.Retryer#NEVER_RETRY ,如需使用Feign的重试,只需使用Ribbon的重试配置即可。因此,对于Camden以及以后的版本,Feign的重试可使用如下属性进行配置:
|
1
2
3
4
|
ribbon: MaxAutoRetries: 1 MaxAutoRetriesNextServer: 2 OkToRetryOnAllOperations: false |
相关Issue可参考:https://github.com/spring-cloud/spring-cloud-netflix/issues/467
Zuul的重试
配置:
|
1
2
3
4
5
6
7
|
zuul: # 开启Zuul的重试 retryable: trueribbon: MaxAutoRetries: 1 MaxAutoRetriesNextServer: 2 OkToRetryOnAllOperations: false |
上面我们使用 zuul.retryable=true 对Zuul全局开启了重试,事实上,也可对指定路由开启/关闭重试:
|
1
|
zuul.routes.<routename>.retryable=true |
局部配置优先级更高。
基于HTTP响应码重试
|
1
2
3
|
clientName: ribbon: retryableStatusCodes: 404,502 |
注意点:
Hystrix的超时时间必须大于超时的时间,否则,一旦Hystrix超时,就没办法继续重试了。
一般来说,不建议将ribbon.OkToRetryOnAllOperations 设为true。因为一旦启用该配置,则表示重试任何操作,包括POST请求,而由于缓存了请求体,此时可能会影响服务器的资源。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。
Spring Cloud重试机制与各组件的重试总结的更多相关文章
- spring cloud要点简介及常用组件
spring cloud基于spring boot spring cloud是通过包装其他技术框架实现的,例如OSS组件,实现了一套通过基于注解.java配置和基于模板开发的微服务框架. spring ...
- Spring Cloud Zuul网关 Filter、熔断、重试、高可用的使用方式。
时间过的很快,写springcloud(十):服务网关zuul初级篇还在半年前,现在已经是2018年了,我们继续探讨Zuul更高级的使用方式. 上篇文章主要介绍了Zuul网关使用模式,以及自动转发机制 ...
- 【Spring Cloud】客户端负载均衡组件——Ribbon(三)
一.负载均衡 负载均衡技术是提高系统可用性.缓解网络压力和处理能力扩容的重要手段之一. 负载均衡可以分为服务器负载均衡和客户端负载均衡,服务器负载均衡由服务器实现,客户端只需正常访问:客户端负载均衡技 ...
- Spring Cloud 功能使用的层面组件(一)
来源:赤峰seo 实际上,Spring Cloud 是一个全家桶式的技术栈,它包含了很多组件.本文先从最核心的几个组件,也就是 Eureka.Ribbon.Feign.Hystrix.Zuul 入手 ...
- springcloud(十七):服务网关 Spring Cloud GateWay 熔断、限流、重试
上篇文章介绍了 Gataway 和注册中心的使用,以及 Gataway 中 Filter 的基本使用,这篇文章我们将继续介绍 Filter 的一些常用功能. 修改请求路径的过滤器 StripPrefi ...
- Spring Cloud(2)主要组件应用实例
SpringCloud SpringCloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.负载均衡.微代理.事件总线.全局锁.决策竞选.分布式会话等等.它运行 ...
- Spring Cloud 熔断机制 -- 断路器
Spring Cloud 入门教程(七): 熔断机制 -- 断路器 对断路器模式不太清楚的话,可以参看另一篇博文:断路器(Curcuit Breaker)模式,下面直接介绍Spring Cloud的断 ...
- Spring Cloud中五花八门的分布式组件我到底该怎么学
分布式架构的演进 在软件行业,一个应用服务随着功能越来越复杂,用户量越来越大,尤其是互联网行业流量爆发式的增长,导致我们需要不断的重构应用的结构来支撑庞大的用户量,最终从一个简单的系统主键演变成了一个 ...
- Spring Cloud(十一):Spring Cloud Zuul网关 Filter、熔断、重试、高可用的使用方式
上篇文章主要介绍了Zuul网关使用模式,以及自动转发机制,但其实Zuul还有更多的应用场景,比如:鉴权.流量转发.请求统计等等,这些功能都可以使用Zuul来实现. Zuul的核心 Filter是Zuu ...
随机推荐
- 数学图形(2.7)sphere sine wave
在球上以SIN曲线的轨迹游走. #http://www.mathcurve.com/courbes3d/couronnetangentoidale/couronnetangentoidale.shtm ...
- Mac下的 /private 是个什么目录?
Mac下的 /private 是个什么目录? 学习了:https://www.zhihu.com/question/19582264/answer/125522314 有图有真想
- Wifidog及认证过程初分析
Wifidog初分析 一.综述 wifidog是搭建无线热点认证系统的解决方案之一,他比nocat.nodog更适合互联网营销思路.常见的使用在openwrt系统上,它实现了路由器和认证服务器的数据交 ...
- photoshop cs6 Mac版本
地址:http://trials2.adobe.com/AdobeProducts/PHSP/13/osx10/Photoshop_13_LS3.dmg 说明:http://www.nowmac.co ...
- Discuz常见小问题-如何设置163邮箱注册验证
参考网址: https://jingyan.baidu.com/album/c843ea0b804a6e77931e4aa7.html?picindex=3 http://www.discuz.net ...
- 【数据压缩】LZW算法原理与源代码解析
转载请注明出处:http://blog.csdn.net/luoshixian099/article/details/50331883 <勿在浮沙筑高台> LZW压缩算法原理很easy,因 ...
- 算法笔记_026:折半查找(Java)
目录 1 问题描述 2 解决方案 2.1 递归法 2.2 迭代法 1 问题描述 首先,了解一下何为折半查找?此处,借用<算法设计与分析基础>第三版上一段文字介绍: 2 解决方案 2.1 递 ...
- input 禁止 复制 粘贴 剪切 操作
1.代码 <Input onCopy={(e)=>{ // 禁止拷贝 e.preventDefault(); }} onPaste={(e)=>{ // 禁止粘贴 e.prevent ...
- Android SDK镜像的介绍使用【转发】
由于一些原因,Google相关很多服务都无法访问,所以在很多时候我们SDK也无法升级,当然通过技术手段肯定可以解决,但是比较麻烦,而且下载速度也不怎么样. 这里笔者介绍一个国内的Android镜像站, ...
- myDate97用法
myDate97用法 CreateTime--2017年5月12日11:00:32Author:Marydon 一.基本用法 官网链接:http://www.my97.net/index.asp ...