本文是 Spring Cloud 专栏的第六篇文章, 了解前五篇文章内容有助于更好的理解本文:
Spring Cloud 第一篇 | Spring Cloud 前言及其常用组件介绍概览
Spring Cloud 第二篇 | 使用并认识 Eureka 注册中心
Spring Cloud 第三篇 | 搭建高可用 Eureka 注册中心
Spring Cloud 第四篇 | 客户端负载均衡 Ribbon
Spring Cloud 第五篇 | 服务熔断 Hystrix
一, Hystrix 仪表盘监控
Hystrix 仪表盘( Hystrix Dashboard), 就像汽车的仪表盘实时显示汽车的各 项数据一样, Hystrix 仪表盘主要用来监控 Hystrix 的实时运行状态, 通过它我们可以看到 HystriX 的各项指标信息, 从而快速发现系统中存在的问题进而解决 要使用 Hystriⅸ 仪表盘功能, 我们首先需要有一个 Hystrix Dashboard, 这个功能我们可以在原来的消费者应用上添加, 让原来的消费者应用具备 Hysr 仪表 盘功能, 但一般地微服务架构思想是推崇服务的拆分, Hystrix Dashboard 也是一个服务, 所以通常会单独创建一个新的工程专门用做 Hystrix Dashboard 服务, Hystrix Dashboard 所处的作用如图
1, 新建一个模块命名为(springcloud-hystrix-dashboard)
2, 添加依赖 hystrix 的 Dashboard 依赖
- <!--hystrix-dashboard 功能的起步依赖, 仪表盘功能 -->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
- </dependency>
此时依赖可能下载不下来, 可以添加阿里云仓库
- <repositories>
- <repository>
- <id>alimaven</id>
- <name>aliyun maven</name>
- <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
- <releases>
- <enabled>true</enabled>
- </releases>
- <snapshots>
- <enabled>false</enabled>
- </snapshots>
- </repository>
- </repositories>
3, 在启动类上添加注解 @EnableHystrixDashboard
4, 端口配置为 3721, 到此我们的 hystrix 监控服务就搭建完毕了, 启动访问 http://localhost:3721/hystrix
Hystrix 仪表盘工程已经创建好了, 我们需要有一个服务, 让这个服务提供一个路径为 / actuator/hystrix.stream 接口, 然后就可以使用 Hystrix 仪表盘来对该服务进行监控了
5, 改造消费者(springcloud-service-consumer)
我们改造消费者服务, 让其能提供 / actuator/hystrix.stream 接口, 步骤如下:
5-1, 消费者项目需要有 Hystrix 的依赖, 在之前案例中使用服务熔断的时候已经加过了
5-2, 需要有一个 springboot 的服务监控依赖, 可以直接添加到父模块依赖中
<!--springboot 提供的服务健康检查监控的依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
5-3, 配置文件需要配置 springboot 监控的端点访问权限
# 用来暴露 endpoints 的, 由于 endpoints 中会包含很多敏感信息, # 除了 health 和 info 两个支持直接访问外, 其他的默认不能直接访问, # 所以我们让他们都能访问(*), 或者指定 springboot 的监控端点访问权限, #* 表示所有的端点都允许访问, 如果只写 hystrix.stream, 他会把默认的 info,health 端点关闭 management: endpoints: web: exposure: include: ["info","health","hystrix.stream"]
从控制台日志上可以看到
2019-08-07 15:58:31.187 INFO 7396 --- [ost-startStop-1] o.s.b.a.e.Web.ServletEndpointRegistrar : Registered '/actuator/hystrix.stream' to hystrix.stream-actuator-endpoint
6, 第一次访问, 要先访问其他带用熔断器的接口, 访问入口: http://localhost:9090/actuator/hystrix.stream
注意: 这里有一个细节需要注意, 要访问 / hystrix.stream 接口, 首先访问消费者 (springcloud-service-consumer) 工程中任意的一个带有熔断器的接口, 否则直接访问 / hystrix.stream 接口时, 会输出一连串 ping: ping: ....
7, 把 http://localhost:9090/actuator/hystrix.stream 填写到 Hystrix Dashboard 仪表盘上, 直接在网上找了一个详细描述图, 如下图
详细参考案例源码: https://gitee.com/coding-farmer/spirngcloud-learn
来源: https://www.cnblogs.com/coding-farmer/p/12032403.html