11.Spring-Cloud-Hystrix之熔断监控Turbine

超、凢脫俗 2022-05-26 07:26 270阅读 0赞

上篇博文中学到了Hystrix Board监控单个应用,除此之外还有一个Turbine提供的监控点/trubine.stream是对集群的监控使用。在复杂的分布式系统中,相同服务的节点经常需要部署上百甚至上千个,很多时候,运维人员希望能够把相同服务的节点状态以一个整体集群的形式展现出来,这样可以更好的把握整个系统的状态。 为此,Netflix提供了一个开源项目(Turbine)来提供把多个hystrix.stream的内容聚合为一个数据源供Dashboard展示。

一:构建Turbine项目

1.pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>8.spring-cloud-turbine</groupId>
  5. <artifactId>turbine</artifactId>
  6. <packaging>jar</packaging>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <name>spring-cloud Maven Webapp</name>
  9. <url>http://maven.apache.org</url>
  10. <!--springboot采用1.5.x 对应springcloud版本为 Dalston -->
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>1.5.2.RELEASE</version>
  15. <relativePath />
  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>Dalston.RELEASE</spring-cloud.version>
  22. </properties>
  23. <dependencies>
  24. <dependency>
  25. <groupId>org.springframework.cloud</groupId>
  26. <artifactId>spring-cloud-starter-turbine</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.cloud</groupId>
  30. <artifactId>spring-cloud-netflix-turbine</artifactId>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-starter-actuator</artifactId>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework.cloud</groupId>
  38. <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
  39. </dependency>
  40. </dependencies>
  41. <dependencyManagement>
  42. <dependencies>
  43. <dependency>
  44. <groupId>org.springframework.cloud</groupId>
  45. <artifactId>spring-cloud-dependencies</artifactId>
  46. <version>${spring-cloud.version}</version>
  47. <type>pom</type>
  48. <scope>import</scope>
  49. </dependency>
  50. </dependencies>
  51. </dependencyManagement>
  52. <!-- 这样变成可执行的jar -->
  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>

2.启动类

  1. package com.niugang;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
  5. import org.springframework.cloud.netflix.turbine.EnableTurbine;
  6. /**
  7. *
  8. * Turbine监控
  9. * @author niugang
  10. *
  11. */
  12. @SpringBootApplication
  13. @EnableHystrixDashboard
  14. @EnableTurbine //启动turbine
  15. public class Application {
  16. public static void main(String[] args) {
  17. SpringApplication.run(Application.class, args);
  18. }
  19. }

3.配置

  1. #指定微服务的名称后续在调用的时候只需要使用该名称就可以进行服务的访问
  2. spring.application.name=hystrix-dashboard-turbine
  3. server.port=9004
  4. #配置Eureka中的serviceId列表,表明监控哪些服务
  5. turbine.appConfig=RIBBON-CONSUMER1,RIBBON-CONSUMER2
  6. turbine.aggregator.clusterConfig= default
  7. turbine.clusterNameExpression= new String("default")
  8. #turbine.combine-host-port=true
  9. #注册中心地址
  10. eureka.client.serviceUrl.defaultZone=http://testhost:8000/eureka/
  • turbine.appConfig :配置Eureka中的serviceId列表,表明监控哪些服务
  • turbine.aggregator.clusterConfig :指定聚合哪些集群,多个使用”,”分割,默认为default。可使用http://.../turbine.stream?cluster=\{clusterConfig之一\}访问
  • turbine.clusterNameExpression : 1. clusterNameExpression指定集群名称,默认表达式appName;

此时:turbine.aggregator.clusterConfig需要配置想要监控的应用名称;

  1. 当clusterNameExpression: default时,turbine.aggregator.clusterConfig可以不写,因为默认就是default;
  2. 当clusterNameExpression: metadata[‘cluster’]时,假设想要监控的应用配置了eureka.instance.metadata-map.cluster: ABC,则需要配置,同时turbine.aggregator.clusterConfig: ABC

二:额外准备

准备ribbon-consumer1,ribbon-consumer2两个服务。

1.ribbon-consumer1

  1. #指定微服务的名称后续在调用的时候只需要使用该名称就可以进行服务的访问
  2. spring.application.name=ribbon-consumer1
  3. server.port=9002
  4. spring.cloud.loadbalancer.retry.enabled=true
  5. #注册中心地址
  6. eureka.client.serviceUrl.defaultZone=http://testhost:8000/eureka/

-————————————————————————————————————————————————————————-

  1. HelloService.java
  2. @CacheResult
  3. @HystrixCommand(fallbackMethod = "queryUserBackMethod",commandProperties = {
  4. @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000"),
  5. @HystrixProperty(name = "requestCache.enabled", value = "true")
  6. })
  7. public String queryUser( String id) {
  8. String body = restTemplate
  9. .postForEntity("http://service-provide/queryUser/{1}", String.class, String.class,id)
  10. .getBody();
  11. return body;
  12. }

2.ribbon-consumer2

  1. #指定微服务的名称后续在调用的时候只需要使用该名称就可以进行服务的访问
  2. spring.application.name=ribbon-consumer2
  3. server.port=9003
  4. spring.cloud.loadbalancer.retry.enabled=true
  5. #注册中心地址
  6. eureka.client.serviceUrl.defaultZone=http://testhost:8000/eureka/

-————————————————————————————————————————————————————————-

  1. HelloService2.java
  2. @CacheResult
  3. @HystrixCommand(fallbackMethod = "queryUserBackMethod",commandProperties = {
  4. @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000"),
  5. @HystrixProperty(name = "requestCache.enabled", value = "true")
  6. })
  7. public String queryUser2( String id) {
  8. String body = restTemplate
  9. .postForEntity("http://service-provide/queryUser/{1}", String.class, String.class,id)
  10. .getBody();
  11. return body;
  12. }

整体结构:

70

以上配置完成,启动注册中心,启动服务调用者,启动服务消费者,启动Turbine,具体如下:

70 1

访问 http://localhost:9004/turbine.stream

70 2

刷新http://localhost:9003/queryUser/5,http://localhost:9002/queryUser/5;让监控有数据信息;

访问:http://localhost:9004/hystrix,红框输入地址,点击Monitor Stream

70 3

70 4

微信公众号

  1. ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L25pdWdhbmcwOTIw_size_16_color_FFFFFF_t_70][]

发表评论

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

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

相关阅读

    相关 使用Turbine聚合监控

    Turbine概述 Turbine真正做的,就是将每一个(指定)服务的Hystrix/stream中的状态信息取出,并集中处理(计算与展示),应该说,它是具有自己独立的调