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中各組件的重試
最近挺多童鞋問我如何配置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請求,而由于緩存了請求體,此時可能會影響服務器的資源。
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:http://www.itmuch.com/spring-cloud-sum/spring-cloud-retry/