答案:通过Spring Boot Actuator暴露监控端点,前端调用接口实现应用监控。具体步骤包括:1. 添加actuator依赖并配置暴露health、metrics等端点;2. 后端配置CORS允许前端访问/actuator/**路径;3. 前端使用fetch或axios请求健康状态和指标数据;4. 生产环境需通过Security、反向代理或中间层限制访问,避免敏感信息泄露。

前端通过 JavaScript 调用 Spring Boot Actuator 接口来实现应用监控,是一种常见的运维需求。要完成这个功能,需要后端暴露安全的监控端点,前端通过 HTTP 请求获取数据并展示。以下是具体实现步骤:
确保 Spring Boot 项目中已引入 spring-boot-starter-actuator 依赖,并正确配置允许访问的端点。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
management:
endpoints:
web:
exposure:
include: health,info,metrics,env
endpoint:
health:
show-details: always
这会将关键监控接口暴露在 /actuator 路径下,例如:/actuator/health、/actuator/metrics。
前端 JS 发起请求属于浏览器跨域场景,需在后端允许来自前端域名的请求。
立即学习“前端免费学习笔记(深入)”;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/actuator/**")
.allowedOrigins("http://localhost:3000") // 前端地址
.allowedMethods("GET")
.allowedHeaders("*");
}
}
根据实际部署情况调整 allowedOrigins 地址。
前端可通过原生 fetch 或 axios 发起 GET 请求获取监控数据。
fetch('http://your-springboot-app/actuator/health')
.then(response => response.json())
.then(data => {
console.log('应用状态:', data.status); // 如 UP 或 DOWN
})
.catch(err => console.error('请求失败:', err));
fetch('http://your-springboot-app/actuator/metrics/jvm.memory.used')
.then(r => r.json())
.then(data => {
data.measurements.forEach(m => {
console.log(`${m.statistic}: ${m.value}`);
});
});
生产环境中直接暴露 Actuator 接口存在风险,建议采取以下措施:
基本上就这些。只要后端开放了安全的监控接口,前端就能通过标准 HTTP 请求实时获取应用状态,用于构建简单的监控面板。关键是注意权限和暴露范围,避免信息泄露。
以上就是前端JS调用SpringBootActuator监控的实现步骤的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号