Hystrix监控的配置详解
来源:互联网 发布:期货交易分析软件ipad 编辑:程序博客网 时间:2024/06/09 20:13
在微服务架构中,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
注解,再次执行上述操作,界面不断输出监控日志。
使用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
则会显示如下监控结果:
以上的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
的配置,这里收集helloworld
和hellowordfeign
日志
- 启动项目hystrix-turbine后,在hystrix-dashboard中输入http://localhost:8089/turbine.stream
,则展示如下聚合结果:
以上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传输监控日志
本文以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
即可展示如下的监控结果:
本文参考了《Spring cloud 与Docker 微服务实战》这本书。
- Hystrix监控的配置详解
- Hystrix系列-4-Hystrix的动态配置
- springcloud-dashboard、turbine监控hystrix的使用
- Hystrix系列-3-Hystrix的配置-通过代码构造配置
- Hystrix监控数据聚合
- springboot admin 监控hystrix
- Spring cloud系列九 Hystrix的配置属性优先级和详解
- springCloud学习02之断路器Hystrix-turbine监控-ribbo/feign对Hystrix的支持
- Hystrix配置介绍
- hystrix配置属性说明
- hystrix 集群监控 turbine 及aop 开发hystrix
- Spring Cloud 的 Hystrix 更多参数配置信息
- Nagios远程监控软件的安装与配置详解
- Nagios远程监控软件的安装与配置详解(上)
- Nagios远程监控软件的安装与配置详解(中)
- Nagios远程监控软件的安装与配置详解(下)
- Nagios远程监控软件的安装与配置详解
- Nagios远程监控软件的安装与配置详解
- 【数据库】sql拼接字符串
- 创建springbootdemo后运行报MongoSocketOpenException错误解决方法
- CentOS安装rz sz命令
- Java中对象的深复制(深克隆)和浅复制(浅克隆)介绍
- mysql查看数据库和表的占用空间大小 作者:@狮子XL 本文为作者原创,转载请注明出处:http://www.cnblogs.com/iiiiher/p/5853133.html 目录 mysq
- Hystrix监控的配置详解
- 使用zmq,probuf,缓冲池实现序列化和反序列化框架(二)-ZMQ介绍
- .NET 环境中使用RabbitMQ
- VirtualBox配置CentOS7的共享文件夹
- CSS3伪类选择器
- m25p80.c debug on Micron spi nor_flash(MT25Q128)
- 传记读书分享
- 软件工程-1:Java开发环境的构建(jdk+eclipse)
- String和Date、Timestamp之间的转换