SpringCloud - 断路器监控Hystrix Dashboard使用详解1(对单个实例做监控)
作者:hangge | 2020-07-22 08:10
一、对单个实例做监控
1,Hystrix Dashboard 介绍
- Hystrix Dashboard 是一款针对 Hystrix 进行实时监控的工具。
- 通过 Hystrix Dashboard 我们可以在直观地看到各 HystrixCommand 和 HystrixObservableCommand 实例的实时数据,比如请求响应时间、请求成功率等等。帮助我们快速发现系统中存在的问题。
2,准备工作
(1)在使用 Hystrix Dashboard 监控单实例节点之前,我们先准备并启动如下几个工程:
这些工程的实现可以参考我之前写的文章:
SpringCloud - 服务注册与发现组件Eureka的使用详解4(服务发现与消费、负载均衡)
- eureka-server 工程:服务注册中心,端口为 1111
- hello-service 工程:HELLO-SERVICE 的服务单元,两个实例启动端口分别为 8081 和 8082
- ribbon-consume 工程:使用 Ribbon 实现的服务消费者,端口为 9000
<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>
(3)接着在 application.properties 中自定义需要暴露哪些端点,这里我们暴露所有端点:
management.endpoints.web.exposure.include=*
(4)最后在项目的主类上添加 @EnableCircuitBreaker 注解开启断路器功能:
@SpringBootApplication @EnableDiscoveryClient @EnableCircuitBreaker public class RibbonConsumerApplication { @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(RibbonConsumerApplication.class, args); } }
(5)重启实例,通过 /actuator 接口查看当前暴露的端点,其中 hystrix.stream 便是后面 Hystrix Dashboard 用来展现监控信息的接口。
(6)使用浏览器访问 http://localhost:9000/actuator/hystrix.stream 会看到如下页面,因为监控的实例本身还没有调用任何服务,所以监控端点也没记录任何信息。
3,搭建 Hystrix Dashborad 项目
(1)接下来我们要构建一个 Hystrix Dashborad 项目对 ribbon-consume 实例进行监控,整个架构如下图所示:
(2)首先创建一个标准的 Spring Boot 工程,编辑 pom.xml 文件,添加相关依赖:
<dependencies> <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> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
(3)接着在主类上添加 @EnableHystrixDashboard 注解启动 Hystric Dashboard 功能:
@SpringBootApplication @EnableHystrixDashboard public class HystrixDashboardApplication { public static void main(String[] args) { SpringApplication.run(HystrixDashboardApplication.class, args); } }
(4)编辑 application.properties 配置文件,设置一个未被占用的端口(此步不是必须的)
spring.application.name=hystric-dashboard server.port=2001
(5)启动项目,浏览器访问 http://localhost:2001/hystrix 即可看到 Hystrix Dashboard 的监控首页。
(1)可以看到 Hystrix Dashboard 共支持三种不同的监控方式:
- 默认的集群监控: http://turbine-hostname:port/turbine.stream
- 指定的集群监控: http://turbine-hostname:port/turbine.stream?cluster=[clusterName]
- 单体应用的监控: http://hystrix-app:port/actuator/hystrix.stream
- 最上面的输入框: 输入上面所说的三种监控方式的地址,用于访问具体的监控信息页面。
- Delay: 该参数用来控制服务器上轮询监控信息的延迟时间,默认 2000 毫秒。
- Title: 该参数对应头部标题 Hystrix Stream 之后的内容,默认会使用具体监控实例的 Url。
(6)Hystrix Dashboard 中间这个输入框中,填入之前 ribbon-consume 服务的监控地址,也就是 http://192.168.60.1:9000/actuator/hystrix.stream,点击 Monitor Stream 按钮,就会跳转到具体的监控页面:
注意:服务地址要填写具体的 ip 地址,如果填写 localhost 或者 127.0.0.1 的话可能会出现“Unable to connect to Command Metric Stream.”错误
(7)页面显示“Loading...”是因为监控的实例本身还没有调用任何服务,所以监控端点也没记录任何信息。我们访问几下 ribbon-consume 服务之前定义的接口 http://l192.168.60.1:9000/hello-consumer 来调用下服务,可以看到监控页面显示如果数据,并且数据也在实时的更新:
附:Hystrix 仪表盘各元素含义
- 实心圆: 共有两种含义。通过颜色的变化代表了实例的健康程度,它的健康度从绿色、黄色、橙色、红色递减。通过圆的大小来代表请求流量的大小,流量越大该实心圆就越大。所以通过该实心圆的展示,就可以在大量的实例中快速的发现故障实例和高压力实例。
- 曲线: 记录2分钟内流量的相对变化,可以通过它来观察到流量的上升和下降趋势。
- 其他数量指标如下图所示:
全部评论(0)