Spring Cloud构建微服务架构Hystrix监控面板

150 阅读2分钟

下面我们基于之前的示例来结合Hystrix Dashboard实现Hystrix指标数据的可视化面板,这里我们将用到下之前实现的几个应用,包括:

eureka-server:服务注册中心
eureka-client:服务提供者
eureka-consumer-ribbon-hystrix:使用ribbon和hystrix实现的服务消费者
由于eureka-consumer-ribbon-hystrix项目中的/consumer接口实现使用了@HystrixCommand修饰,所以这个接口的调用情况会被Hystrix记录下来,以用来给断路器和Hystrix Dashboard使用。断路器我们在上一篇中已经介绍过了,下面我们来具体说说Hystrix Dashboard的构建。

在Spring Cloud中构建一个Hystrix Dashboard非常简单,只需要下面四步:

创建一个标准的Spring Boot工程,命名为:hystrix-dashboard。
编辑pom.xml,具体依赖内容如下:


org.springframework.cloud
spring-cloud-starter-parent
Dalston.SR1



org.springframework.cloud
spring-cloud-starter-hystrix


org.springframework.cloud
spring-cloud-starter-hystrix-dashboard


org.springframework.boot
spring-boot-starter-actuator


为应用主类加上@EnableHystrixDashboard,启用Hystrix Dashboard功能。
@EnableHystrixDashboard
@SpringCloudApplication
public class HystrixDashboardApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixDashboardApplication.class, args);
}
}
根据实际情况修改application.properties配置文件,比如:选择一个未被占用的端口等,此步非必须。
spring.application.name=hystrix-dashboard
server.port=1301
从页面的文字内容中我们可以知道,Hystrix Dashboard共支持三种不同的监控方式,依次为:

默认的集群监控:通过URLhttp://turbine-hostname:port/turbine.stream开启,实现对默认集群的监控。
指定的集群监控:通过URLhttp://turbine-hostname:port/turbine.stream?cluster=[clusterName]开启,实现对clusterName集群的监控。
单体应用的监控:通过URLhttp://hystrix-app:port/hystrix.stream开启,实现对具体某个服务实例的监控。
前两者都对集群的监控,需要整合Turbine才能实现,这部分内容我们将在下一篇中做详细介绍。在本节中,我们主要实现对单个服务实例的监控,所以这里我们先来实现单个服务实例的监控。

既然Hystrix Dashboard监控单实例节点需要通过访问实例的/hystrix.stream接口来实现,自然我们需要为服务实例添加这个端点,而添加该功能的步骤也同样简单,只需要下面两步:

在服务实例pom.xml中的dependencies节点中新增spring-boot-starter-actuator监控模块以开启监控相关的端点,并确保已经引入断路器的依赖spring-cloud-starter-hystrix:


org.springframework.cloud
spring-cloud-starter-hystrix


org.springframework.boot
spring-boot-starter-actuator

确保在服务实例的主类中已经使用@EnableCircuitBreaker或@EnableHystrix注解,开启了断路器功能。
从现在开始,我这边也会将近期研发的springcloud微服务云架构的搭建过程和精髓记录下来,帮助更多有兴趣研发spring cloud框架的朋友,希望可以帮助更多的好学者。大家来一起探讨spring cloud架构的搭建过程及如何运用于企业项目。
源码来源:minglisoft.cn/honghu/tech…