From 80a2e856181ce3aad654313339d318d829d04d08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B7=E5=86=B7?= <2270033969@qq.com> Date: Mon, 25 Mar 2024 19:33:10 +0800 Subject: [PATCH] =?UTF-8?q?:zap:=20=E4=BC=98=E5=8C=96=E5=A4=9A=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=BA=90=E6=A8=A1=E5=BC=8F=E4=B8=8B=E6=97=A5=E5=BF=97?= =?UTF-8?q?SQL=20=E6=A0=BC=E5=BC=8F=E5=8C=96=20filter=20=E7=94=9F=E6=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/JdbcDynamicDataSourceProvider.java | 117 +++++++++--------- .../src/main/resources/dynamic-ds-log.yaml | 7 -- 2 files changed, 61 insertions(+), 63 deletions(-) delete mode 100644 pigx-common/pigx-common-datasource/src/main/resources/dynamic-ds-log.yaml diff --git a/pigx-common/pigx-common-datasource/src/main/java/com/pig4cloud/pigx/common/datasource/config/JdbcDynamicDataSourceProvider.java b/pigx-common/pigx-common-datasource/src/main/java/com/pig4cloud/pigx/common/datasource/config/JdbcDynamicDataSourceProvider.java index 7f5b66445..0dbed092f 100644 --- a/pigx-common/pigx-common-datasource/src/main/java/com/pig4cloud/pigx/common/datasource/config/JdbcDynamicDataSourceProvider.java +++ b/pigx-common/pigx-common-datasource/src/main/java/com/pig4cloud/pigx/common/datasource/config/JdbcDynamicDataSourceProvider.java @@ -25,7 +25,6 @@ import com.pig4cloud.pigx.common.datasource.support.DataSourceConstants; import com.pig4cloud.pigx.common.datasource.util.DsConfTypeEnum; import com.pig4cloud.pigx.common.datasource.util.DsJdbcUrlEnum; import org.jasypt.encryption.StringEncryptor; -import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; import java.sql.ResultSet; import java.sql.SQLException; @@ -40,70 +39,76 @@ import java.util.Map; * 从数据源中获取 配置信息 */ public class JdbcDynamicDataSourceProvider extends AbstractJdbcDataSourceProvider { + private static final String SQL_LOG_FILTER = "sqlLogFilter"; - private final DruidDataSourceProperties properties; + private final DruidDataSourceProperties properties; - private final StringEncryptor stringEncryptor; + private final StringEncryptor stringEncryptor; - public JdbcDynamicDataSourceProvider(DefaultDataSourceCreator defaultDataSourceCreator, - StringEncryptor stringEncryptor, DruidDataSourceProperties properties) { - super(defaultDataSourceCreator, properties.getDriverClassName(), properties.getUrl(), properties.getUsername(), - properties.getPassword()); - this.stringEncryptor = stringEncryptor; - this.properties = properties; - } + public JdbcDynamicDataSourceProvider(DefaultDataSourceCreator defaultDataSourceCreator, + StringEncryptor stringEncryptor, DruidDataSourceProperties properties) { + super(defaultDataSourceCreator, properties.getDriverClassName(), properties.getUrl(), properties.getUsername(), + properties.getPassword()); + this.stringEncryptor = stringEncryptor; + this.properties = properties; + } - /** - * 执行语句获得数据源参数 - * @param statement 语句 - * @return 数据源参数 - * @throws SQLException sql异常 - */ - @Override - protected Map executeStmt(Statement statement) throws SQLException { - ResultSet rs = statement.executeQuery(properties.getQueryDsSql()); + /** + * 执行语句获得数据源参数 + * + * @param statement 语句 + * @return 数据源参数 + * @throws SQLException sql异常 + */ + @Override + protected Map executeStmt(Statement statement) throws SQLException { + ResultSet rs = statement.executeQuery(properties.getQueryDsSql()); - Map map = new HashMap<>(8); - while (rs.next()) { - String name = rs.getString(DataSourceConstants.NAME); - String username = rs.getString(DataSourceConstants.DS_USER_NAME); - String password = rs.getString(DataSourceConstants.DS_USER_PWD); - Integer confType = rs.getInt(DataSourceConstants.DS_CONFIG_TYPE); - String dsType = rs.getString(DataSourceConstants.DS_TYPE); + Map map = new HashMap<>(8); + while (rs.next()) { + String name = rs.getString(DataSourceConstants.NAME); + String username = rs.getString(DataSourceConstants.DS_USER_NAME); + String password = rs.getString(DataSourceConstants.DS_USER_PWD); + Integer confType = rs.getInt(DataSourceConstants.DS_CONFIG_TYPE); + String dsType = rs.getString(DataSourceConstants.DS_TYPE); - DataSourceProperty property = new DataSourceProperty(); - property.setUsername(username); - property.setPassword(stringEncryptor.decrypt(password)); + DataSourceProperty property = new DataSourceProperty(); + property.setUsername(username); + property.setPassword(stringEncryptor.decrypt(password)); - String url; - // JDBC 配置形式 - DsJdbcUrlEnum urlEnum = DsJdbcUrlEnum.get(dsType); - if (DsConfTypeEnum.JDBC.getType().equals(confType)) { - url = rs.getString(DataSourceConstants.DS_JDBC_URL); - } - else { - String host = rs.getString(DataSourceConstants.DS_HOST); - String port = rs.getString(DataSourceConstants.DS_PORT); - String dsName = rs.getString(DataSourceConstants.DS_NAME); - url = String.format(urlEnum.getUrl(), host, port, dsName); - } + String url; + // JDBC 配置形式 + DsJdbcUrlEnum urlEnum = DsJdbcUrlEnum.get(dsType); + if (DsConfTypeEnum.JDBC.getType().equals(confType)) { + url = rs.getString(DataSourceConstants.DS_JDBC_URL); + } else { + String host = rs.getString(DataSourceConstants.DS_HOST); + String port = rs.getString(DataSourceConstants.DS_PORT); + String dsName = rs.getString(DataSourceConstants.DS_NAME); + url = String.format(urlEnum.getUrl(), host, port, dsName); + } - // Druid Config - DruidConfig druidConfig = new DruidConfig(); - druidConfig.setValidationQuery(urlEnum.getValidationQuery()); - property.setDruid(druidConfig); - property.setUrl(url); + // Druid Config + DruidConfig druidConfig = new DruidConfig(); + druidConfig.setProxyFilters(SQL_LOG_FILTER); + druidConfig.setValidationQuery(urlEnum.getValidationQuery()); + property.setDruid(druidConfig); + property.setUrl(url); - map.put(name, property); - } + map.put(name, property); + } - // 添加默认主数据源 - DataSourceProperty property = new DataSourceProperty(); - property.setUsername(properties.getUsername()); - property.setPassword(properties.getPassword()); - property.setUrl(properties.getUrl()); - map.put(DataSourceConstants.DS_MASTER, property); - return map; - } + // 添加默认主数据源 + DataSourceProperty property = new DataSourceProperty(); + property.setUsername(properties.getUsername()); + property.setPassword(properties.getPassword()); + property.setUrl(properties.getUrl()); + + DruidConfig druidConfig = new DruidConfig(); + druidConfig.setProxyFilters(SQL_LOG_FILTER); + property.setDruid(druidConfig); + map.put(DataSourceConstants.DS_MASTER, property); + return map; + } } diff --git a/pigx-common/pigx-common-datasource/src/main/resources/dynamic-ds-log.yaml b/pigx-common/pigx-common-datasource/src/main/resources/dynamic-ds-log.yaml deleted file mode 100644 index 712c24f76..000000000 --- a/pigx-common/pigx-common-datasource/src/main/resources/dynamic-ds-log.yaml +++ /dev/null @@ -1,7 +0,0 @@ -# 动态数据源增加 sqlLogFilter 格式化输出 -spring: - datasource: - dynamic: - druid: - proxyFilters: - - sqlLogFilter