前面的话】书接上文,本文的某些知识依赖我的上一篇SpringCLoud的文章:SpringCloud之Feign,如果没有看过可以先移步去看一下。前文提到了hystrix的应用,以及hystrix的监控,当时我们在实际生产过程中往往会在多个服务中或者说网关集群中使用hystrix,这样我们来监控的是否再去分别查看当时的每个应用的话,效率就会显得很低下呢,这里我们就要用的上文提到的集群监控了。


看单个的Hystrix Dashboard的数据并没有什么多大的价值,要想看这个系统的Hystrix Dashboard数据就需要用到Hystrix Turbine。Hystrix Turbine将每个服务Hystrix Dashboard数据进行了整合。Hystrix Turbine的使用非常简单,只需要引入相应的依赖和加上注解和配置就可以了。
简而言之:Turbine就是聚合监控多个Hystrix Dashboard的数据。

新建一个feign子工程lovin-cloud-turbine,用于后面的操作。下面是主要的pom依赖:
~~~pom

lovincloud
com.eelve.lovincloud
1.0-SNAPSHOT

4.0.0

  1. <artifactId>lovin-cloud-turbine</artifactId>
  2. <packaging>jar</packaging>
  3. <name>lovincloudturbine</name>
  4. <version>0.0.1</version>
  5. <description>turbine监控</description>
  6. <dependencies>
  7. <dependency>
  8. <groupId>org.springframework.cloud</groupId>
  9. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-security</artifactId>
  14. </dependency>
  15. <dependency>
  16. <groupId>org.springframework.boot</groupId>
  17. <artifactId>spring-boot-starter-web</artifactId>
  18. </dependency>
  19. <dependency>
  20. <groupId>de.codecentric</groupId>
  21. <artifactId>spring-boot-admin-starter-client</artifactId>
  22. <version>2.1.6</version>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.cloud</groupId>
  26. <artifactId>spring-cloud-starter-turbine</artifactId>
  27. <version>1.4.7.RELEASE</version>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.cloud</groupId>
  31. <artifactId>spring-cloud-netflix-turbine</artifactId>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-starter-actuator</artifactId>
  36. </dependency>
  37. <!--
  38. <dependency>
  39. <groupId>org.springframework.cloud</groupId>
  40. <artifactId>spring-cloud-starter-turbine-amqp</artifactId>
  41. <version>1.4.7.RELEASE</version>
  42. </dependency>
  43. -->
  44. </dependencies>
  45. <build>
  46. <plugins>
  47. <plugin>
  48. <groupId>org.springframework.boot</groupId>
  49. <artifactId>spring-boot-maven-plugin</artifactId>
  50. </plugin>
  51. </plugins>
  52. </build>

~~~

  • 这里为了安全,我这里还是添加spring-boot-starter-security
  1. server:
  2. port: 8808 # 服务端口号
  3. spring:
  4. application:
  5. name: lovincloudturbine # 服务名称
  6. security:
  7. basic:
  8. enabled: true
  9. user:
  10. name: lovin
  11. password: ${REGISTRY_SERVER_PASSWORD:lovin}
  12. eureka:
  13. client:
  14. serviceUrl:
  15. defaultZone: http://lovin:lovin@localhost:8881/eureka/ # 注册到的eureka服务地址
  16. instance:
  17. leaseRenewalIntervalInSeconds: 10
  18. health-check-url-path: /actuator/health
  19. metadata-map:
  20. user.name: lovin
  21. user.password: lovin
  22. management:
  23. endpoints:
  24. web:
  25. exposure:
  26. include: "*"
  27. endpoint:
  28. health:
  29. show-details: ALWAYS
  30. turbine:
  31. aggregator:
  32. clusterConfig: default # 指定聚合哪些集群,多个使用","分割,默认为default。可使用http://.../turbine.stream?cluster={clusterConfig之一}访问
  33. appConfig: lovinfeignclient,lovinribbonclient ### 配置Eureka中的serviceId列表,表明监控哪些服务
  34. clusterNameExpression: new String("default")
  35. # 1. clusterNameExpression指定集群名称,默认表达式appName;此时:turbine.aggregator.clusterConfig需要配置想要监控的应用名称
  36. # 2. 当clusterNameExpression: default时,turbine.aggregator.clusterConfig可以不写,因为默认就是default
  37. # 3. 当clusterNameExpression: metadata['cluster']时,假设想要监控的应用配置了eureka.instance.metadata-map.cluster: ABC,则需要配置,同时turbine.aggregator.clusterConfig: ABC
  • 配置spring-boot-starter-security,这里为了方便我这里放开所有请求
  1. package com.eelve.lovin.config;
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  4. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  5. /**
  6. * @ClassName SecurityConfig
  7. * @Description TDO
  8. * @Author zhao.zhilue
  9. * @Date 2019/8/16 14:13
  10. * @Version 1.0
  11. **/
  12. @Configuration
  13. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  14. @Override
  15. protected void configure(HttpSecurity http) throws Exception {
  16. http.authorizeRequests().anyRequest().permitAll()
  17. .and().csrf().disable();
  18. }
  19. }
  • 在主类上添加@EnableTurbine,当然也需要注册到注册中心:
  1. package com.eelve.lovin;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. import org.springframework.cloud.netflix.turbine.EnableTurbine;
  6. import org.springframework.cloud.netflix.turbine.stream.EnableTurbineStream;
  7. /**
  8. * @ClassName LovinCloudTurbineApplication
  9. * @Description TDO
  10. * @Author zhao.zhilue
  11. * @Date 2019/8/25 17:17
  12. * @Version 1.0
  13. *
  14. **/
  15. @SpringBootApplication
  16. @EnableDiscoveryClient
  17. @EnableTurbine
  18. public class LovinCloudTurbineApplication {
  19. public static void main(String[] args) {
  20. SpringApplication.run(LovinCloudTurbineApplication.class,args);
  21. }
  22. }
  • 改造lovin-feign-client,使之变成集群,添加第二份配置文件

  • 依次启动eureka的服务端和两个客户端,以及lovin-feign-client、lovin-ribbon-client和新建的lovin-cloud-turbine
    我们可以看到服务已经全部启动成功
    我们可以看到服务已经全部启动成功
  • 然后访问几次http://localhost:8806/getHello和http://localhost:8805/hello使之产生熔断器数据,然后访问http://localhost:8806/hystrix按照提示选择第一个集群监控
    选择聚合监控
    查看详情

  • 修改pom依赖
  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-security</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-web</artifactId>
  13. </dependency>
  14. <dependency>
  15. <groupId>de.codecentric</groupId>
  16. <artifactId>spring-boot-admin-starter-client</artifactId>
  17. <version>2.1.6</version>
  18. </dependency>
  19. <dependency>
  20. <groupId>org.springframework.cloud</groupId>
  21. <artifactId>spring-cloud-starter-turbine</artifactId>
  22. <version>1.4.7.RELEASE</version>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.cloud</groupId>
  26. <artifactId>spring-cloud-netflix-turbine</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-turbine-amqp</artifactId>
  35. <version>1.4.7.RELEASE</version>
  36. </dependency>
  37. </dependencies>
  38. <build>
  39. <plugins>
  40. <plugin>
  41. <groupId>org.springframework.boot</groupId>
  42. <artifactId>spring-boot-maven-plugin</artifactId>
  43. </plugin>
  44. </plugins>
  45. </build>

可以看到这里主要引入了spring-cloud-starter-turbine-amqp依赖,它实际上就是包装了spring-cloud-starter-turbine-stream和pring-cloud-starter-stream-rabbit。

  • 添加连接rabbitmq配置
  1. spring:
  2. rabbitmq:
  3. host: 127.0.0.1
  4. port: 5672
  5. username: guest
  6. password: guest
  • 在应用主类中使用@EnableTurbineStream注解来启用Turbine Stream的配置
  1. package com.eelve.lovin;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. import org.springframework.cloud.netflix.turbine.EnableTurbine;
  6. import org.springframework.cloud.netflix.turbine.stream.EnableTurbineStream;
  7. /**
  8. * @ClassName LovinCloudTurbineApplication
  9. * @Description TDO
  10. * @Author zhao.zhilue
  11. * @Date 2019/8/25 17:17
  12. * @Version 1.0
  13. *
  14. **/
  15. @SpringBootApplication
  16. @EnableDiscoveryClient
  17. @EnableTurbineStream
  18. public class LovinCloudTurbineApplication {
  19. public static void main(String[] args) {
  20. SpringApplication.run(LovinCloudTurbineApplication.class,args);
  21. }
  22. }
  • 添加spring-cloud-netflix-hystrix-amqp依赖
  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-netflix-hystrix-amqp</artifactId>
  4. <version>1.4.7.RELEASE</version>
  5. </dependency>
  • 添加连接rabbitmq配置
  1. spring:
  2. rabbitmq:
  3. host: 127.0.0.1
  4. port: 5672
  5. username: guest
  6. password: guest

注册中心
聚合监控结果

  • 我们可以看到我们调用的服务不再是像再上一篇文章中的直接访问对应的服务,而是通过feign的Ribbon的负载均衡的去调用的,而且这里说明一点,Ribbon的默认机制是轮询。
  1. 直接使用Turbine监控

直接使用Turbine监控

  1. 使用RabbitMQ异步监控

使用RabbitMQ异步监控

  • 监控图示

  • 我们可以在监控信息的左上部分找到两个重要的图形信息:一个实心圆和一条曲线。
  1. 实心圆:共有两种含义。它通过颜色的变化代表了实例的健康程度,如下图所示,它的健康度从绿色、黄色、橙色、红色递减。该实心圆除了颜色的变化之外,它的大小也会根据实例的请求流量发生变化,流量越大该实心圆就越大。所以通过该实心圆的展示,我们就可以在大量的实例中快速的发现故障实例和高压力实例。
  2. 曲线:用来记录2分钟内流量的相对变化,我们可以通过它来观察到流量的上升和下降趋势。

版权声明:本文为eelve原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/eelve/p/11590727.html