|
@@ -8,6 +8,8 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
import java.sql.Connection;
|
|
|
import java.sql.SQLException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Date;
|
|
|
import java.util.regex.Matcher;
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
@@ -21,21 +23,21 @@ public class ProductPostgreSQLUtil {
|
|
|
|
|
|
static {
|
|
|
try {
|
|
|
- HikariConfig config = new HikariConfig();
|
|
|
- config.setJdbcUrl("jdbc:postgresql://192.168.19.59:5432/epower?useUnicode=true&ssl=false");
|
|
|
- config.setUsername("postgres");
|
|
|
- config.setPassword("1qaz2wsx..");
|
|
|
- config.setDriverClassName("org.postgresql.Driver");
|
|
|
- config.setMaximumPoolSize(10); // 最大连接数
|
|
|
- config.setMinimumIdle(5); // 最小空闲连接数
|
|
|
- config.setIdleTimeout(30000); // 空闲连接超时时间(毫秒)
|
|
|
- config.setConnectionTimeout(30000); // 连接超时时间(毫秒)
|
|
|
- config.setMaxLifetime(1800000); // 连接最大生命周期(毫秒)
|
|
|
-
|
|
|
- // 提取 IP 和端口
|
|
|
- extractDbInfo(config.getJdbcUrl());
|
|
|
-
|
|
|
- dataSource = new HikariDataSource(config);
|
|
|
+ HikariConfig config = new HikariConfig();
|
|
|
+ config.setJdbcUrl("jdbc:postgresql://192.168.19.59:5432/epower?useUnicode=true&ssl=false");
|
|
|
+ config.setUsername("postgres");
|
|
|
+ config.setPassword("1qaz2wsx..");
|
|
|
+ config.setDriverClassName("org.postgresql.Driver");
|
|
|
+ config.setMaximumPoolSize(10); // 最大连接数
|
|
|
+ config.setMinimumIdle(5); // 最小空闲连接数
|
|
|
+ config.setIdleTimeout(30000); // 空闲连接超时时间(毫秒)
|
|
|
+ config.setConnectionTimeout(30000); // 连接超时时间(毫秒)
|
|
|
+ config.setMaxLifetime(1800000); // 连接最大生命周期(毫秒)
|
|
|
+
|
|
|
+ // 提取 IP 和端口
|
|
|
+ extractDbInfo(config.getJdbcUrl());
|
|
|
+
|
|
|
+ dataSource = new HikariDataSource(config);
|
|
|
} catch (Exception e) {
|
|
|
// 这里不能抛出异常,否则类加载失败
|
|
|
System.err.println("峰平谷PostgreSQL 数据库初始化失败: " + e.getMessage());
|
|
@@ -52,7 +54,8 @@ public class ProductPostgreSQLUtil {
|
|
|
// 发送钉钉告警
|
|
|
String errorMessage = "【告警】:峰平谷PostgreSQL数据库 连接失败!\n" +
|
|
|
"【数据库 IP】:" + dbHost + "\n" +
|
|
|
- "【端口】:" + dbPort;
|
|
|
+ "【端口】:" + dbPort + "\n" +
|
|
|
+ "【时间】:" + getFormattedTime();
|
|
|
sendDingMessage(errorMessage);
|
|
|
|
|
|
// 这里抛出 RuntimeException,避免返回 null
|
|
@@ -101,4 +104,8 @@ public class ProductPostgreSQLUtil {
|
|
|
private ProductPostgreSQLUtil() {
|
|
|
// 私有构造函数,防止实例化
|
|
|
}
|
|
|
+
|
|
|
+ public static String getFormattedTime() {
|
|
|
+ return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
|
|
|
+ }
|
|
|
}
|