Spring生态下无需第三方工具监控指定Java进程并Web可视化的方案咨询
Spring生态下轻量Java进程监控实现方案(无需第三方监控工具)
当然有,基于Spring生态的轻量组件就能实现需求,不用依赖Grafana、Prometheus这类重型工具,核心思路是用Spring Web/Actuator暴露监控数据,结合系统命令查询进程状态,再通过前端页面展示颜色标识的状态。
可用核心组件
- Spring Boot Web/Actuator:用来构建后端接口返回进程状态数据;Actuator的自定义端点可标准化监控接口,也可以直接用普通Controller快速实现
- Apache Commons Exec(或Java原生ProcessBuilder):简化系统命令执行(如
jps -l、ps aux),查询目标Java进程的存活状态 - Thymeleaf(或静态HTML+JS):渲染监控页面,通过CSS样式根据状态值设置红色/绿色标识
具体实现步骤
1. 依赖配置(pom.xml)
<dependencies> <!-- Spring Web 用于构建接口 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 可选:Actuator 用于标准化监控端点 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- Apache Commons Exec 简化命令执行 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-exec</artifactId> <version>1.3</version> </dependency> <!-- Thymeleaf 用于页面渲染 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies>
2. 进程监控服务实现
封装进程查询逻辑的服务类:
import org.apache.commons.exec.CommandLine; import org.apache.commons.exec.DefaultExecutor; import org.apache.commons.exec.PumpStreamHandler; import org.springframework.stereotype.Service; import java.io.ByteArrayOutputStream; import java.util.HashMap; import java.util.Map; @Service public class ProcessMonitorService { // 定义需要监控的进程列表(key:展示名称,value:目标主类/进程标识) private static final Map<String, String> MONITORED_PROCESSES = new HashMap<>() {{ put("process1", "com.example.Process1Main"); put("process2", "com.example.Process2Main"); }}; public Map<String, String> getProcessStatuses() { Map<String, String> statusMap = new HashMap<>(); try { // 执行jps命令,列出所有Java进程的主类名 CommandLine cmd = CommandLine.parse("jps -l"); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); DefaultExecutor executor = new DefaultExecutor(); executor.setStreamHandler(new PumpStreamHandler(outputStream)); executor.execute(cmd); String output = outputStream.toString(); String[] processes = output.split("\n"); // 匹配目标进程状态 for (Map.Entry<String, String> entry : MONITORED_PROCESSES.entrySet()) { boolean isRunning = false; for (String process : processes) { if (process.contains(entry.getValue())) { isRunning = true; break; } } statusMap.put(entry.getKey(), isRunning ? "GREEN" : "RED"); } } catch (Exception e) { // 命令执行失败时,标记所有进程为RED MONITORED_PROCESSES.keySet().forEach(key -> statusMap.put(key, "RED")); } return statusMap; } }
3. 后端接口实现
用普通Controller直接返回渲染页面:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller public class MonitorController { @Autowired private ProcessMonitorService monitorService; @GetMapping("/monitor") public String getMonitorPage(Model model) { model.addAttribute("processStatuses", monitorService.getProcessStatuses()); return "monitor"; } }
4. 前端页面实现(Thymeleaf模板)
创建src/main/resources/templates/monitor.html,渲染带颜色标识的表格:
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Java进程监控</title> <style> .status-green { color: green; font-weight: bold; } .status-red { color: red; font-weight: bold; } table { border-collapse: collapse; width: 50%; margin: 20px auto; } th, td { border: 1px solid #ddd; padding: 8px; text-align: left; } th { background-color: #f2f2f2; } </style> </head> <body> <h2 style="text-align: center;">Java进程监控状态</h2> <table> <thead> <tr> <th>Process Name</th> <th>Status</th> </tr> </thead> <tbody> <tr th:each="entry : ${processStatuses}"> <td th:text="${entry.key}"></td> <td> <span th:class="${entry.value == 'GREEN' ? 'status-green' : 'status-red'}" th:text="${entry.value}"></span> </td> </tr> </tbody> </table> </body> </html>
补充说明
- 若不想用Apache Commons Exec,可直接用Java原生
ProcessBuilder执行命令,逻辑类似,仅需自行处理输入输出流 - Windows服务器需将
jps -l替换为jps.exe -l或tasklist命令匹配进程名 - 可添加Spring
@Scheduled定时任务定期刷新状态,或前端用JS定时请求接口更新页面
内容的提问来源于stack exchange,提问作者Uday
相关产品推荐
相关产品推荐

