代码信息
本篇文章涉及代码版本
组件 | 版本 |
---|---|
Spring Boot | 2.0.8.RELEASE |
Spring Cloud | Finchley.SR1 |
本篇文章涉及应用
应用 | 说明 |
---|---|
base-eureka | 服务发现 |
base-producer | 服务提供方 |
base-hystrix-cluster | 熔断器集群 |
base-hystrix-turbine | 熔断器集群数据监视 |
集群数据的监控
之前在Hystrix使用dashboard的时候说过对集群的监控只有等我们引入Turbine 组件之后才能使用,那么今天就尝试把Turbine 组件引入Hystrix
在之前我们需要把之前编写的base-hystrix进行修改,变更成多个服务集群的base-hystrix-cluster项目。修改后的配置文件
spring:
profiles: hystrix1
application:
name: base-hystrix-cluster
server:
port: 8405
eureka:
client:
service-url:
defaultZone: http://localhost:8000/eureka/
logging:
file: ${spring.application.name}.log
# 配置监控端点
management:
endpoints:
web:
exposure:
# 默认配置中只有"health","info",假如不进行hystrix.stream这个参数配置会出现无法连接情况
include: ["health","info","hystrix.stream"]
---
spring:
profiles: hystrix2
application:
name: base-hystrix-cluster
server:
port: 8406
eureka:
client:
service-url:
defaultZone: http://localhost:8000/eureka/
logging:
file: ${spring.application.name}.log
# 配置监控端点
management:
endpoints:
web:
exposure:
# 默认配置中只有"health","info",假如不进行hystrix.stream这个参数配置会出现无法连接情况
include: ["health","info","hystrix.stream"]
---
spring:
profiles: hystrix3
application:
name: base-hystrix-cluster
server:
port: 8407
eureka:
client:
service-url:
defaultZone: http://localhost:8000/eureka/
logging:
file: ${spring.application.name}.log
# 配置监控端点
management:
endpoints:
web:
exposure:
# 默认配置中只有"health","info",假如不进行hystrix.stream这个参数配置会出现无法连接情况
include: ["health","info","hystrix.stream"]
引入Turbine组件
构建maven依赖
首先需要引入的依赖
<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>
参数配置application.yml
base-hystrix-turbine
的配置需要指定监控的服务base-hystrix-cluster,以及给这个监控提供一个自定义的名称
spring:
application:
name: base-hystrix-turbine
server:
port: 8410
eureka:
client:
service-url:
defaultZone: http://localhost:8000/eureka/
logging:
file: ${spring.application.name}.log
turbine:
# 指定了需要收集监控信息的服务名
app-config: base-hystrix-cluster
# 此集群监控的名字
cluster-name-expression: new String("default")
# 让同一主机上的服务通过主机名与端口号的组合来进行区分
combine-host-port: true
代码编写
启动类上需要配置@EnableTurbine注解这个时候一个简单的集群监控系统就完成了。
@EnableTurbine
@EnableEurekaClient
@SpringBootApplication
public class HystrixTurbineApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixTurbineApplication.class, args);
}
}
测试
现在让我们测试一下目前这些个配置是否发挥了作用
启动数据监视器
因为这次的项目都没有开启@EnableHystrixDashboard所以我们还是用之前的base-hystrix-dashboard
的项目。
我们访问base-hystrix-dashboard
项目的/hystrix可以看到熟悉的页面。
这次我们监控的内容修改为base-hystrix-turbine
项目的turbine.stream地址,然后可以进入下面的页面
然后我们依次访问base-hystrix-cluster
项目的三个应用接口会发现无论访问那一个都会被系统监控到。
到目前为止看起来Turbine开始发挥作用了。
需要注意的是,dashboard监控的原理是因为hystrix接口实现使用了@HystrixCommand修饰,所以这个接口的调用情况会被Hystrix记录下来。所以你上面的接口假如并没有被这个注解修饰,使用dashboard就什么都看不到了
本篇文章并未贴出所有代码,涉及的源码下载地址:https://gitee.com/daifylearn/cloud-learn
ps.上述的所有项目都是可以成功运行的。但是在后期为了实现每个应用端口尽量不冲突会有些许调整,而后续某次作死调整结构和名称可能会导致部分项目无法运行o(╯□╰)o,如果发现请留言我进行修改。