Hystrix-dashboard是一款针对Hystrix进行实时监控的工具,通过Hystrix Dashboard我们可以在直观地看到各Hystrix Command的请求响应时间, 请求成功率等数据。但是只使用Hystrix Dashboard的话, 你只能看到单个应用内的服务信息, 这明显不够. 我们需要一个工具能让我们汇总系统内多个服务的数据并显示到Hystrix Dashboard上, 这个工具就是Turbine.用数据、图表展示更加直观,让数据说话,一图顶千言。
一、Hystrix Dashboard
1.引入依赖
在EurekaConsumer项目中引入Hystrix Dashboard也比较容易,只需在pom.xml中引入依赖,主要引入spring-cloud-starter-netflix-hystrix、spring-cloud-starter-netflix-hystrix-dashboard、spring-boot-starter-actuator。
xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0modelVersion> <parent> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-parentartifactId> <version>2.1.6.RELEASEversion> <relativePath/> parent> <groupId>com.examplegroupId> <artifactId>EurekaConsumerartifactId> <version>0.0.1-SNAPSHOTversion> <name>EurekaConsumername> <description>Demo project for Spring Bootdescription> <properties> <java.version>1.8java.version> <spring-cloud.version>Greenwich.SR1spring-cloud.version> properties> <dependencies> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> dependency> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId> dependency> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-openfeignartifactId> dependency> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-openfeign-coreartifactId> <version>2.1.2.RELEASEversion> dependency> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-netflix-hystrixartifactId> <version>2.1.2.RELEASEversion> dependency> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboardartifactId> <version>2.1.2.RELEASEversion> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-actuatorartifactId> <version>2.1.6.RELEASEversion> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-testartifactId> <scope>testscope> dependency> dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-dependenciesartifactId> <version>${spring-cloud.version}version> <type>pomtype> <scope>importscope> dependency> dependencies> dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-maven-pluginartifactId> plugin> plugins> build> <packaging>warpackaging> project>
2.启用@EnableHystrixDashboard、@EnableCircuitBreaker
在main方法类中增加注解@EnableHystrixDashboard、@EnableCircuitBreaker,如果只是添加注解还是不够的,运行的时候会报404的错误,还需要在EurekaConsumerApplication类中增加ServletRegistrationBean,设置映射的url。
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.context.annotation.Bean; import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet; @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients @EnableHystrixDashboard @EnableCircuitBreaker public class EurekaConsumerApplication { public static void main(String[] args) { SpringApplication.run(EurekaConsumerApplication.class, args); } @Bean public ServletRegistrationBean getServlet() { HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet(); ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet); registrationBean.setLoadOnStartup(1); registrationBean.addUrlMappings("/hystrix.stream"); registrationBean.setName("HystrixMetricsStreamServlet"); return registrationBean; } }
3.输入http://localhost:9001/hystrix时,会显示如下页面
在上面的页面输入http://localhost:9001/hystrix.stream,点击Monitor Stream,然后请求http://localhost:9001/hello?name=cuiyw,就会在点击页面弹出的新页面中看到监控。
二、Turbine
在复杂的分布式系统中,相同服务的节点经常需要部署上百甚至上千个,很多时候,运维人员希望能够把相同服务的节点状态以一个整体集群的形式展现出来,这样可以更好的把握整个系统的状态。 为此,Netflix提供了一个开源项目(Turbine)来提供把多个hystrix.stream的内容聚合为一个数据源供Dashboard展示。
1.引入依赖
这里新建了HystrixTurbine项目,在项目中主要引入spring-cloud-starter-netflix-hystrix-dashboard、spring-cloud-starter-netflix-turbine、spring-boot-starter-actuator、spring-cloud-starter-netflix-turbine-stream。
xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0modelVersion> <parent> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-parentartifactId> <version>2.1.6.RELEASEversion> <relativePath/> parent> <groupId>com.examplegroupId> <artifactId>HystrixTurbineartifactId> <version>0.0.1-SNAPSHOTversion> <name>HystrixTurbinename> <description>Demo project for Spring Bootdescription> <properties> <java.version>1.8java.version> <spring-cloud.version>Greenwich.SR2spring-cloud.version> properties> <dependencies> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboardartifactId> dependency> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-netflix-turbineartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-testartifactId> <scope>testscope> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-actuatorartifactId> <version>2.1.6.RELEASEversion> dependency> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-netflix-turbine-streamartifactId> <version>2.0.1.RELEASEversion> dependency> dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-dependenciesartifactId> <version>${spring-cloud.version}version> <type>pomtype> <scope>importscope> dependency> dependencies> dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-maven-pluginartifactId> plugin> plugins> build> <packaging>warpackaging> project>
2.设置配置文件
spring.application.name=hystrix-dashboard-turbine
server.port=8002
turbine.appConfig=spring-cloud-consumerA,spring-cloud-consumerB
turbine.aggregator.clusterConfig= default
turbine.clusterNameExpression= new String("default")
eureka.client.serviceUrl.defaultZone=http://localhost:8088/eureka/
spring.main.allow-bean-definition-overriding=true
turbine.instanceUrlSuffix.default =hystrix.stream
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
3.设置@EnableHystrixDashboard、@EnableTurbineStream、@EnableTurbine
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; import org.springframework.cloud.netflix.turbine.EnableTurbine; import org.springframework.cloud.netflix.turbine.stream.EnableTurbineStream; @SpringBootApplication @EnableHystrixDashboard @EnableTurbineStream @EnableTurbine public class HystrixTurbineApplication { public static void main(String[] args) { SpringApplication.run(HystrixTurbineApplication.class, args); } }
4.测试
分别启动EurekaServer、EurekaClient,分别修改EurekaConsumer,设置端口9001、9002,以及对应的spring.application.name为spring-cloud-consumerA、spring-cloud-consumerB,并启动,最后启动HystrixTurbine。
在浏览器中输入http://localhost:8002/hystrix,在出现的页面中输入http://localhost:8002/turbine.stream ,点击Monitor Stream,在浏览器中分别输入http://localhost:9002/hello?name=cuiyw、http://localhost:9001/hello?name=cuiyw,之后可以观察监控页面。由于这里只有一个接口一个生产者,所以只显示了一个。
这里还有一个要注意的,由于未在配置文件中设置turbine.instanceUrlSuffix.default =hystrix.stream,导致打开监控页面时一直在loading。日志报com.netflix.turbine.monitor.instance.InstanceMonitor$MisconfiguredHostException: [{"timestamp":"2019-07-21T14:05:10.222+0000","status":404,"error":"Not Found","message":"No message available","path":"/actuator/hystrix.stream"}]错误。