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

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

Springboot: 2.1.6.RELEASEjava

SpringCloud: Greenwich.SR1node

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

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

1. Hystrix Dashboard

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

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>

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

  • 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和熔斷器express


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

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,將會看到以下界面:springboot

圖中會有一些提示:app

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

相關文章
相關標籤/搜索