[目录]
参考
开发环境
| 名称 | 版本 |
|---|---|
| 操作系统 | 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
之前项目的含义及创建,请参考
项目调用关系图

修改子模块-test-invoice-web
application.yml 文件注释 servlet
server:port: 8080# servlet:# 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
<artifactId>test-invoice-web1</artifactId><name>test-invoice-web1</name>
application.yml
(端口为 8081,name 为 service-feign-web1)
server:port: 8081# servlet:# context-path: /enta/apispring:application:# 服务名,即serviceIdname: service-feign-web1sleuth:web:client:enabled: true # 启用为分布式web client 客户端sampler:probability: 1.0 # 将采样比例设置为 1.0,也就是全部都需要。默认是 0.1
bootstrap.properties
#由于 logback-spring.xml的加载在 application.properties之前,所以之前的配置 logback-spring.xml无法获取到 spring.application.name属性,因此这里将该属性移动到最先加载的 bootstrap.properties配置文件中。#https://cloud.tencent.com/developer/article/1067431spring.application.name=service-feign-web1
WebApplication1
package com.test.invoice;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;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;/*** 启动类-web2** @author:* @version:* @date: 2019-09-19 10:07*///禁用 security 验证@SpringBootApplication(exclude = org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class)@EnableFeignClients //使用feign客户端,https://blog.csdn.net/andy_zhang2007/article/details/86680622@EnableEurekaClient //启用服务注册与发现//modify by v_hwhao 20190828 - 加入熔断监控Hystrix Dashboard和Turbine@EnableHystrixDashboard //启用 Hystrix Dashboard@EnableCircuitBreaker //开启熔断器功能public class WebApplication1 {public static void main(String[] args){SpringApplication.run(WebApplication1.class,args);}}
创建子模块-test-invoice-turbine
test-invoice-cloud 上右键,新建模块

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

ArtifactId test-invoice-turbine

模块名称 test-invoice-turbine

创建下图中的类和文件

模块简介
这个模块的目的就是为了监控 test-invoice-web,test-invoice-web1 两个项目
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"><parent><artifactId>test-invoice-cloud</artifactId><groupId>com-test-invoice</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>test-invoice-turbine</artifactId><name>test-invoice-turbine</name><packaging>war</packaging><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><!--spring boot 1.X:spring-cloud-starter-hystrix-dashboard--><artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId></dependency><!--Turbine --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-turbine</artifactId></dependency></dependencies></project>
application.yml
这里配置监控了 service-feign-web,service-feign-web1
即 test-invoice-web,test-invoice-web1 定义的服务名称
server:port: 8090# servlet:# context-path: /enta/apispring:application:# 服务名,即serviceIdname: test-invoice-turbine# 服务注册与发现相关配置eureka:#自定义实例编号instance:instance-id: ${spring.application.name}:${server.port}# 优先使用IP地址方式进行注册服务prefer-ip-address: true# 配置使用指定IP# ip-address: 127.0.0.1client:# 服务注册地址serviceUrl:# defaultZone: http://127.0.0.1:8772/eureka/defaultZone: http://127.0.0.1:8772/eureka/turbine:app-config: service-feign-web,service-feign-web1 #配置 Eureka 中的 serviceId 列表,表明监控哪些服务,多个以”,”分隔aggregator:clusterConfig: default #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: ABCcluster-name-expression: new String("default")combine-host-port: true #合并同一个host多个端口的数据
入口类-TurbineApplication
package com.test.invoice;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;import org.springframework.cloud.netflix.turbine.EnableTurbine;/*** Trubine 入口类** @author:* @version:* @date: 2019-09-19 09:36*/@SpringBootApplication@EnableEurekaClient //启用服务注册与发现@EnableHystrixDashboard//启用 Hystrix Dashboard@EnableTurbine//启用 Turbinepublic class TurbineApplication {public static void main(String[] args){SpringApplication.run(TurbineApplication.class,args);}}
配置类-HystrixConfig
package com.test.invoice.config;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;/*** Turbine 配置** @author:* @version:* @date: 2019-09-19 09:37*/@Configurationpublic class HystrixConfig {@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;}}
启动项目
启动项目 test-invoice-eureka
启动项目 test-invoice-service
启动项目 test-invoice-service1
启动项目 test-invoice-web
启动项目 test-invoice-web1
启动项目 test-invoice-turbine
如果不知道怎么启动多个项目,可参考

查看 Eureka 项目情况

开始监控
访问 http://localhost:8090/hystrix
填写监控信息,开始监控
http://localhost:8090/turbine.streamDelay:100Title:service-feign-web

点击 Monitor Stream 进入监控页面
使用 Postman 访问接口
1.访问 test-invoice-web query 接口
http://localhost:8080/Inv/Api/Test/Query{"page":1,"pageSize":3,"name":"luoma"}

2.访问 test-invoice-web2 get 接口
http://localhost:8081/Inv/Api/Test/Get{"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