在复杂的分布式系统中,相同服务的节点经常需要部署上百甚至上千个,运维人员希望能够把相同服务的节点状态以一个整体集群的形式展现出来,这样可以更好的把握整个系统的状态。 为此,Netflix 提供了一个开源项目 Hystrix Turbine 来提供把多个 hystrix.stream 的内容聚合为一个数据源供 Dashboard 展示。
一、准备工作
1.1、创建启动一个注册中心,二个服务消费者,一个服务提供者。
请参考:Spring Cloud Hystrix 断路器组件1:熔断器
- piao-server:注册中心服务端,端口2000。
- piao-client:客户端服务提供者,端口2010。
- piao-ribbon:客户端服务消费者,端口2005。
- piao-ribbon2:客户端服务消费者2,端口2006。
根据我们的参考文章,加入了断路器的代码。这里我们还需要改造消费者应用,在加入端点依赖和配置。
1.2、添加端点依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
1.3、添加配置,暴露所有信息
management.endpoints.web.exposure.include=*
1.4、查看端点信息
访问地址:http://localhost:2005/actuator
二、创建 Hystrix Turbine 监控
2.1、创建 piao-ribbon 项目。
我们这里使用IDEA创建,文件-》新建-》项目-》下一步到如下的页面:
然后在点下一步,直到完成。
2.2、修改依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.piao</groupId>
<artifactId>piao-turbine</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>piao-turbine</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR9</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.3、添加配置
编辑项目的 application.properites 文件,添加如下相关配置:
#服务注册中心端口号
server.port=2082
#指定服务名称
spring.application.name=piao-turbine
#指定服务注册中心的地址
eureka.client.serviceUrl.defaultZone=http://localhost:2000/eureka/
#配置监控域支持本地地址
hystrix.dashboard.proxy-stream-allow-list:localhost,127.0.0.1
#指定需要收集监控信息的服务名
turbine.app-config=piao-ribbon
#指定集群名称
turbine.cluster-name-expression=new String('default')
#让同一主机上的服务通过主机名与端口号的组合进行区分
turbine.combine-host-port=true
- turbine.combine-host-port:默认情况下会以 host 来区分不同的服务,这会使得在本地调试的时候,本机上的不同服务聚合成一个服务来统计。
- turbine.cluster-name-expression:当服务数量非常多的时候,可以启动多个 Turbine 服务来构建不同的聚合集群,而该参数可以用来区分这些不同的聚合集群。
在启动类上添加 @EnableTurbine 注解。@EnableHystrixDashboard 注解也需要添加,否则无法访问监控页面。
@EnableTurbine
@EnableHystrixDashboard
@EnableDiscoveryClient
@SpringBootApplication
public class PiaoTurbineApplication {
public static void main(String[] args) {
SpringApplication.run(PiaoTurbineApplication.class, args);
}
}
2.4、启动项目
请求地址:http://localhost:2082/hystrix
输入集群地址:http://127.0.0.1:2082/turbine.stream,点击Monitor Stream。
我们随机访问:http://127.0.0.1:2006/consumer,http://127.0.0.1:2005/consumer服务消费者的地址。
我们启动了两个 piao-ribbon 服务消费者,监控页面中只是展示了一个监控图,这是由于这两个实例是同一服务,而对于集群来说我们关注的是服务集群的高可用性,所以 Turbine 会将相同服务作为整体来看待,并汇总成一个监控图。