SpringCloud 组件之集成 Hystrix Dashboard
- 一、Hystrix Dashboard 是什么?
- 二、创建项目实现 Hystrix Dashboard
- 1.添加依赖
- 2.添加配置
- 3.启动类加入注解
- 4.启动类注入 ServletRegistrationBean
- 5.启动服务
一、Hystrix Dashboard 是什么?
Hystrix 仪表盘主要用来监控 Hystrix 的实时运行状态,通过它我们可以看到 Hystrix 的各项指标信息,从而快速发现系统中存在的问题进而解决它。
二、创建项目实现 Hystrix Dashboard
1.添加依赖
pom.xml 文件中加入依赖 spring-boot-starter-actuator 和 spring-cloud-starter-netflix-hystrix-dashboard,spring-boot-starter-actuator 为服务监控依赖:
1
2
3
4
5
6
7
8
9<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency>
2.添加配置
application.yml 文件中添加配置用来暴露 endpoints,由于 endpoints 中会包含很多敏感信息,除了 health 和 info 两个支持直接访问外,其他的默认不能直接访问,所以进行指定:
1
2
3
4
5
6management: endpoints: web: exposure: include: hystrix.stream
3.启动类加入注解
在启动类中加入注解 @EnableHystrixDashboard,开启Hystrix Dashboard:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20package com.chaoyue.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.dashboard.EnableHystrixDashboard; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableEurekaClient @EnableFeignClients @EnableHystrixDashboard public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } }
4.启动类注入 ServletRegistrationBean
在启动类注入 ServletRegistrationBean,该 Bean 的作用是在 Hystrix Dashboard 中打开 hystrix.stream:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32package com.chaoyue.consumer; import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.context.annotation.Bean; @SpringBootApplication @EnableEurekaClient @EnableFeignClients @EnableHystrixDashboard public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } @Bean public ServletRegistrationBean getServlet(){ HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet(); ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet); registrationBean.setLoadOnStartup(1); registrationBean.addUrlMappings("/hystrix.stream"); registrationBean.setName("HystrixMetricsStreamServlet"); return registrationBean; } }
5.启动服务
启动服务。浏览器输入:http://localhost:8082/hystrix
监控 Stream 输入:http://localhost:8082/hystrix.stream,点击【Monitor Stream】
最后
以上就是鲜艳老鼠最近收集整理的关于【SpringCloud深入浅出系列】SpringCloud组件之集成Hystrix Dashboard一、Hystrix Dashboard 是什么?二、创建项目实现 Hystrix Dashboard的全部内容,更多相关【SpringCloud深入浅出系列】SpringCloud组件之集成Hystrix内容请搜索靠谱客的其他文章。
发表评论 取消回复