概述

除了隔离依赖服务的调用以外,Hystri×还提供了准实时的调用监控(HystrixDashboard) ,Hystrix会持续地记录所有通过Hystrix发起的请求的执行信息,并以统计报表和图形的形式展示给用户,包括每秒执行多少清求多少成功,多少失败等。Netfli×通过hystrix-metrics-event-stream项目实现了对以上指标的监控。SpringCloud也提供了Hystrix Dashboard的整合,对监控内容转化成可视化界面。

创建cloud-consumer-hystrix-dashboard9001

pom

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <artifactId>cloud2020</artifactId>
  7. <groupId>com.sgy.cloud2020</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <groupId>com.sgy.cloud2020</groupId>
  11. <artifactId>cloud-consumer-hystrix-dashboard9001</artifactId>
  12. <version>1.0-SNAPSHOT</version>
  13. <name>cloud-consumer-hystrix-dashboard9001</name>
  14. <!-- FIXME change it to the project's website -->
  15. <url>http://www.example.com</url>
  16. <properties>
  17. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  18. <maven.compiler.source>1.8</maven.compiler.source>
  19. <maven.compiler.target>1.8</maven.compiler.target>
  20. </properties>
  21. <dependencies>
  22. <dependency>
  23. <groupId>com.sgy.cloud2020</groupId>
  24. <artifactId>cloud-consumer</artifactId>
  25. <version>${project.version}</version>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.cloud</groupId>
  29. <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
  30. </dependency>
  31. </dependencies>
  32. </project>

父依赖中有如下的依赖

<?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">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.sgy.cloud2020</groupId>
  <artifactId>cloud2020</artifactId>
  <version>1.0-SNAPSHOT</version>
  <modules>
    <module>cloud-provider-payment8090</module>
    <module>cloud-consumer-order80</module>
    <module>cloud-api-commons</module>
    <module>cloud-provider</module>
    <module>cloud-consumer</module>
      <module>cloud-eureka-server7001</module>
    <module>cloud-eureka-server7002</module>
      <module>cloud-provider-payment8091</module>
    <module>cloud-provider-payment8092</module>
      <module>cloud-consumerzk-order80</module>
      <module>cloud-providerconsul-payment8093</module>
      <module>cloud-consumerconsul-order80</module>
      <module>cloud-consumer-feign-order80</module>
      <module>cloud-provider-hystrix-payment8090</module>
      <module>cloud-consumer-feign-hystrix-order80</module>
      <module>cloud-consumer-hystrix-dashboard9001</module>
  </modules>
  <packaging>pom</packaging>

  <!--统一管理jar包版本-->
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <java.version>1.8</java.version>

    <fastjson.version>1.2.46</fastjson.version>
    <lombok.version>1.18.12</lombok.version>
    <druid-version>1.1.21</druid-version>
    <log4j-over-slf4j.version>1.7.26</log4j-over-slf4j.version>
    <httpclient.version>4.5.5</httpclient.version>
    <commons-lang3.version>3.7</commons-lang3.version>
    <beanUtils.version>1.9.3</beanUtils.version>
    <mybatis.spring.boot.starter.version>2.0.1</mybatis.spring.boot.starter.version>
    <mapper.spring.boot.starter.version>2.1.5</mapper.spring.boot.starter.version>
    <junit.version>4.13</junit.version>
    <pagehelper.spring.boot.starter.version>1.2.5</pagehelper.spring.boot.starter.version>
  </properties>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper-spring-boot-starter</artifactId>
        <version>${pagehelper.spring.boot.starter.version}</version>
      </dependency>

      <!-- springboot 2.2.2 -->
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>2.2.2.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <!--spring cloud Hoxton.SR1-->
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Hoxton.SR1</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>

      <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-alibaba-dependencies</artifactId>
        <version>2.1.0.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>

       <!--.......其余依赖-->
    </dependencies>
  </dependencyManagement>
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>2.3.0.RELEASE</version>
        <configuration>
          <fork>true</fork>
          <addResources>true</addResources>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

yml

server:
  port: 9001

主启动类

@SpringBootApplication
@EnableHystrixDashboard
public class HystrixDashboard9001 {
    public static void main( String[] args ) {
        SpringApplication.run(HystrixDashboard9001.class,args);

    }
}

启动测试

启动cloud-consumer-hystrix-dashboard9001,该微服务后续会监控所有的微服务提供者
使用浏览器访问 链接

1610803433557.png

特别说明

所有微服务提供类都需要如下监控依赖配置

<!-- 添加健康监控依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

监控实战

8090服务提供者主启动类注解

@EnableEurekaClient
@EnableCircuitBreaker

特别说明

新版本Hystrix需要在启动类中指定监控路径

@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
public class PaymentHystrixMain8090 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentHystrixMain8090.class,args);
    }

    /**
     * 此配置是为了服务监控而配置,与服务容错本身无关,SpringCloud升级后的坑
     * ServletRegistrationBean因为springboot的默认路径不是"/hystrix.stream",
     * 只要在自己的项目里配置上下面的servlet就可以了
     */
    @Bean
    public ServletRegistrationBean getServlet(){
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }
}

所有provider 微服务提供类都需要监控依赖配置

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

启动 9001 该微服务后续将监控provider 微服务,出现下面图标搭建成功

1610803433603.png

开始实战

启动Eurka服务注册中心

启动8090微服务提供者

9001监控8090,启动 监控的provider 8090,点击下图的monitor stream测试

1610803433650.png

然后我们进行正确的访问:8090微服务带服务熔断

1610803433679.png

1610803433708.png

当我们连续的访问8090微服务带服务熔断 ,监控结果如下

1610803433744.png

进行错误的访问 8090微服务带服务熔断 ,熔断器被打开

1610803433779.png

图解监控界面

1610803433816.png

1610803433896.png

实心圆

实心圆:共有两种含义。它通过颜色的变化代表了实例的健康程度,它的健康度从绿色<黄色<橙色<红色递减

该实心圆除了颜色的变化之外,它的大小也会根据实例的请求流量发生变化,流量越大该实心圆就越大。所以通过该实心圆的展示,就可以在大量的实例中快速的发现故障实例和高压力实例

曲线含义

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