springcloud-Dashboard 流量监控 (九)
新建springcloud-consumer-dashboard-82模块
- pom.xml
关键依赖
org.springframework.cloud
spring-cloud-starter-hystrix-dashboard
1.4.6.RELEASE
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-hystrix
1.4.6.RELEASE
其它依赖
org.springframework.cloud
spring-cloud-starter-feign
1.4.6.RELEASE
org.springframework.cloud
spring-cloud-starter-eureka
1.4.6.RELEASE
org.springframework.cloud
spring-cloud-starter-ribbon
1.4.6.RELEASE
com.dong
springcloud-common
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-devtools
- application.yml
server:
port: 82
- 主启动类 @EnableHystrixDashboard开启流量监控
package com.dong.consumer3;
@SpringBootApplication
@EnableHystrixDashboard
public class ConsumerThreeApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerThreeApplication.class, args);
}
}
到此dashboard流量监控模块就搭建完毕了。(可以认为它是一个工具,用来监控某个模块的访问量)
springcloud-provider-8001 配置流量监控
- 添加依赖
org.springframework.cloud
spring-cloud-starter-hystrix
1.4.6.RELEASE
org.springframework.boot
spring-boot-starter-actuator
- 主启动类 关键注解@EnableDiscoveryClient
注意:起初没有使用@EnableCircuitBreaker注解,访问http://localhost:8001/actuator/hystrix.stream 一直z在ping。并且流量监控页面也是一直loading。最后发现需要加上@EnableCircuitBreaker 添加熔断的支持。
package com.dong.provider;
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableCircuitBreaker
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
@Bean
public ServletRegistrationBean hystrixMetricsStreamServlet(){
ServletRegistrationBean registrationBean = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
//访问该页面就是监控页面
registrationBean.addUrlMappings("/actuator/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}
- controller 访问的接口上加一个@HystrixCommand注解,可以不写相关熔断的方法。监控页面就可以监控到了。(此注解的详细用户见)
@GetMapping("/dept/get/{id}")
@HystrixCommand
public Dept getDept(@PathVariable("id") Long id) {
Dept dept = deptService.queryById(id);
if (dept == null) {
throw new RuntimeException("Fail");
}
return dept;
}
测试
启动springcloud-provider-8001,springcloud-consumer-dashboard-82和Eureka
启动springcloud-consumer-dashboard-82可能会报一些错,我们可以不管它。
访问:http://localhost:8001/actuator/hystrix.stream 如果一直ping,我们可以访问http://localhost:8001/dept/get/1 。还是不行就多访问几次,就可以ping到相关信息。
最后访问:http://localhost:82/hystrix
填入要监测的地址,delay和title后,点击Monitor Stream就能跳到对应的监控页面了。
最后多次访问http://localhost:8001/dept/get/1 我们发现上图箭头指的灰色区域会逐渐变大。这就是说访问量越多,圈越大。
补充
- 七色
- 一圈
实心圆:公有两种含义,他通过颜色的变化代表了实例的健康程度
它的健康程度从绿色<黄色<橙色<红色递减
该实心圆除了颜色的变化之外,它的大小也会根据实例的请求流量发生变化,流量越大,该实心圆就越大,所以通过该实心圆的展示,就可以在大量的实例中快速发现故障实例和高压力实例。
- 一线
曲线:用来记录2分钟内流量的相对变化,可以通过它来观察到流量的上升和下降趋势!
- 整图说明

参考教程:https://www.kuangstudy.com/