dependencies { compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-netflix-eureka-client', version: '2.1.5.RELEASE' compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-netflix-hystrix-dashboard' }
2)编写application.yaml配置文件,非常简单的代码
server: port: 1000 spring: application: name: hystrix-monitoring eureka: instance: hostname: localhost client: fetch-registry: true register-with-eureka: true service-url: defaultZone: http://localhost:8761/eureka/
3)编写启动类添加@EnableHystrixDashboard注解,标记开启断路器仪表盘功能
package org.wesson.springcloud.hystrix; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; @EnableHystrixDashboard @EnableDiscoveryClient @SpringBootApplication public class HystrixMonitoringApplication { public static void main(String[] args) { SpringApplication.run(HystrixMonitoringApplication.class, args); } }
4)测试
Step1:访问http://localhost:8761/,注册到的服务如下图:
此图是 Hystrix Dashboard 的监控首页,该页面中并没有具体的监控信息。
从页面文字内容中我们可以知道,Hystrix Dashboard 支持三种不同的监控方式,分别是:默认的集群监控、自定义集群监控与单体应用的监控。在这次示例中我们只使用单体应用的监控,通过 URL https://hystrix-app:port/actuator/hystrix.stream 开启,实现对具体某个服务实例的监控。
Hystrix Dashboard 的监控首页还有其他两个参数分别是 Delay 和 Title:
-
Delay:
-
Title:该参数会对应点击 Monitor Stream 之后监控页面头部标题 (Hystrix Stream),默认会使用具体监控实例的 URL,可以通过配置该信息来展示更适合的标题。
三、Fegin整合Hystrix Dashboard
compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator'
management:
endpoints:
web:
exposure:
include: 'hystrix.stream'
3)启动类必须添加@EnableCircuitBreaker注解。我看过很多其他博主的博客,被监控的服务并不需要引入hystrix dashboard依赖,多此一举。官网解释添加actuator依赖才是重点
@EnableCircuitBreaker
4)测试
Step1:运行 eureka-server 启动类,端口为8761
Step2:运行 provider-service 启动类,端口为8081
Step3:运行 api-fegin 启动类,端口为9090
Step4:运行 hystrix-monitoring 启动类,端口为1000
Step5:先访问http://localhost:8761/,结果如下图:
Step6:多次访问http://localhost:9090/client/info,返回结果如下:
Step7:本章第二节测试访问过Hystrix Dashboard首页,并填写 Input 框信息如下: