SpringCloud-23-Hystrix 故障监控

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在服务端也需要引入来提供监控信息



    
        spring-cloud-microservice
        org.example
        1.0-SNAPSHOT
    
    4.0.0

    microservice-cloud-dept-hystrix-dashboard-8009
    
        
        
            org.springframework.boot
            spring-boot-starter-web
            
                
                    org.springframework.boot
                    spring-boot-starter-tomcat
                
            
        
        
        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
        
            junit
            junit
            test
        
        
        
            org.springframework
            springloaded
            1.2.8.RELEASE
        
        
        
            ch.qos.logback
            logback-core
        
        
            org.apache.logging.log4j
            log4j-core
        
        
        
            org.springframework.boot
            spring-boot-starter-jetty
        
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-hystrix-dashboard
            2.2.10.RELEASE
        
    



    
        org.springframework.boot
        spring-boot-maven-plugin
        
            
                
                    org.projectlombok
                    lombok
                
            
        
    




  • 在 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 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 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”,结果如下图。SpringCloud-23-Hystrix 故障监控_第1张图片

  • 重启 microservice-cloud-provider-dept-hystrix-8004,并将以下信息填到 Hystrix 监控页面中,如下图SpringCloud-23-Hystrix 故障监控_第2张图片

  • 点击下方的 Monitor Stream 按钮,跳转到 Hystrix 对microservice-cloud-provider-dept-hystrix-8004 的监控页面,如下图。SpringCloud-23-Hystrix 故障监控_第3张图片

  • 使用浏览器多次访问“http://localhost/consumer/dept/Hystrix/circuitBreaker/124567”和 http://localhost/consumer/dept/Hystrix/circuitBreaker/124”,查看 Hystrix 监控页面,如下图。SpringCloud-23-Hystrix 故障监控_第4张图片

  • 如何看这个报表

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

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

  • 整体例图:SpringCloud-23-Hystrix 故障监控_第7张图片

  • 一般我们监控的是服务的每个HystrixCommand的一套指标。Hystrix仪表板以有效的方式显示每个断路器的运行状况,所以这块会由多个报表SpringCloud-23-Hystrix 故障监控_第8张图片

下一篇:SpringCloud-24-Gateway:Spring Cloud API网关组件介绍

你可能感兴趣的:(SpringCloud,分布式,Hystrix,spring,cloud,hystrix,spring)