写在前面
本篇文章仅作为近日参考其他文章后,自己实践的记录和总结,场景到细节尚有很多不足,有待补充和修正。这是spring cloud组件terbine的简单使用记录,实际应当属于Hystrix模块,这里单独说明,也方便应用。
概述
HystrixTerbine是为分布式集群环境而诞生,在查看服务节点的Hystrix dashboard时,按照传统的方式,我们需要在微服务使用@EnableHystrixDashboard来启用Hystrix仪表盘,并使用ip:port/hystrix这种路径来查看微服务节点的Hystrix组件运行状态,而在分布式环境中,会有多个微服务,每个微服务会有多个节点(instance),这些节点会分布在多个主机服务器,端口号也不尽相同,因此使用传统的方式来访问显得非常笨拙。HystrixTerbine就是为了解决这个问题,它通过注册中心(通常是eureka)来采集指定微服务列表的所有节点,并将Hystrix运行信息集成在一个页面。
简单使用
首先需要新建一个微服务,引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency
看到上面的依赖配置我们发现,并没有eureka client的依赖,那是因为spring-cloud-starter-netflix-turbine依赖已经包含
然后回到正题,配置yaml文件,需要注册到eruaka服务器,并添加terbin配置
spring:
application:
name: common-turbine
server:
port: 8810
eureka:
client:
serviceUrl:
defaultZone: http://username:[email protected]:8761/eureka
instance:
instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${server.port}
turbine:
aggregator:
#监控所有微服务集群
#监控地址:http://127.0.0.1:8810/turbine.stream
#在hystrix仪表盘中监控上面的地址即可
clusterConfig: default
#要监控的微服务serviceId ,多个以英文逗号分隔,配置的微服务要使用到hystrix才行
appConfig: consumer,producer
#集群名称表达式,对应turbine.aggregator.clusterConfig,需要注意格式
clusterNameExpression: "'default'"
然后就可以启动集群环境,访问terbine服务的控制台页面,以此为例,访问地址为:
http://127.0.0.1:8810/hystrix
页面如下:
然后输入地址http://127.0.0.1:8810/turbine.stream点击"Monitor Stream"即可访问配置的微服务的所有节点hystrix仪表盘信息
通过上面的页面我们不难发现,terbine提供三种hystrix仪表盘访问方式
方式一(Cluster via Turbine)
查看集群环境的已配置的微服务的所有节点hystrix仪表盘,上面的简单使用部分就是此方式的配置
方式二(Cluster via Turbine)
查看指定微服务的所有节点hystrix仪表盘,微服务名称(clustername)要大写,
输入地址 http://127.0.0.1:8810/turbine.stream?cluster=CONSUMER 并点击"Monitor Stream"即可访问配置的微服务的所有节点hystrix仪表盘信息。
yaml配置形式如下
turbine:
aggregator:
#监控单个微服务集群
#单独监控地址:http://127.0.0.1:8810/turbine.stream?cluster=MCC-FEIGN-HYSTRIX
#单独监控地址:http://127.0.0.1:8810/turbine.stream?cluster=MCC-RIBBON-HYSTRIX
#单独监控地址:http://127.0.0.1:8810/turbine.stream?cluster=MCC-RIBBON-HYSTRIX-PROPAGATING
#在hystrix仪表盘中监控以上3个地址即可
# clusterConfig必须为mcc-feign-hystrix,mcc-ribbon-hystrix,mcc-ribbon-hystrix-propagating对应的大写
clusterConfig: CONSUMER
appConfig: consumer
方式三(Single Hystrix App)
直接查看某个微服务节点的hystrix仪表盘,使用方式一和方式二的yaml配置类型,都同时支持此方式的访问
了解更多
server.context-path变化可能会导致采集失败
假设现在有个微服务consumer,通过fein+hystrix访问producer服务。
yaml没有配置 server.context-path。默认为 / 。
consumer现有一个接口,可以通过身份证查询年龄
@GetMapping("/c/getByFeign/{id}")
public String getByFeign(@PathVariable String id) {
return producerFeign.get(id);
}
访问接口如下:
修改consumer服务的server.context-path
server:
port: 9010
context-path: /app
此时按照原地址127.0.0.1:9010/c/getByFeign/340121199901018888访问则报错404,此时须要使用新的访问地址127.0.0.1:901/app/c/getByFeign/340121199901018888
由此可见,当 server.context-path变更时,微服务的访问路径也需要随之改变,此时Hystrix Terbine也需要为之提供配置以作适配。否则terbine将无法采集到任何信息(不会报错)。
可以通过instanceUrlSuffix参数来配置访问路径后缀,如下
turbine:
aggregator:
clusterConfig: CONSUMER
appConfig: consumer
instanceUrlSuffix:
CONSUMER: /app/hystrix.stream