【转载】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. <!--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需要配置想要监控的应用名称;
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/5http://localhost:9002/queryUser/5;让监控有数据信息;

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

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值