Spring Cloud重試機(jī)制與各組件的重試總結(jié)
SpringCloud重試機(jī)制配置
首先聲明一點(diǎn),這里的重試并不是報(bào)錯以后的重試,而是負(fù)載均衡客戶端發(fā)現(xiàn)遠(yuǎn)程請求實(shí)例不可到達(dá)后,去重試其他實(shí)例。
@Bean @LoadBalanced RestTemplate restTemplate() { HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory(); httpRequestFactory.setReadTimeout(5000); httpRequestFactory.setConnectTimeout(5000); return new RestTemplate(httpRequestFactory); }
feign重試機(jī)制
feign默認(rèn)是通過自己包下的Retryer進(jìn)行重試配置,默認(rèn)是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取消重試
@Bean Retryer feignRetryer() { return Retryer.NEVER_RETRY; }
feign請求超時設(shè)置
@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組件的重試。本篇進(jìn)行一個總結(jié)。
Spring Cloud中的重試機(jī)制應(yīng)該說是比較混亂的,不同的版本有一定區(qū)別,實(shí)現(xiàn)也不大一樣,好在Spring Cloud Camden之后已經(jīng)基本穩(wěn)定下來,Dalston中又進(jìn)行了一些改進(jìn),詳情暫且不表。
下面我們來詳細(xì)探討。
筆者使用的版本是 Spring Cloud Dalston SR4 ,同樣適應(yīng)于Edgware 以及更高版本,對于Dalston 此前的版本,本文不做討論,大家可自行研究。
Ribbon+RestTemplate的重試
對于整合了Ribbon的RestTemplate,例如一個RestTemplate添加了@LoadBalanced 注解:
@Bean @LoadBalanced public RestTemplate restTemplate() { SimpleClientHttpRequestFactory simpleClientHttpRequestFactory = new SimpleClientHttpRequestFactory(); simpleClientHttpRequestFactory.setConnectTimeout(1000); simpleClientHttpRequestFactory.setReadTimeout(1000); return new RestTemplate(simpleClientHttpRequestFactory); }
在此基礎(chǔ)上,使用如下配置,即可實(shí)現(xiàn)重試:
spring: cloud: loadbalancer: retry: enabled: true ribbon: # 同一實(shí)例最大重試次數(shù),不包括首次調(diào)用 MaxAutoRetries: 1 # 重試其他實(shí)例的最大重試次數(shù),不包括首次所選的server MaxAutoRetriesNextServer: 2 # 是否所有操作都進(jìn)行重試 OkToRetryOnAllOperations: false
Feign的重試
Feign本身也具備重試能力,在早期的Spring Cloud中,F(xiàn)eign使用的是 feign.Retryer.Default#Default()
,重試5次。但Feign整合了Ribbon,Ribbon也有重試的能力,此時,就可能會導(dǎo)致行為的混亂。
Spring Cloud意識到了此問題,因此做了改進(jìn),將Feign的重試改為 feign.Retryer#NEVER_RETRY
,如需使用Feign的重試,只需使用Ribbon的重試配置即可。因此,對于Camden以及以后的版本,F(xiàn)eign的重試可使用如下屬性進(jìn)行配置:
ribbon: MaxAutoRetries: 1 MaxAutoRetriesNextServer: 2 OkToRetryOnAllOperations: false
相關(guān)Issue可參考:https://github.com/spring-cloud/spring-cloud-netflix/issues/467
Zuul的重試
配置:
zuul: # 開啟Zuul的重試 retryable: true ribbon: MaxAutoRetries: 1 MaxAutoRetriesNextServer: 2 OkToRetryOnAllOperations: false
上面我們使用 zuul.retryable=true
對Zuul全局開啟了重試,事實(shí)上,也可對指定路由開啟/關(guān)閉重試:
zuul.routes.<routename>.retryable=true
局部配置優(yōu)先級更高。
基于HTTP響應(yīng)碼重試
clientName: ribbon: retryableStatusCodes: 404,502
注意點(diǎn):
Hystrix的超時時間必須大于超時的時間,否則,一旦Hystrix超時,就沒辦法繼續(xù)重試了。
一般來說,不建議將ribbon.OkToRetryOnAllOperations
設(shè)為true。因?yàn)橐坏﹩⒂迷撆渲?,則表示重試任何操作,包括POST請求,而由于緩存了請求體,此時可能會影響服務(wù)器的資源。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
ConcurrentHashMap是如何實(shí)現(xiàn)線程安全的你知道嗎
這篇文章主要介紹了ConcurrentHashMap是如何實(shí)現(xiàn)線程安全的你知道嗎,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10Java實(shí)現(xiàn)將txt/word/pdf轉(zhuǎn)成圖片并在線預(yù)覽的功能
本文將基于aspose-words(用于txt、word轉(zhuǎn)圖片),pdfbox(用于pdf轉(zhuǎn)圖片),封裝成一個工具類來實(shí)現(xiàn)txt、word、pdf等文件轉(zhuǎn)圖片的需求并實(shí)現(xiàn)在線預(yù)覽功能,需要的可以參考一下2023-05-05SpringBoot+Prometheus+Grafana實(shí)現(xiàn)應(yīng)用監(jiān)控和報(bào)警的詳細(xì)步驟
這篇文章主要介紹了SpringBoot+Prometheus+Grafana實(shí)現(xiàn)應(yīng)用監(jiān)控和報(bào)警的詳細(xì)步驟,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02springboot2.0整合logback日志的詳細(xì)代碼
這篇文章主要介紹了springboot2.0整合logback日志的應(yīng)用場景分析,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02application作用域?qū)崿F(xiàn)用戶登錄擠掉之前登錄用戶代碼
這篇文章主要介紹了application作用域?qū)崿F(xiàn)用戶登錄擠掉之前登錄用戶代碼,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11