package com.ruoyi.web.controller; import java.util.Date; import java.util.HashMap; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import com.ruoyi.common.annotation.Anonymous; import com.ruoyi.common.core.domain.AjaxResult; /** * 系统健康检查接口(无认证,用于负载均衡/监控探活) */ @RestController public class HealthController { @Autowired private JdbcTemplate jdbcTemplate; @Autowired private Environment environment; @GetMapping("/api/health") @Anonymous public AjaxResult health() { Map info = new HashMap<>(); info.put("status", "UP"); info.put("timestamp", new Date()); // 数据库检查 try { jdbcTemplate.queryForObject("SELECT 1", Integer.class); info.put("database", "UP"); } catch (Exception e) { info.put("database", "DOWN"); info.put("dbError", e.getMessage()); } // 内存信息 Runtime rt = Runtime.getRuntime(); Map mem = new HashMap<>(); mem.put("total", rt.totalMemory() / 1024 / 1024 + "MB"); mem.put("free", rt.freeMemory() / 1024 / 1024 + "MB"); mem.put("max", rt.maxMemory() / 1024 / 1024 + "MB"); info.put("memory", mem); // 系统信息 info.put("os", System.getProperty("os.name")); info.put("java", System.getProperty("java.version")); info.put("activeProfiles", String.join(",", environment.getActiveProfiles())); return AjaxResult.success(info); } }