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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
@RestController @RequestMapping("/health") public class HealthCheckController { private final HealthIndicator healthIndicator; private final DatabaseHealthChecker databaseHealthChecker; private final RedisHealthChecker redisHealthChecker; public HealthCheckController(HealthIndicator healthIndicator, DatabaseHealthChecker databaseHealthChecker, RedisHealthChecker redisHealthChecker) { this.healthIndicator = healthIndicator; this.databaseHealthChecker = databaseHealthChecker; this.redisHealthChecker = redisHealthChecker; }
@GetMapping public ResponseEntity<Map<String, Object>> health() { Map<String, Object> health = new HashMap<>(); health.put("status", "UP"); health.put("timestamp", System.currentTimeMillis()); health.put("instance", getInstanceInfo()); return ResponseEntity.ok(health); }
@GetMapping("/detailed") public ResponseEntity<Map<String, Object>> detailedHealth() { Map<String, Object> health = new HashMap<>(); Map<String, Object> checks = new HashMap<>(); HealthStatus dbStatus = databaseHealthChecker.check(); checks.put("database", dbStatus); HealthStatus redisStatus = redisHealthChecker.check(); checks.put("redis", redisStatus); HealthStatus jvmStatus = healthIndicator.getJvmHealth(); checks.put("jvm", jvmStatus); HealthStatus diskStatus = healthIndicator.getDiskHealth(); checks.put("disk", diskStatus); health.put("status", determineOverallStatus(checks)); health.put("checks", checks); health.put("timestamp", System.currentTimeMillis()); return ResponseEntity.ok(health); }
@GetMapping("/ready") public ResponseEntity<Map<String, Object>> readiness() { Map<String, Object> readiness = new HashMap<>(); boolean dbReady = databaseHealthChecker.isReady(); boolean redisReady = redisHealthChecker.isReady(); if (dbReady && redisReady) { readiness.put("status", "READY"); return ResponseEntity.ok(readiness); } else { readiness.put("status", "NOT_READY"); readiness.put("reason", "Dependencies not ready"); return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body(readiness); } }
@GetMapping("/live") public ResponseEntity<Map<String, Object>> liveness() { Map<String, Object> liveness = new HashMap<>(); liveness.put("status", "ALIVE"); liveness.put("timestamp", System.currentTimeMillis()); return ResponseEntity.ok(liveness); } private String determineOverallStatus(Map<String, Object> checks) { for (Object check : checks.values()) { if (check instanceof HealthStatus) { HealthStatus status = (HealthStatus) check; if (!"UP".equals(status.getStatus())) { return "DOWN"; } } } return "UP"; } private Map<String, Object> getInstanceInfo() { Map<String, Object> info = new HashMap<>(); info.put("hostname", getHostname()); info.put("port", getServerPort()); info.put("pid", getProcessId()); return info; } private String getHostname() { try { return InetAddress.getLocalHost().getHostName(); } catch (Exception e) { return "unknown"; } } private int getServerPort() { return 8080; } private long getProcessId() { return ProcessHandle.current().pid(); } }
class HealthStatus { private String status; private Map<String, Object> details; private long timestamp; public HealthStatus(String status) { this.status = status; this.details = new HashMap<>(); this.timestamp = System.currentTimeMillis(); } public HealthStatus(String status, Map<String, Object> details) { this.status = status; this.details = details != null ? details : new HashMap<>(); this.timestamp = System.currentTimeMillis(); } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public Map<String, Object> getDetails() { return details; } public void setDetails(Map<String, Object> details) { this.details = details; } public long getTimestamp() { return timestamp; } public void setTimestamp(long timestamp) { this.timestamp = timestamp; } }
@Component public class DatabaseHealthChecker { private final DataSource dataSource; public DatabaseHealthChecker(DataSource dataSource) { this.dataSource = dataSource; } public HealthStatus check() { try (Connection connection = dataSource.getConnection()) { try (PreparedStatement stmt = connection.prepareStatement("SELECT 1")) { try (ResultSet rs = stmt.executeQuery()) { if (rs.next()) { Map<String, Object> details = new HashMap<>(); details.put("responseTime", System.currentTimeMillis()); return new HealthStatus("UP", details); } } } } catch (Exception e) { Map<String, Object> details = new HashMap<>(); details.put("error", e.getMessage()); return new HealthStatus("DOWN", details); } return new HealthStatus("DOWN"); } public boolean isReady() { return "UP".equals(check().getStatus()); } }
@Component public class RedisHealthChecker { private final RedisTemplate<String, Object> redisTemplate; public RedisHealthChecker(RedisTemplate<String, Object> redisTemplate) { this.redisTemplate = redisTemplate; } public HealthStatus check() { try { long startTime = System.currentTimeMillis(); String result = redisTemplate.opsForValue().get("health:check").toString(); long responseTime = System.currentTimeMillis() - startTime; Map<String, Object> details = new HashMap<>(); details.put("responseTime", responseTime); details.put("result", result); return new HealthStatus("UP", details); } catch (Exception e) { Map<String, Object> details = new HashMap<>(); details.put("error", e.getMessage()); return new HealthStatus("DOWN", details); } } public boolean isReady() { return "UP".equals(check().getStatus()); } }
@Component public class HealthIndicator { public HealthStatus getJvmHealth() { Runtime runtime = Runtime.getRuntime(); long totalMemory = runtime.totalMemory(); long freeMemory = runtime.freeMemory(); long usedMemory = totalMemory - freeMemory; long maxMemory = runtime.maxMemory(); double memoryUsage = (double) usedMemory / maxMemory; Map<String, Object> details = new HashMap<>(); details.put("totalMemory", totalMemory); details.put("usedMemory", usedMemory); details.put("freeMemory", freeMemory); details.put("maxMemory", maxMemory); details.put("memoryUsage", memoryUsage); String status = memoryUsage > 0.9 ? "DOWN" : "UP"; return new HealthStatus(status, details); } public HealthStatus getDiskHealth() { File root = new File("/"); long totalSpace = root.getTotalSpace(); long freeSpace = root.getFreeSpace(); long usedSpace = totalSpace - freeSpace; double diskUsage = (double) usedSpace / totalSpace; Map<String, Object> details = new HashMap<>(); details.put("totalSpace", totalSpace); details.put("usedSpace", usedSpace); details.put("freeSpace", freeSpace); details.put("diskUsage", diskUsage); String status = diskUsage > 0.9 ? "DOWN" : "UP"; return new HealthStatus(status, details); } }
|