前面一章,咱们讲解了如何整合
Hystrix
。而在实际状况下,使用了Hystrix
的同时,还会对其进行实时的数据监控,反馈各种指标数据。今天咱们就将讲解下Hystrix Dashboard
和Turbine
.其中Hystrix Dashboard
是一款针对Hystrix
进行实时监控的工具,经过Hystrix Dashboard
咱们能够在直观地看到各Hystrix Command
的请求响应时间, 请求成功率等数据,监控单个实例内的指标状况。后者Turbine
,可以将多个实例指标数据进行聚合的工具。html
Hystrix-dashboard(仪表盘)
是一款针对Hystrix进行实时监控的工具,经过Hystrix Dashboard
咱们能够在直观地看到各Hystrix Command
的请求响应时间, 请求成功率等数据。java
建立一个spring-cloud-hystrix-dashboard
工程。git
0.引入POM依赖。github
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency>
1.启动类加入@EnableHystrixDashboard
注解,开启仪表盘功能。web
@SpringBootApplication @EnableHystrixDashboard @Slf4j public class HystrixDashboardApplication { public static void main(String[] args) { SpringApplication.run(HystrixDashboardApplication.class, args); log.info("spring-cloud-hystrix-dashboard启动!"); } }
2.配置文件修改下,指定端口和应用名称。spring
#应用名称 spring.application.name=hystrix-dashboard #端口号 server.port=9696
3.启动应用,访问:http://127.0.0.1:9696/hystrix ,就能够看见以下页面了:express
从首页的监控页面能够看出,此时还没有配置监控应用。并且,从页面咱们也能够看出,一共有三种数据源形式,即不一样的监控方式:api
注意:2.0
以后,默认的监控端点地址加了上下文路径actuator
。可经过management.endpoints.web.base-path
属性进行修改,默认是:actuator
微信
如今,咱们改造下spring-cloud-hystrix
项目,开启端点,同时启用监控端点hystrix.stream
。 0.引入端点依赖。并发
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
1.配置文件开启端点hystrix.stream
。这里须要注意,2.0
以后,默认只开启了端点info
、health
。其余的须要经过management.endpoints.web.exposure.include
进行额外配置。
#开启监控端点 management.endpoints.web.exposure.include=hystrix.stream
如今咱们启动spring-cloud-hystrix
。而后添加:http://127.0.0.1:8038/actuator/hystrix.stream 到仪表盘中。
填写了标题后,点击按钮Monitor Stream
,就能够进入监控页面了。
此时,咱们访问下:http://192.168.2.108:8038/feign 。由于服务spring-cloud-eureka-client
未启动,因此会触发熔断方法,多访问几回,再次查看监控页面,就能够看见相关数据了。
此时,能够启动下服务spring-cloud-eureka-client
,而后再次访问下接口。
不一样的颜色对应断路器监控的百分比
关于监控界面的参数解读,这里直接转至博客园:一抹书香的图例,地址:https://www.cnblogs.com/chenweida/p/9025589.html
hystrix
只能实现单个微服务的监控,但是通常项目中是微服务是以集群的形式搭建,一个一个的监控不现实。而Turbine
的原理是,创建一个turbine
服务,并注册到eureka
中,并发现eureka
上的hystrix
服务。经过配置turbine
会自动收集所需hystrix
的监控信息,最后经过dashboard
展示,以达到集群监控的效果。
简单来讲,就是经过注册到注册中心,发现其余服务的hystrix
服务,而后进行聚合数据,最后经过自身的端点输出到仪表盘上进行个性化展现。这咱们就监控一个turbine
应用便可,当有新增的应用加入时,咱们只须要配置下turbine
参数便可。
建立spring-cloud-hystrix-turbine
工程。 0.引入POM依赖。
<!-- turbine依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-turbine</artifactId> </dependency> <!-- eureka client依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
1.启动类加入注解@EnableTurbine
和@EnableDiscoveryClient
.
/** * turbine服务示例 * @author oKong * */ @SpringBootApplication @EnableTurbine @EnableDiscoveryClient @Slf4j public class HystrixTurbineApplication { public static void main(String[] args) { SpringApplication.run(HystrixTurbineApplication.class, args); log.info("spring-cloud-hystrix-turbine启动!"); } }
2.配置文件加入注册中心及turbine
相关配置信息。
#应用名称 spring.application.name=hystrix-tuibine #端口号 server.port=9698 #指定注册中心地址 eureka.client.service-url.defaultZone=http://127.0.0.1:1000/eureka # 启用ip配置 这样在注册中心列表中看见的是以ip+端口呈现的 eureka.instance.prefer-ip-address=true # 实例名称 最后呈现地址:ip:2000 eureka.instance.instance-id=${spring.cloud.client.ip-address}:${server.port} #turbine配置 # 须要监控的应用名称,默认逗号隔开,内部使用Stringutils.commaDelimitedListToStringArray分割 turbine.app-config=hystrix-example # 集群名称 turbine.cluster-name-expression="default" # true 同一主机上的服务经过host和port的组合来进行区分,默认为true # false 时 在本机测试时 监控中host集群数会为1了 由于本地host是同样的 turbine.combine-host-port=true
2.修改spring-cloud-hystrix
应用,建立一个application-turbine.properties
配置文件,里面就设置一个端口好区别下实例。 application-turbine.properties
server.port=8039
2.此时启动下应用,同时启动spring-cloud-hystrix
应用,设置不一样的spring.profiles.active
值,以此启动多个实例。在仪表盘应用:http://127.0.0.1:9696/hystrix 中添加:http://127.0.0.1:9698/turbine.stream ,以后点击按钮:Monitor Stream
,此时界面是loading
状态。接着屡次访问:http://127.0.0.1:8038/feign?name=oKong ,并能够看见有数据了。
以后,咱们接着访问:http://127.0.0.1:8039/feign?name=oKong ,能够看见Hosts
变成2了。
一点疑问:关于hystrix dashboard
是监控Hystrix Command
的指标状况,当咱们监控的方法都一致时,是否是区分不了具体是哪一个服务的了?按目前的演示demo中,是没有看出具体是哪一个应用出现了异常。。不知道是否是使用姿式不对,以为不该该是这样的吧。。
加了个配置文件applicatioon-test
,端口号不一致,建立个新的api接口加上HystrixCommand
,最后出现的图例确实是按照方法名来的。这应该不是巧合了吧。。
还但愿有了解这方面的同窗,能答疑解惑下。目前是用pinpoint
了,对这块不是很熟悉。并且pinpoint
也仅仅是监控了下,具体深刻还没有了解。。路漫漫其修远兮呀!
本章节主要是对
Hystrix
数据进行实时监控进行了讲解,介绍了单体应用和集群应用的监控示例。这块也只是简单的使用,未进行深刻了解过,看官网收集监控数据还可使用消息代理
的方式进行收集,目前默认是使用HTTP
的协议进行收集的,有兴趣的同窗能够自行试试,这和后面会讲解的zikpin
微服务跟踪是相似的,后者也可使用消息代理
进行异步
数据收集,能够提供性能。
目前互联网上大佬都有分享
SpringCloud
系列教程,内容可能会相似,望多多包涵了。原创不易,码字不易,还但愿你们多多支持。若文中有错误之处,还望提出,谢谢。
499452441
lqdevOps
我的博客:http://blog.lqdev.cn
源码示例:https://github.com/xie19900123/spring-cloud-learning
原文地址:http://blog.lqdev.cn/2018/09/26/SpringCloud/chapter-six/