4.Spring Cloud-Hystrix Dashboard监控数据聚合(Turbine)

2019年09月19日 11:58 · 阅读(863) ·

[目录]

参考

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

springcloud(五):熔断监控Hystrix Dashboard和Turbine

开发环境

名称 版本
操作系统 Windows 10 X64
JDK JDK1.8(jdk-8u151-windows-x64)
IntelliJ IDEA IntelliJ IDEA 2018.3
Maven Maven 3.6.0

代码说明

本博客的代码基于 3.Spring Cloud-熔断监控 Hystrix Dashboard 的基础上添加代码,请务必弄懂之后再继续

源码下载

见附件

理论知识

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

项目结构说明

3.Spring Cloud-熔断监控 Hystrix Dashboard 的基础上,新增了两个项目

1.test-invoice-turbine
2.test-invoice-web1

之前项目的含义及创建,请参考

IntelliJ IDEA 2018.3 创建 Maven 多模块(Module)项目+负载均衡

3.Spring Cloud-熔断监控 Hystrix Dashboard

项目调用关系图

修改子模块-test-invoice-web

application.yml 文件注释 servlet

  1. server:
  2. port: 8080
  3. # servlet:
  4. # context-path: /enta/api

创建子模块-test-invoice-web1

模块简介

这个模块就是 test-invoice-web 的复制版,创建的目的是为了和 test-invoice-web 一起进行监控,因为监控单个模块的话看不出效果

所有的文件内容和 test-invoice-web 一致,下面我只列出不一致的地方

开始创建

test-invoice-cloud 上右键,新建模块

左侧面板选择 Maven,不勾选 Create from archetype 选项,如下图,点击 下一个 即可。

ArtifactId test-invoice-web1

模块名称 test-invoice-web1

创建下图中的类和文件

复制 test-invoice-web 对应文件的内容,下面我只列出不一致的文件内容

pom.xml

  1. <artifactId>test-invoice-web1</artifactId>
  2. <name>test-invoice-web1</name>

application.yml

(端口为 8081,name 为 service-feign-web1)

  1. server:
  2. port: 8081
  3. # servlet:
  4. # context-path: /enta/api
  5. spring:
  6. application:
  7. # 服务名,即serviceId
  8. name: service-feign-web1
  9. sleuth:
  10. web:
  11. client:
  12. enabled: true # 启用为分布式web client 客户端
  13. sampler:
  14. probability: 1.0 # 将采样比例设置为 1.0,也就是全部都需要。默认是 0.1

bootstrap.properties

  1. #由于 logback-spring.xml的加载在 application.properties之前,所以之前的配置 logback-spring.xml无法获取到 spring.application.name属性,因此这里将该属性移动到最先加载的 bootstrap.properties配置文件中。
  2. #https://cloud.tencent.com/developer/article/1067431
  3. spring.application.name=service-feign-web1

WebApplication1

  1. package com.test.invoice;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
  5. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  6. import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
  7. import org.springframework.cloud.openfeign.EnableFeignClients;
  8. /**
  9. * 启动类-web2
  10. *
  11. * @author:
  12. * @version:
  13. * @date: 2019-09-19 10:07
  14. */
  15. //禁用 security 验证
  16. @SpringBootApplication(exclude = org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class)
  17. @EnableFeignClients //使用feign客户端,https://blog.csdn.net/andy_zhang2007/article/details/86680622
  18. @EnableEurekaClient //启用服务注册与发现
  19. //modify by v_hwhao 20190828 - 加入熔断监控Hystrix Dashboard和Turbine
  20. @EnableHystrixDashboard //启用 Hystrix Dashboard
  21. @EnableCircuitBreaker //开启熔断器功能
  22. public class WebApplication1 {
  23. public static void main(String[] args){
  24. SpringApplication.run(WebApplication1.class,args);
  25. }
  26. }

创建子模块-test-invoice-turbine

test-invoice-cloud 上右键,新建模块

左侧面板选择 Maven,不勾选 Create from archetype 选项,如下图,点击 下一个 即可。

ArtifactId test-invoice-turbine

模块名称 test-invoice-turbine

创建下图中的类和文件

模块简介

这个模块的目的就是为了监控 test-invoice-webtest-invoice-web1 两个项目

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>test-invoice-cloud</artifactId>
  7. <groupId>com-test-invoice</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>test-invoice-turbine</artifactId>
  12. <name>test-invoice-turbine</name>
  13. <packaging>war</packaging>
  14. <properties>
  15. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  16. </properties>
  17. <dependencies>
  18. <dependency>
  19. <groupId>org.springframework.cloud</groupId>
  20. <!--spring boot 1.X:spring-cloud-starter-hystrix-dashboard-->
  21. <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
  22. </dependency>
  23. <!--Turbine -->
  24. <dependency>
  25. <groupId>org.springframework.cloud</groupId>
  26. <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
  27. </dependency>
  28. </dependencies>
  29. </project>

application.yml

这里配置监控了 service-feign-web,service-feign-web1

test-invoice-web,test-invoice-web1 定义的服务名称

  1. server:
  2. port: 8090
  3. # servlet:
  4. # context-path: /enta/api
  5. spring:
  6. application:
  7. # 服务名,即serviceId
  8. name: test-invoice-turbine
  9. # 服务注册与发现相关配置
  10. eureka:
  11. #自定义实例编号
  12. instance:
  13. instance-id: ${spring.application.name}:${server.port}
  14. # 优先使用IP地址方式进行注册服务
  15. prefer-ip-address: true
  16. # 配置使用指定IP
  17. # ip-address: 127.0.0.1
  18. client:
  19. # 服务注册地址
  20. serviceUrl:
  21. # defaultZone: http://127.0.0.1:8772/eureka/
  22. defaultZone: http://127.0.0.1:8772/eureka/
  23. turbine:
  24. app-config: service-feign-web,service-feign-web1 #配置 Eureka 中的 serviceId 列表,表明监控哪些服务,多个以”,”分隔
  25. aggregator:
  26. clusterConfig: default #turbine.aggregator.clusterConfig :指定聚合哪些集群,多个使用”,”分割,默认为default。可使用http://.../turbine.stream?cluster={clusterConfig之一}访问
  27. #turbine.clusterNameExpression :
  28. #1. clusterNameExpression指定集群名称,默认表达式appName;此时:turbine.aggregator.clusterConfig需要配置想要监控的应用名称;
  29. #2. 当clusterNameExpression: default时,turbine.aggregator.clusterConfig可以不写,因为默认就是default;
  30. #3. 当clusterNameExpression: metadata[‘cluster’]时,假设想要监控的应用配置了eureka.instance.metadata-map.cluster: ABC,则需要配置,同时turbine.aggregator.clusterConfig: ABC
  31. cluster-name-expression: new String("default")
  32. combine-host-port: true #合并同一个host多个端口的数据

入口类-TurbineApplication

  1. package com.test.invoice;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  5. import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
  6. import org.springframework.cloud.netflix.turbine.EnableTurbine;
  7. /**
  8. * Trubine 入口类
  9. *
  10. * @author:
  11. * @version:
  12. * @date: 2019-09-19 09:36
  13. */
  14. @SpringBootApplication
  15. @EnableEurekaClient //启用服务注册与发现
  16. @EnableHystrixDashboard//启用 Hystrix Dashboard
  17. @EnableTurbine//启用 Turbine
  18. public class TurbineApplication {
  19. public static void main(String[] args){
  20. SpringApplication.run(TurbineApplication.class,args);
  21. }
  22. }

配置类-HystrixConfig

  1. package com.test.invoice.config;
  2. import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
  3. import org.springframework.boot.web.servlet.ServletRegistrationBean;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. /**
  7. * Turbine 配置
  8. *
  9. * @author:
  10. * @version:
  11. * @date: 2019-09-19 09:37
  12. */
  13. @Configuration
  14. public class HystrixConfig {
  15. @Bean(name = "hystrixRegistrationBean")
  16. public ServletRegistrationBean servletRegistrationBean() {
  17. ServletRegistrationBean registration = new ServletRegistrationBean(
  18. new HystrixMetricsStreamServlet(), "/hystrix.stream");
  19. registration.setName("hystrixServlet");
  20. registration.setLoadOnStartup(1);
  21. return registration;
  22. }
  23. @Bean(name = "hystrixForTurbineRegistrationBean")
  24. public ServletRegistrationBean servletTurbineRegistrationBean() {
  25. ServletRegistrationBean registration = new ServletRegistrationBean(
  26. new HystrixMetricsStreamServlet(), "/actuator/hystrix.stream");
  27. registration.setName("hystrixForTurbineServlet");
  28. registration.setLoadOnStartup(1);
  29. return registration;
  30. }
  31. }

启动项目

启动项目 test-invoice-eureka
启动项目 test-invoice-service
启动项目 test-invoice-service1
启动项目 test-invoice-web
启动项目 test-invoice-web1
启动项目 test-invoice-turbine

如果不知道怎么启动多个项目,可参考

启动多个项目

查看 Eureka 项目情况

访问 http://localhost:8772/

开始监控

访问 http://localhost:8090/hystrix

填写监控信息,开始监控

  1. http://localhost:8090/turbine.stream
  2. Delay:100
  3. Title:service-feign-web

点击 Monitor Stream 进入监控页面

使用 Postman 访问接口

1.访问 test-invoice-web query 接口

  1. http://localhost:8080/Inv/Api/Test/Query
  2. {"page":1,"pageSize":3,"name":"luoma"}

2.访问 test-invoice-web2 get 接口

  1. http://localhost:8081/Inv/Api/Test/Get
  2. {"id":"","name":"测试数据-菲克-1","version":""}

查看监控信息

回到点击 Monitor Stream 进入的监控页面,页面已经变成了

监控成功

题外

1.访问 http://localhost:8090/turbine.stream

可以看到页面不断刷新以获取实时的监控数据

2.Dependency ‘xxx’ not found 更少… (Ctrl+F1) Inspection info: Inspects a Maven model for resolution p

Dependency ‘xxx’ not found 更少… (Ctrl+F1) Inspection info: Inspects a Maven model for resolution p


相关附件 4.Spring Cloud-Hystrix Dashboard监控数据聚合(Turbine) | 大小:0.16M | 下载