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

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://www.xdnf.cn/news/13803.html

相关文章:

  • 基于 8.6 万蛋白质结构数据,融合量子力学计算的机器学习方法挖掘 69 个全新氮-氧-硫键
  • OrangePi 5 Max EMMC 系统烧录时下载成功,启动失败解决方案
  • 高开放性具身智能AIBOX平台—专为高校实验室与科研项目打造的边缘计算基座(让高校和科研院所聚焦核心算法)
  • 打卡第43天:Grad CAM与Hook函数
  • 【ffmpeg】windows端安装ffmpeg
  • ES集群的节点
  • 深度学习入门(4):resnet50
  • 今日行情明日机会——20250612
  • Python小酷库系列:Python中的JSON工具库(1)
  • 106.给AI回答添加点赞收藏功能
  • PCI总线概述
  • ubuntu22.04使用系统默认的中文输入法,打字只输入英文字符怎么操作才能打字中文
  • 模型合并(model merge)
  • 如何搭建独立站并使用Cloak斗篷技术
  • Intel J1900通讯管理机,支持8网8串,EFT过载保护
  • MTK APEX测光系统中各变量具体的计算方式探究
  • FastDFS 分布式存储系统深度解析与实践指南
  • FEMFAT许可有效期
  • 强化学习笔记之策略梯度
  • 赛元微8051系列触控按键的开发
  • 高效管理发票-发票真伪验证接口-发票查验-发票验真api
  • 【Spring AI 1.0.0】Spring AI 1.0.0框架快速入门(2)——提示词
  • python打卡day52@浙大疏锦行
  • BROOKS 5850E 系列质量流量控制器和流量计Mass Flow Controllers Meters
  • 6月软硬件技术问答第一期
  • riverpod最基本例子:在一个组件里更新状态,在另一个组件里获取更新的数据
  • 龙虎榜——20250612
  • JavaScript原型,原型链。
  • Elasticsearch 的自动补全以及RestAPI的使用
  • Arduino入门教程:1-1、先跑起来(点亮LED打印Helloworld)