在开始使用 Turbine 之前,我们先回顾一下上一篇中实现的架构,如下图所示:
其中,我们构建的内容包括:
下面,我们将在上述架构基础上,引入 Turbine 来对服务的 Hystrix 数据进行聚合展示。 这里我们将分别介绍两种聚合方式。
创建一个标准的 Spring Boot 工程,命名为:turbine。
POM 配置
在 pom.xml 中添加以下依赖
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-turbineartifactId>
dependency>
启动类
在启动类上使用 @EnableTurbine 注解开启 Turbine
@EnableTurbine
@SpringBootApplication
public class TurbineApplication {
public static void main(String[] args) {
SpringApplication.run(TurbineApplication.class, args);
}
}
配置文件
在 application.yml 加入 Eureka 和 Turbine 的相关配置
spring:
application:
name: turbine
server:
port: 8080
management:
port: 8081
eureka:
client:
service-url:
defaultZone: http://localhost:7000/eureka/
turbine:
app-config: eureka-consumer-hystrix
cluster-name-expression: new String("default")
combine-host-port: true
参数说明
注意:new String(“default”) 这个一定要用 String 来包一下,否则启动的时候会抛出异常:
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 对集群的监控能力。分别启动
访问 Hystrix Dashboard 并开启对 http://localhost:8080/turbine.stream 的监控,这时候,我们将看到针对服务 eureka-consumer-hystrix 的聚合监控数据。
Spring Cloud 在封装 Turbine 的时候,还实现了基于消息代理的收集实现。所以,我们可以将所有需要收集的监控信息都输出到消息代理中,然后 Turbine 服务再从消息代理中异步的获取这些监控信息,最后将这些监控信息聚合并输出到 Hystrix Dashboard 中。通过引入消息代理,我们的 Turbine 和 Hystrix Dashoard 实现的监控架构可以改成如下图所示的结构:
从图中我们可以看到,这里多了一个重要元素:RabbitMQ。下面,我们可以来构建一个新的应用来实现基于消息代理的 Turbine 聚合服务。
创建一个标准的 Spring Boot 工程,命名为:turbine-stream-rabbitmq
POM
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-turbine-streamartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-stream-rabbitartifactId>
dependency>
配置文件
spring:
application:
name: turbine-stream-rabbitmq
eureka:
client:
service-url:
defaultZone: http://localhost:7000/eureka/
启动类
@SpringBootApplication
@EnableTurbineStream
public class TurbineStreamRabbitmqApplication {
public static void main(String[] args) {
SpringApplication.run(TurbineStreamRabbitmqApplication.class, args);
}
@Bean
public ConfigurableCompositeMessageConverter integrationArgumentResolverMessageConverter(CompositeMessageConverterFactory factory) {
return new ConfigurableCompositeMessageConverter(factory.getMessageConverterForAllRegistered().getConverters());
}
}
改造服务调用者
以之前的 eureka-consumer-hystrix 项目为基础,在 pom.xml 里加入以下依赖
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-netflix-hystrix-streamartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-stream-rabbitartifactId>
dependency>
再在启动类上加上 @EnableHystrix 注解
@EnableHystrix
@EnableFeignClients
@SpringBootApplication
public class EurekaConsumerHystrixApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaConsumerHystrixApplication.class, args);
}
}
测试
分别启动 eureka-consumer-hystrix、turbine-stream-rabbitmq 这两个项目,然后在 RabbitMQ 的管理后台可以看到,自动创建了一个 Exchange 和 Queue