Spring Cloud架构教程 (二)Hystrix监控数据聚合

上一篇咱们介绍了使用Hystrix Dashboard来展现Hystrix用于熔断的各项度量指标。经过Hystrix Dashboard,咱们能够方便的查看服务实例的综合状况,好比:服务调用次数、服务调用延迟等。可是仅经过Hystrix Dashboard咱们只能实现对服务当个实例的数据展示,在生产环境咱们的服务是确定须要作高可用的,那么对于多实例的状况,咱们就须要将这些度量指标数据进行聚合。下面,在本篇中,咱们就来介绍一下另一个工具:Turbine。html

准备工做

在开始使用Turbine以前,咱们先回顾一下上一篇中实现的架构,以下图所示:spring

其中,咱们构建的内容包括:express

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

动手试一试

下面,咱们将在上述架构基础上,引入Turbine来对服务的Hystrix数据进行聚合展现。这里咱们将分别介绍两种聚合方式。架构

经过HTTP收集聚合

具体实现步骤以下:app

  • 建立一个标准的Spring Boot工程,命名为:turbine。
  • 编辑pom.xml,具体依赖内容以下:
<parent>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-parent</artifactId>
	<version>Dalston.SR1</version>
	<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencies>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-turbine</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-actuator</artifactId>
	</dependency>
</dependencies>
  • 建立应用主类TurbineApplication,并使用@EnableTurbine注解开启Turbine。
@Configuration
@EnableAutoConfiguration
@EnableTurbine
@EnableDiscoveryClient
public class TurbineApplication {

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

}
  • application.properties加入eureka和turbine的相关配置,具体以下:
spring.application.name=turbine

server.port=8989
management.port=8990

eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/

turbine.app-config=eureka-consumer-ribbon-hystrix
turbine.cluster-name-expression="default"
turbine.combine-host-port=true

参数说明异步

  • turbine.app-config参数指定了须要收集监控信息的服务名;
  • turbine.cluster-name-expression 参数指定了集群名称为default,当咱们服务数量很是多的时候,能够启动多个Turbine服务来构建不一样的聚合集群,而该参数能够用来区分这些不一样的聚合集群,同时该参数值能够在Hystrix仪表盘中用来定位不一样的聚合集群,只须要在Hystrix Stream的URL中经过cluster参数来指定;
  • turbine.combine-host-port参数设置为true,可让同一主机上的服务经过主机名与端口号的组合来进行区分,默认状况下会以host来区分不一样的服务,这会使得在本地调试的时候,本机上的不一样服务聚合成一个服务来统计。

在完成了上面的内容构建以后,咱们来体验一下Turbine对集群的监控能力。分别启动eureka-servereureka-clienteureka-consumer-ribbon-hystrixturbine以及hystrix-dashboard。访问Hystrix Dashboard,并开启对http://localhost:8989/turbine.stream`的监控,这时候,咱们将看到针对服务`eureka-consumer-ribbon-hystrix`的聚合监控数据。spring-boot

而此时的架构以下图所示:工具

经过消息代理收集聚合

Spring Cloud在封装Turbine的时候,还实现了基于消息代理的收集实现。因此,咱们能够将全部须要收集的监控信息都输出到消息代理中,而后Turbine服务再从消息代理中异步的获取这些监控信息,最后将这些监控信息聚合并输出到Hystrix Dashboard中。经过引入消息代理,咱们的Turbine和Hystrix Dashoard实现的监控架构能够改为以下图所示的结构:post

从图中咱们能够看到,这里多了一个重要元素:RabbitMQ。对于RabbitMQ的安装与基本时候咱们能够查看以前的《Spring Boot中使用RabbitMQ》一文,这里不作过多的说明。下面,咱们能够来构建一个新的应用来实现基于消息代理的Turbine聚合服务,具体步骤以下:spa

  • 建立一个标准的Spring Boot工程,命名为:turbine-amqp
  • 编辑pom.xml,具体依赖内容以下:
<parent>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-parent</artifactId>
	<version>Dalston.SR1</version>
	<relativePath /> <!-- lookup parent from repository -->
</parent>

<dependencies>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-turbine-amqp</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-actuator</artifactId>
	</dependency>
</dependencies>

能够看到这里主要引入了spring-cloud-starter-turbine-amqp依赖,它实际上就是包装了spring-cloud-starter-turbine-streampring-cloud-starter-stream-rabbit

注意:这里咱们须要使用Java 8来运行

  • 在应用主类中使用@EnableTurbineStream注解来启用Turbine Stream的配置。
@Configuration
@EnableAutoConfiguration
@EnableTurbineStream
@EnableDiscoveryClient
public class TurbineApplication {

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

}
  • 配置application.properties文件:
spring.application.name=turbine-amqp

server.port=8989
management.port=8990

eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/

对于Turbine的配置已经完成了,下面咱们须要对服务消费者eureka-consumer-ribbon-hystrix作一些修改,使其监控信息可以输出到RabbitMQ上。这个修改也很是简单,只须要在pom.xml中增长对spring-cloud-netflix-hystrix-amqp依赖,具体以下:

<dependencies>
	...
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-netflix-hystrix-amqp</artifactId>
	</dependency>
</dependencies>

在完成了上面的配置以后,咱们能够继续以前的全部项目(除turbine之外),并经过Hystrix Dashboard开启对http://localhost:8989/turbine.stream的监控,咱们能够得到如以前实现的一样效果,只是这里咱们的监控信息收集时是经过了消息代理异步实现的。源码来源

相关文章
相关标签/搜索