Spring Cloud Hystrix(仪表盘+Turbine集群)(4)

阳光穿透心脏的1/2处 2022-05-11 04:14 260阅读 0赞

70

在Spring Cloud 中构建一个Hystrix Dashboard非常容易只需要4步:

1.创建一个Spring Boot工程,命名为hystrix-dashboard。

2.编辑pom.xml文件,如:

  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. <groupId>com.example</groupId>
  6. <artifactId>hystrix-dashboard</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>hystrix-dashboard</name>
  10. <description>Demo project for Spring Boot</description>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>2.0.5.RELEASE</version>
  15. <relativePath/> <!-- lookup parent from repository -->
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. <java.version>1.8</java.version>
  21. <spring-cloud.version>Finchley.SR1</spring-cloud.version>
  22. </properties>
  23. <dependencies>
  24. <dependency>
  25. <groupId>org.springframework.cloud</groupId>
  26. <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-actuator</artifactId>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.cloud</groupId>
  34. <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework.boot</groupId>
  38. <artifactId>spring-boot-starter-test</artifactId>
  39. <scope>test</scope>
  40. </dependency>
  41. </dependencies>
  42. <dependencyManagement>
  43. <dependencies>
  44. <dependency>
  45. <groupId>org.springframework.cloud</groupId>
  46. <artifactId>spring-cloud-dependencies</artifactId>
  47. <version>${spring-cloud.version}</version>
  48. <type>pom</type>
  49. <scope>import</scope>
  50. </dependency>
  51. </dependencies>
  52. </dependencyManagement>
  53. <build>
  54. <plugins>
  55. <plugin>
  56. <groupId>org.springframework.boot</groupId>
  57. <artifactId>spring-boot-maven-plugin</artifactId>
  58. </plugin>
  59. </plugins>
  60. </build>
  61. </project>

3.在主类上添加@EnableHystrixDashboard注解启用Hystrix Dashboard功能

4.修改application.properties,如:

  1. spring.application.name=hystrix-dashboard
  2. server.port=2001

启动后访问http://localhost:2001/hystrix

70 1

这是Hystrix Dashboard的监控首页,该页面没有具体的监控信息。从页面上可以看出它支持三种监控方式:

1.默认的集群监控:http://turbine-hostname:port/turbine.stream

2.指定的集群监控:http://turbine-hostname:port/turbine.stream?cluster=[clusterName]

3.单体应用的监控:http://hystrix-app:port/actuator/hystrix.stream

前两者都是对集群的监控,需要整合Turbine实现。

而开启单点应用的集群监控只需要两步:

1.在服务消费者中新增如下配置文件:

  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. <groupId>com.example</groupId>
  6. <artifactId>eureka-ribbon</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>eureka-ribbon</name>
  10. <description>Demo project for Spring Boot</description>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>2.0.4.RELEASE</version>
  15. <relativePath/> <!-- lookup parent from repository -->
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. <java.version>1.8</java.version>
  21. <spring-cloud.version>Finchley.SR1</spring-cloud.version>
  22. </properties>
  23. <dependencies>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-web</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.cloud</groupId>
  30. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.cloud</groupId>
  34. <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework.cloud</groupId>
  38. <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
  39. </dependency>
  40. <!--仪表盘-->
  41. <dependency>
  42. <groupId>org.springframework.boot</groupId>
  43. <artifactId>spring-boot-starter-actuator</artifactId>
  44. </dependency>
  45. <dependency>
  46. <groupId>org.springframework.cloud</groupId>
  47. <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
  48. </dependency>
  49. <dependency>
  50. <groupId>org.springframework.boot</groupId>
  51. <artifactId>spring-boot-starter-test</artifactId>
  52. <scope>test</scope>
  53. </dependency>
  54. </dependencies>
  55. <dependencyManagement>
  56. <dependencies>
  57. <dependency>
  58. <groupId>org.springframework.cloud</groupId>
  59. <artifactId>spring-cloud-dependencies</artifactId>
  60. <version>${spring-cloud.version}</version>
  61. <type>pom</type>
  62. <scope>import</scope>
  63. </dependency>
  64. </dependencies>
  65. </dependencyManagement>
  66. <build>
  67. <plugins>
  68. <plugin>
  69. <groupId>org.springframework.boot</groupId>
  70. <artifactId>spring-boot-maven-plugin</artifactId>
  71. </plugin>
  72. </plugins>
  73. </build>
  74. </project>

2.在其主类上添加@EnableCircuitBreaker注解开启断路器功能(服务消费者)

3.SpringBoot2.0需要配置如下一个Bean:

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

然后开启服务消费者,服务提供者,服务注册中心:

70 2

Ok配置成功。

在http://localhost:2001/hystrix 下填写地址

70 3

70 4

首页上的参数:

Delay:该参数用来控制服务器上轮询监控信息的延迟时间,默认为2000ms,可以通过配置该属性降低客户端的网络和cpu消耗。

Title:该参数对应了上图Hystrix Stream之后的内容,默认为具体监控实例的URL,可以通过配置该信息展示合适标题。

从监控信息可以看到两个重要的图形信息:一个实心圆和一个曲线。

实心圆:有两种含义:通过颜色变化代表了实例的健康程度,从绿色,黄色,橙色,红色递减。除此之外,它的大小也会根据请求流量的变化而变化,流量越大实心圆就越大。

曲线:用来记录两分钟内流量的相对变化,可以用它来观察流量的上升和下降趋势。

70 5

#

Turbine集群监控

70 6

1.新建一个SpringBoot项目

2.修改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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.example</groupId>
  6. <artifactId>turbine</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>turbine</name>
  10. <description>Demo project for Spring Boot</description>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>2.0.5.RELEASE</version>
  15. <relativePath/> <!-- lookup parent from repository -->
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. <java.version>1.8</java.version>
  21. <spring-cloud.version>Finchley.SR1</spring-cloud.version>
  22. </properties>
  23. <dependencies>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-actuator</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.cloud</groupId>
  30. <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-starter-test</artifactId>
  35. <scope>test</scope>
  36. </dependency>
  37. </dependencies>
  38. <dependencyManagement>
  39. <dependencies>
  40. <dependency>
  41. <groupId>org.springframework.cloud</groupId>
  42. <artifactId>spring-cloud-dependencies</artifactId>
  43. <version>${spring-cloud.version}</version>
  44. <type>pom</type>
  45. <scope>import</scope>
  46. </dependency>
  47. </dependencies>
  48. </dependencyManagement>
  49. <build>
  50. <plugins>
  51. <plugin>
  52. <groupId>org.springframework.boot</groupId>
  53. <artifactId>spring-boot-maven-plugin</artifactId>
  54. </plugin>
  55. </plugins>
  56. </build>
  57. </project>

3.在主类上使用@EnableTurbine和@EnableTurbine注解:

  1. @EnableDiscoveryClient
  2. @EnableTurbine
  3. @SpringBootApplication
  4. public class TurbineApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(TurbineApplication.class, args);
  7. }
  8. }

4.修改application.properties

  1. spring.application.name=turbine
  2. server.port=8989
  3. management.server.port=8990
  4. eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
  5. turbine.app-config=ribbon-consumer
  6. turbine.cluster-name-expression="default"
  7. turbine.combine-host-port=true
  8. turbine.instanceUrlSuffix.default = /hystrix.stream

然后启动服务注册中心,服务提供者,服务消费者,仪表盘等服务。

在上面的配置文件中,turbine.app-config参数指定了需要收集监控信息的服务名,turbine.cluster-name-expression指定了集群默认名为default,当服务多的时候可以启动多个Tubine服务来构建不同的聚合集群,该参数可以用来区分这些不同的聚合集群。同时该参数值可以在Hystrix仪表盘中用cluster参数来指定。turbine.combine-host-port参数设为true,可以让同一主机上的服务通过主机名与端口号的组合进行区分。默认情况下会以host来区分不同的服务,这会使本地调试时,本机上的不同服务聚合成一个服务来统计。

这里我启动了两个服务消费者进行测试:

70 7

OK~~~~

参考《Spring Cloud 微服务实战》

发表评论

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

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

相关阅读