上一篇,我们讲了这个Hystrix 整合到项目中,Hystrix其实还提供了准时实时监控(Hystrix Dashboard),Hystrix会持续记录说有通过Hystrix发起的请求的执行信息,并以统计报表和图形的形式展示给用户,包括每秒执行多少请求多少成功,多少失败等。Netflix通过hystrix-metrics-event-stream项目实现了对以上指标的监控。Spring Cloud也提供了Hystrix Dashboard的整合,对监控内容转化成可视化界面。

新建 pcloud-consumer-hystrix-dashboard9001

spring 监控api时间 spring cloud监控_微服务


spring 监控api时间 spring cloud监控_zookeeper_02


修改pom文件:

<?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>
  <parent>
    <artifactId>pcloud</artifactId>
    <groupId>com.younger.springcloud</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <groupId>com.younger.springcloud</groupId>
  <artifactId>pcloud-consumer-hystrix-dashboard9001</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>pcloud-consumer-hystrix-dashboard9001</name>
  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <scope>runtime</scope>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

新增application.yml

server:
  port: 9001

新增主启动类:HystrixDashboardMain9001

package com.younger.springcloud;

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

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

所有Provider微服务提供类(8001/8002/8005)都需要监控依赖配置

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

测试验证:

启动9001

spring 监控api时间 spring cloud监控_spring 监控api时间_03


访问地址: http://localhost:9001/hystrix

spring 监控api时间 spring cloud监控_spring_04

修改 pcloud-provider-hystrix-user8005

spring 监控api时间 spring cloud监控_spring_05

@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;
    }

这个配置就是为了服务监控,这个是由于SpringCloud升级后的问题,需要新增这个配置。

启动项目测试:

spring 监控api时间 spring cloud监控_spring cloud_06


9001 需要监控8005,填写监控地址:http://localhost:8005/hystrix.stream

spring 监控api时间 spring cloud监控_微服务_07


点击Monitor Stream

spring 监控api时间 spring cloud监控_spring_08


测试地址: 1、 http://localhost:8005/user/hystrix/circuitBreaker/1(成功的结果)

spring 监控api时间 spring cloud监控_spring 监控api时间_09


2、 http://localhost:8005/user/hystrix/circuitBreaker/-1 (失败的结果)

spring 监控api时间 spring cloud监控_spring cloud_10


怎么看这个呢?

1个圈:

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

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

1根线:

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

spring 监控api时间 spring cloud监控_spring_11


spring 监控api时间 spring cloud监控_微服务_12