SpringCloud构建微服务之HystrixDashboard服务监控

前言

在前面我们讲解了如何利用Hystrix进行服务保护,但是只有工具的使用而没有监控,我们就无法在第一时间发现哪个服务出现了问题,也不能判断服务整体的健康状况及运行状态。所以我们还要做好相关的监控工作。Hystrix就提供了Hystrix Dashboard对使用了HystrixCommand的服务状况进行监控。

什么是HystrixDashboard?

Hystrix Dashboard 它主要用来实时监控Hystrix的各项指标信息,这些信息是每个HystrixCommand执行过程中的信息,包括每秒执行多少请求多少成功,多少失败,并以统计报表和图形的形式展示,可以让我们直观的看到服务的状态,并能及时的发现问题。

集成HystrixDashboard

spring-cloud-hystrix工程下新建项目consumer-hystrix-dashboard,并将consumer-hystrix代码与配置copy过来:
在这里插入图片描述

修改pom文件,增加Hystrix Dashboard依赖:

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

完整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">
    <parent>
        <artifactId>spring-cloud-hystrix</artifactId>
        <groupId>com.chaytech</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>consumer-hystrix-dashboard</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.chaytech</groupId>
            <artifactId>spring-cloud-model</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>com.chaytech</groupId>
            <artifactId>spring-cloud-api</artifactId>
            <version>${project.version}</version>
        </dependency>

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

        <!-- ribbon相关-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <!--feign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>

        <!--hystrix-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>

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

        <!-- 修改后立即生效,热部署 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>

    </dependencies>

</project>

修改application.yml文件,将端口号改为9001

server:
  port: 9001

eureka:
  client: #客户端注册进eureka服务列表内
    register-with-eureka: false
    service-url:
      defaultZone: http://eureka7001:7001/eureka/,http://eureka7002:7002/eureka/,http://eureka7003:7003/eureka/ #(集群)

修改启动类,增加@EnableHystrixDashboard注解,开启hysterix监控:

@SpringBootApplication
// 本服务启动后会自动注册进eureka服务中
@EnableEurekaClient
@EnableFeignClients(basePackages ={"com.chaytech.**"} )
@ComponentScan(basePackages = {"com.chaytech.**"})
@EnableHystrix // 开启hysterix
@EnableHystrixDashboard // 开启hysterix监控
public class UserConsumerHystrixDashboard9001_Application {

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

启动eureka集群、用户微服务、以及刚刚新建的consumer-hystrix-dashboard服务,如下图所示:
在这里插入图片描述

我们先请求一下接口,看接口是否正常:
在这里插入图片描述
可以看到接口是通的。
接着访问监控地址:http://127.0.0.1:9001/hystrix
在这里插入图片描述
上图所示的这个页面就是Hystrix Dashboard的导航页,Hystrix Dashboard导航页不会展示具体监控信息,而是提供三种监控的方式去选择:

  1. 默认的集群监控,通过URL:http://turbine-hostname:port/turbine.stream,查看默认集群的监控信息。
  2. 指定的集群监控,通过URL:http://turbine-hostname:port/turbine.stream?cluster=[clusterName],查看指定集群(clusterName)的监控信息。
  3. 单个实例的监控,通过URL:http://hystrix-app:port/hystrix.stream,查看具体某个服务实例的监控信息。

我们先通过单个实例监控的方式查看一下监控信息:
在这里插入图片描述
输入监控的服务地址:http://127.0.0.1:9001/hystrix.stream

Delay:该参数用来控制服务器上轮询监控信息的延迟时间,默认为2000毫秒,可以通过配置该属性来降低客户端的网络和CPU消耗。

Title:该参数对应了头部标题Hystrix Stream之后的内容,默认会使用具体监控实例的URL,可以通过配置该信息来展示更合适的标题

然后点击按钮Monitor Stream,会跳转到可视化监控页面:
在这里插入图片描述
在这里插入图片描述
如果出现上面图中所示的内容,是因为此时@HystrixCommand标记的接口还没有任何调用,也就还没生成stream信息,我们来调用几次,stream信息生成后,监控面板会自动刷新。
也可直接访问刚刚输入的监控地址,查看生成stream的信息:
在这里插入图片描述

通过调用几次接口之后,可以看到监控面板显示出了监控信息。
在这里插入图片描述
那么怎么来看呢?

实心圆:共有两种含义。它通过颜色的变化代表了实例的健康程度,它的健康度从绿色<黄色<橙色<红色递减。 该实心圆除了颜色的变化之外,它的大小也会根据实例的请求流量发生变化,流量越大该实心圆就越大。所以通过该实心圆的展示,就可以在大量的实例中快速的发现故障实例和高压力实例。

为了更好的展示服务不同健康状态下面板的变化,我们将用户微服务停到,再次请求接口:
在这里插入图片描述
从上图可以看到,由于我们将用户服务停掉了,接口无法正常通讯了,实心圆就变成了红色。

然后等待一会,当接口没有请求了,实心圆就会再恢复到正常状态:
在这里插入图片描述
监控个指标项说明:
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值