上一篇涨介绍了Hystrix的Dashboard来展示单个实例的监测数据,但实际情况中,我们会部署N个微服务,那么不可能再用这种方式一个接着一个输入查看,基于这种场景,Hystrix可以利用Turbine实现数据聚合。对于老司机而言,Turbine这个单词不会陌生,中文意思为涡轮机,所以顾名思义,用来描述聚合很形象。
(一)创建一个hystrix-turbine监控数据聚合
新建工程后,子pom.xml文件添加以下依赖:
<dependencies>
<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>
</dependencies>
然后创建一个application启动类(需要添加注解@EnableDiscoveryClient
查看注册中心的注册服务):
package com.ningmeng.turbine;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
@EnableTurbine // 开启turbine监控数据聚合
@EnableHystrixDashboard // 开启hystrix仪表盘
@EnableDiscoveryClient
@SpringBootApplication
public class HystrixTurbineApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixTurbineApplication.class, args);
}
}
最后配置application.yml文件:
#工程端口号
server:
port: 5002
spring:
application:
name: hystrix-turbine #工程名,后面各个服务间调用接口要用到
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
#eureka注册中心地址
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ #eureka服务器所在的地址
#聚合的工程 这里演示聚合ribbon-consumer,feign-consumer这两个消费工程
turbine:
aggregator:
cluster-config: default
app-config: ribbon-consumer,feign-consumer
cluster-name-expression: new String("default")
注意:new String(“default”)必须这么写,否则启动的时候会抛出异常org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field ‘default‘ cannot be found on object of type ‘com.netflix.appinfo.InstanceInfo‘ - maybe not public or not valid?
参数说明:
turbine.aggregator.cluster-config参数设定cluster名字,当使用default,默认聚合turbine.appConfig中设定的所有服务名的数据;
turbine.app-config参数设定需要收集监控信息的服务名;
turbine.cluster-name-expression 参数指定了集群名称为 default,当我们服务数量非常多的时候,可以启动多个 Turbine 服务来构建不同的聚合集群,而该参数可以用来区分这些不同的聚合集群,同时该参数值可以在 Hystrix 仪表盘中用来定位不同的聚合集群,只需要在 Hystrix Stream 的 URL 中通过 cluster 参数来指定。
最后依次启动eureka-server、service-provider(两个实例)、feign-consumer、ribbon-consumer以及hystrix-turbine工程,然后访问http://localhost:5002/hystrix,可看到页面:
我们能通过红色框中的两种方式查看监控聚合数据(注意与bashboard监控面板的区别,turbine拼接的是turbine-hostname:port(即hystrix-turbine的ip以及端口号),bashboard拼接的是hystrix-app:port(即调用服务的消费工程的ip以及端口号,比如ribbon-consumer或者feign-consumer))
第一种方式:直接输入http://localhost:5002/turbine.stream,然后假如处于loading状态,我们先调用服务,之后能看到以下内容:
第二种方式在输入url后拼接cluster(即turbine.aggregator.cluster-config的值):
修改application.yml配置文件:
#工程端口号
server:
port: 5002
spring:
application:
name: hystrix-turbine #工程名,后面各个服务间调用接口要用到
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
#eureka注册中心地址
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ #eureka服务器所在的地址
#聚合的工程 这里演示聚合ribbon-consumer,feign-consumer这两个消费工程
turbine:
aggregator:
cluster-config: FEIGN-CONSUMER
app-config: feign-consumer
# cluster-name-expression: new String("default")
参数说明:cluster-config配置的名称必须为eureka注册中心上已经注册的服务名,且为大写。
然后访问http://localhost:5002/turbine.stream?cluster=FEIGN-CONSUMER,cluster参数必须与turbine.aggregator.cluster-config中的条目匹配,只能看到feign-consumer服务的监控数据:
本篇完结!
github代码地址:https://github.com/huijunzeng/springCloudDemo.git