Spring Cloud架构教程 (一)Hystrix监控面板

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

  • eureka-server:服务注册中心
  • eureka-client:服务提供者
  • eureka-consumer-ribbon-hystrix:使用ribbon和hystrix实现的服务消费者

因为eureka-consumer-ribbon-hystrix项目中的/consumer接口实现使用了@HystrixCommand修饰,因此这个接口的调用状况会被Hystrix记录下来,以用来给断路器和Hystrix Dashboard使用。断路器咱们在上一篇中已经介绍过了,下面咱们来具体说说Hystrix Dashboard的构建。spring

动手试一试

在Spring Cloud中构建一个Hystrix Dashboard很是简单,只须要下面四步:服务器

  • 建立一个标准的Spring Boot工程,命名为:hystrix-dashboard。
  • 编辑pom.xml,具体依赖内容以下:
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Dalston.SR1</version>
<relativePath />
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
  • 为应用主类加上@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

到这里咱们已经完成了基本配置,接下来咱们能够启动该应用,并访问:http://localhost:1301/hystrix,咱们能够看到以下页面:网络

这是Hystrix Dashboard的监控首页,该页面中并无具体的监控信息。从页面的文字内容中咱们能够知道,Hystrix Dashboard共支持三种不一样的监控方式,依次为:app

  • 默认的集群监控:经过URLhttp://turbine-hostname:port/turbine.stream开启,实现对默认集群的监控。
  • 指定的集群监控:经过URLhttp://turbine-hostname:port/turbine.stream?cluster=[clusterName]开启,实现对clusterName集群的监控。
  • 单体应用的监控:经过URLhttp://hystrix-app:port/hystrix.stream开启,实现对具体某个服务实例的监控。

前二者都对集群的监控,须要整合Turbine才能实现,这部份内容咱们将在下一篇中作详细介绍。在本节中,咱们主要实现对单个服务实例的监控,因此这里咱们先来实现单个服务实例的监控。spring-boot

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

  • 在服务实例pom.xml中的dependencies节点中新增spring-boot-starter-actuator监控模块以开启监控相关的端点,并确保已经引入断路器的依赖spring-cloud-starter-hystrix
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
  • 确保在服务实例的主类中已经使用@EnableCircuitBreaker@EnableHystrix注解,开启了断路器功能。

到这里已经完成了全部的配置,咱们能够在Hystrix Dashboard的首页输入http://localhost:2101/hystrix.stream,已启动对“eureka-consumer-ribbon-hystrix”的监控,点击“Monitor Stream”按钮,此时咱们能够看到以下页面:ui

在对该页面介绍前,咱们先看看在首页中咱们尚未介绍的两外两个参数:spa

  • Delay:该参数用来控制服务器上轮询监控信息的延迟时间,默认为2000毫秒,咱们能够经过配置该属性来下降客户端的网络和CPU消耗。
  • Title:该参数对应了上图头部标题Hystrix Stream以后的内容,默认会使用具体监控实例的URL,咱们能够经过配置该信息来展现更合适的标题。源码来源
相关文章
相关标签/搜索