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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| @RestController @RequestMapping("/health") public class HealthCheckController { @Autowired private RedisTemplate<String, Object> redisTemplate; @Autowired private DataSource dataSource;
@GetMapping("/basic") public ResponseEntity<Map<String, Object>> basicHealth() { Map<String, Object> health = new HashMap<>(); health.put("status", "UP"); health.put("timestamp", System.currentTimeMillis()); health.put("service", "backend-service"); return ResponseEntity.ok(health); }
@GetMapping("/detailed") public ResponseEntity<Map<String, Object>> detailedHealth() { Map<String, Object> health = new HashMap<>(); health.put("status", "UP"); health.put("timestamp", System.currentTimeMillis()); Map<String, Object> database = checkDatabase(); health.put("database", database); Map<String, Object> redis = checkRedis(); health.put("redis", redis); Map<String, Object> jvm = checkJVM(); health.put("jvm", jvm); Map<String, Object> threadPool = checkThreadPool(); health.put("threadPool", threadPool); return ResponseEntity.ok(health); }
private Map<String, Object> checkDatabase() { Map<String, Object> db = new HashMap<>(); try { Connection connection = dataSource.getConnection(); boolean isValid = connection.isValid(5); connection.close(); db.put("status", isValid ? "UP" : "DOWN"); db.put("responseTime", System.currentTimeMillis()); } catch (Exception e) { db.put("status", "DOWN"); db.put("error", e.getMessage()); } return db; }
private Map<String, Object> checkRedis() { Map<String, Object> redis = new HashMap<>(); try { long start = System.currentTimeMillis(); String result = redisTemplate.opsForValue().get("health:check").toString(); long responseTime = System.currentTimeMillis() - start; redis.put("status", "UP"); redis.put("responseTime", responseTime); } catch (Exception e) { redis.put("status", "DOWN"); redis.put("error", e.getMessage()); } return redis; }
private Map<String, Object> checkJVM() { Map<String, Object> jvm = new HashMap<>(); Runtime runtime = Runtime.getRuntime(); long totalMemory = runtime.totalMemory(); long freeMemory = runtime.freeMemory(); long usedMemory = totalMemory - freeMemory; long maxMemory = runtime.maxMemory(); jvm.put("totalMemory", totalMemory); jvm.put("freeMemory", freeMemory); jvm.put("usedMemory", usedMemory); jvm.put("maxMemory", maxMemory); jvm.put("memoryUsage", (double) usedMemory / maxMemory); return jvm; }
private Map<String, Object> checkThreadPool() { Map<String, Object> threadPool = new HashMap<>(); ThreadMXBean threadBean = ManagementFactory.getThreadMXBean(); int threadCount = threadBean.getThreadCount(); int peakThreadCount = threadBean.getPeakThreadCount(); threadPool.put("threadCount", threadCount); threadPool.put("peakThreadCount", peakThreadCount); return threadPool; } }
|