詳解SpringCloud微服務架構之Hystrix斷路器,springcloudhystrix

來源:互聯網
上載者:User

詳解SpringCloud微服務架構之Hystrix斷路器,springcloudhystrix

一:什麼是Hystrix

在分布式環境中,許多服務依賴項中的一些將不可避免地失敗。Hystrix是一個庫,通過添加延遲容差和容錯邏輯來協助您控制這些分布式服務之間的互動。Hystrix通過隔離服務之間的訪問點,停止其間的級聯故障以及提供回退選項,從而提高系統的整體彈性。

Hystrix旨在執行以下操作

1:對通過第三方用戶端庫訪問(通常通過網路)的依賴關係提供保護並控制延遲和故障。

2:隔離複雜分布式系統中的級聯故障。

3:快速發現故障,儘快恢複。

4:回退,儘可能優雅地降級。

5:啟用近即時監控,警報和操作控制。

二:為什麼需要Hystrix?

大型分布式系統中,一個用戶端或者服務依賴外部服務,如果一個服務宕了,那麼由於我們設定了服務調用系統逾時時間,勢必會影響相應時間,在高並發的情況下大多數伺服器的線程池就出現阻塞(BLOCK),影響整個線上服務的穩定性。

(圖片官方圖片)

當一切都健康時,請求可以看起來像這樣

當許多後端服務系統中的一個宕掉時,整個使用者請求:

如果多個用戶端調用同一個異常服務的時候,出現的情況是:

 

三:Hystrix解決什麼問題?

分布式架構中的應用程式具有幾十個依賴關係,每個依賴關係在某個時刻將不可避免的出現異常。如果應用程式不與這些外部故障隔離,則可能出現線程池阻塞,引起系統雪崩。

例如,對於依賴30個服務的應用程式,每個服務的正常已耗用時間為99.99%,您可以:

99.99%的30次方 = 99.7%正常已耗用時間

0.3%的10億次請求= 3,000,000次故障

2+小時宕機/月,即使所有依賴關係正常已耗用時間。

當使用Hystrix進行熔斷後,每個依賴關係彼此隔離了,限制了當發生延遲時的阻塞。

 

四:Hystrix結合Feign使用

建立一個工程eureka_feign_hystrix_client

pom.xml檔案內容

<dependencies>    <dependency>      <groupId>org.springframework.cloud</groupId>      <artifactId>spring-cloud-starter-feign</artifactId>    </dependency>    <dependency>      <groupId>org.springframework.cloud</groupId>      <artifactId>spring-cloud-starter-eureka</artifactId>    </dependency>    <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-web</artifactId>    </dependency>    <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-actuator</artifactId>    </dependency>    <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-test</artifactId>      <scope>test</scope>    </dependency>  </dependencies>  <dependencyManagement>    <dependencies>      <dependency>        <groupId>org.springframework.cloud</groupId>        <artifactId>spring-cloud-dependencies</artifactId>        <version>Brixton.SR5</version>        <type>pom</type>        <scope>import</scope>      </dependency>    </dependencies>  </dependencyManagement> 

建立開機檔案

FeignHystrixApplication

@SpringBootApplication@EnableDiscoveryClient@EnableFeignClientspublic class FeignHystrixApplication {  public static void main(String[] args) {    SpringApplication.run(FeignHystrixApplication.class, args);  }} 

UserClient類

@FeignClient(value = "biz-service-0",fallback = UserClientHystrix.class)public interface UserClient {  @RequestMapping(method = RequestMethod.GET, value = "/getuser")  public User getuserinfo();  @RequestMapping(method = RequestMethod.GET, value = "/getuser")  public String getuserinfostr();  @RequestMapping(method = RequestMethod.GET, value = "/info")  public String info();} 

建立熔斷類UserClientHystrix

@Servicepublic class UserClientHystrix implements UserClient {   @Override  public User getuserinfo() {    throw new NullPointerException(" User getuserinfo() 服務不可用。。");  }  @Override  public String getuserinfostr() {    return " UserClientHystrix getuserinfostr() is fallback 服務不可用。。";  }  @Override  public String info() {    return " UserClientHystrix info() is fallback 服務不可用。。";  }} 

當網路出現異常的時候或直接跳轉到這裡實作類別裡面

建立action類

UserController

@Autowired  UserClient userClient;  @RequestMapping(value = "/getuserinfo", method = RequestMethod.GET)  public User getuserinfo() {    return userClient.getuserinfo();  }  @RequestMapping(value = "/getuserinfostr", method = RequestMethod.GET)  public String getuserinfostr() {    return userClient.getuserinfostr();  }  @RequestMapping(value = "/info", method = RequestMethod.GET)  public String info() {    return userClient.info();  } 

先啟動:eureka_register_service(註冊中心)工程

然後運行我們寫好的FeignHystrixApplication

這個時候我們明顯發現沒有運行biz-service-0 服務,那麼我們 開啟 http://127.0.0.1:8005/getuserinfostr

出現

UserClientHystrix getuserinfostr() is fallback 服務不可用。。

這個就是我們自訂的熔斷返回結果

如果不用熔斷 頁面會出現這個

Whitelabel Error PageThis application has no explicit mapping for /error, so you are seeing this as a fallback.Wed Mar 22 14:32:21 CST 2017There was an unexpected error (type=Internal Server Error, status=500).getuserinfo failed and fallback failed. 

代碼地址:https://github.com/zhp8341/SpringCloudDemo

本人也看了一些Hystrix相關原理,由於沒有全部看完所以暫時沒有寫上去,本文是結合Feign使用和學習。

有興起的可以看下官方的Hystrix很詳細,就是看起來有點費勁,

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.