国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

Spring Cloud重試機制與各組件重試的示例分析

發(fā)布時(shí)間:2021-09-04 11:55 來(lái)源:億速云 閱讀:0 作者:小新 欄目: 開(kāi)發(fā)技術(shù)

這篇文章將為大家詳細講解有關(guān)Spring Cloud重試機制與各組件重試的示例分析,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

SpringCloud重試機制配置

首先聲明一點(diǎn),這里的重試并不是報錯以后的重試,而是客戶(hù)端發(fā)現遠程請求實(shí)例不可到達后,去重試其他實(shí)例。

@Bean
@LoadBalanced
RestTemplate restTemplate() {
  HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
  httpRequestFactory.setReadTimeout(5000);
  httpRequestFactory.setConnectTimeout(5000);
  return new RestTemplate(httpRequestFactory);
}

feign重試機制

feign默認是通過(guò)自己包下的Retryer進(jì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中各組件的重試

最近挺多童鞋問(wèn)我如何配置Spring Cloud xxx組件的重試。本篇進(jìn)行一個(gè)總結。

Spring Cloud中的重試機制應該說(shuō)是比較混亂的,不同的版本有一定區別,實(shí)現也不大一樣,好在Spring Cloud Camden之后已經(jīng)基本穩定下來(lái),Dalston中又進(jìn)行了一些改進(jìn),詳情暫且不表。

下面我們來(lái)詳細探討。

筆者使用的版本是 Spring Cloud Dalston SR4 ,同樣適應于Edgware 以及更高版本,對于Dalston 此前的版本,本文不做討論,大家可自行研究。

Ribbon+RestTemplate的重試

對于整合了Ribbon的RestTemplate,例如一個(gè)RestTemplate添加了@LoadBalanced 注解:

@Bean
@LoadBalanced
public RestTemplate restTemplate() {
 SimpleClientHttpRequestFactory simpleClientHttpRequestFactory = new SimpleClientHttpRequestFactory();
 simpleClientHttpRequestFactory.setConnectTimeout(1000);
 simpleClientHttpRequestFactory.setReadTimeout(1000);
 return new RestTemplate(simpleClientHttpRequestFactory);
}

在此基礎上,使用如下配置,即可實(shí)現重試:

spring:
 cloud:
 loadbalancer:
  retry:
  enabled: true
ribbon:
 # 同一實(shí)例最大重試次數,不包括首次調用
 MaxAutoRetries: 1
 # 重試其他實(shí)例的最大重試次數,不包括首次所選的server
 MaxAutoRetriesNextServer: 2
 # 是否所有操作都進(jìn)行重試
 OkToRetryOnAllOperations: false

Feign的重試

Feign本身也具備重試能力,在早期的Spring Cloud中,Feign使用的是 feign.Retryer.Default#Default()  ,重試5次。但Feign整合了Ribbon,Ribbon也有重試的能力,此時(shí),就可能會(huì )導致行為的混亂。

Spring Cloud意識到了此問(wèn)題,因此做了改進(jìn),將Feign的重試改為 feign.Retryer#NEVER_RETRY  ,如需使用Feign的重試,只需使用Ribbon的重試配置即可。因此,對于Camden以及以后的版本,Feign的重試可使用如下屬性進(jìn)行配置:

ribbon:
 MaxAutoRetries: 1
 MaxAutoRetriesNextServer: 2
 OkToRetryOnAllOperations: false

相關(guān)Issue可參考:https://github.com/spring-cloud/spring-cloud-netflix/issues/467

Zuul的重試

配置:

zuul:
 # 開(kāi)啟Zuul的重試
 retryable: true
ribbon:
 MaxAutoRetries: 1
 MaxAutoRetriesNextServer: 2
 OkToRetryOnAllOperations: false

上面我們使用 zuul.retryable=true 對Zuul全局開(kāi)啟了重試,事實(shí)上,也可對指定路由開(kāi)啟/關(guān)閉重試:

zuul.routes.<routename>.retryable=true

局部配置優(yōu)先級更高。

基于HTTP響應碼重試

clientName:
 ribbon:
  retryableStatusCodes: 404,502

注意點(diǎn):

Hystrix的超時(shí)時(shí)間必須大于超時(shí)的時(shí)間,否則,一旦Hystrix超時(shí),就沒(méi)辦法繼續重試了。

一般來(lái)說(shuō),不建議將ribbon.OkToRetryOnAllOperations 設為true。因為一旦啟用該配置,則表示重試任何操作,包括POST請求,而由于緩存了請求體,此時(shí)可能會(huì )影響的資源。

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。

野花WWW成人免费视频| 精品久久久久香蕉网| 高清性色生活片老熟女| 欧美精品久久天天躁| 亚洲AV无码一区东京热| 欧美A级在线现免费观看|