第五篇:SpringCloud之斷路器監控(Hystrix Dashboard)

Hystrix Dashboard簡介

在微服務架構中爲例保證程序的可用性,防止程序出錯導致網絡阻塞,出現了斷路器模型。斷路器的狀況反應了一個程序的可用性和健壯性,它是一個重要指標。Hystrix Dashboard是一款針對Hystrix進行實時監控的圖形化界面工具,通過Hystrix Dashboard我們可以在直觀地看到各Hystrix Command的請求響應時間, 請求成功率等數據。

準備工作

我們在第一篇文章的工程的基礎上更改,重新命名爲:springcloud-hystrix-dashboard。

添加依賴

在pom的工程文件引入相應的依賴:

<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>
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>

其中,這三個依賴是必須的,缺一不可。

在程序的入口ProducerApplication類,加上@EnableHystrix註解開啓斷路器,這個是必須的,並且需要在程序中聲明斷路點HystrixCommand;加上@EnableHystrixDashboard註解,開啓HystrixDashboard

@SpringBootApplication
@EnableEurekaClient
@RestController
@EnableHystrix
@EnableHystrixDashboard
public class ProducerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProducerApplication.class, args);
    }

    @Value("${server.port}")
    String port;

    @HystrixCommand(fallbackMethod = "helloError")
    @RequestMapping("/hello")
    public String hello(@RequestParam String name) {
        return "hello " + name + ",i am from port:" + port;
    }
    
    public String helloError(String name) {
        return "hello," + name + ",sorry,error!";
    }
}

運行程序: 依次開啓sso-server 和sso-service-A.

Hystrix Dashboard圖形展示

3、測試
啓動工程後訪問 http://localhost:8089/hystrix,將會看到如下界面:
在這裏插入圖片描述
圖中會有一些提示:

Cluster via Turbine (default cluster): http://turbine-hostname:port/turbine.stream
Cluster via Turbine (custom cluster): http://turbine-hostname:port/turbine.stream?cluster=[clusterName]
Single Hystrix App: http://hystrix-app:port/hystrix.stream

大概意思就是如果查看默認集羣使用第一個url,查看指定集羣使用第二個url,單個應用的監控使用最後一個,我們暫時只演示單個應用的所以在輸入框中輸入: http://localhost:8089/hystrix.stream ,輸入之後點擊 monitor,進入頁面。如果沒有請求會先顯示Loading …,訪問http://localhost:8089/hystrix.stream 也會不斷的顯示ping。
請求服務http://localhost:8089/hello?name=chanjay,就可以看到監控的效果了,首先訪問http://localhost:8089/hystrix.stream,顯示如下:
在這裏插入圖片描述
說明已經返回了監控的各項結果

到監控頁面就會顯示如下圖:
在這裏插入圖片描述
其實就是http://localhost:8089/hystrix.stream返回結果的圖形化顯示,Hystrix Dashboard Wiki上詳細說明了圖上每個指標的含義,如下圖:
在這裏插入圖片描述

到此單個應用的熔斷監控已經完成。

源碼下載:https://github.com/chenjary/SpringCloud/tree/master/springcloud-hystrix-dashboard

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