微服务spring cloud-使用Turbine聚合监控数据
使用Turbine聚合监控数据
使用/hystrix.stream端点监控单个微服务实例。然而,使用微服务架构的应用系统一般会
包含若干个微服务,每个微服务通常都会部署多个实例。如果每次只能查看单个实例的监控
数据,就必须在Hystrix Dashboard上切换想要监控的地址,这显然很不方便。如何解决该问题呢?
Turbine简介
Turbine是一个聚合Hystrix监控数据的工具,他可以将所有相关/hystrix.stream端点的数据聚合
到一个组合的道一个组合的/turbine.streeam中,从而让集群的监控更加方便。
使用Turbine监控多个微服务
1.为了让Turbine监控两个以上的微服务,现将项目microservice-consumer-movie-feign-
hystrix-fallback-stream的application.yml改成如下内容:
server:
port: 8020 #端口号
spring:
application:
name: movie-feign-hystrixstream #服务名称
eureka:
instance:
prefer-ip-address: true
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
2.创建一个Maven项目,ArtifactId是
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-turbine</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
3.在启动类上添加@EnableTurbine
@SpringBootApplication
@EnableTurbine
public class TurbineApplication {
public static void main(String[] args){
SpringApplication.run(TurbineApplication.class, args);
}
}
4.编写配置文件application.yml
server:
port: 8031
spring:
application:
name: turbine
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
instance:
prefer-ip-address: true
turbine:
app-config: movie-hystrix,movie-feign-hystrixstream
cluster-name-expression: new String('default')
combine-host-port: true
instanceUrlSuffix: /actuator/hystrix.stream
使用以上配置,Turbine会在Eureka Server中找到movie-hystrix、movie-feign-hystrixstream这两个微服务,并聚合两个微服务的监控数据。
5.测试
1.启动项目microservice-discovery-eureka。
2.启动项目microservice-provider-user。
3.启动项目microservice-consumer-movie-ribbon-hystrix。
4.启动项目microservice-consumer-movie-feign-hystrix-fallback-stream。
5.启动项目microservice-hystrix-turbine。
6.启动microservice-hystrix-dashboard。
7.访问http://localhost:8010/user1,让microservice-consumer-movie-ribbon-hystrix微服务产生监控数据
8.访问http://localhost:8020/user1,让microservice-consumer-movie-feign-hystrix-fallback-stream微服务产生监控数据
9.打开Hystrix Dashboarad首页http://localhost:8030/hystrix.stream,在URL一栏填入http://localhost:8031/turbine.stream,随意指定一个Title并点击Monitor Stream按钮。
使用消息中间件收集数据
先安装ERlang,再安装RabbitMQ
操作流程参考:https://www.cnblogs.com/shanyou/p/4067250.html
修改微服务
1.复制项目microservice-consumer-movie-ribbon-hystrix,将ArtifactId修改为microservice-consumer-movie-ribbon-hystrix-turbine-mq。
2.添加以下依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
3.在配置文件application.yml中添加如下内容,连接RabbitMQ。
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
这样微服务就改造完成了
改造Turbine
1.复制microservice-hystrix-turbine,将ArtifactId修改为microservice-hystrix-turbine-mq。
2.在pom.xml添加
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-turbine-stream</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
同时,删除
<dependency>
<groupId>org.springframework.cloud</groupId>
<<artifactId>spring-cloud-netflix-turbine</artifactId>
</dependency>
3.修改启动类,把@EnableTurbine修改为@EnableTurbineStream。
4.修改配置文件application.yml,添加如下内容。
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
同时删除:
turbine:
app-config: movie-hystrix,movie-feign-hystrixstream
cluster-name-expression: new String('default')
5.测试
启动项目microservice-discovery-eureka。
启动项目microservice-provider-user。
启动项目microservice-consumer-movie-ribbon-hystrix-turbine-mq.
启动项目microservice-hystrix-turbine-mq。
访问http://localhot:8010/user/1,可获得正常结果。
访问http://localhost:8031,会发现Turbine能持续不断的显示监控数据
注意点
本文大部分内容转载自周立的《Spring Cloud与Docker微服务架构实战》