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
| package com.example.database.monitor;
import com.zaxxer.hikari.HikariDataSource; import com.zaxxer.hikari.HikariPoolMXBean; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component;
import javax.management.MBeanServer; import javax.management.ObjectName; import java.lang.management.ManagementFactory; import java.util.HashMap; import java.util.Map;
@Component public class ConnectionPoolMonitor { private final HikariDataSource primaryDataSource; private final HikariDataSource secondaryDataSource; private final MBeanServer mBeanServer; private static final double ACTIVE_CONNECTION_THRESHOLD = 0.8; private static final long CONNECTION_WAIT_THRESHOLD = 5000; private static final int MAX_CONNECTION_CREATION_RATE = 10; public ConnectionPoolMonitor(HikariDataSource primaryDataSource, HikariDataSource secondaryDataSource) { this.primaryDataSource = primaryDataSource; this.secondaryDataSource = secondaryDataSource; this.mBeanServer = ManagementFactory.getPlatformMBeanServer(); }
@Scheduled(fixedRate = 30000) public void monitorConnectionPools() { try { monitorDataSource("Primary", primaryDataSource); monitorDataSource("Secondary", secondaryDataSource); } catch (Exception e) { System.err.println("连接池监控异常: " + e.getMessage()); } }
private void monitorDataSource(String name, HikariDataSource dataSource) { try { HikariPoolMXBean poolMXBean = dataSource.getHikariPoolMXBean(); int activeConnections = poolMXBean.getActiveConnections(); int idleConnections = poolMXBean.getIdleConnections(); int totalConnections = poolMXBean.getTotalConnections(); int threadsAwaitingConnection = poolMXBean.getThreadsAwaitingConnection(); double usageRate = (double) activeConnections / dataSource.getMaximumPoolSize(); Map<String, Object> metrics = new HashMap<>(); metrics.put("dataSource", name); metrics.put("activeConnections", activeConnections); metrics.put("idleConnections", idleConnections); metrics.put("totalConnections", totalConnections); metrics.put("threadsAwaitingConnection", threadsAwaitingConnection); metrics.put("usageRate", usageRate); metrics.put("maxPoolSize", dataSource.getMaximumPoolSize()); System.out.printf("[%s] 连接池状态 - 活跃: %d, 空闲: %d, 总计: %d, 等待: %d, 使用率: %.2f%%%n", name, activeConnections, idleConnections, totalConnections, threadsAwaitingConnection, usageRate * 100); checkAlerts(name, metrics); } catch (Exception e) { System.err.printf("监控数据源 %s 时发生异常: %s%n", name, e.getMessage()); } }
private void checkAlerts(String dataSourceName, Map<String, Object> metrics) { double usageRate = (Double) metrics.get("usageRate"); int threadsAwaitingConnection = (Integer) metrics.get("threadsAwaitingConnection"); if (usageRate > ACTIVE_CONNECTION_THRESHOLD) { sendAlert(String.format("【告警】%s 数据源连接池使用率过高: %.2f%%", dataSourceName, usageRate * 100)); } if (threadsAwaitingConnection > 0) { sendAlert(String.format("【告警】%s 数据源有 %d 个线程正在等待连接", dataSourceName, threadsAwaitingConnection)); } }
private void sendAlert(String message) { System.err.println("🚨 " + message); }
public Map<String, Object> getDetailedStatistics() { Map<String, Object> statistics = new HashMap<>(); try { HikariPoolMXBean primaryPool = primaryDataSource.getHikariPoolMXBean(); Map<String, Object> primaryStats = new HashMap<>(); primaryStats.put("activeConnections", primaryPool.getActiveConnections()); primaryStats.put("idleConnections", primaryPool.getIdleConnections()); primaryStats.put("totalConnections", primaryPool.getTotalConnections()); primaryStats.put("threadsAwaitingConnection", primaryPool.getThreadsAwaitingConnection()); primaryStats.put("maxPoolSize", primaryDataSource.getMaximumPoolSize()); primaryStats.put("minIdle", primaryDataSource.getMinimumIdle()); HikariPoolMXBean secondaryPool = secondaryDataSource.getHikariPoolMXBean(); Map<String, Object> secondaryStats = new HashMap<>(); secondaryStats.put("activeConnections", secondaryPool.getActiveConnections()); secondaryStats.put("idleConnections", secondaryPool.getIdleConnections()); secondaryStats.put("totalConnections", secondaryPool.getTotalConnections()); secondaryStats.put("threadsAwaitingConnection", secondaryPool.getThreadsAwaitingConnection()); secondaryStats.put("maxPoolSize", secondaryDataSource.getMaximumPoolSize()); secondaryStats.put("minIdle", secondaryDataSource.getMinimumIdle()); statistics.put("primary", primaryStats); statistics.put("secondary", secondaryStats); statistics.put("timestamp", System.currentTimeMillis()); } catch (Exception e) { statistics.put("error", "获取统计信息失败: " + e.getMessage()); } return statistics; } }
|