使用IDEA开发Spring Cloud项目(七)熔断器的监控

概述

在整个熔断器中,有一个组件叫Hystrix Dashboard,Hystrix Dashboard可以可视化查看实时监控数据,让我们可以发现哪些服务端口出现了熔断;

本系列源码地址:https://github.com/lhmyy521125/toher-springcloud-sample

准备工作

还是复制上一章的toher-springcloud-sample5-feign-hystrix 项目,命名为toher-springcloud-sample6-hystrix-dashboard,按照以前章节的方法,再次新建一个module, 工程名为 web-hystrix-dashboard
在这里插入图片描述
因为该工程仅仅是作为hystrix的一个可视化监控工程,我们无需注册到服务中心,就简单的一个springboot项目;
pom.xml 文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>cn.toher</groupId>
        <artifactId>springcloud-sample</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>cn.toher</groupId>
    <artifactId>web-hystrix-dashboard</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>web-hystrix-dashboard</name>
    <description>Demo project for Spring Boot</description>


    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>
    </dependencies>

</project>

修改工程配置YML文件

server:
  #服务端口
  port: 9101
spring:
  application:
    #服务名称
    name: web-hystrix-dashboard

改造service-feign工程(Ribbon同理操作)

首先引入所需组件,注意Ribbon 和 Feign的区别,回去查阅一下源码,你会发现toher-springcloud-sample5-feign-hystrix的源码中,并没有引入spring-cloud-starter-netflix-hystrix, 因为Feign是自带断路器的,所以我们无需引入,但是并没有整合到监控的模块,所以我们需要重新引入依赖包

概括来说 不管接下来项目中是采用Ribbon 还是 Feign 都默认添加下面两个依赖即可;

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

修改配置文件,因为我们引入了 spring-boot-starter-actuator 这个组件,默认actuator监控不会对外暴露hystrix.stream端点,默认情况下,只会暴露health和info端点,所以我们开启它;

spring:
  application:
    name: service-feign

server:
  port: 9004

feign:
  hystrix:
    enabled: true

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

management:
  endpoints:
    web:
      exposure:
        #actuator监控对外暴露hystrix.stream端点,默认情况下,只会暴露health和info端点
        include: health, info, hystrix.stream

最后启动类上添加 @@EnableHystrix

@EnableHystrix
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceFeignApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceFeignApplication.class, args);
    }

}

测试效果

依次启动 eureka-server , service-hello , service-feign ;我们先看看actuator 暴露出来的hystrix.stream 信息是什么样的;请求格式:http://ip:port/actuator/hystrix.stream 我们service-feign 端口为9004 访问效果如下:
在这里插入图片描述
这么密密麻麻的监控信息 ,我要怎么看怎么理解啊? 没错接下来就是启动我们创建的 web-hystrix-dashboard 工程;
在这里插入图片描述
文本框中的URL就是我们刚才已经配置好的 http://ip:port/actuator/hystrix.stream ; 输入对应值后,点击Monitor Stream按钮,结果如下:
在这里插入图片描述
各颜色的数字状态可以参考 界面右上角 ,不同颜色对应不同的状态
在这里插入图片描述
重新打开一个窗口,多次在浏览器地址栏刷新访问 http://localhost:9004/helloFeign?name=Feign 返回查看监控页面,查看对应结果把~

结语

本章为大家介绍了,如果开启配置熔断监控 和 可视化监控Hystrix Dashboard 配置运行以及简单说明,本章仅用了Feign的作为演示,Ribbon操作流程都是一致的,就不需要赘述了;

下一篇:路由网关Zuul

  • 15
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Micro麦可乐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值