PostgreSQLUtil.java 3.4 KB

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