1. 程式人生 > >SpringCloud之斷路器監控(Hystrix Dashboard)(九)

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

bre 進行 忽略 red bad pid mapping 來源 模型

斷路器
斷路器模式源於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
@SpringBootApplication
public 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
@RestController
public 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)(九)