Dashboard是什么:
Hystrix提供了对于微服务调用状态的监控信息,但是需要结合spring-boot-actuator模块一起使用。Hystrix Dashboard是Hystrix的一个组件,Hystrix Dashboard提供一个断路器的监控面板,可以使我们更好的监控服务和集群的状态。
Dashboard作用:
Hystrix Dashboard主要用来实时监控Hystrix的各项指标信息。通过Hystrix Dashboard反馈的实时信息,可以帮助我们快速发现系统中存在的问题。使用基于Hystrix的提供者访问数据库表数据,每访问一次都会记录是否成功以及最近10s错误百分比、超时数、熔断数、线程拒绝数、错误请求数、失败/异常数、服务请求频率等相关信息。
Dashboard的使用
第一步:创建dashboard监控页面
1.创建一个监控Module:Dashboard
2.导入依赖:实时监控Hystrix的各项指标信息,导入dashborad与hystrix的依赖
<dependencies>
<!--Hystrix依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!--dashboard依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
</dependencies>
3.配置yml,配置一个端口
server:
port: 9001
4.编写启动类,开启监控:启动类的包名结构必须和之前创建服务一致,不然扫描不到(公司开发一个项目,同一个项目不可能有其他的包名结构吧)。
@EnableHystrixDashboard注解开启监控
@SpringBootApplication
@EnableHystrixDashboard
public class DeptConsumerDashBoard_9001 {
public static void main(String[] args) {
SpringApplication.run(DeptConsumerDashBoard_9001.class,args);
}
}
5.监控界面
第二步:对需要监控的服务提供者进行监控
1.在启动类上注册servlet:将监控的数据显示到监控页面(固定写法)
@SpringBootApplication
@EnableEurekaClient //在服务启动后,自动注册到eureka中
@EnableDiscoveryClient//服务发现
public class DeptProvider_8001 {
public static void main(String[] args){
SpringApplication.run(DeptProvider_8001.class,args);
}
//注册一个servlet
@Bean
public ServletRegistrationBean hystrixMetricsStreamServlet(){
ServletRegistrationBean registrationBean = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
registrationBean.addUrlMappings("/actuator/hystrix.stream");
return registrationBean;
}
}
2.对服务提供者中需要监控的接口进行监控
接口上添加 @HystrixCommand注解,用来标识要把哪些接口方法展示在dashboard上
@PostMapping("/dept/add")
@HystrixCommand
public boolean add(@RequestBody Dept dept){
return deptService.addDept(dept);
}
@GetMapping("/dept/get/{id}")
@HystrixCommand
public Dept get(@PathVariable("id") Long id){
return deptService.queryById(id);
}
@GetMapping("/dept/list")
@HystrixCommand
public List<Dept> qureyAll(){
return deptService.queryAll();
}
3.开启熔断开关:
@EnableCircuitBreaker //服务开启hystrix 不开启hystrix,就不能监控dashboard
@SpringBootApplication
@EnableEurekaClient //在服务启动后,自动注册到eureka中
@EnableDiscoveryClient//服务发现
@EnableCircuitBreaker //增加服务对熔断的支持
public class DeptProvider_8001 {
...
}
4.详细监控信息
图解: