Ken的杂谈
  • Ken的杂谈 (current)
  • 关于
  • 杂谈
    Java Spring Spring Boot Spring Cloud MyBatis C# .NET Core .NET ASP.NET Core ASP.NET ClassLibrary Mono 数据库 MySQL SQL Server 网络 缓存 Web Linux CentOS Ubuntu macOS Windows openEuler Nginx ServiceStack JS/JQ 正则 工具 IDE Grafana Exceptions CI/CD Solution 微服务 Arch Docker 杂谈
  • 系列
    Java 快速入门系列教程 Spring Boot 入门教程 Spring Boot 2.x 入门教程 Spring Cloud 入门教程 .NET Core 快速入门教程 ASP.NET Core 2.1 入门教程 CentOS 7 快速上手教程 Ubuntu快速上手入门教程 Hyper-V基础教程 Docker入门教程
  • GitHub

Spring Cloud 入门教程6、Hystrix Dashboard监控数据聚合(Turbine)

Java Spring Boot Spring Cloud 微服务 @ 2018-06-27 19:59:39 · 阅读:(9305)

一、前言

1、什么是Turbine?

Turbine是Netflix开源的将Server-Sent Event(SSE)的JSON数据流聚合成单个流的工具。我们可以通过Turbine将Hystrix生产的监控数据(JSON)合并到一个流中,方便我们对存在多个实例的应用进行监控。

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.

2、本篇环境信息

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

3、准备工作

参考上一篇:https://ken.io/note/spring-cloud-hystrix-dashboard-quickstart
基于源码:https://github.com/ken-io/springcloud-course/tree/master/chapter-05

  • 准备Eureka Server、服务提供者

启动Eureka Server: http://localhost:8800
启动Test Service:http://localhost:8602

  • 服务消费者+Hystrix Dashboard准备

修改feignclient项目:支持Turbine

上一篇中提到,Hystrix Dashboard是通过指定的URL查看监控信息。
例如:http://localhost:8605/hystrix.stream 可以查看单个应用单个实例的Hystrix监控信息。
也可以通过 [ http://turbine-hostname:port/turbine.stream ] 这样的URL查看聚合的监控信息。

其中Turbine.stream实际上是Turbine(version 1.0)通过访问对应服务每个实例的 http://hystrix-app:port/actuator/hystrix.stream 地址,然后把监控信息聚合起来。所有我们需要添加对应的Servlet注解,以支持通过该URL访问hystrix.stream信息

修改 HystrixConfiguration.java
增加 servletTurbineRegistrationBean() 满足Turbine的读取规则

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:8607

为了满足测试需要,这次我们启动三个实例。

二、监控数据聚合:Turbine

1、创建Turbine项目

按照惯例,使用maven-archtype-quickstart模板创建项目

项 说明
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>

2、配置启动类

修改\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);
    }
}

3、配置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多个端口的数据

4、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全部聚合到了一起。

三、备注

  • 本篇代码示例

https://github.com/ken-io/springcloud-course/tree/master/chapter-06

  • 延伸阅读

https://ken.io/note/netflix-hystrix-intro-principle

  • 本文参考

https://eacdy.gitbooks.io/spring-cloud-book/content/2%20Spring%20Cloud/2.4.3%20Turbine.html


  • 系列名称:Spring Cloud 入门教程
  • 上一篇:Spring Cloud 入门教程5、服务容错监控:Hystrix Dashboard
  • 下一篇:Spring Cloud 入门教程7、服务网关(Zuul)

Ken的杂谈

本文由 ken.io 创作,采用CC BY 3.0 CN协议 进行许可。 可自由转载、引用、甚至修改,但需署名作者且注明出处。

Java Spring Boot Spring Cloud 微服务

随笔目录


    © Copyright 2012-2025 Ken的杂谈

    豫ICP备10025012号-1

    ASP.NET Core(6.0) on Linux