第三篇 服务的监控Hystrix Dashboard (Hystrix)

    科技2024-05-17  86

    一、Hystrix Dashboard是什么

    Hystrix提供了对于微服务调用状态的监控信息,但是需要结合spring-boot-actuator模块一起使用。

    Hystrix Dashboard主要用来实时监控Hystrix的各项指标信息。通过Hystrix Dashboard反馈的实时信息,可以帮助我们快速发现系统中存在的问题

    二、我们先来看下Hystrix Dashboard长什么样子

    1、新建一个maven工程,命名为hystrix-dashboard,pom中引入了Hystrix Dashboard的相关依赖。

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>my-springcloud</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>hystrix-dashboard</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> </project>

    2、项目启动类:AppHystrixDashboard.java。启动类中增加了@EnableHystrixDashboard注解

    @SpringBootApplication @EnableHystrixDashboard public class AppHystrixDashboard { public static void main(String[] args) { SpringApplication.run(AppHystrixDashboard.class); } }

    3、配置文件:application.yml

    server: port: 9000 hystrix: dashboard: proxy-stream-allow-list: localhost

    启动项目,浏览器访问 http://localhost:9000/hystrix 即可看到Hystrix Dashboard的监控首页

    从首页中我们可以看出并没有具体的监控信息,从页面上的文件内容我们可以知道,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

     

    三、我们先来看下单个服务实例的监控

    从http://hystrix-app:port/actuator/hystrix.stream连接中可以看出,Hystrix Dashboard监控单节点实例需要访问实例的actuator/hystrix.stream接口,我们自然就需要为服务实例添加这个端点

    第一步、我们在之前的ribbon微服务中添加springboot监控的依赖

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>my-springcloud</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>ribbon-consumer</artifactId> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <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> </dependencies> </project>

    第二步、开放所有端点

    spring: application: name: ribbon-consumer server: port: 3000 #指定服务中心的地址 eureka: client: service-url: defaultZone: http://localhost:1111/eureka/ #所有端点开放 management: endpoints: web: exposure: include: '*'

    因为spring Boot 2.0.x以后的actuator只暴露了info和health2个端点,这里我们把所有端点开放,include: '*'代表开放所有端点

    第三步、开启ribbon消费者、Hystrix Dashboard

    浏览器访问http://localhost:3000/actuator/hystrix.stream会看到如下页面,因为监控的实例本身还没有调用任何服务,所以监控端点也没记录任何信息

    新开一个浏览器tab页,访问下ribbon的服务,重新刷新下刚才的页面可以看到已经有数据返回了,说明我们的监控端点生效了

    从浏览器中的信息可以看出这些信息是刚刚请求微服务时所记录的监控信息,但是直接去看这些信息肯定是不友好的,所以Hystrix Dashboard就派上用场了

    在Hystrix Dashboard中间这个输入框中,填入服务监控地址,也就 http://localhost:3000/actuator/hystrix.stream

    点击Monitor Stream按钮,就会跳转到具体的监控页面:

     

    四、Hystrix仪表盘相关概念

    多次请求,会发现监控页面的数据也在实时的更新

     

    下面来介绍下图形中各元素的具体含义:

    实心圆: 共有两种含义。通过颜色的变化代表了实例的健康程度,它的健康度从绿色、黄色、橙色、红色递减。通过圆的大小来代表请求流量的大小,流量越大该实心圆就越大。所以通过该实心圆的展示,就可以在大量的实例中快速的发现故障实例和高压力实例。曲线: 记录2分钟内流量的相对变化,可以通过它来观察到流量的上升和下降趋势。其他数量指标:

    Processed: 0.011, SQL: 8