上一篇文章当中,使用“Hystrix Dashboard”进行单体应用的监控,其实没有多大的意义,在实际应用中,我们要监控的应用往往是一个集群,这个时候我们就得采取Turbine集群监控了。Turbine有一个重要的功能就是汇聚监控信息,并将汇聚到的监控信息提供给Hystrix Dashboard来集中展示和监控。那我们就来看看Turbine集群监控如何使用。
本章是基于上一章的基础上进行实现,需要4个微服务应用程序,一个Eureka Server服务注册中心(spring-cloud-eureka-server);两个Hystrix Dashboard(spring-cloud-hystrix-dashboard-1和spring-cloud-hystrix-dashboard-2),spring-cloud-hystrix-dashboard-2的项目和spring-cloud-hystrix-dashboard-1一样;一个Hystrix Turbine(spring-cloud-hystrix-turbine)。
如下:
spring-cloud-eureka-server:8080
spring-cloud-hystrix-dashboard-1:8081
spring-cloud-hystrix-dashboard-2:8082
spring-cloud-hystrix-turbine:8083
第一步:创建一个普通的Spring Boot工程
spring-cloud-eureka-server、spring-cloud-hystrix-dashboard-1和spring-cloud-hystrix-dashboard-2创建工程不再重述,具体创建过程请查看之前的文章。
创建一个普通的Spring Boot工程,并命名为spring-cloud-hystrix-turbine。
第二步:添加依赖
添加“spring-cloud-starter-netflix-turbine”依赖,目的是聚合“Hystrix Dashboard”监控。
<parent>
<groupId>com.brimen</groupId>
<artifactId>spring-cloud-brimen-hystrix-dashboard</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath/>
</parent>
<dependencies>
<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>
<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</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>
</dependencies>
第三步:再启动项目上添加注解
@SpringBootApplication
@EnableEurekaClient
@RestController
@EnableHystrix
@EnableHystrixDashboard
@EnableCircuitBreaker
@EnableTurbine
public class SpringCloudHystrixTurbineApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudHystrixTurbineApplication.class, args);
}
}
第四步:添加配置
server:
port: 8083
spring:
application:
name: spring-cloud-hystrix-turbine
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8080/eureka/
management:
endpoints:
web:
exposure:
include: "*"
cors:
allowed-origins: "*"
allowed-methods: "*"
turbine:
#配置要监控的应用
app-config: spring-cloud-hystrix-dashboard-1,spring-cloud-hystrix-dashboard-2
aggregator:
#指定聚合哪些集群,多个使用","分割,默认为default。可使用http://.../turbine.stream?cluster={clusterConfig之一}访问
clusterConfig: default
clusterNameExpression: new String("default")
# 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: ABC
combine-host: true
instanceUrlSuffix:
default: actuator/hystrix.stream
第五步:查看监控视图
创建完成以后,我们依次启动:
spring-cloud-eureka-server
spring-cloud-hystrix-dashboard-1
spring-cloud-hystrix-dashboard-2
spring-cloud-hystrix-turbine
启动完成以后,我们在浏览器访问一下spring-cloud-hystrix-dashboard-1和spring-cloud-hystrix-dashboard-2写好的接口,对两个接口分别进行访问:http://localhost:8081/hello1 和 http://localhost:8082/hello2 让数据进行流动起来,否则的话,我们无法对数据进行监控。
第六步:进入“Hystrix Web”页面
输入服务监控流:http://localhost:8083/turbine.stream
Delay:2000
Title:my
第七步:查看监控图
通过Hystrix Turbine这样就实现了集群监控,通过此图我们可以清晰的看到“hello1”和“hello2”数据请求状况,这个图看不懂的,可以查看我的上一篇文章,都有详细的介绍。