飞道的博客

Spring Cloud学习笔记(八)熔断器数据聚合监控Turbine

300人阅读  评论(0)

上一篇内容学习了Hystrix Dashboard查看熔断信息,通常情况下,查看单个的Hystrix熔断信息意义不大,需要把信息聚合到一起,这个时候就需要用到Turbine了,本篇学习内容,基于上一篇学习代码,我们在添加一个Service-Consumer项目,具体内容可以下载工程代码,分别命名为Service-Consumer-A和Service-Consumer-B。在建一个Service-Turbine工程,主要用来显示聚合监控数据。

首先在新建模块中引入POM依赖:


  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0 </modelVersion>
  5. <parent>
  6. <groupId>org.dothwinds </groupId>
  7. <artifactId>spring-cloud-study </artifactId>
  8. <version>1.0.0 </version>
  9. </parent>
  10. <groupId>org.dothwinds </groupId>
  11. <artifactId>spring-cloud-study-service-turbine </artifactId>
  12. <version>1.0.0 </version>
  13. <name>spring-cloud-study-service-turbine </name>
  14. <dependencies>
  15. <dependency>
  16. <groupId>org.springframework.cloud </groupId>
  17. <artifactId>spring-cloud-starter-netflix-eureka-client </artifactId>
  18. </dependency>
  19. <dependency>
  20. <groupId>org.springframework.boot </groupId>
  21. <artifactId>spring-boot-starter-web </artifactId>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.springframework.cloud </groupId>
  25. <artifactId>spring-cloud-starter-openfeign </artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.cloud </groupId>
  29. <artifactId>spring-cloud-starter-netflix-hystrix </artifactId>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.springframework.cloud </groupId>
  33. <artifactId>spring-cloud-starter-netflix-hystrix-dashboard </artifactId>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.springframework.boot </groupId>
  37. <artifactId>spring-boot-starter-actuator </artifactId>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.springframework.cloud </groupId>
  41. <artifactId>spring-cloud-starter-netflix-turbine </artifactId>
  42. </dependency>
  43. </dependencies>
  44. </project>

 然后在项目启动类上,加入注解@EnableTurbine


  
  1. package org.dothwinds.serviceconsumer;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  5. import org.springframework.cloud.netflix.hystrix.EnableHystrix;
  6. import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
  7. import org.springframework.cloud.netflix.turbine.EnableTurbine;
  8. import org.springframework.cloud.openfeign.EnableFeignClients;
  9. @SpringBootApplication
  10. @EnableEurekaClient
  11. @EnableFeignClients
  12. @EnableHystrix
  13. @EnableHystrixDashboard
  14. @EnableTurbine
  15. public class SpringCloudStudyServiceConsumerApplication {
  16. public static void main(String[] args) {
  17. SpringApplication.run(SpringCloudStudyServiceConsumerApplication.class, args);
  18. }
  19. }

然后我们来修改配置文件application.yml文件,加入turbine的配置内容


  
  1. server:
  2. port: 11000
  3. spring:
  4. application:
  5. name: service-turbine #应用名称,会显示在eureka server中
  6. eureka:
  7. client:
  8. service-url:
  9. defaultZone: http: //localhost:8100/eureka/ #eureka server地址
  10. management:
  11. endpoints:
  12. web:
  13. exposure:
  14. include: "*"
  15. cors:
  16. allowed-origins: "*"
  17. allowed-methods: "*"
  18. #turbine配置信息
  19. turbine:
  20. #需要聚合监控的应用名称,多个以逗号隔开
  21. app-config: service-cosumer-a,service-cosumer-b
  22. aggregator:
  23. #聚合哪些集群,默认为 default
  24. clusterConfig: default
  25. #集群名称为 default
  26. clusterNameExpression: new String( "default")

分别启动Eureka和两个Service-Consumer服务,还有Service-Turbine的服务:

接下来进入Turbine的监控界面,http://localhost:11000/turbine.stream返回如下信息:

 

分别访问Service-Consumer的地址:http://localhost:10001/testLoadBalancehttp://localhost:10002/testLoadBalance,这两个地址多刷新几次,然后打开http://localhost:11000/hystrix,输入http://localhost:11000/turbine.stream,点击Monitor Stream按钮,成功返回:

遇到的问题:

启动Service-Turbine服务之后,访问/actuator/hystrix.stream报了404错误,返回类似的如下错误信息

[{"timestamp":"2020-03-26T13:02:10.935+0000","status":404,"error":"Not Found","message":"No message available","path":"/actuator/hystrix.stream"}]

 这个时候我们需要把监控的服务都在启动类中加上下面的代码,否则默认是找不到的(原因在此:https://blog.csdn.net/ddxd0406/article/details/79643059


  
  1. @Bean
  2. public ServletRegistrationBean getServlet() {
  3. HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
  4. ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
  5. registrationBean.setLoadOnStartup( 1);
  6. registrationBean.addUrlMappings( "/actuator/hystrix.stream");
  7. registrationBean.setName( "HystrixMetricsStreamServlet");
  8. return registrationBean;
  9. }

参考资料:https://cloud.spring.io/spring-cloud-static/Greenwich.SR5/single/spring-cloud.html

代码:https://gitee.com/dothwinds/Spring-Cloud-Study/tree/master/spring-cloud-study-turbine


转载:https://blog.csdn.net/Dothwinds/article/details/105112152
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场