一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Spring Cloud重試機制與各組件的重試總結

Spring Cloud重試機制與各組件的重試總結

2021-02-23 11:19周立_itmuch Java教程

這篇文章主要給大家介紹了關于Spring Cloud中重試機制與各組件的重試的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。

springcloud重試機制配置

首先聲明一點,這里的重試并不是報錯以后的重試,而是負載均衡客戶端發現遠程請求實例不可到達后,去重試其他實例。

Spring Cloud重試機制與各組件的重試總結

?
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);
}

Spring Cloud重試機制與各組件的重試總結

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/

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产一卡2卡3卡4卡公司科普 | 美日毛片| 桥本有菜作品在线 | a级aaaaaaaa毛片| 爱情岛论坛亚洲永久入口口 | 古装一级无遮挡毛片免费观看 | 日韩一区在线播放 | 青草青草久热精品视频在线网站 | 纲手被鸣人插 | 五月香婷婷 | 免费国产网站 | sihu国产午夜精品一区二区三区 | aⅴ免费视频 | 欧美日韩国产在线人成 | 无码区国产区在线播放 | 婷婷国产在线 | 欧美视频黑鬼大战白妞 | ai换脸杨颖被啪在线观看 | 成人影院在线观看视频 | 狠狠色综合久久婷婷色天使 | 99久久久久国产精品免费 | 无码人妻99久久密AV | 第一福利在线观看永久视频 | aaa毛片视频免费观看 | 亚洲男人的天堂网站 | 色狼屋| 丝袜足控免费网站xx动漫漫画 | 午夜理论片日本中文在线 | 4tube欧美高清 | 天堂成人影院 | 欧美高清3dfreexxxx性 | 四虎影视紧急入口地址大全 | 青青国产在线视频 | 免费成年网 | 午夜精品久久久内射近拍高清 | 亚洲一区二区精品视频 | 狠狠撸在线影院 | 国产91网站在线观看 | 美女在线看永久免费网址 | 猛男强攻变sao货 | 被夫上司侵犯了中文字幕 |