Spring Cloud 入門教程六、Hystrix Dashboard監控數據聚合(Turbine)

1、前言

一、什麼是Turbine?

Turbine是Netflix開源的將Server-Sent Event(SSE)的JSON數據流聚合成單個流的工具。咱們能夠經過Turbine將Hystrix生產的監控數據(JSON)合併到一個流中,方便咱們對存在多個實例的應用進行監控。java

Turbine can be used with any data source that fits the supported JSON format of key/value pairs delivered via Server Sent Events. However, it is specifically intended for aggregating streams output by Hystrix so that will be used in all examples.git

二、本篇環境信息

框架 版本
Spring Boot 2.0.0.RELEASE
Spring Cloud Finchley.RELEASE
JDK 1.8.x

三、準備工做

啓動Eureka Server: http://localhost:8800
啓動Test Service:http://localhost:8602github

  • 服務消費者+Hystrix Dashboard準備

修改feignclient項目:支持Turbineweb

上一篇中提到,Hystrix Dashboard是經過指定的URL查看監控信息。
例如:http://localhost:8605/hystrix.stream 能夠查看單個應用單個實例的Hystrix監控信息。
也能夠經過 [ http://turbine-hostname:port/turbine.stream ] 這樣的URL查看聚合的監控信息。spring

其中Turbine.stream其實是Turbine(version 1.0)經過訪問對應服務每一個實例的 http://hystrix-app:port/actuator/hystrix.stream 地址,而後把監控信息聚合起來。全部咱們須要添加對應的Servlet註解,以支持經過該URL訪問hystrix.stream信息express

修改 HystrixConfiguration.java
增長 servletTurbineRegistrationBean() 知足Turbine的讀取規則apache

package io.ken.springcloud.feignclient.configuration;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HystrixConfiguration {

    @Bean(name = "hystrixRegistrationBean")
    public ServletRegistrationBean servletRegistrationBean() {
        ServletRegistrationBean registration = new ServletRegistrationBean(
                new HystrixMetricsStreamServlet(), "/hystrix.stream");
        registration.setName("hystrixServlet");
        registration.setLoadOnStartup(1);
        return registration;
    }

    @Bean(name = "hystrixForTurbineRegistrationBean")
    public ServletRegistrationBean servletTurbineRegistrationBean() {
        ServletRegistrationBean registration = new ServletRegistrationBean(
                new HystrixMetricsStreamServlet(), "/actuator/hystrix.stream");
        registration.setName("hystrixForTurbineServlet");
        registration.setLoadOnStartup(1);
        return registration;
    }
}

啓動FeignClient:http://localhost:8605,http://localhost:8606,http://localhost:8607app

爲了知足測試須要,此次咱們啓動三個實例。框架

2、監控數據聚合:Turbine

一、建立Turbine項目

按照慣例,使用maven-archtype-quickstart模板建立項目maven

說明
GroupId io.ken.springcloud.turbine
ArtifactId 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>

    <groupId>io.ken.springcloud.turbine</groupId>
    <artifactId>turbine</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>turbine</name>
    <url>http://ken.io</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>

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

        <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>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <finalName>turbine</finalName>
    </build>

</project>

二、配置啓動類

修改\src\main\java\io\ken\springcloud\turbine\App.java

@EnableTurbine:啓用Turbine
@EnableHystrixDashboard:啓用Hystrix Dashboard

package io.ken.springcloud.turbine;

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

@EnableHystrixDashboard
@EnableTurbine
@EnableDiscoveryClient
@SpringBootApplication
public class App 
{
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

三、配置Turbine

在\src\main下建立文件夾resources文件夾並設置爲Resources Root

在resources文件夾下建立application.yml文件並配置Turbine

server:
  port: 8801

spring:
  application:
    name: turbine

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8800/eureka/

turbine:
  app-config: feignclient
  cluster-name-expression: new String("default")
  combine-host-port: true

參數說明:

參數 ken.io的說明
turbine.app-config 指定須要監控的servicename,多個service以,間隔
turbine.cluster-name-expression 指定集羣名稱,默認爲default,當設立了多個集羣時,能夠在Hystrix指定集羣名稱來查看監控
turbine.combine-host-port 合併同一個host多個端口的數據

四、Turbine測試

啓動項目後,訪問 http://localhost:8801/hystrix

image

輸入指定鏈接:http://localhost:8801/turbine.stream
Delay(查詢監控信息的延遲時間),Tile能夠自定義,也能夠默認。填寫完畢點擊 Monitor Stream 便可查看合併後的監控圖表。

而後分別開啓:http://localhost:8605/hystrix.stream ,http://localhost:8606/hystrix.stream ,http://localhost:8607/hystrix.stream
三個hystrix.stream的監控

接着分別訪問:http://localhost:8605/ti ,http://localhost:8606/ti ,http://localhost:8607/ti

監控面板某個瞬間的截圖以下:

image

經過對比發現,右上角的Tuebine聚合視圖已經把三個實例的Hystrix所有聚合到了一塊兒。

相關文章
相關標籤/搜索