Dashboard是Hystrix的仪表盘监控界面,可以对其他服务进行图形化的监控,方便查看其它服务的情况。
新建模块 cloud-consumer-hystrix-dashboard9001
pom文件加入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
配置application.yml文件
server:
port: 9001
spring:
application:
name: cloud-consumer-hystrix-dashboard
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/xxx?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
username: root
password: *****
eureka:
client:
#false表示不向服务端注册自己
register-with-eureka: true
#false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
fetch-registry: true
service-url:
#设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/ #集群版
instance:
instance-id: HystrixDashboard9001
prefer-ip-address: true #访问路径可以显示IP地址
mybatis-plus:
configuration:
map-underscore-to-camel-case: true
auto-mapping-behavior: full
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath*:mapper/**/*Mapper.xml
global-config:
# 逻辑删除配置
db-config:
# 删除前
logic-not-delete-value: 1
# 删除后
logic-delete-value: 0
hystrix:
dashboard:
proxy-stream-allow-list: "*"
主启动类 (加上注解@EnableHystrixDashboard)
@SpringBootApplication
@EnableHystrixDashboard
public class HystrixDashboardMain9001
{
public static void main(String[] args)
{
SpringApplication.run(HystrixDashboardMain9001.class,args);
}
}
cloud-consumer-hystrix-dashboard9001模块就搭建成功,启动后访问 http://localhost:9001/hystrix
会出现如下界面
在对想要监控的服务模块的主启动类中添加方法 。
/**
* 此配置是为了服务监控而配置,与服务容错本身无关,springCloud升级后的坑
* ServletRegistrationBean因为springboot的默认路劲不是"/hystrix.stream",
* 只要在自己的项目里配置上下面的servlet就可以了
* @return
*/
@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;
}
注意:被监控的服务模块一定要有依赖
<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>
配置完毕后,在之前打开的图形化界面填写监控地址
点击按钮进入监控界面
调用被监控的服务接口,就可以看到相关的一些信息