123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- package org.jeecg.modules.utils;
- import cn.hutool.http.HttpUtil;
- import cn.hutool.json.JSONObject;
- import com.zaxxer.hikari.HikariConfig;
- import com.zaxxer.hikari.HikariDataSource;
- 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;
- @Slf4j
- public class PostgreSQLUtil {
- private static final String WEBHOOK_URL = "https://oapi.dingtalk.com/robot/send?access_token=11065895a52e659bc789a84ae9a81483e2eb9f02c4611ceacb9865f4e51fa2df";
- private static String dbHost = "未知";
- private static String dbPort = "未知";
- private static HikariDataSource dataSource;
- static {
- try {
- HikariConfig config = new HikariConfig();
- config.setJdbcUrl("jdbc:postgresql://192.168.13.197:5432/epower?useUnicode=true&ssl=false¤tSchema=history");
- 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());
- }
- }
- public static Connection getConnection() {
- try {
- if (dataSource == null) {
- throw new SQLException("数据源未初始化,无法获取数据库连接!");
- }
- return dataSource.getConnection();
- } catch (SQLException e) {
- // 发送钉钉告警
- String errorMessage = "【告警】:峰平谷PostgreSQL数据库 连接失败!\n" +
- "【数据库 IP】:" + dbHost + "\n" +
- "【端口】:" + dbPort + "\n" +
- "【时间】:" + getFormattedTime();
- sendDingMessage(errorMessage);
- // 这里抛出 RuntimeException,避免返回 null
- throw new RuntimeException("PostgreSQL 连接失败", e);
- }
- }
- private static void extractDbInfo(String jdbcUrl) {
- Pattern pattern = Pattern.compile("jdbc:postgresql://([^:/?]+):(\\d+)");
- Matcher matcher = pattern.matcher(jdbcUrl);
- if (matcher.find()) {
- dbHost = matcher.group(1);
- dbPort = matcher.group(2);
- }
- }
- public static void sendDingMessage(String content) {
- JSONObject json = new JSONObject();
- json.set("msgtype", "text");
- JSONObject text = new JSONObject();
- text.set("content", content);
- json.set("text", text);
- String response = HttpUtil.post(WEBHOOK_URL, json.toString());
- log.info("钉钉消息发送结果:{}", response);
- }
- public static void close(Connection connection) {
- if (connection != null) {
- try {
- connection.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- // 关闭连接池
- public static void shutdown() {
- if (dataSource != null) {
- dataSource.close();
- }
- }
- private PostgreSQLUtil() {
- // 私有构造函数,防止实例化
- }
- public static String getFormattedTime() {
- return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
- }
- }
|