跟我學SpringCloud | 第五篇:熔斷監控Hystrix Dashboard和Turbine

来源:https://www.cnblogs.com/babycomeon/archive/2019/07/05/11130009.html
-Advertisement-
Play Games

SpringCloud系列教程 | 第五篇:熔斷監控Hystrix Dashboard和Turbine Springboot: 2.1.6.RELEASE SpringCloud: Greenwich.SR1 如無特殊說明,本系列教程全採用以上版本 Hystrix dashboard是一款針對Hys ...


SpringCloud系列教程 | 第五篇:熔斷監控Hystrix Dashboard和Turbine

Springboot: 2.1.6.RELEASE

SpringCloud: Greenwich.SR1

如無特殊說明,本系列教程全採用以上版本

Hystrix-dashboard是一款針對Hystrix進行實時監控的工具,通過Hystrix Dashboard我們可以在直觀地看到各Hystrix Command的請求響應時間, 請求成功率等數據。但是只使用Hystrix Dashboard的話, 你只能看到單個應用內的服務信息, 這明顯不夠。我們需要一個工具能讓我們彙總系統內多個服務的數據並顯示到Hystrix Dashboard上, 這個工具就是Turbine。

1. Hystrix Dashboard

創建一個新的項目hystrix-dashboard,延用上一篇文章提到的eureka和producer兩個項目。

1. hystrix-dashboard pom.xml 依賴包管理

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.springcloud</groupId>
    <artifactId>hystrix-dashboard</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>hystrix-dashboard</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</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>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

前面介紹過的包我這裡不再多說,講幾個前面沒有見過的包:

  • actuator: 這個包是用來做服務監控的,很多監控相關的功能都會用到這個包,具體的內容我這裡先不講,後面會專門有一篇來講這個。
  • hystrix-dashboard: 這個是今天的主角,hystrix-dashboard幫我們封裝好了hystrix的監控面板。

2. 啟動類 HystrixDashboardApplication.java

package com.springcloud.hystrixdashboard;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrixDashboard
@EnableCircuitBreaker
public class HystrixDashboardApplication {

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

    @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;
    }

}

啟動類添加啟用Hystrix Dashboard和熔斷器


註意: 各位看官這裡一定要註意,我在這裡註冊了HystrixMetricsStreamServlet,在springboot1.x版本下,這裡是無需註冊的,在2.x版本後,這裡才需要註冊HystrixMetricsStreamServlet,並且顯示的給出訪問路徑。

3. 配置文件

server:
  port: 8081
spring:
  application:
    name: spring-cloud-hystrix-dashboard
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
feign:
  hystrix:
    enabled: true

4. 測試

又到了測試時間,我們先把昨天的eureka和producer CV到今天的工作目錄下,順次啟動服務,HystrixDashboard最後啟動,啟動完成後我們訪問:http://localhost:8081/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:8081/hystrix.stream ,輸入之後點擊 monitor,進入頁面。

如果沒有請求會先顯示Loading ...,訪問http://localhost:8081/hystrix.stream 也會不斷的顯示ping。

我們請求一下昨天用過的鏈接:http://localhost:8081/hello/spring。

馬上就能看到統計信息了。

可以再訪問一下http://localhost:8081/hystrix.stream,顯示如下:

ping: 

data: {"type":...}

data: {"type":...}

說明已經返回了監控的各項結果.

到了監控頁面就會顯示如下圖所示:

其實就是http://localhost:8081/hystrix.stream返回結果的圖形化顯示,Hystrix Dashboard Wiki上詳細說明瞭圖上每個指標的含義,如下圖:

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

2. Turbine

在複雜的分散式系統中,相同服務的節點經常需要部署上百甚至上千個,很多時候,運維人員希望能夠把相同服務的節點狀態以一個整體集群的形式展現出來,這樣可以更好的把握整個系統的狀態。 為此,Netflix提供了一個開源項目(Turbine)來提供把多個hystrix.stream的內容聚合為一個數據源供Dashboard展示。

1. Turbine pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.springcloud</groupId>
    <artifactId>turbine</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>turbine</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

    <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>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-turbine</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>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2. 配置文件

server:
  port: 8888
spring:
  application:
    name: hystrix-dashboard-turbine
turbine:
  app-config: node01,node02
  aggregator:
    cluster-config: default
  cluster-name-expression: new String("default")
  combine-host-port: true
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  • turbine.appConfig: 配置Eureka中的serviceId列表,表明監控哪些服務
  • turbine.aggregator.clusterConfig: 指定聚合哪些集群,多個使用”,”分割,預設為default。可使用http://.../turbine.stream?cluster={clusterConfig之一}訪問
  • turbine.clusterNameExpression: 1. clusterNameExpression指定集群名稱,預設表達式appName;此時:turbine.aggregator.clusterConfig需要配置想要監控的應用名稱;2. 當clusterNameExpression: default時,turbine.aggregator.clusterConfig可以不寫,因為預設就是default;3. 當clusterNameExpression: metadata[‘cluster’]時,假設想要監控的應用配置了eureka.instance.metadata-map.cluster: ABC,則需要配置,同時turbine.aggregator.clusterConfig: ABC

3. 啟動類

啟動類添加@EnableTurbine,激活對Turbine的支持

package com.springcloud.turbine;

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

@SpringBootApplication
@EnableHystrixDashboard
@EnableTurbine
public class TurbineApplication {

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

}

4. 創建消費者集群

將昨天的消費者copy2份到今天的文件路徑下,修改名稱為consumers-node01,consumers-node02

增加依賴包

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

啟動類增加HystrixMetricsStreamServlet註冊

package com.springcloud.consumers;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class ConsumersApplication {

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

    @Bean
    public ServletRegistrationBean getServlet(){
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/actuator/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }
}

註意: 這裡註冊的HystrixMetricsStreamServlet訪問路徑是"/actuator/hystrix.stream",這是因為Turbine預設訪問的是這個路徑。

兩個配置文件大家自行修改啟動埠號,spring.application.name分別修改為node01和node02

5. 測試

現在依次啟動註冊中心,兩個消費者和Turbine,順次訪問兩個消費者,並訪問 http://localhost:8888/turbine.stream, 會返回和上面一樣的一穿ping的信息。

並且會不斷刷新以獲取實時的監控數據,說明和單個的監控類似,返回監控項目的信息。

進行圖形化監控查看,輸入:http://localhost:8888/hystrix,返回酷酷的小熊界面,輸入: http://localhost:8888/turbine.stream,然後點擊 Monitor Stream ,可以看到出現了倆個監控列表(這是理論情況)

註意,這裡本地環境無法測試成功,本地環境只能顯示一個服務,Turbine日誌中會報錯

你的主機中的軟體中止了一個已建立的連接。

原因目前不明,已經在github上提Issues。

示例代碼-Github


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 方法一:chrome直接訪問下麵地址下載安裝:(需要翻牆) 方法二:手動安裝chrome擴展程式 1、獲取vue-devtools項目,進入vue官網>生態系統>工具>devtools ,把vue-devtools項目clone到本地,命令行執行內容如下: 2、添加chrome擴展程式,如下圖: 完 ...
  • Vue Function-based API RFC 一齣來,感覺 vue 越來越像 react 了。新立項目,決定嘗試下 react.js。下麵是 react 集成 cesium,核心部分是 webpack 的配置。 一、安裝 create-react-app npm install -g cre ...
  • 就目前來說,畢業生如果想畢業就找到高薪的工作,互聯網成為了第一個選擇,在所有的職業中,不靠任何關係,全憑自己的能力就業,就是程式開發,而web前端開發是目最很熱門的行業,在未來五年之內,web前端開發工程師的需求一直在增大,在這裡為大家詳細解釋一下這個高大上的行業! 前端是一個相對比較新的行業。但在 ...
  • (再聲明一下,為了簡單暴力的講解AST的轉換過程,這裡的編譯內容以"'Hello' + ' World'"作為案例) 上一篇基本上花了一整篇講完了scanner的Init方法,接下來就是Scan了,Init的方法基本上都是在Stream類下操作,但是本節回到了scanner層級。 雖然這裡只有簡簡單 ...
  • 前言 最近在學習的過程中發現了我之前未曾瞭解過的一些特性,發現有些很有趣並且在處理一些問題的時候可以給我一個新的思路。 這裡我將這些特性介紹給大家。 4 個有趣的 JS 特性 利用 a 標簽解析 URL 有的時候我們需要從一個 URL 中提取功能變數名稱,查詢關鍵字,變數參數值等,一般我們會自己去解析 UR ...
  • [2019.07.04 學習筆記2] 1.行級元素(inline element),與其他元素位於同一行。 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>行內元素</title> 6 < ...
  • Webpack已經流行好久了,但很多同學使用webpack時還是一頭霧水,一下看到那麼多文檔、各種配置、各種loader、plugin立馬就暈頭轉向了。我也不例外,以至於很長一段時間對webpack都是一知半解的狀態。但是想要繼續做好前端,webpack是必須得跨過的一道坎,其實掌握webpack並... ...
  • 如果第二次看到我的文章,歡迎右側掃碼訂閱我喲~ 👉 每周五早8點 按時送達。當然了,也會時不時加個餐~ 在一個分散式系統的開發團隊中,有一些問題是很容易產生程式員之間矛盾的。 其中之一就是「業務歸屬」,就是當新加/修改一個業務的時候,代碼變更應該放到你負責的系統還是我負責的系統里? 一些業務輪廓很 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...