您的位置:首页 > 编程语言 > Java开发

SpringCloud系列:单体服务监控和基于Turbine集群监控服务实现

2017-12-14 00:00 1091 查看
摘要: 基于Hystrix Dashboard实现单体服务调用及频次的监控,实现对分布式集群服务Turbine集群监控服务功能

一、概述

前面文章介绍基于Eureka注册服务提供者和消费者,使用Feign、Ribbon、Hystrix实现服务间的调用、负载均衡及服务熔断和降级功能。而服务的调用频次、接口健康状况等还无法直接观察,本文就是基于Hystrix Dashboard实现单体服务及集群服务的监控功能。

二、监控服务实现

1. 创建监控Dashboard

创建普通的SpringBoot项目hystrix-dashboard,该项目是用来收集监控信息并显示的管理服务,在pom.xml文件中增加如下依赖

<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

在HystrixDashboardApplication主方法上添加@EnableHystrixDashboard注解,启用HystrixDashboard功能,向服务注册中心注册本服务

@EnableHystrixDashboard
@SpringBootApplication
public class HystrixDashboardApplication {

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

修改配置文件

spring.application.name=hystrix-dashboard
server.port=9104

启动服务,访问http://localhost:9104/hystrix ,进入Hystrix Dashboard首页



该页面中并没有具体的监控信息。从页面的文字内容中我们可以知道,HystrixDashboard共支持三种不同的监控方式,依次为:

默认的集群监控:Cluster via Turbine (default cluster): http://turbine-hostname:port/turbine.stream
指定的集群监控:Cluster via Turbine (custom cluster): http://turbine-hostname:port/turbine.stream?cluster=[clusterName]
单体应用的监控:Single Hystrix App: http://hystrix-app:port/hystrix.stream
前两者都对集群的监控,需要整合Turbine才能实现。首页的两外两个参数,

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

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

2. 监控单体服务

Hystrix Dashboard监控单实例节点需要通过访问实例的http://hystrix-app:port/hystrix.stream 接口来实现,自然我们需要为服务实例添加这个端点,而添加该功能的步骤也同样简单,只需要在需要监控的服务的pom文件中增加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
注解,开启了断路器功能,我们这里在eureka-consumer服务上添加该功能,启动服务。在Hystrix Dashboard的首页输http://10.17.5.48:9102/hystrix.stream,已启动对eureka-consumer的监控,点击“Monitor Stream”按钮,可以看到如下页面:



用postman多调用几次,就可以看到数据的变化



在监控信息的左上部分找到两个重要的图形信息:一个实心圆和一条曲线

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

曲线:用来记录2分钟内流量的相对变化,可以通过它来观察到流量的上升和下降趋势。

3. 监控集群服务

在上述架构基础上,引入Turbine来对服务的Hystrix数据进行聚合展示,创建普通的SpringBoot项目turbine-server,在pom.xml文件中增加如下依赖

<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

在主方法上添加注解

@EnableTurbine
@EnableDiscoveryClient
@SpringBootApplication
public class TurbineServerApplication {

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

修改配置文件

spring.application.name=turbine-server
server.port=9105
management.port=9106
eureka.instance.preferIpAddress=true
eureka.instance.instance-id=${spring.cloud.client.ipAddress}:${spring.application.name}:${server.port}
eureka.client.serviceUrl.defaultZone=http://10.17.5.45:9911/eureka/,http://10.17.5.46:9912/eureka/

#参数指定了需要收集监控信息的服务名
turbine.app-config=eureka-consumer
#参数指定了集群名称为default,当服务数量非常多的时候,可以启动多个Turbine服务来构建不同的聚合集群
turbine.cluster-name-expression="default"
#参数设置为true,可以让同一主机上的服务通过主机名与端口号的组合来进行区分,默认情况下会以host来区分不同的服务
turbine.combine-host-port=true

启动turbine-server,可以看到,收集了两台集群机器上的信息



访问Hystrix Dashboard, 开启http://localhost:9105/turbine.stream监控,如下图



三、小结

码云地址:https://gitee.com/gengkangkang/springcloud.git

github地址:https://github.com/gengkangkang/springcloud.git

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐