一、说明
上一篇文章我们学习了 Hystrix Dashboard,用他来做数据监控,可以在页面看到加了 @HystrixCommand 注解的接口被访问的次数,成功和失败等状态。如果 client01 配置了 Hystrix Dashboard ,那么这个页面是通过访问 http://client01:8763/hystrix 打开的
![]()
然后输入 http://client01:8763/actuator/hystrix.stream 进入监控页面,可以在这个页面监控 client01 的接口情况。
![]()
但是,如果现在 client02 也配置了 Hystrix Dashboard ,那么你只能再打开一个监控页面才可以看到 client02 的接口情况。
有没有一种东西可以同时监控所有配置了 Hystrix Dashboard 的 client 呢?
有。那就是 Turbine 。你只需要在其中一个 client ,比如 client01 中配置 Turbine ,在配置文件里指定要监控哪些配置了 Hystrix Dashboard 的 client ,包括 client01 本身,就可以开始监控了。
接下来我们新建一个 turbine server 。
二、添加依赖
<!-- trubine -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>
完整 pom 如下
<?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.0.3.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.dhsg.sc</groupId>
<artifactId>turbine-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>turbine-server</name>
<description>turbine-server</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<!-- eureka client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<!-- trubine -->
<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>
</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>
</project>
三、在启动类添加注解 @EnableTurbine
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
@SpringBootApplication
@EnableEurekaClient
@EnableTurbine
@EnableHystrix
@EnableHystrixDashboard
public class TurbineServerApplication {
public static void main(String[] args) {
SpringApplication.run(TurbineServerApplication.class, args);
}
}
四、修改配置文件
management:
endpoints:
web:
exposure:
include: "*"
# Turbine 相关配置
cors:
allowed-origins: "*"
allowed-methods: "*"
# Turbine 相关配置
turbine:
app-config: client01,eureka-client
aggregator:
clusterConfig: default
clusterNameExpression: new String("default")
combine-host: true
instanceUrlSuffix:
default: actuator/hystrix.stream
完整配置文件如下
server:
port: 8765
spring:
application:
name: turbine-server
management:
endpoints:
web:
# Turbine 相关配置
cors:
allowed-origins: "*"
allowed-methods: "*"
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
# 为了在服务注册中心里显示实际的 IP 地址,需添加下面的配置
instance:
instance-id: ${spring.cloud.client.ip-address}:${server.port}
prefer-ip-address: true
# Turbine 相关配置
turbine:
app-config: client01,eureka-client
aggregator:
clusterConfig: default
clusterNameExpression: new String("default")
combine-host: true
instanceUrlSuffix:
default: actuator/hystrix.stream
从配置文件中可以知道,监控了 client01 和 eureka-client
五、启动项目
访问 http://turbine-server:8765/hystrix
![]()
输入 http://turbine-server:8765/turbine.stream ,进入监控页面
![]()
网友评论