1. 程式人生 > >微服務SpringCloud之熔斷監控Hystrix Dashboard和Turbine

微服務SpringCloud之熔斷監控Hystrix Dashboard和Turbine

Hystrix-dashboard是一款針對Hystrix進行實時監控的工具,通過Hystrix Dashboard我們可以在直觀地看到各Hystrix Command的請求響應時間, 請求成功率等資料。但是隻使用Hystrix Dashboard的話, 你只能看到單個應用內的服務資訊, 這明顯不夠. 我們需要一個工具能讓我們彙總系統內多個服務的資料並顯示到Hystrix Dashboard上, 這個工具就是Turbine.用資料、圖表展示更加直觀,讓資料說話,一圖頂千言。

一、Hystrix Dashboard

1.引入依賴

在EurekaConsumer專案中引入Hystrix Dashboard也比較容易,只需在pom.xml中引入依賴,主要引入spring-cloud-starter-netflix-hystrix、spring-cloud-starter-netflix-hystrix-dashboard、spring-boot-starter-actuator。

<?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.example</groupId>
    <artifactId>EurekaConsumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>EurekaConsumer</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-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-openfeign</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-openfeign-core -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-openfeign-core</artifactId>
            <version>2.1.2.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-hystrix -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
            <version>2.1.2.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-hystrix-dashboard -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
            <version>2.1.2.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-actuator -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>2.1.6.RELEASE</version>
        </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>

    <packaging>war</packaging>
</project>
View Code

2.啟用@EnableHystrixDashboard、@EnableCircuitBreaker

在main方法類中增加註解@EnableHystrixDashboard、@EnableCircuitBreaker,如果只是添加註解還是不夠的,執行的時候會報404的錯誤,還需要在EurekaConsumerApplication類中增加ServletRegistrationBean,設定對映的url。

package com.example.demo;

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.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrixDashboard
@EnableCircuitBreaker

public class EurekaConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaConsumerApplication.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;
      }
}
View Code

3.輸入http://localhost:9001/hystrix時,會顯示如下頁面

 

在上面的頁面輸入http://localhost:9001/hystrix.stream,點選Monitor Stream,然後請求http://localhost:9001/hello?name=cuiyw,就會在點選頁面彈出的新頁面中看到監控。

二、Turbine

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

1.引入依賴

這裡新建了HystrixTurbine專案,在專案中主要引入spring-cloud-starter-netflix-hystrix-dashboard、spring-cloud-starter-netflix-turbine、spring-boot-starter-actuator、spring-cloud-starter-netflix-turbine-stream。

<?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.example</groupId>
    <artifactId>HystrixTurbine</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>HystrixTurbine</name>
    <description>Demo project for Spring Boot</description>

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

    <dependencies>
        <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>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-actuator -->
         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>2.1.6.RELEASE</version>
        </dependency>
                <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-turbine-stream -->
         <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-turbine-stream</artifactId>
            <version>2.0.1.RELEASE</version>
        </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>

    <packaging>war</packaging>
</project>
View Code

2.設定配置檔案

spring.application.name=hystrix-dashboard-turbine
server.port=8002
turbine.appConfig=spring-cloud-consumerA,spring-cloud-consumerB
turbine.aggregator.clusterConfig= default
turbine.clusterNameExpression= new String("default")
eureka.client.serviceUrl.defaultZone=http://localhost:8088/eureka/
spring.main.allow-bean-definition-overriding=true
turbine.instanceUrlSuffix.default =hystrix.stream

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.設定@EnableHystrixDashboard、@EnableTurbineStream、@EnableTurbine

package com.example.demo;

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;
import org.springframework.cloud.netflix.turbine.stream.EnableTurbineStream;


@SpringBootApplication
@EnableHystrixDashboard
@EnableTurbineStream
@EnableTurbine
public class HystrixTurbineApplication {

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

4.測試

分別啟動EurekaServer、EurekaClient,分別修改EurekaConsumer,設定埠9001、9002,以及對應的spring.application.name為spring-cloud-consumerA、spring-cloud-consumerB,並啟動,最後啟動HystrixTurbine。

在瀏覽器中輸入http://localhost:8002/hystrix,在出現的頁面中輸入http://localhost:8002/turbine.stream ,點選Monitor Stream,在瀏覽器中分別輸入http://localhost:9002/hello?name=cuiyw、http://localhost:9001/hello?name=cuiyw,之後可以觀察監控頁面。由於這裡只有一個介面一個生產者,所以只顯示了一個。

這裡還有一個要注意的,由於未在配置檔案中設定turbine.instanceUrlSuffix.default =hystrix.stream,導致開啟監控頁面時一直在loading。日誌報com.netflix.turbine.monitor.instance.InstanceMonitor$MisconfiguredHostException: [{"timestamp":"2019-07-21T14:05:10.222+0000","status":404,"error":"Not Found","message":"No message available","path":"/actuator/hystrix.stream"}]錯誤。

 

 

&n