如何在Spring Boot中获取服务器运行信息及系统资源数据?
在Spring Boot/Java中获取服务器系统信息的方法
核心API说明
Java本身提供了无需额外依赖的API来获取系统硬件与运行状态信息,对应Node.js的os模块,主要用到两类工具:
java.lang.Runtime:获取当前Spring Boot应用进程的内存信息java.lang.management.OperatingSystemMXBean:获取服务器操作系统层面的运行时间、物理内存等全局信息
具体实现代码
1. 获取系统运行时间
通过OperatingSystemMXBean直接获取系统启动后的毫秒数,可转换为易读的天/时/分/秒格式:
import java.lang.management.ManagementFactory; import java.lang.management.OperatingSystemMXBean; // 获取系统运行毫秒数 public long getSystemUptimeMs() { OperatingSystemMXBean osBean = ManagementFactory.getOperatingSystemMXBean(); return osBean.getSystemUptime(); } // 转换为友好显示格式 public String formatUptime(long uptimeMs) { long seconds = uptimeMs / 1000; long minutes = seconds / 60; long hours = minutes / 60; long days = hours / 24; return String.format("%d天 %d时 %d分 %d秒", days, hours % 24, minutes % 60, seconds % 60); }
2. 获取总内存与空闲内存
方式一:JVM进程内存(Runtime类)
如果需要关注当前Spring Boot应用自身的内存使用情况:
public void getJvmMemoryStats() { Runtime runtime = Runtime.getRuntime(); // JVM已分配的总内存(字节) long totalMemory = runtime.totalMemory(); // JVM当前空闲内存(字节) long freeMemory = runtime.freeMemory(); // JVM可申请的最大内存(字节) long maxMemory = runtime.maxMemory(); // 转换为MB单位输出 System.out.println("JVM总内存: " + totalMemory / (1024 * 1024) + " MB"); System.out.println("JVM空闲内存: " + freeMemory / (1024 * 1024) + " MB"); System.out.println("JVM最大可用内存: " + maxMemory / (1024 * 1024) + " MB"); }
方式二:服务器物理内存(OperatingSystemMXBean)
如果需要获取整个服务器的全局内存状态,可使用Oracle/OpenJDK提供的扩展实现类:
import com.sun.management.OperatingSystemMXBean; import java.lang.management.ManagementFactory; public void getSystemMemoryStats() { OperatingSystemMXBean osBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean(); // 服务器总物理内存(字节) long totalPhysicalMemory = osBean.getTotalPhysicalMemorySize(); // 服务器空闲物理内存(字节) long freePhysicalMemory = osBean.getFreePhysicalMemorySize(); // 转换为GB单位输出 System.out.println("服务器总内存: " + totalPhysicalMemory / (1024 * 1024 * 1024) + " GB"); System.out.println("服务器空闲内存: " + freePhysicalMemory / (1024 * 1024 * 1024) + " GB"); }
集成到管理员仪表盘
可以将上述方法封装为SystemInfoService,再通过Spring Boot的Controller暴露接口,返回JSON格式数据供前端仪表盘调用:
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/admin/dashboard") public class DashboardController { @GetMapping("/system-status") public SystemStatus getSystemStatus() { SystemStatus status = new SystemStatus(); status.setUptime(formatUptime(getSystemUptimeMs())); // 填充内存等其他状态信息 Runtime runtime = Runtime.getRuntime(); status.setJvmTotalMemory(runtime.totalMemory() / (1024 * 1024)); status.setJvmFreeMemory(runtime.freeMemory() / (1024 * 1024)); return status; } // 省略工具方法和SystemStatus实体类定义 }
补充:获取操作系统基础信息
对应Node.js的os.platform()和os.arch(),Java可直接通过System.getProperty()获取:
// 获取操作系统名称(如Windows 10、Linux) System.getProperty("os.name"); // 获取操作系统架构(如amd64、x86) System.getProperty("os.arch"); // 获取操作系统版本 System.getProperty("os.version");
内容的提问来源于stack exchange,提问作者Youzef
相关产品推荐
相关产品推荐

