Spring-cloud学习笔记— Hystrix的Dashboard监控仪表盘
1. Hystrix的Dashboard监控仪表盘作用
- 如果我们想看到
Hystrix
相关数据,⽐如有多少请求、多少成功、多少失败、多少降级等,其实是基于SpringBoot健康监控的 - 引入SpringBoot健康监控后,访问
/actuator/hystrix.stream
接⼝可以获取到监控的⽂字信息,但是不直观,所以Hystrix官⽅还提供了基于图形化的DashBoard(仪表板)
监控平台。 Hystrix仪表板
可以显示每个断路器(被@HystrixCommand注解的⽅法)
的状态。Hystrix
健康监控
2. 创建Hystrix的DashBoard(仪表板)服务
注意: Hystrix官⽅提供的基于图形化的DashBoard(仪表板)
健康监控平台也是一个单独的服务,所以也要先引入jar包
,之后添加注解开启监控平台
引入依赖
<dependencies> <!--hystrix--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <!--hystrix 仪表盘--> <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-eureka-client</artifactId> </dependency> </dependencies>
启动类添加@EnableHystrixDashboard激活仪表盘
@SpringBootApplication // 开启hystrix dashboard @EnableHystrixDashboard public class HystrixDashboardApplication9000 { public static void main(String[] args) { SpringApplication.run(HystrixDashboardApplication.class, args); } }
配置yml文件
server: port: 9000 Spring: application: name: lagou-cloud-hystrix-dashboard eureka: client: # eureka server的路径 serviceUrl: # 把 eureka 集群中的所有 url 都填写了进来,也可以只写一台,因为各个 eureka server 可以同步注册表 defaultZone: http://lagoucloudeurekaservera:8761/eureka/,http://lagoucloudeurekaserverb:8762/eureka/ instance: #使用ip注册,否则会使用主机名注册了(此处考虑到对老版本的兼容,新版本经过实验都是ip) prefer-ip-address: true #自定义实例显示格式,加上版本号,便于多版本管理,注意是ip-address,早期版本是ipAddress instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@
在被监测的微服务中注册监控servlet(⾃动投递微服务,监控数据就是来⾃于这个微服务)
/** * 在被监控的微服务中注册一个serlvet,后期我们就是通过访问这个servlet来获取该服务的Hystrix监控数据的 * 前提:被监控的微服务需要引入springboot的actuator功能 * @return */ @Bean public ServletRegistrationBean getServlet(){ HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet(); ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet); registrationBean.setLoadOnStartup(1); registrationBean.addUrlMappings("/actuator/hystrix.stream"); registrationBean.setName("HystrixMetricsStreamServlet"); return registrationBean; }
被监控微服务发布之后,可以直接访问监控servlet(ip+端口号/actuator/hystrix.stream)
,但是得到的数据并不直观,后期可以结合仪表盘更友好的展示
3. 访问Dashboard仪表盘
- 访问测试 :http://localhost:9000/hystrix
- 输⼊监控的微服务端点地址,展示监控的详细数据,⽐如监控服务消费者
http://localhost:8090/actuator/hystrix.stream
百分⽐
:10s内错误请求百分⽐实⼼圆
:⼤⼩
:代表请求流量的⼤⼩,流量越⼤球越⼤颜⾊
:代表请求处理的健康状态,从绿⾊到红⾊递减,绿⾊代表健康,红⾊就代表很不健康曲线波动图
:记录了2分钟内该⽅法上流量的变化波动图,判断流量上升或者下降的趋势