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

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

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

服務(wù)器之家 - 編程語言 - Java教程 - Spring Boot中如何使用斷路器詳解

Spring Boot中如何使用斷路器詳解

2021-05-28 11:35只喝牛奶的殺手 Java教程

這篇文章主要給大家介紹了關(guān)于Spring Boot中如何使用斷路器的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前言

隨著使用 spring 進(jìn)行開發(fā)的個人和企業(yè)越來越多,spring 也慢慢從一個單一簡潔的小框架變成一個大而全的開源軟件,spring 的邊界不斷的進(jìn)行擴充,到了后來 spring 幾乎可以做任何事情了,市面上主流的開源軟件、中間件都有 spring 對應(yīng)組件支持,人們在享用 spring 的這種便利之后,也遇到了一些問題。

斷路器本身是電路上的一種過載保護(hù)裝置,當(dāng)線路中有電器發(fā)生短路時,它能夠及時的切斷故障電路以防止嚴(yán)重后果發(fā)生。通過服務(wù)熔斷(也可以稱為斷路)、降級、限流(隔離)、異步rpc等手段控制依賴服務(wù)的延遲與失敗,防止整個服務(wù)雪崩。一個斷路器可以裝飾并且檢測了一個受保護(hù)的功能調(diào)用。根據(jù)當(dāng)前的狀態(tài)決定調(diào)用時被執(zhí)行還是回退。通常情況下,一個斷路器實現(xiàn)三種類型的狀態(tài):open、half-open以及closed:

  • closed狀態(tài)的調(diào)用被執(zhí)行,事務(wù)度量被存儲,這些度量是實現(xiàn)一個健康策略所必備的。
  • 倘若系統(tǒng)健康狀況變差,斷路器就處在open狀態(tài)。此種狀態(tài)下,所有調(diào)用會被立即回退并且不會產(chǎn)生新的調(diào)用。open狀態(tài)的目的是給服務(wù)器端回復(fù)和處理問題的時間。
  • 一旦斷路器進(jìn)入一個open狀態(tài),超時計時器開始計時。如果計時器超時,斷路器切換到half-open狀態(tài)。在half-open狀態(tài)調(diào)用間歇性執(zhí)行以確定問題是否已解決。如果解決,狀態(tài)切換回closed狀態(tài)。

Spring Boot中如何使用斷路器詳解

斷路器背后的基本思想非常簡單。將受保護(hù)的函數(shù)調(diào)用包裝在斷路器對象中,該對象監(jiān)視故障。一旦故障達(dá)到某個閾值,斷路器就會跳閘,并且所有對斷路器的進(jìn)一步調(diào)用都會返回錯誤,而根本不會進(jìn)行受保護(hù)的呼叫。通常,如果斷路器跳閘,您還需要某種監(jiān)控器警報。

Spring Boot中如何使用斷路器詳解

如何快速使用hystrix呢?下面跟著我1234……

1、加入@enablecircuitbreaker注解

?
1
2
3
4
5
6
7
8
@enablecircuitbreaker
@springbootapplication
@enableeurekaclient
@enablefeignclientspublic class droolsappapplication {
 public static void main(string[] args) {
  springapplication.run(droolsappapplication.class, args);
 }
}

hystrix整體執(zhí)行過程,首先,command會調(diào)用run方法,如果run方法超時或者拋出異常,且啟用了降級處理,則調(diào)用getfallback方法進(jìn)行降級;

Spring Boot中如何使用斷路器詳解

2、使用@hystrixcommand注解

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@hystrixcommand(fallbackmethod = "reliable")
 public string readinglist() {
  for (int i = 0; i < 10; i++) {
   try {
    thread.sleep(1000);
   } catch (interruptedexception e) {
    e.printstacktrace();
   }
  }
  return "jinpingmei";
 }
 
 public string reliable() {
  return "you love interesting book";
 }

3、添加引用

?
1
2
compile("org.springframework.cloud:spring-cloud-starter-hystrix")
compile('org.springframework.cloud:spring-cloud-starter-turbine')

4、設(shè)置超時時間

?
1
hystrix.command.default.execution.isolation.thread.timeoutinmilliseconds=5000

執(zhí)行結(jié)果如下:

Spring Boot中如何使用斷路器詳解

正常應(yīng)該返回:

Spring Boot中如何使用斷路器詳解

你不要喜歡jinpingmei,要喜歡有意思的書;這樣使用好舒服啊,@enablecircuitbreaker這個注解就這么強大嗎?

hystrixcommandaspect 通過aop攔截所有的@hystrixcommand注解的方法,從而使得@hystrixcommand能夠集成到spring boot中,

hystrixcommandaspect的關(guān)鍵代碼如下:

1.方法 hystrixcommandannotationpointcut() 定義攔截注解hystrixcommand

 2.方法 hystrixcollapserannotationpointcut()定義攔截注解hystrixcollapser

 3.方法methodsannotatedwithhystrixcommand(…)通過@around(…)攔截所有hystrixcommand和hystrixcollapser注解的方法。詳細(xì)見方法注解

?
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
36
37
38
39
40
41
42
@aspect
public class hystrixcommandaspect {
 
 private static final map<hystrixpointcuttype, metaholderfactory> meta_holder_factory_map;
 
 static {
  meta_holder_factory_map = immutablemap.<hystrixpointcuttype, metaholderfactory>builder()
    .put(hystrixpointcuttype.command, new commandmetaholderfactory())
    .put(hystrixpointcuttype.collapser, new collapsermetaholderfactory())
    .build();
 }
 
 @pointcut("@annotation(com.netflix.hystrix.contrib.javanica.annotation.hystrixcommand)")
 
 public void hystrixcommandannotationpointcut() {
 }
 
 @pointcut("@annotation(com.netflix.hystrix.contrib.javanica.annotation.hystrixcollapser)")
 public void hystrixcollapserannotationpointcut() {
 }
 
 @around("hystrixcommandannotationpointcut() || hystrixcollapserannotationpointcut()")
 public object methodsannotatedwithhystrixcommand(final proceedingjoinpoint joinpoint) throws throwable {
  method method = getmethodfromtarget(joinpoint);
  validate.notnull(method, "failed to get method from joinpoint: %s", joinpoint);
  if (method.isannotationpresent(hystrixcommand.class) && method.isannotationpresent(hystrixcollapser.class)) {
   throw new illegalstateexception("method cannot be annotated with hystrixcommand and hystrixcollapser " +
     "annotations at the same time");
  }
  metaholderfactory metaholderfactory = meta_holder_factory_map.get(hystrixpointcuttype.of(method));
  metaholder metaholder = metaholderfactory.create(joinpoint);
  hystrixinvokable invokable = hystrixcommandfactory.getinstance().create(metaholder);
  executiontype executiontype = metaholder.iscollapserannotationpresent() ?
    metaholder.getcollapserexecutiontype() : metaholder.getexecutiontype();
  object result;
  try {
   result = commandexecutor.execute(invokable, executiontype, metaholder);
  } catch (hystrixbadrequestexception e) {
   throw e.getcause();
  }
  return result;
 }

那么hystrixcommandaspect是如何初始化,是通過hystrixcircuitbreakerconfiguration實現(xiàn)的

?
1
2
3
4
5
6
7
@configuration
public class hystrixcircuitbreakerconfiguration {
 
@bean
public hystrixcommandaspect hystrixcommandaspect() {
return new hystrixcommandaspect();
}

那么誰來觸發(fā)hystrixcircuitbreakerconfiguration執(zhí)行初始化

先看spring-cloud-netflix-core**.jar包的spring.factories里有這段配置,是由注解enablecircuitbreaker觸發(fā)

?
1
2
org.springframework.cloud.client.circuitbreaker.enablecircuitbreaker=\
org.springframework.cloud.netflix.hystrix.hystrixcircuitbreakerconfiguration

那么@enablecircuitbreaker如何觸發(fā)hystrixcircuitbreakerconfiguration

通過源碼查看,此類通過@import初始化enablecircuitbreakerimportselector類

?
1
2
3
4
5
6
7
@target(elementtype.type)
@retention(retentionpolicy.runtime)
@documented
@inherited
@import(enablecircuitbreakerimportselector.class)
public @interface enablecircuitbreaker {
}

enablecircuitbreakerimportselector是springfactoryimportselector子類。此類在初始化后,會執(zhí)行selectimports(annotationmetadata metadata)的方法。此方法會根據(jù)注解啟動的注解(這里指@enablecircuitbreaker)從spring.factories文件中獲取其配置需要初始化@configuration類(這里是org.springframework.cloud.netflix.hystrix.hystrixcircuitbreakerconfiguration),從而最終初始化hystrixcommandaspect 類,從而實現(xiàn)攔截hystrixcommand的功能

以上就是通過@enablecircuitbreake可以開啟hystrix的原理。hystrix用到了觀察者模式abstractcommand.executecommandandobserve()模式,下次我們來深入說一下觀察者模式。歡迎拍磚!

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對服務(wù)器之家的支持。

原文鏈接:http://www.cnblogs.com/viaiu/p/9534153.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 娇妻在床上迎合男人 | 日b在线| 加勒比一本大道香蕉在线视频 | 黑人巨荃大战乌克兰美女 | 日韩亚洲国产激情在线观看 | 91成人啪国产啪永久地址 | 亚洲精品中文 | 国产 国语对白 露脸正在播放 | 九九免费高清在线观看视频 | mm在线 | 423hk四虎| 麻豆小视频在线观看 | 国产成人愉拍免费视频 | 亚洲国产欧美日韩在线一区 | 精品午夜中文字幕熟女人妻在线 | 亚洲精品福利你懂 | 19+韩国女主播激情vip视频在线 | 我和寂寞孕妇的性事 | 国产aⅴ一区二区三区 | 爱爱调教 | 亚洲色图首页 | 精品综合久久久久久88小说 | 美女露全身永久免费网站 | 久久久久青草大香线综合精品 | 禁漫H天堂免费A漫 | 欧美久久一区二区三区 | 外女思春台湾三级 | 国产小情侣自拍 | 啪啪国产视频 | 国产精品自在线 | 小鸟酱在线看 | 波多野结衣不卡 | 国产精品秒播无毒不卡 | 国产青青草 | jj视频免费 | 波多野结衣在线中文字幕 | www.四虎影 | 亚洲国产欧美在线人成 | 欧美日韩一区二区三区在线播放 | 关晓彤被调教出奶水的视频 | 狠狠插入 |