【转载】11.Spring-Cloud-Hystrix之熔断监控Turbine

   上篇博文中学到了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. <parent>
  11. <groupId>org.springframework.boot groupId>
  12. <artifactId>spring-boot-starter-parent artifactId>
  13. <version>1.5.2.RELEASE version>
  14. <relativePath />
  15. parent>
  16. <properties>
  17. <project.build.sourceEncoding>UTF-8 project.build.sourceEncoding>
  18. <project.reporting.outputEncoding>UTF-8 project.reporting.outputEncoding>
  19. <java.version>1.8 java.version>
  20. <spring-cloud.version>Dalston.RELEASE spring-cloud.version>
  21. properties>
  22. <dependencies>
  23. <dependency>
  24. <groupId>org.springframework.cloud groupId>
  25. <artifactId>spring-cloud-starter-turbine artifactId>
  26. dependency>
  27. <dependency>
  28. <groupId>org.springframework.cloud groupId>
  29. <artifactId>spring-cloud-netflix-turbine artifactId>
  30. dependency>
  31. <dependency>
  32. <groupId>org.springframework.boot groupId>
  33. <artifactId>spring-boot-starter-actuator artifactId>
  34. dependency>
  35. <dependency>
  36. <groupId>org.springframework.cloud groupId>
  37. <artifactId>spring-cloud-starter-hystrix-dashboard artifactId>
  38. dependency>
  39. dependencies>
  40. <dependencyManagement>
  41. <dependencies>
  42. <dependency>
  43. <groupId>org.springframework.cloud groupId>
  44. <artifactId>spring-cloud-dependencies artifactId>
  45. <version>${spring-cloud.version} version>
  46. <type>pom type>
  47. <scope>import scope>
  48. dependency>
  49. dependencies>
  50. dependencyManagement>
  51. <build>
  52. <plugins>
  53. <plugin>
  54. <groupId>org.springframework.boot groupId>
  55. <artifactId>spring-boot-maven-plugin artifactId>
  56. plugin>
  57. plugins>
  58. build>
  59. 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需要配置想要监控的应用名称;
2. 当clusterNameExpression: default时,turbine.aggregator.clusterConfig可以不写,因为默认就是default;
3. 当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. }

整体结构:

 

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

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

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

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

 

 

 

你可能感兴趣的:(java)