PostgreSQLUtil.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package org.jeecg.modules.utils;
  2. import cn.hutool.http.HttpUtil;
  3. import cn.hutool.json.JSONObject;
  4. import com.zaxxer.hikari.HikariConfig;
  5. import com.zaxxer.hikari.HikariDataSource;
  6. import lombok.extern.slf4j.Slf4j;
  7. import java.sql.Connection;
  8. import java.sql.SQLException;
  9. import java.text.SimpleDateFormat;
  10. import java.util.Date;
  11. import java.util.regex.Matcher;
  12. import java.util.regex.Pattern;
  13. @Slf4j
  14. public class PostgreSQLUtil {
  15. private static final String WEBHOOK_URL = "https://oapi.dingtalk.com/robot/send?access_token=11065895a52e659bc789a84ae9a81483e2eb9f02c4611ceacb9865f4e51fa2df";
  16. private static String dbHost = "未知";
  17. private static String dbPort = "未知";
  18. private static HikariDataSource dataSource;
  19. static {
  20. try {
  21. HikariConfig config = new HikariConfig();
  22. config.setJdbcUrl("jdbc:postgresql://192.168.13.197:5432/epower?useUnicode=true&ssl=false&currentSchema=history");
  23. config.setUsername("postgres");
  24. config.setPassword("1qaz2wsx..");
  25. config.setDriverClassName("org.postgresql.Driver");
  26. config.setMaximumPoolSize(10);
  27. config.setMinimumIdle(5);
  28. config.setIdleTimeout(30000);
  29. config.setConnectionTimeout(30000);
  30. config.setMaxLifetime(1800000);
  31. // 提取 IP 和端口
  32. extractDbInfo(config.getJdbcUrl());
  33. dataSource = new HikariDataSource(config);
  34. } catch (Exception e) {
  35. // 这里不能抛出异常,否则类加载失败
  36. System.err.println("峰平谷PostgreSQL 数据库初始化失败: " + e.getMessage());
  37. }
  38. }
  39. public static Connection getConnection() {
  40. try {
  41. if (dataSource == null) {
  42. throw new SQLException("数据源未初始化,无法获取数据库连接!");
  43. }
  44. return dataSource.getConnection();
  45. } catch (SQLException e) {
  46. // 发送钉钉告警
  47. String errorMessage = "【告警】:峰平谷PostgreSQL数据库 连接失败!\n" +
  48. "【数据库 IP】:" + dbHost + "\n" +
  49. "【端口】:" + dbPort + "\n" +
  50. "【时间】:" + getFormattedTime();
  51. sendDingMessage(errorMessage);
  52. // 这里抛出 RuntimeException,避免返回 null
  53. throw new RuntimeException("PostgreSQL 连接失败", e);
  54. }
  55. }
  56. private static void extractDbInfo(String jdbcUrl) {
  57. Pattern pattern = Pattern.compile("jdbc:postgresql://([^:/?]+):(\\d+)");
  58. Matcher matcher = pattern.matcher(jdbcUrl);
  59. if (matcher.find()) {
  60. dbHost = matcher.group(1);
  61. dbPort = matcher.group(2);
  62. }
  63. }
  64. public static void sendDingMessage(String content) {
  65. JSONObject json = new JSONObject();
  66. json.set("msgtype", "text");
  67. JSONObject text = new JSONObject();
  68. text.set("content", content);
  69. json.set("text", text);
  70. String response = HttpUtil.post(WEBHOOK_URL, json.toString());
  71. log.info("钉钉消息发送结果:{}", response);
  72. }
  73. public static void close(Connection connection) {
  74. if (connection != null) {
  75. try {
  76. connection.close();
  77. } catch (SQLException e) {
  78. e.printStackTrace();
  79. }
  80. }
  81. }
  82. // 关闭连接池
  83. public static void shutdown() {
  84. if (dataSource != null) {
  85. dataSource.close();
  86. }
  87. }
  88. private PostgreSQLUtil() {
  89. // 私有构造函数,防止实例化
  90. }
  91. public static String getFormattedTime() {
  92. return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
  93. }
  94. }