Hystrix斷路器在微服務架構SpringCloud場景中應用詳解

Hystrix是什麼?

在分佈式環境中,許多服務依賴項中的一些將不可避免地失敗。Hystrix是一個庫,通過添加延遲容差和容錯邏輯來幫助您控制這些分佈式服務之間的交互。Hystrix通過隔離服務之間的訪問點,停止其間的級聯故障以及提供回退選項,從而提高系統的整體彈性。
文章篇幅過長。感興趣的朋友可以提前收藏加關注,後續會有類似文章更新。分享一下個人整理的架構進階學習路線以及學習資料,需要的朋友可以自行領取:Java高級架構學習資料分享+架構師成長之路​
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

<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
@EnableFeignClients
public 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

@Service
public 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 服務,
出現
UserClientHystrix getuserinfostr() is fallback 服務不可用。
這個就是我們自定義的熔斷返回結果
如果不用熔斷 頁面會出現這個

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

爲了不影響大家的閱讀效果,文章不再編寫過長篇幅。個人整理了更多資料以PDF文件的形式分享給大家,需要查閱的程序員朋友可以來免費領取。還有我的學習筆記PDF文件也免費分享給有需要朋友!Java高級架構學習資料分享+架構師成長之路​

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章