springcloud重试机制配置
首先声明一点,这里的重试并不是报错以后的重试,而是负载均衡客户端发现远程请求实例不可到达后,去重试其他实例。
|
1
2
3
4
5
6
7
8
|
@bean
@loadbalanced
resttemplate 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
|
@bean
retryer feignretryer() {
return retryer.never_retry;
}
|
feign请求超时设置
|
1
2
3
4
5
6
7
|
@bean
request.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 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
@loadbalanced
public 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: true
ribbon:
# 同一实例最大重试次数,不包括首次调用
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: true
ribbon:
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请求,而由于缓存了请求体,此时可能会影响服务器的资源。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对快网idc的支持。
原文链接:http://www.itmuch.com/spring-cloud-sum/spring-cloud-retry/
相关文章
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-05-25 97
-
2025-05-29 66
-
2025-05-29 95
-
2025-06-04 99
-
Debian 8或Debian 9(64 位)安装 .NET Core
2025-05-29 109



