ruoyi-vue-plus-监控功能
# Admin 监控中心
Spring Actuator 用于 管理和监控应用程序的拓展模块 . 它提供了端点对实例进行监控 , 这些端点会暴露接口 , 我们能够通过暴露的接口获取到 实例的运行状态、性能指标、健康状态等信息
参考文档
- 官方文档 : https://docs.spring.io (opens new window)
- ruoyi-vue-plus应用文档 : https://plus-doc.dromara.org (opens new window)
提示
endpoints端点 是指监控和管理应用交互的一组接口 . 这些接口提供了实例运行的内置信息
# 依赖
ruoyi-monlitor-admin
模块
<!-- Web模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 安全认证 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- 服务端 -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>
<!-- 客户端 (监控自己) -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
# 配置
# ruoyi-monlitor-admin模块配置
application.yml
配置文件
主要关注 : 账号密码、访问路径
server:
port: 9090
spring:
application:
name: ruoyi-monitor-admin
profiles:
active: @profiles.active@
logging:
config: classpath:logback-plus.xml
--- # 监控中心服务端配置
spring:
security:
user:
name: ruoyi
password: 123456
boot:
admin:
ui:
title: RuoYi-Vue-Plus服务监控中心
context-path: /admin
--- # Actuator 监控端点的配置项
management:
endpoints:
web:
exposure:
# 监控所有端点
include: '*'
endpoint:
health:
# 始终显示健康细节
show-details: ALWAYS
logfile:
external-file: ./logs/ruoyi-monitor-admin.log
application-{环境}.yml
配置文件
--- # 监控中心配置
spring.boot.admin.client:
# 增加客户端开关
enabled: true
url: http://localhost:9090/admin
instance:
service-host-type: IP
username: ruoyi
password: 123456
AdminServerConfig
配置类 (server配置类)
@Configuration
@EnableAdminServer
public class AdminServerConfig {
/**
* 懒加载Bean 用的时候才会进行加载
* 没有 Executor类 时才会进行当前Bean的初始化
**/
@Lazy
@Bean(name = TaskExecutionAutoConfiguration.APPLICATION_TASK_EXECUTOR_BEAN_NAME)
@ConditionalOnMissingBean(Executor.class)
public ThreadPoolTaskExecutor applicationTaskExecutor(TaskExecutorBuilder builder) {
return builder.build();
}
}
SecurityConfig
配置类 (安全配置)
@EnableWebSecurity
public class SecurityConfig {
private final String adminContextPath;
public SecurityConfig(AdminServerProperties adminServerProperties) {
this.adminContextPath = adminServerProperties.getContextPath();
}
@Bean
public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");
successHandler.setDefaultTargetUrl(adminContextPath + "/");
return httpSecurity
.headers().frameOptions().disable()
.and().authorizeRequests()
// 放行 静态目录、登录、监控自身路径
.antMatchers(adminContextPath + "/assets/**"
, adminContextPath + "/login"
, "/actuator"
, "/actuator/**"
).permitAll()
.anyRequest().authenticated()
.and()
.formLogin().loginPage(adminContextPath + "/login")
.successHandler(successHandler).and()
.logout().logoutUrl(adminContextPath + "/logout")
.and()
.httpBasic().and()
.csrf()
.disable()
.build();
}
}
# ruoyi-admin模块配置
application.yml
配置文件
--- # Actuator 监控端点的配置项
management:
endpoints:
web:
exposure:
# 监控所有端点
include: '*'
endpoint:
health:
# 总是展示健康细节
show-details: ALWAYS
logfile:
# 日志信息目录
external-file: ./logs/sys-console.log
application-{环境}.yml
配置文件
--- # 监控中心配置
spring.boot.admin.client:
# 增加客户端开关
enabled: true
url: http://localhost:9090/admin
instance:
service-host-type: IP
username: ruoyi
password: 123456
# 拓展模块监控
拓展模块的服务一般情况是与主服务分开运行的 , 监控拓展模块的服务需要走一下流程配置
大致流程
- 引入依赖
- 配置追加端点监控
- 进入控制中心查看即可
引入依赖
<!-- 暴露端点 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 引入被监控的客户端 -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
配置文件追加端点监控
application.yml
配置文件
--- # Actuator 监控端点的配置项
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: ALWAYS
logfile:
external-file: ./logs/ruoyi-monitor-admin.log
--- # 客户端配置
spring.boot.admin.client:
# 增加客户端开关
enabled: true
# 服务端地址
url: http://localhost:9090/admin
instance:
service-host-type: IP