概述
在微服务架构中为例保证程序的可用性,防止程序出错导致网络阻塞,出现了断路器模型。断路器的状况反应了一个程序的可用性和健壮性,它是一个重要指标。Hystrix Dashboard是作为断路器状态的一个组件,提供了数据监控和友好的图形化界面。
Hystrix的主要作用是会采集每一个HystrixCommand的信息指标,把每一个断路器的信息指标显示的Hystrix仪表盘上。
创建工程
pom.xml
microservice-hystrix-dashboard/pom.xml
<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>
<groupId>com.itmuch.cloud</groupId>
<artifactId>microservice-spring-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>microservice-hystrix-dashboard</artifactId>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
</dependencies>
</project>
说明:
1)加入依赖spring-cloud-starter-hystrix-dashboard;
application.yml
server:
port: 8030
EurekaApplication
com.itmuch.cloud.EurekaApplication
package com.itmuch.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@EnableHystrixDashboard
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
说明:
1)启动类加上@EnableHystrixDashboard注解,开启HystrixDashboard;
测试
启动工程
1)microservice-discovery-eureka
2)microservice-consumer-movie-ribbon-with-hystrix
3)microservice-hystrix-dashboard
查看eureka
http://localhost:8761/
访问Hystrix Dashboard
http://localhost:8030/hystrix
这是Hystrix Dashboard的监控页面,该页面并没有具体的监控信息。从页面的文字内容我们可以知道,Hystrix Dashboard共支持三种不同的监控方式,如下所示:
①默认的集群监控:通过URL http://turbine-hostname:port/turbine.stream开启,实现对默认集群的监控。
②指定的集群监控:通过URL http://turbine-hostname:port/turbine.stream?cluster=[clusterName]开启,实现对clusterName集群的监控。
③单体应用的监控:通过URL http://hystrix-app:port/hystrix.stream开启,实现对具体某个服务实例的监控。
前两者都是对集群的监控,需要整合Turbine才能实现,其他文章会提到,这里采用的是单体应用监控,尝试尝试,先简单后复杂;
http://localhost:8030/hystrix
说明:
1)最上面一个当然就是填写监控的URL。除此之外,还有两个参数需要我们填写:
第一个是Delay参数。Delay参数用控制服务器上轮询监控信息的延迟时间,默认为2000毫秒,可以通过配置该属性来降低客户端的网络和CPU消耗。
第二个是Title参数。Title参数对应了上图头部标题Hystrix Stream之后的内容,默认会使用具体监控实例的URL,可以通过配置该信息来展示更合适的标题。
2)访问http://localhost:8030/hystrix;
3)分别输入http://localhost:8010/hystrix.stream
Title:microservice-consumer-movie-ribbon-with-hystrix
4)点击Monitor Stream按钮,进入如下界面:
5)若出现上面Loading...,多访问microservice-consumer-movie-ribbon-with-hystrix服务再看看;
访问电影服务
http://localhost:8010/movie/1
说明:
1)可以访问多次;
查看Hystrix Dashboard的变化
访问http://localhost:8010/hystrix.stream,看看
可以看到一些数据不断的输出;
再看看看板
说明:
1)基本会接近实时的描述microservice-consumer-movie-ribbon-with-hystrix服务访问情况;
仪表盘参数说明
在这里我们可以直观的看到各Hystrix Command的请求响应时间, 请求成功率等数据。可以就可以知道这个服务对应的状态和性能了。
不过这样显然是不够的,因为这样我们只能看到单个应用的服务信息。比如说,下单功能,可能需要调用几十个微服务,我们希望看到的是这个功能对应的这些微服务的性能,而不是一个微服务一个微服务的去查看。所以我们需要一个工具能让我们汇总系统内多个服务的数据并显示到Hystrix Dashboard上,使用Turbine可以做到这一点。值得注意的是,当使用Hystrix Dashboard来监控Spring Cloud Zuul构建的API网关时,Thread Pools信息会一直处于Loading状态。这是由于Zuul默认会使用信号量来实现隔离,只有通过Hystrix配置把隔离机制改为线程池的方式才能够得以展示。
==============================
QQ群:143522604
群里有相关资源
欢迎和大家一起学习、交流、提升!
==============================