欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Cloud重試機(jī)制與各組件的重試總結(jié)

 更新時間:2017年11月29日 10:40:45   作者:周立_itmuch  
這篇文章主要給大家介紹了關(guān)于Spring Cloud中重試機(jī)制與各組件的重試的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。

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)文章

最新評論