🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
Hystrix提供了<mark>准实时的调用监控(Hystrix Dashboard)</mark>,Hystrix会持续地记录所有<mark>通过Hystrix发起的请求</mark>的执行信息,<mark>并以统计报表和图形的形式展示给用户</mark>,包括每秒执行多少次请求,有多少次成功,多少次失败等。 <br/> Netflix通过 hystrix-metrics-event-stream 项目实现了对以上指标的监控。Spring Cloud也提供了Hystrix Dashboard的整合,对监控内容转化成可视化界面。 <br/> 实现 Hystrix Dashboard 服务监控的步骤如下: **1. 构建Hystrix监控模块:cloud-comsumer-hystrix-dashboard9001** **2. 在当前模块的`pom.xml`中添加 Hystrix 相关依赖** ```xml <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> ... </dependencies> ``` **3. 当前模块的`resources/application.yml`** ```yml server: port: 9001 spring: application: name: cloud-consumer-hystrix-dashboard ``` **4. 在当前模块的启动类上添加注解`@EnableHystrixDashboard`** ```java @SpringBootApplication @EnableHystrixDashboard public class DashboardMain9001 { public static void main(String[] args) { SpringApplication.run(DashboardMain9001.class, args); } } ``` **5. 验证当前模块是否构建成功** 启动当前模块,然后访问 http://localhost:9001/hystrix ,显示如下页面则构建成功! ![](https://img.kancloud.cn/dd/be/ddbe0c0cd6ee3ffd7f0e785fcc3e6c01_780x405.jpg) **6. 在需要被监控的模块的`pom.xml`中添加 actuator 和 hystrix 相关依赖** <mark>只有添加了 Hystrix 依赖的模块才能被监控</mark>。如何构建 Hystrix 模块参考【服务熔断->Hystrix服务端构建】小节。 ``` 下面将监控模块:cloud-provider-hystrix-payment8001 ``` **7. 验证 cloud-provider-hystrix-payment8001 能否被 hystrix-dashborad 监控** 启动被监控模块 cloud-provider-hystrix-payment8001 与 监控模块 cloud-comsumer-hystrix-dashboard9001。 (1)访问 http://localhost:8001/payment/hystrix/circuit/10 测试被监控模块是否成功启动。 (2)访问 http://localhost:8001/hystrix.stream 测试被监控是否被监控到,得到如下类似响应信息则是被监控到了。 ``` data: {"type":"HystrixCommand","name":"paymentCircuitBreaker","group":"PaymentServiceImpl", "currentTime":1636957796450,"isCircuitBreakerOpen":false,"errorPercentage":0,"errorCount":0, "requestCount":0,"rollingCountBadRequests":0,"rollingCountCollapsedRequests":0,"rollingCountEmit":0, "rollingCountExceptionsThrown":0,"rollingCountFailure":0,"rollingCountFallbackEmit":0, "rollingCountFallbackFailure":0,"rollingCountFallbackMissing":0, "rollingCountFallbackRejection":0,... ``` **8. 通过图形化查看被监控模块的情况** (1)访问 hystrix-dashboard 监控端 http://localhost:9001/hystrix 。 (2)将被监控模块的监控地址 [http://localhost:8001/hystrix.stream](http://localhost:8001/hystrix.stream) 填写到 hystrix-dashboard 监控端。 ![](https://img.kancloud.cn/2b/dc/2bdc6164eeb47b870a77774581308b20_1347x416.jpg) `Delay`:该参数用来控制服务器上轮询监控信息的延迟时间,默认为2000毫秒,可以通过配置该属性来降低客户端的网络和CPU消耗。 (3)点击 【Monitor Stream】 按钮后出现如界面。 ![](https://img.kancloud.cn/d2/81/d281ef1a81dc913a7fc3dc86d0166660_1417x447.jpg) 多刷新几次 http://localhost:8001/payment/hystrix/circuit/10 ,模拟访问被监控端,图形就会有波动了。 (4)读图。 ![](https://img.kancloud.cn/b5/ba/b5ba498b55f4e889cb7069de2981ea09_1915x924.jpg)