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

服務(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 cloud 使用Hystrix 實現(xiàn)斷路器進(jìn)行服務(wù)容錯保護(hù)的方法

spring cloud 使用Hystrix 實現(xiàn)斷路器進(jìn)行服務(wù)容錯保護(hù)的方法

2021-04-25 11:37JAVA開發(fā)老菜鳥 Java教程

本篇文章主要介紹了spring cloud 使用Hystrix 實現(xiàn)斷路器進(jìn)行服務(wù)容錯保護(hù)的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

在微服務(wù)中,我們將系統(tǒng)拆分為很多個服務(wù)單元,各單元之間通過服務(wù)注冊和訂閱消費的方式進(jìn)行相互依賴。但是如果有一些服務(wù)出現(xiàn)問題了會怎么樣?

比如說有三個服務(wù)(abc),a調(diào)用b,b調(diào)用c。由于網(wǎng)絡(luò)延遲或c本身代碼有問題導(dǎo)致b遲遲得不到回應(yīng),這樣b調(diào)用c的請求就會被掛起,等待。

在高并發(fā)的訪問的情況下,這些掛起的線程得不到釋放,使后續(xù)的請求阻塞,最終導(dǎo)致b也掛掉了。依次類推,a可能也會掛掉,進(jìn)而使整個系統(tǒng)全部崩潰。

為了解決整個問題,spring cloud 使用hystrix進(jìn)行服務(wù)容錯保護(hù),包括斷路器、線程隔離等一系列的保護(hù)功能,今天我們就來看下如何通過hystrix實現(xiàn)斷路器。

一、什么是spring cloud hystrix?什么是斷路器?

spring cloud hystrix是基于netflix的開源框架hystrix實現(xiàn)的,其目的是為了通過控制那些訪問遠(yuǎn)程系統(tǒng)、服務(wù)和第三方的節(jié)點,從而對延遲和故障提供強(qiáng)大的容錯能力。

斷路器類似于我們家里面強(qiáng)電箱里面用到的漏電斷路保護(hù)器,當(dāng)服務(wù)單元出現(xiàn)故障(類似于電器發(fā)生短路),通過斷路器的故障監(jiān)控功能(類似于保險絲),向調(diào)用方返回一個錯誤響應(yīng),避免長時間等待,從而避免故障蔓延到整個系統(tǒng)。

二、沒有斷路器的情況下,頁面展示

還記得我們前面寫的spring cloud 入門系列二:使用eureka 進(jìn)行服務(wù)治理里面的三個服務(wù)(eureka/hello-service/hello-consumer)嗎?我們基于這個進(jìn)行實驗。

1.啟動eureka服務(wù)注冊中心,端口號1111

2.啟動hello-service服務(wù)提供者,這里我們啟動兩個服務(wù),端口號分別為9090,9091

3.啟動hello-consumer服務(wù)消費者,端口號為9999;這個時候我們多次訪問http://localhost:9999/hello-consumer是沒有問題的

4.將hello-service端口號為9091的服務(wù)關(guān)掉,再去多次訪問http://localhost:9999/hello-consumer,報錯了

spring cloud 使用Hystrix 實現(xiàn)斷路器進(jìn)行服務(wù)容錯保護(hù)的方法

ps:這里說明下,為什么要多次訪問,是因為我們通過ribbon實現(xiàn)了負(fù)載均衡,訪問http://localhost:9999/hello-consumer的時候,會輪詢訪問hello-service的兩個服務(wù),當(dāng)訪問到端口號是9091的服務(wù)時才報錯,訪問9090的服務(wù)就不會有問題。

三、斷路器代碼實現(xiàn)

接下來我們看下如何進(jìn)行代碼實現(xiàn),我們不去修改服務(wù)注冊中心和服務(wù)提供者,只需要修改服務(wù)消費者h(yuǎn)ello-consumer。

1.修改pom文件,引入hystrix依賴

?
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
43
44
45
46
47
48
49
<project xmlns="http://maven.apache.org/pom/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
  xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelversion>4.0.0</modelversion>
  <groupid>com.sam</groupid>
  <artifactid>hello-consumer</artifactid>
  <version>0.0.1-snapshot</version>
  <parent>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-parent</artifactid>
    <version>1.5.1.release</version>
  </parent>
 
  <properties>
    <javaversion>1.8</javaversion>
  </properties>
 
  <dependencymanagement>
    <dependencies>
      <dependency>
        <groupid>org.springframework.cloud</groupid>
        <artifactid>spring-cloud-dependencies</artifactid>
        <version>camden.sr6</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
 
  </dependencymanagement>
 
  <dependencies>
    <!-- 引入eureka 客戶端依賴 -->
    <dependency>
      <groupid>org.springframework.cloud</groupid>
      <artifactid>spring-cloud-starter-eureka</artifactid>
    </dependency>
    <!-- 引入ribbon 依賴 ,用來實現(xiàn)負(fù)載均衡,我們這里只是使用先不作其他介紹 -->
    <dependency>
      <groupid>org.springframework.cloud</groupid>
      <artifactid>spring-cloud-starter-ribbon</artifactid>
    </dependency>
    <!-- 引入hystrix 依賴 ,用來實現(xiàn)服務(wù)容錯保護(hù)-->
    <dependency>
      <groupid>org.springframework.cloud</groupid>
      <artifactid>spring-cloud-starter-hystrix</artifactid>
    </dependency>
 
  </dependencies>
</project>

2.修改啟動類,追加注解@enablecircuitbreaker,開啟斷路器

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@enablediscoveryclient
@springbootapplication
@enablecircuitbreaker
public class consumerapp {
 
 
  //@bean 應(yīng)用在方法上,用來將方法返回值設(shè)為為bean
  @bean
  @loadbalanced //@loadbalanced實現(xiàn)負(fù)載均衡
  public resttemplate resttemplate() {
    return new resttemplate();
  }
  
  public static void main(string[] args) {
    springapplication.run(consumerapp.class, args);
  }
}

這個時候你會發(fā)現(xiàn),這個啟動類加了三個注解,這個是不是很麻煩?沒關(guān)系,我們可以使用注解@springcloudapplication

?
1
2
3
4
5
6
7
8
9
10
11
12
13
@springcloudapplication
public class consumerapp {
  //@bean 應(yīng)用在方法上,用來將方法返回值設(shè)為為bean
  @bean
  @loadbalanced //@loadbalanced實現(xiàn)負(fù)載均衡
  public resttemplate resttemplate() {
    return new resttemplate();
  }
  
  public static void main(string[] args) {
    springapplication.run(consumerapp.class, args);
  }
}

@springcloudapplication = @enablediscoveryclient +@springbootapplication+@enablecircuitbreaker,從源碼就能看出來:

?
1
2
3
4
5
6
7
8
9
@target(elementtype.type)
@retention(retentionpolicy.runtime)
@documented
@inherited
@springbootapplication
@enablediscoveryclient
@enablecircuitbreaker
public @interface springcloudapplication {
}

3.追加service

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@service
public class consumerservice {
  
  @autowired
  resttemplate resttemplate;
 
  @hystrixcommand(fallbackmethod = "errormsg")
  public string consumer() {
    // 調(diào)用hello-service服務(wù),注意這里用的是服務(wù)名,而不是具體的ip+port
    resttemplate.getforobject("http://hello-service/hello", string.class);
    return "hello consumer finish !!!";
  }
 
  public string errormsg() {
    return "error!!!";
  }
}

我們把原來controller里面的調(diào)用resttemplate的實現(xiàn)放到service里面,并且通過@hystrixcommand來指定回調(diào)方法,當(dāng)出現(xiàn)錯誤時調(diào)用該方法。

4.修改controller 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
 *這里不再直接調(diào)用resttemplate,
 *而是通過調(diào)用service進(jìn)行實現(xiàn)
 *
 */
@restcontroller
public class consumercontroller {
 
  @autowired
//  resttemplate resttemplate;
  consumerservice service;
  
  
  @requestmapping("/hello-consumer")
  public string helloconsumer() {
//    //調(diào)用hello-service服務(wù),注意這里用的是服務(wù)名,而不是具體的ip+port
//    resttemplate.getforobject("http://hello-service/hello", string.class);
    return service.consumer();
  }
}

5.測試,多次訪問,當(dāng)報錯的時候,會顯示如下內(nèi)容

 spring cloud 使用Hystrix 實現(xiàn)斷路器進(jìn)行服務(wù)容錯保護(hù)的方法

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://www.cnblogs.com/sam-uncle/p/8972281.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日韩a无吗一区二区三区 | 91嫩草私人成人亚洲影院 | chinesexxxx高中生gay | 91九色国产porny| 国产精品香蕉夜间视频免费播放 | 久久大胆视频 | 国产ab| 秋霞一级毛片 | 五月婷婷丁香在线视频 | 美女视频ww8888网网 | 日本又大又硬又粗的视频 | 香蕉tv亚洲专区在线观看 | 996热视频| 精品在线视频一区 | 日处女b| 色综合网天天综合色中文男男 | asian4you裸模 | 国产一区日韩二区欧美三区 | 丝袜兔女郎被啪在线观看91 | 狠狠色综合久久婷婷色天使 | 性xxxx中国 | 久久aa毛片免费播放嗯啊 | 国产成人精品高清在线 | 久久精品一区二区三区资源网 | xxx86日本人| 消息称老熟妇乱视频一区二区 | 免费一级欧美片在线观免看 | 国产二区三区 | 免费日本在线 | 亚洲国产精品自产在线播放 | 免费国产福利 | 精品国语国产在线对白 | 久久国产精品福利影集 | 亚洲va天堂va国产va久久 | 饭冈加奈子黑人解禁在线播放 | 精品无码一区二区三区中文字幕 | japanese超丰满人妖 | 青青草99 | 成年人天堂| 国产最新进精品视频 | 国产日韩欧美在线一二三四 |