Spring Boot + Spring Cloud 構建微服務系統(五):熔斷監控面板(Hystrix Dashboard)

Hystrix Dashboard

Hystrix-dashboard是一款針對Hystrix進行實時監控的工具,經過Hystrix Dashboard咱們能夠在直觀地看到各Hystrix Command的請求響應時間, 請求成功率等數據。java

添加依賴

咱們新建一個工程 spring-cloud-consul-monitor,修改 pom 文件,添加相關依賴。git

pom.xmlspring

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

啓動類

在啓動類中添加註解 @EnableHystrixDashboard 開啓熔斷監控支持。springboot

ConsuleMonitorApplication.javaapp

package com.louis.spring.cloud.consul.monitor;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

@EnableHystrixDashboard
@SpringBootApplication
public class ConsuleMonitorApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConsuleMonitorApplication.class, args);
    }
}

修改配置

修改配置文件,配置啓動端口和應用名稱。spring-boot

application.yml工具

server:
  port: 8531
spring:
  application:
    name: spring-cloud-consul-monitor

配置監控路徑

注意,若是你使用的是2.x等比較新的版本,須要在 Hystrix 的消費端配置監控路徑,咱們這裏消費端是 spring-cloud-consul-consumer, 因此修改它的啓動類。測試

ConsuleConsumerApplication.javaui

    // 此配置是爲了服務監控而配置,與服務容錯自己無關,
    // ServletRegistrationBean由於springboot的默認路徑不是"/hystrix.stream",
    // 只要在本身的項目裏配置上下面的servlet就能夠了
    @Bean
    public ServletRegistrationBean getServlet() {
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }

測試效果

前後啓動 spring-cloud-consul-producer、 spring-cloud-consul-consumer、spring-cloud-consul-monitor 服務。spa

訪問 http://localhost:8531/hystrix,會看到以下圖所示界面。

 

此時沒有任何具體的監控信息,須要輸入要監控的消費者地址及監控信息的輪詢時間和標題。

Hystrix Dashboard 共支持三種不一樣的監控方式:

單體Hystrix 消費者:經過URL http://hystrix-app:port/hystrix.stream 開啓,實現對具體某個服務實例的監控。

默認集羣監控:經過URL http://turbine-hostname:port/turbine.stream 開啓,實現對默認集羣的監控。

自定集羣監控:經過URL http://turbine-hostname:port/turbine.stream?cluster=[clusterName] 開啓,實現對clusterName集羣的監控。

咱們這裏如今是對單體 Hystrix 消費者的監控,後面整合 Turbine 集羣的時候再說明後兩種的監控方式。

 

咱們先訪問 http://localhost:8521/feign/call, 查看要監控的服務是否能夠正常訪問。

確認服務能夠正常訪問以後,在監控地址內輸入 http://localhost:8521/hystrix.stream,而後點擊 Monitor Stream 開始監控。

 

剛進去,頁面先顯示 loading... 信息, 屢次訪問 http://localhost:8521/feign/call 以後,統計圖表信息以下圖所示。

 

各個指標的含義參見下圖。

 

源碼下載

碼雲:https://gitee.com/liuge1988/spring-cloud-demo.git


做者:朝雨憶輕塵
出處:https://www.cnblogs.com/xifengxiaoma/ 版權全部,歡迎轉載,轉載請註明原文做者及出處。

相關文章
相關標籤/搜索