SpringCloud(Hoxton.SR3)基础篇:第七章、Hystrix的Dashboard和Turbine使用
一 、turbine简介
Hystrix Dashboard它主要用来实时监控Hystrix的各项指标信息。引入Turbine,通过它来汇集监控信息(多个微服务集群的hystrix.stream信息),并将聚合后的信息提供给Hystrix Dashboard来集中展示和监控。
二、项目介绍
引入Turbine来聚合consumer-movie-ribbon-with-hystrix,consumer-movie-feign-with-hystrix服务的监控信息,并输出给Hystrix Dashboard来进行展示。
相关微服务
- 注册中心eureka
- 服务提供微服务provider-user
注意Feign获取hystrix.stream信息需要添加以下bean
/** * 低版本直接启动即可使用 http://ip:port/hystrix.stream 查看监控信息 * 高版本需要添加本方法方可使用 http://ip:port/actuator/hystix.stream 查看监控信息 * * @return */ @Bean public ServletRegistrationBean<HystrixMetricsStreamServlet> getServlet() { HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet(); ServletRegistrationBean<HystrixMetricsStreamServlet> registrationBean = new ServletRegistrationBean<HystrixMetricsStreamServlet>(streamServlet); registrationBean.setLoadOnStartup(1); registrationBean.addUrlMappings("/actuator/hystrix.stream"); registrationBean.setName("HystrixMetricsStreamServlet"); return registrationBean; }
三、Turbine和Dashboard搭建
(1) pom.xml引入依赖
<!-- turbine依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-turbine</artifactId> </dependency> <!-- dashboard依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency>
(2)application.yml配置
server: port: 8030 spring: application: name: springcloud-hystrix-turbine #eureka相关配置 eureka: client: service-url: defaultZone: http://user:password123@localhost:8761/eureka instance: prefer-ip-address: true #Turbine相关配置 turbine: aggregator: clusterConfig: default appConfig: consumer-movie-ribbon-with-hystrix,consumer-movie-feign-with-hystrix clusterNameExpression: "'default'" #new String("default")
(3)springboot启动类
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; import org.springframework.cloud.netflix.turbine.EnableTurbine; @SpringBootApplication //启用Dashboard注解 @EnableHystrixDashboard //启用Turbine注解 @EnableTurbine public class HystrixTurbineApplication { public static void main(String[] args) { SpringApplication.run(HystrixTurbineApplication.class, args); } }
(4)启动测试
- 启动eureka
- 启动provider-user微服务
- 启动consumer-movie-feign-with-hystrix启动两个服务,端口(8040)、端口(8041)用来模拟集群
- 启动consumer-movie-ribbon-with-hystrix启动两个服务,端口(8010)、端口(8011)用来模拟集群
- 启动springcloud-hystrix-turbine服务
全部服务启动成功,eureka界面如下
测试单个服务的hystrix.stream数据获取浏览器输入:http://localhost:8040/actuator/hystrix.stream
ps:浏览器用chrome,用Microsoft Edge浏览器(win10)会直接下载文件。获取数据成功截图
测试turbine,浏览器输入:http://localhost:8030/turbine.stream
浏览器输入: http://localhost:8030/hystrix 打开Dashboard界面,输入turbine收集地址http://localhost:8030/turbine.stream
点击Monitor Stream按钮,若是不统计对应节点,显示图表,可能是未进行服务访问调用。到各个消费者服务(ribbon,feign)进行服务调用例如:http://localhost:8010/movie/3
各个指标的含义参见下图。
四、修改context-path导致hystrix.stream地址变更
相关修改内容如下
(1)新建服务consumer-movie-ribbon-with-hystrix2,修改application.yml
server:
port: 8011
#增加context-path访问路径 地址变为 http://localhost:8011/ribbon/movie/1
servlet:
context-path: /ribbon
(2)新建springcloud-hystrix-turbine2,修改application.yml,测试时为单个微服务
#Turbine相关配置
turbine:
aggregator:
clusterConfig: CONSUMER-MOVIE-RIBBON-WITH-HYSTRIX2
appConfig: consumer-movie-ribbon-with-hystrix2
#修改turbine访问微服务hystrix.stream的路径
instanceUrlSuffix:
CONSUMER-MOVIE-RIBBON-WITH-HYSTRIX2: ribbon/actuator/hystrix.stream
(3)测试结果
测试服务:http://localhost:8011/ribbon/movie/3
测试Hystrix.stream地址:http://localhost:8011/ribbon/actuator/hystrix.stream
测试turbine,地址:http://localhost:8030/turbine.stream?cluster=CONSUMER-MOVIE-RIBBON-WITH-HYSTRIX2。用dashboard展示
五、独立hystrix.stream数据获取端口
相关修改内容如下
(1)新建服务consumer-movie-ribbon-with-hystrix3,修改application.yml.添加management.server.port配置.
server:
port: 8011
#增加context-path访问路径 地址变为 http://localhost:8011/ribbon/movie/1
servlet:
context-path: /ribbon
spring:
application:
name: consumer-movie-ribbon-with-hystrix3
#eureka客户端连接配置
eureka:
client:
healthcheck:
enabled: true
service-url:
#注册中心地址
defaultZone: http://user:password123@localhost:8761/eureka
instance:
#将ip注册到eureka上
prefer-ip-address: true
#微服务向eureka注册实例名${spring.cloud.client.ip-address} 表示ip地址 spring2.0以上为ip-address
instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${spring.application.instance_id:${server.port}}
#修改home-page-url-path告诉eureka更改homepageUrlPath路径为 localhost:8011/ribbon
home-page-url-path: /ribbon
#开放health,info,hystrix.stream页面路径访问 http://localhost:8011/actuator/hystrix.stream
#spring-boot-starter-acturator
management:
server:
port: 8081
endpoints:
web:
exposure:
include: health,info,hystrix.stream #根据需求增删路径
(3)新建springcloud-hystrix-turbine3,修改application.yml,测试时为单个微服务
server:
port: 8030
spring:
application:
name: springcloud-hystrix-turbine3
#eureka相关配置
eureka:
client:
service-url:
defaultZone: http://user:password123@localhost:8761/eureka
instance:
prefer-ip-address: true
#Turbine相关配置
turbine:
aggregator:
clusterConfig: CONSUMER-MOVIE-RIBBON-WITH-HYSTRIX3
appConfig: consumer-movie-ribbon-with-hystrix3
#显示日志
logging:
level:
root: INFO
com.netflix.turbine.monitor: DEBUG
(4)测试
测试服务:http://localhost:8011/ribbon/movie/3
测试Hystrix.stream地址:http://localhost:8081/actuator/hystrix.stream
测试turbine,地址:http://localhost:8030/turbine.stream?cluster=CONSUMER-MOVIE-RIBBON-WITH-HYSTRIX3。用dashboard展示
参考文献:https://blog.csdn.net/chengqiuming/article/details/81588477