当前位置: 首页 > news >正文

06_项目集成 Spring Actuator 并实现可视化页面

📊 06_项目集成 Spring Actuator 并实现可视化页面

🧩 一、引入 Spring Actuator 依赖

pom.xml 文件中添加以下依赖:

<!-- Spring Boot Actuator -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

⚙️ 二、Spring Actuator 配置

2.1 配置端点访问前缀

Spring Boot 默认的 Actuator 端点访问地址是:

http://localhost:8080/actuator

若希望自定义访问路径(如改为 /Myactuator),需在 application.yml 中添加如下配置:

management:endpoints:web:base-path: /Myactuator

配置后访问地址变为:

http://localhost:8080/Myactuator

2.2 暴露所有端点

默认情况下,Actuator 只暴露部分端点(如 healthinfo)。为了访问所有可用端点,可添加如下配置:

management:endpoints:web:base-path: /actuatorexposure:include: "*"

说明:include: "*" 表示暴露所有端点;如需按需暴露,可填写具体端点名,如:include: health,info,metrics

2.3 启用指定端点

如需开放指定端点如 health 端点:

management:endpoint:health:enabled: true

2.4 开放 health 端点的详细信息

默认情况下,请求 127.0.0.1:8080/actuator/health 的响应信息如下:

{"status":"UP"}

如需展示详细信息,需进行如下配置:

management:endpoint:health:show-details: always

2.5 启用 info 端点(info 端点和其他端点启用方式略有不同)

management:info:env:enabled: true

拓展:展示项目信息

info:app:name: "视频AI审核系统"version: "v1.0.0"description: "视频AI审核系统"

🔍 三、Actuator 常见端点说明

端点名称说明
/actuator/health应用健康状态
/actuator/info应用配置信息(通过 info.* 配置)
/actuator/metrics应用指标信息(CPU、内存、线程等)
/actuator/env当前环境属性配置
/actuator/beansSpring 容器中的 Bean 列表
/actuator/mappings所有请求映射路径
/actuator/threaddump当前线程快照
/actuator/loggers日志级别控制接口

🖥️ 四、可视化页面集成

4.1 使用 Spring Boot Monitor 实现可视化界面

Spring Boot Monitor 可以对 Spring Boot 应用做监控,对 Spring Boot Actuator 的指标做显示。工具来源于 Spring Boot Admin,只能整合在 Spring Boot 应用内使用。

引入依赖

<dependency><groupId>cn.pomit</groupId><artifactId>spring-boot-monitor</artifactId><version>0.0.4</version>
</dependency>

4.2 访问方式

应用启动后,访问以下地址即可查看监控可视化界面:

http://localhost:8080/monitor

可查看应用的基本信息、内存、线程、GC、健康状态等指标。

五、安全配置

由于 Actuator 和 Monitor 会暴露较多系统信息,必须进行访问控制。

5.1 引入安全配置依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId>
</dependency>

5.2 配置登录账号密码

spring:security:user:name: adminpassword: adminroles: ADMIN

5.3 添加请求校验配置类

@Configuration
@EnableWebSecurity
public class SecurityConfig {@Beanpublic SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {http.authorizeHttpRequests(auth -> auth// 登录后可以访问 monitor 页面.requestMatchers(new AntPathRequestMatcher("/monitor/**")).hasRole("ADMIN")// 登录后可以访问 actuator 接口.requestMatchers(new AntPathRequestMatcher("/actuator/**")).hasRole("ADMIN")// 其他请求全部放行.anyRequest().permitAll()).httpBasic(Customizer.withDefaults());return http.build();}
}

注意:Spring Security 默认会在角色前添加 ROLE_ 前缀,因此 hasRole("ADMIN") 实际匹配的是 ROLE_ADMIN


文章转载自:

http://74FwxOH3.hLfgm.cn
http://w9fqB5Dz.hLfgm.cn
http://8Rgvmsa0.hLfgm.cn
http://99ZnAA8C.hLfgm.cn
http://8BeO6xZE.hLfgm.cn
http://3pZTxjI8.hLfgm.cn
http://l79fD6DM.hLfgm.cn
http://6Ez3SRCY.hLfgm.cn
http://YkOLHxg6.hLfgm.cn
http://jJa92XXT.hLfgm.cn
http://VO6I4snJ.hLfgm.cn
http://Ru3slA7A.hLfgm.cn
http://2LGQ0FP0.hLfgm.cn
http://rgQRrl6U.hLfgm.cn
http://7POXdnPL.hLfgm.cn
http://K5zpsnUQ.hLfgm.cn
http://T2C1DX0D.hLfgm.cn
http://BAyhSS0U.hLfgm.cn
http://oyAq4B2w.hLfgm.cn
http://Bq3Ib0GB.hLfgm.cn
http://CjAllDXA.hLfgm.cn
http://Gfp7a2vx.hLfgm.cn
http://WakRoQA7.hLfgm.cn
http://Ns9tdcgJ.hLfgm.cn
http://ehKdmNkz.hLfgm.cn
http://2z9Vyyb3.hLfgm.cn
http://7p40ZnMA.hLfgm.cn
http://wvtjLjhq.hLfgm.cn
http://JgkMbktP.hLfgm.cn
http://JCpmp96X.hLfgm.cn
http://www.dtcms.com/a/246796.html

相关文章:

  • physicsnemo开源程序是开源深度学习框架,用于使用最先进的 Physics-ML 方法构建、训练和微调深度学习模型
  • Spring @Value 典型用法
  • stm32温湿度-超声波-LCD1602结合项目(Proteus仿真程序)
  • 脱离 Kubernetes,基于原生 Spring Cloud + 云 API 的轻量级自管理微服务平台架构设计
  • 【C++】入门题目之定义Dog类
  • 实现图片懒加载
  • C++11 Type Aliases:从入门到精通
  • 关于UEFI:UEFI/BIOS 固件分析
  • Java 8 Map 新增方法详解
  • 51la批量创建站点繁琐?悟空统计一站式高效解决方案
  • HALCON第四讲->几何变换
  • C++中的RAII技术:资源获取即初始化
  • 【C++】ImGui:不足半兆的桌面程序
  • DWS层新增指标处理方案
  • Vue3+TypeScript实现访问者模式
  • Lesson 27 A wet night
  • MySQL 和 PostgreSQL,到底选择哪个?
  • 基于llamafactory微调千问大模型(实战)
  • error report
  • 备忘录模式:状态管理的时光机器
  • Elasticsearch 的自动补全以及RestAPI的使用
  • vue3 导出表格,合并单元格,设置单元格宽度,文字居中,修改文字颜色
  • 一篇文章理解js闭包和作用于原理
  • 模板字符串使用点击事件【VUE3】
  • shell使用for循环批量统计文件的行数
  • spring boot项目整合mybatis实现多数据源的配置
  • Day13_C语言基础(C语言考试试卷)
  • 测试完成的标准是什么?
  • CoSchedule Headline Analyzer:分析标题情感强度与可读性
  • 深度学习-163-MCP技术之使用Cherry Studio调用本地自定义mcp-server