SpringCloud重試機製配置
首先聲明一點,這裡的重試並不是報錯以後的重試,而是負載均衡客戶端發現遠程請求實例不可到達後,去重試其他實例。
@Bean@LoadBalancedRestTemplate restTemplate() { HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory(); httpRequestFactory.setReadTimeout(5000); httpRequestFactory.setConnectTimeout(5000); return new RestTemplate(httpRequestFactory);}feign重試機制
feign默認是通過自己包下的Retryer進行重試配置,默認是5次
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取消重試
@BeanRetryer feignRetryer() {return Retryer.NEVER_RETRY;}feign請求超時設置
@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 註解:
@Bean@LoadBalancedpublic RestTemplate restTemplate() { SimpleClientHttpRequestFactory simpleClientHttpRequestFactory = new SimpleClientHttpRequestFactory(); simpleClientHttpRequestFactory.setConnectTimeout(1000); simpleClientHttpRequestFactory.setReadTimeout(1000); return new RestTemplate(simpleClientHttpRequestFactory);}在此基礎上,使用如下配置,即可實現重試:
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的重試可使用如下屬性進行配置:
ribbon: MaxAutoRetries: 1 MaxAutoRetriesNextServer: 2 OkToRetryOnAllOperations: false
相關Issue可參考:https://github.com/spring-cloud/spring-cloud-netflix/issues/467
Zuul的重試
配置:
zuul: # 開啟Zuul的重試retryable: trueribbon: MaxAutoRetries: 1 MaxAutoRetriesNextServer: 2 OkToRetryOnAllOperations: false
上面我們使用zuul.retryable=true對Zuul全局開啟了重試,事實上,也可對指定路由開啟/關閉重試:
zuul.routes.<routename>.retryable=true
局部配置優先級更高。
基於HTTP響應碼重試
clientName: ribbon: retryableStatusCodes: 404,502
注意點:
Hystrix的超時時間必須大於超時的時間,否則,一旦Hystrix超時,就沒辦法繼續重試了。
一般來說,不建議將ribbon.OkToRetryOnAllOperations設為true。因為一旦啟用該配置,則表示重試任何操作,包括POST請求,而由於緩存了請求體,此時可能會影響服務器的資源。
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對武林網的支持。