Java基础、中级、高级、架构面试资料

Spring Cloud 教程第十一章 turbine 对 Hystrix 的集群监控

JAVA herman 2349浏览 0评论
公告:“业余草”微信公众号提供免费CSDN下载服务(只下Java资源),关注业余草微信公众号,添加作者微信:xttblog2,发送下载链接帮助你免费下载!
本博客日IP超过2000,PV 3000 左右,急需赞助商。
极客时间所有课程通过我的二维码购买后返现24元微信红包,请加博主新的微信号:xttblog2,之前的微信号好友位已满,备注:返现
受密码保护的文章请关注“业余草”公众号,回复关键字“0”获得密码
所有面试题(java、前端、数据库、springboot等)一网打尽,请关注文末小程序
视频教程免费领
腾讯云】1核2G5M轻量应用服务器50元首年,高性价比,助您轻松上云

上一章我们做了 Hystrix 的监控,使用了 Hystrix Dashboard(控制台)。Hystrix Dashboard 的缺点是只能监控一个服务,在实际生产一个一个的监控太麻烦了,也太复杂了,给运维带来的是灾难。于是我们就需要一种对所有服务的熔断都能监控的组件 Turbine。本文将具体的介绍如何使用 Turbine 对所有的服务的断路状态进行监控。

Turbine 是 Netflix 提供的一个开源项目,来提供把多个 hystrix.stream 的内容聚合为一个数据源供 Dashboard 展示。在开始之前,我在网上找到了一个 Hystrix Dashboard(控制台)各项数字的含义,如下图所示:

Dashboard 参数详解

下面我们开始本文的内容,同样的我们先新建一个工程:xttblog-hystrix-turbine。pom.xml 文件只比 xttblog-hystrix-dashboard 多了两个 starter。

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-turbine</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-netflix-turbine</artifactId>
</dependency>

然后是配置 yml 文件:

server:
  port: 8999

eureka:
  instance:
    hostname: localhost
  client:
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:8761/eureka/

spring:
  application:
    name: xttblog-hystrix-turbine

#feign:
#  hystrix:
#    enabled: true

turbine:
  aggregator:
    # 指定聚合哪些集群,多个使用","分割,默认为default。可使用http://.../turbine.stream?cluster={clusterConfig之一}访问
    cluster-config: default
  cluster-name-expression: "'default'"
  app-config: xttblog-hystrix-dashboard,xttblog-ribbon-hystrix
  # 1. clusterNameExpression指定集群名称,默认表达式appName;此时:turbine.aggregator.clusterConfig需要配置想要监控的应用名称
  # 2. 当clusterNameExpression: default时,turbine.aggregator.clusterConfig可以不写,因为默认就是default
  # 3. 当clusterNameExpression: metadata['cluster']时,假设想要监控的应用配置了eureka.instance.metadata-map.cluster: ABC,则需要配置,同时turbine.aggregator.clusterConfig: ABC

启动类上,需要配置上 @EnableHystrixDashboard 和 @EnableTurbine 注解。

@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrixDashboard
//@EnableTurbineStream
@EnableTurbine
public class HystrixTurbineApplication {
    public static void main(String[] args) {
        SpringApplication.run(HystrixTurbineApplication.class, args);
    }
}

最后我们依次启动 xttblog-eureka-serverxttblog-cloud-producerxttblog-feign-hystrixxttblog-hystrix-turbine。然后各个服务都依次访问一下,能正常访问说明没问题。最后,我们在把 xttblog-cloud-producer 给关闭,再访问 xttblog-feign-hystrix(http://localhost:8999/turbine.stream) 相当于是发生熔断。就能在 xttblog-hystrix-turbine 中看到效果。

Turbine 的整体界面看起来太丑了,不符合国人的审美观。后面有机会了,可以给它的界面换一套模版,做的类似阿里的 Sentinel 控制台的一样的效果。

Turbine 还可以和 rabbitmq 进行整合。如果需要整合 rabbitmq ,就需要开启 @EnableTurbineStream 注解。

Turbine 只能监控 hystrix 服务,不是 hystrix 服务,不能监控。查看监控信息时,需要先访问以下 hystrix 服务。如果有其他问题,可以给我留言。本文相关源代码已上传至:https://github.com/xmt1139057136/xttblog-cloud

业余草公众号

最后,欢迎关注我的个人微信公众号:业余草(yyucao)!可加作者微信号:xttblog2。备注:“1”,添加博主微信拉你进微信群。备注错误不会同意好友申请。再次感谢您的关注!后续有精彩内容会第一时间发给您!原创文章投稿请发送至532009913@qq.com邮箱。商务合作也可添加作者微信进行联系!

本文原文出处:业余草: » Spring Cloud 教程第十一章 turbine 对 Hystrix 的集群监控