博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springCloud(12):使用Hystrix实现微服务的容错处理-Hystrix的监控
阅读量:6881 次
发布时间:2019-06-26

本文共 3909 字,大约阅读时间需要 13 分钟。

一、简介

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,启动访问: 后,再访问: ,会重复出现如下内容:

这是因为系统会不断地刷新以获取实时的监控数据.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、访问,可看到Hystrix Dashbord的主页,如下:

   

 2、随意设置一个title,并点击Monitor Stream,这里Title是:测试,URl是:

   

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

三、使用Turbine聚合监控数据

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

3.1、Turbine简介

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

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)

 第二步:访问,让hystrix-consumer-movie微服务产生监控数据

 第三步:访问,让ribbon-consumer-movie微服务产生监控数据

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

  

问题:在一些场景下,如微服务与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)

 第二步:访问,可正常获取结果

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

  

本文转自我爱大金子博客51CTO博客,原文链接http://blog.51cto.com/1754966750/1949857如需转载请自行联系原作者
我爱大金子
你可能感兴趣的文章
【原创翻译】布尔值(boolean)
查看>>
关于scrapy的piplines
查看>>
RESTful API
查看>>
通向架构师的道路(第一天)之Apache整合Tomcat - lifetragedy的专栏 - 博客频道 - CSDN.NET...
查看>>
Javascript创建对象的7种模式
查看>>
Shell工作笔记01
查看>>
项目、软件开发过程中版本术语
查看>>
CSS实现背景透明,文字不透明(各浏览器兼容)
查看>>
【转】[大学引导]超级链接、字体颜色、音乐播放公式
查看>>
T-SQL中INSERT、UPDATE
查看>>
Linux下Nginx服务器配置Modsecurity实现Web应用防护系统
查看>>
openSUSE13.2安装ruby和rails
查看>>
python 高级函数
查看>>
F.Cards with Numbers
查看>>
简单入门Buffer
查看>>
OO第四阶段总结
查看>>
javascript总结02
查看>>
创建windows服务
查看>>
HTML5 入门基础
查看>>
【转载】读懂IL代码就这么简单(二)
查看>>