SpringCloud之斷路器監控(Hystrix Dashboard)(九)

來源:互聯網
上載者:User

標籤:圖形化   相關   auth   ext   模式   template   開啟   友好   imp   

斷路器
斷路器模式源於Martin Fowler的Circuit Breaker一文。“斷路器”本身是一種開關裝置,用於在電路上保護線路過載,當線路中有電器發生短路時,“斷路器”能夠及時的切斷故障電路,防止發生過載、發熱、甚至起火等嚴重後果。

在分布式架構中,斷路器模式的作用也是類似的,當某個服務單元發生故障(類似用電器發生短路)之後,通過斷路器的故障監控(類似熔斷保險絲),向調用方返回一個錯誤響應,而不是長時間的等待。這樣就不會使得線程因調用故障服務被長時間佔用不釋放,避免了故障在分布式系統中的蔓延。

斷路器監控
在微服務架構中為例保證程式的可用性,防止程式出錯導致網路阻塞,出現了斷路器模型。斷路器的狀況反應了一個程式的可用性和健壯性,它是一個重要指標。Hystrix Dashboard是作為斷路器狀態的一個組件,提供了資料監控和友好的圖形化介面

改造項目
複製項目 spring-cloud-ribbon-consumer-hystrix,修改名稱 spring-cloud-ribbon-consumer-hystrix-dashboard 在它的基礎上進行改造。 Feign的改造和這一樣。

在 pom的工程檔案引入相應的依賴:

<dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-starter-hystrix</artifactId></dependency><dependency> <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency>   <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId></dependency>

開啟 HD
修改 RibbonConsumerApplication.java 類

在程式的入口 RibbonConsumerApplication類,加上 @EnableHystrix註解開啟斷路器,這個是必須的,並且需要在程式中聲明斷路點 @HystrixCommand;加上 @EnableHystrixDashboard註解,開啟 HystrixDashboard,歡迎大家一起學習研究相關技術願意瞭解源碼的朋友直接求求交流分享技術:2147775633

package io.ymq.example.ribbon.consumer.hystrix;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.cloud.netflix.hystrix.EnableHystrix;import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;import org.springframework.context.annotation.Bean;..import org.springframework.web.client.RestTemplate;@EnableHystrix@EnableDiscoveryClient@EnableHystrixDashboard@SpringBootApplicationpublic class RibbonConsumerApplication {   @LoadBalanced   @Bean   RestTemplate restTemplate() {       return new RestTemplate();   public static void main(String[] args) {       SpringApplication.run(RibbonConsumerApplication.class, args);                       

聲明斷路點
聲明斷路點 @HystrixCommand(fallbackMethod="defaultStores")

package io.ymq.example.ribbon.consumer.hystrix;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;* 描述:調用提供者的 `home` 方法* @author yanpenglei@RestControllerpublic class ConsumerController {   @Autowired   private RestTemplate restTemplate;   @HystrixCommand(fallbackMethod = "defaultStores")   @GetMapping(value = "/hello")   public String hello() {       return restTemplate.getForEntity("http://eureka-provider/", String.class).getBody();   public String defaultStores() {   return "feign + hystrix Dashboard ,提供者服務掛了";}

@HystrixCommand 表明該方法為 hystrix包裹,可以對依賴服務進行隔離、降級、快速失敗、快速重試等等 hystrix相關功能 該註解屬性較多,下面講解其中幾個

fallbackMethod 降級方法

commandProperties 普通配置屬性,可以配置 HystrixCommand對應屬性,例如採用線程池還是訊號量隔離、熔斷器熔斷規則等等

ignoreExceptions 忽略的異常,預設 HystrixBadRequestException不計入失敗

groupKey() 組名稱,預設使用類名稱

commandKey 命令名稱,預設使用方法名

更多詳細源碼參考來源:http://×××/honghu/technology.html

SpringCloud之斷路器監控(Hystrix Dashboard)(九)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.