SpringCloud-23-Hystrix 故障监控

32 篇文章 1 订阅
5 篇文章 0 订阅
8.9 Hystrix 故障监控
  • Hystrix 还提供了准实时的调用监控(Hystrix Dashboard)功能,Hystrix 会持续地记录所有通过Hystrix 发起的请求的执行信息,并以统计报表的形式展示给用户,包括每秒执行请求的数量、成功请求的数量和失败请求的数量等。

  • 下面通过一个实例来搭建 Hystrix Dashboard客户端,监控 microservice-cloud-provider-dept-hystrix-8004 的运行情况

  • 在基础工程下新建一个名为 microservice-cloud-dept-hystrix-dashboard-8008 的子模块,并在其 pom.xml 中添加以下依赖

注意:监控依赖spring-boot-starter-actuator在服务端也需要引入来提供监控信息

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-cloud-microservice</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>microservice-cloud-dept-hystrix-dashboard-8009</artifactId>
    <dependencies>
        <!--Spring Boot Web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--devtools 开发工具-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <!--spring-boot test 测试只能在test包中测试-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--junit 测试-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- 修改后立即生效,热部署 这个热部署重启得更快 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
            <version>1.2.8.RELEASE</version>
        </dependency>
        <!--日志-->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
        </dependency>
        <!--jetty-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
        <!-- Spring Boot 监控模块,完善监控信息-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--hystrix dashboard依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
            <version>2.2.10.RELEASE</version>
        </dependency>
    </dependencies>

<build>
<plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <excludes>
                <exclude>
                    <groupId>org.projectlombok</groupId>
                    <artifactId>lombok</artifactId>
                </exclude>
            </excludes>
        </configuration>
    </plugin>
</plugins>
</build>

</project>
  • 在 microservice-cloud-dept-hystrix-dashboard-8009 的 application.yml 中添加以下配置。
server:
  port: 8009
spring:
  application:
    name: microServiceCloudProviderDeptHystrixDashboard #微服务名称,对外暴漏的微服务名称,十分重要

#http://eureka7001.com:8008/hystrix 熔断器监控页面
#localhost:8004/actuator/hystrix.stream 监控地址
hystrix:
  dashboard:
    proxy-stream-allow-list:
      "localhost"   #允许的代理主机名列表
# spring cloud 使用 Spring Boot actuator 监控完善信息
# Spring Boot 2.50对 actuator 监控屏蔽了大多数的节点,只暴露了 heath 节点,本段配置(*)就是为了开启所有的节点
management:
  endpoints:
    web:
      exposure:
        include: health,info  #应包含的端点 ID 或 '' 全部,* 在yaml 文件属于关键字,所以需要加双引号
        #include的值也可以改成*,但建议还是最小暴露原则,用啥开启啥

proxy-stream-allow-list的配置允许的代理主机名列表,可见HystrixDashboardProperties源码类

具体使用规则可以查看HystrixDashboardConfiguration的isAllowedToProxy方法

private boolean isAllowedToProxy(String proxyUrlString)
      throws MalformedURLException {

   URL proxyUrl = new URL(proxyUrlString);
   String host = proxyUrl.getHost();
   PathMatcher pathMatcher = new AntPathMatcher(".");
   Optional<String> optionalPattern = properties.getProxyStreamAllowList()
         .stream().filter(pattern -> pathMatcher.match(pattern, host))
         .findFirst();

   if (optionalPattern.isPresent()) {
      return true;
   }
   return false;
}
  • 在 microservice-cloud-dept-hystrix-dashboard-8009 的主启动类上添加 @EnableHystrixDashboard 注解,开启 Hystrix 监控功能,代码如下
package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

@SpringBootApplication
@EnableHystrixDashboard //开启 Hystrix 监控功能
public class MicroserviceCloudDeptHystrixDashboardApplication {

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

}
  • 还需要在 microservice-cloud-provider-dept-hystrix-8004 的 com.example.config 包下,创建一个名为 HystrixDashboardConfig 的配置类.代码如下。

注意:

Hystrix dashboard 监控界面必须配置,相当于要在web.xml中配置

HystrixDashboardConfig 别弄错位置

UrlMappings中的url集合就是后续在监控页面填的监控地址,

package com.example.config;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;

/**
 * @author CNCLUKZK
 * @create 2022/9/21-0:19
 */
@Configuration
public class HystrixDashboardConfig {

    /**
     *  Hystrix dashboard 监控界面必须配置,相当于要在web.xml中配置Servlet
     * @return
     */
    @Bean
    public ServletRegistrationBean registrationBean(){
        HystrixMetricsStreamServlet metricsStreamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(metricsStreamServlet);
        servletRegistrationBean.setLoadOnStartup(1);
        ArrayList<String> urlList = new ArrayList<>();
        urlList.add("/actuator/hystrix.stream");
        servletRegistrationBean.setUrlMappings(urlList);
        servletRegistrationBean.setName("hystrix.stream");
        return servletRegistrationBean;
    }
}
  • 启动 microservice-cloud-dept-hystrix-dashboard-8009,使用浏览器访问“http://eureka7001.com:9002/hystrix”,结果如下图。在这里插入图片描述

  • 重启 microservice-cloud-provider-dept-hystrix-8004,并将以下信息填到 Hystrix 监控页面中,如下图在这里插入图片描述

  • 点击下方的 Monitor Stream 按钮,跳转到 Hystrix 对microservice-cloud-provider-dept-hystrix-8004 的监控页面,如下图。在这里插入图片描述

  • 使用浏览器多次访问“http://localhost/consumer/dept/Hystrix/circuitBreaker/124567”和 http://localhost/consumer/dept/Hystrix/circuitBreaker/124”,查看 Hystrix 监控页面,如下图。在这里插入图片描述

  • 如何看这个报表

    • 7色:Short-Circuited短路;Rejected:拒绝;Failure:失败在这里插入图片描述
  • 一圈:实心圆:公有两种含义,他通过颜色的变化代表了实例的健康程度。它的健康程度从绿色<黄色<橙色<红色递减。该实心圆除了颜色的变化之外,它的大小也会根据实例的请求流量发生变化,流量越大,该实心圆就越大,所以通过该实心圆的展示,就可以在大量的实例中快速发现故障实例和高压力实例。
    在这里插入图片描述

  • 一线:曲线:用来记录2分钟内流量的相对变化,可以通过它来观察到流量的上升和下降趋势!

  • 整体例图:在这里插入图片描述

  • 一般我们监控的是服务的每个HystrixCommand的一套指标。Hystrix仪表板以有效的方式显示每个断路器的运行状况,所以这块会由多个报表在这里插入图片描述

下一篇:SpringCloud-24-Gateway:Spring Cloud API网关组件介绍
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值