Spring Cloud(F版)Hystrix仪表盘和Turbine集群监控

深藏阁楼爱情的钟 2021-10-09 04:46 532阅读 0赞

在上一篇文章【Spring Cloud(F版)断路器Hystrix】中,简单的介绍了Hystrix的使用,以及结合Feign如何使用Hystrix进行降级处理。在我们生产中,Hystrix断路器是非常实用的,当一个服务提供者出现问题之后,不至于直接将500的错误或者其他不友好的错误信息直接返回给用户,而是通过断路器的降级处理,给出一个友好的提示,提高了用户的体验感。

介绍完Hystrix的使用,我们再来说说这个Hystrix仪表盘,Hystrix仪表盘是根据系统一段时间内发生的请求情况来展示的可视化面板,这些信息能让我们很直观的看到系统运行的情况。

——————————————————————————————————————————————————————

环境搭建

在这里插入图片描述
这是我搭建的一个maven多模块的测试项目,如果不知道IDEA如何创建多模块项目的可以看一下【IntelliJ IDEA创建Maven多模块项目】这篇文章。

此项目中,commons是公共类模块,存放实体类、工具类等,是一个普通的java项目,consumers是消费者(Spring Boot项目),这里我创建两个模块分别是移动端和pc端这两个消费者,providers是提供者(Spring Boot项目),同样也创建了用户以及订单两个提供者。

eureka-server则是服务注册中心,这个就不过多讲解了可以看之前的几篇文章。

这里提供者和消费者都需要引入commons模块的依赖

添加依赖

我们在消费者consumer-mobile项目中,添加以下依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-actuator</artifactId>
  4. </dependency>
添加配置信息
  1. management:
  2. endpoints:
  3. web:
  4. exposure:
  5. include:
  6. - hystrix.stream

搭建仪表盘项目

创建一个新的项目hystrix-dashboard,不清楚如何创建项目的可以查看【IDEA 社区版 创建spring cloud项目】这篇文章。

添加依赖:
  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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.0.3.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.clarezhou.example</groupId>
  12. <artifactId>hystrix-dashboard</artifactId>
  13. <version>1.0-SNAPSHOT</version>
  14. <name>hystrix-dashboard</name>
  15. <description>断路器仪表盘</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. <spring-cloud.version>Finchley.SR3</spring-cloud.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.cloud</groupId>
  23. <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.cloud</groupId>
  27. <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-starter-actuator</artifactId>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-starter-web</artifactId>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.springframework.boot</groupId>
  39. <artifactId>spring-boot-starter-test</artifactId>
  40. <scope>test</scope>
  41. </dependency>
  42. </dependencies>
  43. <dependencyManagement>
  44. <dependencies>
  45. <dependency>
  46. <groupId>org.springframework.cloud</groupId>
  47. <artifactId>spring-cloud-dependencies</artifactId>
  48. <version>${spring-cloud.version}</version>
  49. <type>pom</type>
  50. <scope>import</scope>
  51. </dependency>
  52. </dependencies>
  53. </dependencyManagement>
  54. <build>
  55. <plugins>
  56. <plugin>
  57. <groupId>org.springframework.boot</groupId>
  58. <artifactId>spring-boot-maven-plugin</artifactId>
  59. </plugin>
  60. </plugins>
  61. </build>
  62. </project>
添加注解

在主程序入口,添加@EnableHystrixDashboard,开启仪表盘。

  1. package com.clarezhou.example;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
  5. /**
  6. * @author clarezhou
  7. */
  8. @SpringBootApplication
  9. @EnableHystrixDashboard
  10. public class HystrixDashboardApplication {
  11. public static void main(String[] args) {
  12. SpringApplication.run(HystrixDashboardApplication.class, args);
  13. }
  14. }
添加属性配置

在application.yml中添加属性信息

  1. server:
  2. port: 8200
  3. spring:
  4. application:
  5. name: hystrix-dashboard
启动仪表盘项目

启动hystrix-dashboard项目,然后输入localhost:8200/hystrix,就能看到以下界面。
在这里插入图片描述

分别启动eureka-server项目,provider-user项目,consumer-mobile项目

然后在浏览器中输入:http://localhost:8101/actuator/hystrix.stream 可以看到打印以下信息。在这里插入图片描述

然后在下面输入要监测的服务,然后Monitor Stream。
在这里插入图片描述
然后就能看到以下信息,以下是我分别访问了sayHelloByName , getUserInfo ,sayHello1三个接口。
在这里插入图片描述

圆圈:实心圆的颜色从绿黄橙红依次递减,表示服务的健康程度依次降低,圆越大,表示服务的流量越大,通过这些颜色,可以快速的发现故障、具体的实例以及请求压力等。

曲线:代表2分钟内流量的变化

圆圈右边的数字:
第一列分别是请求成功的数量,短路/熔断数量,错误的请求数量。
第二列分别是超时的请求数量,线程池拒绝数量,失败的请求数量。
百分比是最近10秒内错误的比率

Host 和 Cluster:机器和集群的请求频率
Circuit :断路器的状态 open/cloused
Hosts Median Mean :百分位的延迟数
Thread Pools : 线程池的指标,核心线程池的指标,队列大小等。

——————————————————————————————————————————————————————
Hystrix dashboard只能监控单体应用,而我们实际应用中都是集群的,单体并不是特别的有用,而Turbine则是集群监控,汇聚所有hystrix.stream流的方案,然后在Hystrix dashboard中显示。

好了,废话不多说,开始搭建例子。

在上面项目的基础上,创建一个新的项目,取名为turbin-server,这里如何创建项目就不再赘述了。

添加依赖
  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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.0.3.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com..clarezhou.example</groupId>
  12. <artifactId>turbine-server</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>turbine-server</name>
  15. <description>集群监控</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. <spring-cloud.version>Finchley.SR3</spring-cloud.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.cloud</groupId>
  23. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-actuator</artifactId>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.cloud</groupId>
  31. <artifactId>spring-cloud-netflix-hystrix-dashboard</artifactId>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework.cloud</groupId>
  35. <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.springframework.boot</groupId>
  39. <artifactId>spring-boot-starter-test</artifactId>
  40. <scope>test</scope>
  41. </dependency>
  42. </dependencies>
  43. <dependencyManagement>
  44. <dependencies>
  45. <dependency>
  46. <groupId>org.springframework.cloud</groupId>
  47. <artifactId>spring-cloud-dependencies</artifactId>
  48. <version>${spring-cloud.version}</version>
  49. <type>pom</type>
  50. <scope>import</scope>
  51. </dependency>
  52. </dependencies>
  53. </dependencyManagement>
  54. <build>
  55. <plugins>
  56. <plugin>
  57. <groupId>org.springframework.boot</groupId>
  58. <artifactId>spring-boot-maven-plugin</artifactId>
  59. </plugin>
  60. </plugins>
  61. </build>
  62. </project>
添加注解

在项目启动入口程序,添加@EnableTurbine和@EnableHystrixDashboard注解

  1. @SpringBootApplication
  2. @EnableDiscoveryClient
  3. @EnableHystrixDashboard
  4. @EnableTurbine
  5. public class TurbineServerApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(TurbineServerApplication.class, args);
  8. }
  9. }
添加配置信息
  1. server:
  2. port: 8300
  3. spring:
  4. application:
  5. name: turbine
  6. security:
  7. user:
  8. name: admin
  9. password: 123456
  10. eureka:
  11. client:
  12. serviceUrl:
  13. defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@localhost:8081/eureka
  14. turbine:
  15. app-config: consumer-web,consumer-mobile
  16. cluser-name-expression: "default"
  17. combine-host-port: true

turbine.app-config=consumer-web 这个配置是指监控的应用
turbine.cluster-name-expression=”default”,表示集群的名字为default
turbine.combine-host-port=true表示同一主机上的服务通过host和port的组合来进行区分,默认情况下是使用host来区分,这样会使本地调试有问题

发表评论

表情:
评论列表 (有 0 条评论,532人围观)

还没有评论,来说两句吧...

相关阅读