liu
2026-09-22 0837c3ff473499d8a267924125c776b34cc8b517
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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<String, Object> 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<String, Object> 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);
    }
}