一、简介

Hystrix提供了几乎实时的监控。HystrixCommand和HystrixObserv-ableCommand在执行时,会生成执行结果和运行指标,比如每秒执行的请求数、成功数等,这些监控数据对分析应用系统的状态很有用。


使用Hystrix的模块hystrix-metrics-event-stream,就可将这些监控的指标信息以text/event-stream的格式暴露给外部系统。spring-cloud-starter-hystrix已包含该模块,在此基础上,只须为项目添加spring-boot-starter-actuator,就可使用/hystrix.stream端点获得Hystrix的监控信息了。


我们以前的项目spring-hystrix-consumer中就已经包含了spring-cloud-starter-hystrix、spring-boot-starter-actuator,启动访问:http://localhost:8087/user/1 后,再访问:http://localhost:8087/manage/hystrix.stream ,会重复出现如下内容:

wKiom1maPZvQU-M8AAD290ZwrgM885.jpg

这是因为系统会不断地刷新以获取实时的监控数据.Hystrix的监控指标非常全面,例如HystrixCommand的名称、group名称、断路器状态、错误率、错误数等。

二、使用Hystrix Dashboard可视化监控数据

前面通过访问/hystrix.stream端点获得的数据很难一眼看出系统当前的运行状态,可是使用Hystrix Dashboard可以让监控数据图形化、可视化。

操作:

 1、创建一个maven工程,加入Hystrix Dashboard依赖

    <dependency>
        <groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
    </dependency>

 2、编写启动类,添加@EnableHystrixDashboard注解

 3、配置yml文件端口8086

测试:

 1、访问http://localhost:8087/hystrix.stream,可看到Hystrix Dashbord的主页,如下:

  wKiom1maSoeBBuTuAAFa7YMncZU258.jpg 


 2、随意设置一个title,并点击Monitor Stream,这里Title是:测试,URl是:http://localhost:8087/manage/hystrix.stream

   wKiom1maT7iTdnGsAACpgiSlN9I816.jpg

注意:此处没有将Hystrix Dashboard注册到Eureka Server上,在生产环境中,为了更方便的管理Hystrix Dashboard,可将其注册到Eureka Server上。

三、使用Turbine聚合监控数据

前面使用的/hystrix.stream端点监控单个微服务。然而在使用微服务架构的应用系统一般会包含多个微服务,每个微服务通常都会部署多个实例。如果每次只能查看单个实例的监控数据,就必须在Hystrix Dashboard上切换想要监控的地址,这显然很不方便。

3.1、Turbine简介

Turbine是一个聚合Hystrix监控数据的工具,它可将所有相关/hystrix.stream端点的数据聚合到一个组合的/turbine.stream中,从而让集群的监控更加方便。

wKiom1maU5jBWjnIAAA6qQybiqo607.jpg

3.2、使用Turbine监控多个微服务

a、创建一个maven项目,添加turbine依赖:

<!-- turbine -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-turbine</artifactId>
</dependency>

b、在启动类上添加@EnableTurbine注解

c、编写application.yml配置文件

spring:
  profiles:
    active:
    - dev
  application:
    name: hystrix-turbine
eureka:
  client:
    service-url:
      defaultZone: http://liuy2:5010/eureka/ # 设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址,多个用逗号分隔
  instance:
    prefer-ip-address: true
turbine:
  app-config: hystrix-consumer-movie,ribbon-consumer-movie  # 参数指定了需要收集监控信息的服务名
  cluster-name-expression: "'default'" # 参数指定了集群名称为
---
spring:
  profiles:
    active: dev
server:
  port: 5014

说明:使用上面配置,Turbine会在Eureka Server中找到hystrix-consumer-movie和ribbon-consumer-movie这两个微服务,并聚合这两个微服务的监控数据。

d、测试

 第一步:依次启动Eureka Server(4010)、provide-user(4011)、hystrix-consumer-movie(5012)、ribbon-consumer-movie(5011)、hystrix-turbine(5014)、hystrix-dashboard(5013)

 第二步:访问http://localhost:5012/user/1,让hystrix-consumer-movie微服务产生监控数据

 第三步:访问http://localhost:5011/user/1,让ribbon-consumer-movie微服务产生监控数据

 第四步:打开Hystrix Dashboard首页http://localhost:5013/hystrix.stream,在URL栏填写http://localhost:5014/turbine.stream,随意填写title,点击Monitor Stream后出现如下图:

  wKiom1mbirHhhFJjAACC2V2qPCM523.png

问题:在一些场景下,如微服务与Turbine网络不通,监控数据怎么处理? -- 使用消息中间件收集数据

3.3、使用rabbitmq收集监控数据

改造微服务:hystrix-consumer-movie

 a、添加以下依赖

    <dependency>
        <groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-netflix-hystrix-stream</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
    </dependency>

 b、在application.yml中添加

    spring:
      rabbitmq:
        host: 192.168.175.13
        port: 5672
        username: liuy
        password: 123456

改造:hystrix-turbine

 a、添加以下依赖

    <dependency>
        <groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-turbine-stream</artifactId>
    </dependency>
    <!-- rabbit -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
    </dependency>

   注意:此处删除spring-cloud-starter-turbine依赖

 b、修改启动类,将@EnableTurbine改成@EnableTurbineStream

 c、修改application.yml

    spring:
      rabbitmq:
        host: 192.168.175.13
        port: 5672
        username: liuy
        password: 123456

   同时删除:turbine


测试:

 第一步:依次启动Eureka Server(4010)、provide-user(4011)、hystrix-dashboard(5013)、hystrix-consumer-movie-rabbitmq(5020)、hystrix-turbine-rabbitmq(5021)

 第二步:访问http://localhost:5020/user/1,可正常获取结果

 第三步:打开Hystrix Dashboard首页http://localhost:5013/hystrix.stream,在URL栏填写http://localhost:5021/,随意填写title,点击Monitor Stream后出现如下图:

  wKioL1mbv4GAwOJoAACbz850ojw096.png