springCloud(12):使用Hystrix实现微服务的容错处理-Hystrix的监控

本文涉及的产品
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
简介:

一、简介

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依赖

1
2
3
4
     <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依赖:

1
2
3
4
5
<!-- turbine -->
< dependency >
     < groupId >org.springframework.cloud</ groupId >
     < artifactId >spring-cloud-starter-turbine</ artifactId >
</ dependency >

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

c、编写application.yml配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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、添加以下依赖

1
2
3
4
5
6
7
8
     < 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中添加

1
2
3
4
5
6
     spring:
       rabbitmq:
         host: 192.168.175.13
         port: 5672
         username: liuy
         password: 123456

改造:hystrix-turbine

 a、添加以下依赖

1
2
3
4
5
6
7
8
9
     < 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

1
2
3
4
5
6
     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

本文转自我爱大金子博客51CTO博客,原文链接http://blog.51cto.com/1754966750/1949857如需转载请自行联系原作者

我爱大金子
相关实践学习
RocketMQ一站式入门使用
从源码编译、部署broker、部署namesrv,使用java客户端首发消息等一站式入门RocketMQ。
消息队列 MNS 入门课程
1、消息队列MNS简介 本节课介绍消息队列的MNS的基础概念 2、消息队列MNS特性 本节课介绍消息队列的MNS的主要特性 3、MNS的最佳实践及场景应用 本节课介绍消息队列的MNS的最佳实践及场景应用案例 4、手把手系列:消息队列MNS实操讲 本节课介绍消息队列的MNS的实际操作演示 5、动手实验:基于MNS,0基础轻松构建 Web Client 本节课带您一起基于MNS,0基础轻松构建 Web Client
相关文章
|
18天前
|
监控 Java 微服务
第八章 Spring Cloud 之 Hystrix
第八章 Spring Cloud 之 Hystrix
14 0
|
1月前
Springcloud-ribbon和hystrix配置
Springcloud-ribbon和hystrix配置
9 0
|
2月前
|
SpringCloudAlibaba Java 测试技术
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(六)Hystrix(豪猪哥)的使用
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(六)Hystrix(豪猪哥)的使用
46 1
|
2月前
|
监控 微服务
Hystrix熔断器设计思想(学习笔记)附(服务监控hystrixDashboard识图)
Hystrix熔断器设计思想(学习笔记)附(服务监控hystrixDashboard识图)
18 0
|
4月前
|
监控 Java Sentinel
springcloud4-服务熔断hystrix及sentinel
springcloud4-服务熔断hystrix及sentinel
26 0
|
4月前
|
Prometheus 监控 Cloud Native
SpringCloud微服务实战——搭建企业级开发框架(四十五):【微服务监控告警实现方式二】使用Actuator(Micrometer)+Prometheus+Grafana实现完整的微服务监控
无论是使用SpringBootAdmin还是使用Prometheus+Grafana都离不开SpringBoot提供的核心组件Actuator。提到Actuator,又不得不提Micrometer,从SpringBoot2.x开始,Actuator的功能实现都是基于Micrometer的。
270 0
|
5月前
|
监控 数据可视化 Java
Spring Cloud Hystrix:服务容错保护
Spring Cloud Hystrix:服务容错保护
161 0
|
5月前
|
Java 微服务 Spring
Spring Cloud OpenFeign:基于Ribbon和Hystrix的声明式服务调用
Spring Cloud OpenFeign:基于Ribbon和Hystrix的声明式服务调用
45 0
|
5月前
|
监控 负载均衡 数据可视化
SpringCloud - Hystrix断路器-服务熔断与降级和HystrixDashboard
SpringCloud - Hystrix断路器-服务熔断与降级和HystrixDashboard
30 0
|
3天前
|
存储 监控 API
构建高效微服务架构:后端开发的现代实践
【5月更文挑战第9天】 在本文中,我们将深入探讨如何在后端开发中构建一个高效的微服务架构。通过分析不同的设计模式和最佳实践,我们将展示如何提升系统的可扩展性、弹性和维护性。我们还将讨论微服务架构在处理复杂业务逻辑和高并发场景下的优势。最后,我们将分享一些实用的工具和技术,以帮助开发者实现这一目标。