上一篇只是大概介绍了一下断路器Hystrix Dashboard监控,如何使用Hystrix Dashboard监控微服务的状态呢?这篇看看Ribbon如何整合断路器监控Hystrix Dashboard。今天的项目主要整合sc-eureka-client-consumer-ribbon-hystrix项目和sc-hystrix-dashboard项目
1、 新建项目sc-ribbon-hystrix-dashboard,对应的pom.xml文件
<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> <groupId>spring-cloudgroupId> <artifactId>sc-ribbon-hystrix-dashboardartifactId> <version>0.0.1-SNAPSHOTversion> <packaging>jarpackaging> <parent><groupId>org.springframework.bootgroupId><artifactId>spring-boot-starter-parentartifactId><version>2.0.4.RELEASEversion>parent> <dependencyManagement><dependencies><dependency><groupId>org.springframework.cloudgroupId><artifactId>spring-cloud-dependenciesartifactId><version>Finchley.RELEASEversion><type>pomtype><scope>importscope>dependency> dependencies>dependencyManagement> <properties><project.build.sourceEncoding>UTF-8project.build.sourceEncoding><maven.compiler.source>1.8maven.compiler.source><maven.compiler.target>1.8maven.compiler.target>properties> <dependencies><dependency><groupId>org.springframework.cloudgroupId><artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>dependency> org.springframework.cloudspring-cloud-starter-ribbon1.4.5.RELEASE--> <dependency><groupId>org.springframework.cloudgroupId><artifactId>spring-cloud-starter-netflix-ribbonartifactId>dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId>dependency> org.springframework.cloudspring-cloud-starter-hystrix1.4.5.RELEASE--> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-netflix-hystrixartifactId>dependency> org.springframework.cloudspring-cloud-starter-hystrix-dashboard1.4.5.RELEASE--> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboardartifactId>dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-actuatorartifactId>dependency> dependencies>project>
可以看到这个pom.xml文件是sc-eureka-client-consumer-ribbon-hstrix项目和sc-hystrix-dashboard项目的pom.xml的并集
2、 新建spring boot 启动类HystrixDashboardApplication.java
package sc.consumer; import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.cloud.netflix.hystrix.EnableHystrix;import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; @SpringBootApplication @EnableEurekaClient @EnableHystrix @EnableHystrixDashboardpublic class HystrixDashboardApplication { public static void main(String[] args) { SpringApplication.run(HystrixDashboardApplication.class, args); } }
可以看到这个启动类的注解也是sc-eureka-client-consumer-ribbon-hstrix项目和sc-hystrix-dashboard项目的启动类的并集
3、 其他的java类就不多说了,项目接口如下
4、 分别启动配置中心和sc-eureka-server和服务提供者sc-eureka-client-provider
5、 启动项目sc-ribbon-hystrix-dashboard
方式一:http://127.0.0.1:5600/hystrix验证是否启动成功
方式二:访问http://127.0.0.1:5600/hystrix.stream
这个是访问DashboardServletConfig.java这个servlet对应的地址
上篇说了一下下图这段英文的大概意思
6、 演示一下单应用模式下的断路器监控Hystrix Dashboard
在url里输入:http://127.0.0.1:5600/hystrix.stream(IP和端口对应服务消费者的IP和端口)
然后点击Monitor Stream按钮
7、 使用postman访问任何接口,以获取用户列表为例(http://127.0.0.1:5600/cli/user/listUser),多访问几次在查看对应的监控界面
发现监控界面的图出现了动态变化,这个就是对服务调用的监控。图中相关单元的含义可以查看:
https://github.com/Netflix-Skunkworks/hystrix-dashboard/wiki
http://127.0.0.1:5600/hystrix.stream对应的界面也出现了大量数据,这个也是对服务调用监控的数据。
其他接口也自行测试。