Hystrix监控的配置详解

来源:互联网 发布:期货交易分析软件ipad 编辑:程序博客网 时间:2024/06/09 20:13

hystrix监控.png

在微服务架构中,hystrix处理容错外,还有实时监控功能,在服务发生调用时,会将每秒请求数、成功请求数等运行指标记录下来。

本文示例代码:springcloud-demo
其中本文相关的项目有:
- 服务发现 Eureka Server: discovery
- 链路追踪 sleuth+zipkin:trace
- 服务提供者:hello
- 服务提供者: world
- 服务消费者: helloworld
- 服务消费者: helloworld-feign
- 服务调用监控: hystrix-dashboard
- 监控聚合: hystrix-turbine
- 监控聚合(消息中间件): hystrix-turbine-mq

通过接口的监控

启动helloworld项目后,访问http://localhost:8020/message后,再次访问http://localhost:8020/hystrix.stream可以看到界面不断地输出监控日志,监控日志里包含了各种指标(各种指标信息请参考官方文档)。

按照同样的步骤操作helloworld-feign项目后,却发现找不到该页面,这是因为需要做一些如下配置:
pom.xml引入hystrix依赖

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

在启动类上加上@EnableCircuitBreaker注解,再次执行上述操作,界面不断输出监控日志。
helloworld-feign的hystrix监控日志.png

使用hystrix-board对监控进行图形化展示

上面的日志信息不够直观,借助hystrix-dashboard可对监控进行图形化展示。
- 在service创建hystrix-dashboard项目
- 引入hystrix-dashboard依赖

    <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>        </dependency>
  • 启动类配置@EnableHystrixDashboard注解(同时配置@EnableDiscoveryClient向注册中心注册,方便管理)
  • 配置文件设置端口
server:  port: 8087eureka:  client:    serviceUrl:      defaultZone: http://localhost:8761/eureka/  instance:    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${server.port}spring:  application:    name: hystrix-dashboard
  • 测试结果
    启动hystrix-dashboard后,输入http://localhost:8087/hystrix地址,出现文章第一张图所示的界面。在第一个文本框输入http://localhost:8020/hystrix.stream或者http://localhost:8030/hystrix.stream后点击Monitor Stream则会显示如下监控结果:

20.png

以上的hystrix-dashboard每次只能输入一个监控地址,在微服务架构中往往有很多无法需要监控,该怎么办呢?

可以使用Turbine聚合监控数据,让hystrix-dashboard显示这个聚合数据后的地址。

Turbine聚合监控数据

  • 创建一个hystrix-turbine项目,引入如下maven依赖
<dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-starter-turbine</artifactId>        </dependency>
  • 启动类上配置@EnableTurbine注解
  • 配置文件application.yml指明要从收集哪些微服务的监控数据
server:  port: 8088eureka:  client:    serviceUrl:      defaultZone: http://localhost:8761/eureka/  instance:    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${server.port}    prefer-ip-address: truespring:  application:    name: hystrix-turbineturbine:  app-config: helloworld,helloworldfeign  cluster-name-expression: "'default'"

注意turbine的配置,这里收集helloworldhellowordfeign日志
- 启动项目hystrix-turbine后,在hystrix-dashboard中输入http://localhost:8089/turbine.stream,则展示如下聚合结果:

使用Turbine聚合hystrix监控数据

以上Turbine聚合微服务的监控数据,然后在hystrix-dashboard展示多个微服务的实时监控数据。 但是Turbine也有它的局限性,比如服务之间无法通信,服务不在Eureka Server上注册,则Turbine无法收集到微服务的日志。那么这种情况下,需要借助消息中间件来解决。

通过消息中间件RabbitMQ收集监控数据

微服务在发生调用时,将监控数据存储到RabbitMQ,监控从RabbitMQ获取数据进行实时展示,这样有利于微服务和监控端进行解耦,可以解决不在服务中心注册的服务的监控数据也可以被收集到。

  • 安装RabbitMQ
    使用如下docker命令安装RabbitMQ,并暴露相应的端口(本人开发机为linux环境,docker的使用可以极大提高软件安装和项目部署的效率,推荐使用docker)
 docker run -d --name rabbitmq -p 5673:5672 -p 15673:15672 docker.io/rabbitmq:3-management

这样向外暴露5673的连接端口和15673的web端口,默认用户名密码是guest
安装后的RabbitMQ.png

  • 微服务端修改,以便向RabbitMQ传输监控日志
    本文以helloworld为例改造微服务,pom中引入依赖
 <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>

配置文件中加上连接RabbitMQ的配置

spring:  rabbitmq:    host: localhost    port: 5673    username: guest    password: guest

重启helloworld,并在浏览器输入http://localhost:8020/message形成监控数据。

  • 新建hystrix-turbine-mq项目
    项目中引入turbine和rabbitmq依赖
    <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-starter-turbine-stream</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-starter-stream-rabbit</artifactId>        </dependency>

启动类上配置@EnableTurbineStream注解
配置文件:

server:  port: 8089eureka:  client:    serviceUrl:      defaultZone: http://localhost:8761/eureka/  instance:    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${server.port}    prefer-ip-address: truespring:  application:    name: hystrix-turbine-mq  rabbitmq:    host: localhost    port: 5673    username: guest    password: guest

这样指明了监控的数据来源。
- 测试结果
启动hystrix-dashboard后,输入http://localhost:8089 即可展示如下的监控结果:
RabbitMQ收集监控数据.png

本文参考了《Spring cloud 与Docker 微服务实战》这本书。

阅读全文
0 0