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

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

成都創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:做網(wǎng)站、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的頭屯河網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

比如說(shuō)有三個(gè)服務(wù)(ABC),A調(diào)用B,B調(diào)用C。由于網(wǎng)絡(luò)延遲或C本身代碼有問題導(dǎo)致B遲遲得不到回應(yīng),這樣B調(diào)用C的請(qǐng)求就會(huì)被掛起,等待。

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

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

一、什么是Spring Cloud Hystrix?什么是斷路器?

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

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

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

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

1.啟動(dòng)eureka服務(wù)注冊(cè)中心,端口號(hào)1111

2.啟動(dòng)hello-service服務(wù)提供者,這里我們啟動(dòng)兩個(gè)服務(wù),端口號(hào)分別為9090,9091

3.啟動(dòng)hello-consumer服務(wù)消費(fèi)者,端口號(hào)為9999;這個(gè)時(shí)候我們多次訪問http://localhost:9999/hello-consumer是沒有問題的

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

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

PS:這里說(shuō)明下,為什么要多次訪問,是因?yàn)槲覀兺ㄟ^ribbon實(shí)現(xiàn)了負(fù)載均衡,訪問http://localhost:9999/hello-consumer的時(shí)候,會(huì)輪詢?cè)L問hello-service的兩個(gè)服務(wù),當(dāng)訪問到端口號(hào)是9091的服務(wù)時(shí)才報(bào)錯(cuò),訪問9090的服務(wù)就不會(huì)有問題。

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

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

1.修改POM文件,引入Hystrix依賴

<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 依賴 ,用來(lái)實(shí)現(xiàn)負(fù)載均衡,我們這里只是使用先不作其他介紹 -->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-ribbon</artifactId>
    </dependency>
    <!-- 引入hystrix 依賴 ,用來(lái)實(shí)現(xiàn)服務(wù)容錯(cuò)保護(hù)-->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-hystrix</artifactId>
    </dependency>

  </dependencies>
</project>

2.修改啟動(dòng)類,追加注解@EnableCircuitBreaker,開啟斷路器

@EnableDiscoveryClient
@SpringBootApplication
@EnableCircuitBreaker
public class ConsumerApp {


  //@Bean 應(yīng)用在方法上,用來(lái)將方法返回值設(shè)為為bean
  @Bean
  @LoadBalanced //@LoadBalanced實(shí)現(xiàn)負(fù)載均衡
  public RestTemplate restTemplate() {
    return new RestTemplate();
  }
  
  public static void main(String[] args) {
    SpringApplication.run(ConsumerApp.class, args);
  }
}

這個(gè)時(shí)候你會(huì)發(fā)現(xiàn),這個(gè)啟動(dòng)類加了三個(gè)注解,這個(gè)是不是很麻煩?沒關(guān)系,我們可以使用注解@SpringCloudApplication

@SpringCloudApplication
public class ConsumerApp {
  //@Bean 應(yīng)用在方法上,用來(lái)將方法返回值設(shè)為為bean
  @Bean
  @LoadBalanced //@LoadBalanced實(shí)現(xiàn)負(fù)載均衡
  public RestTemplate restTemplate() {
    return new RestTemplate();
  }
  
  public static void main(String[] args) {
    SpringApplication.run(ConsumerApp.class, args);
  }
}

@SpringCloudApplication = @EnableDiscoveryClient +@SpringBootApplication+@EnableCircuitBreaker,從源碼就能看出來(lái):

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
public @interface SpringCloudApplication {
}

3.追加service

@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!!!";
  }
}

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

4.修改controller 

/**
 *這里不再直接調(diào)用restTemplate,
 *而是通過調(diào)用service進(jìn)行實(shí)現(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.測(cè)試,多次訪問,當(dāng)報(bào)錯(cuò)的時(shí)候,會(huì)顯示如下內(nèi)容

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

網(wǎng)站標(biāo)題:springcloud使用Hystrix實(shí)現(xiàn)斷路器進(jìn)行服務(wù)容錯(cuò)保護(hù)的方法
鏈接URL:http://bm7419.com/article6/jdihig.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、App開發(fā)、商城網(wǎng)站、用戶體驗(yàn)、網(wǎng)站維護(hù)、關(guān)鍵詞優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司