diff --git a/report-core/pom.xml b/report-core/pom.xml index 7a41ea41..7ecfd528 100644 --- a/report-core/pom.xml +++ b/report-core/pom.xml @@ -108,11 +108,6 @@ mysql-connector-java 8.0.28 - - com.alibaba - druid - 1.2.0 - org.flywaydb flyway-core diff --git a/report-core/src/main/java/com/anjiplus/template/gaea/business/config/DruidProperties.java b/report-core/src/main/java/com/anjiplus/template/gaea/business/config/DruidProperties.java deleted file mode 100644 index bed72ad5..00000000 --- a/report-core/src/main/java/com/anjiplus/template/gaea/business/config/DruidProperties.java +++ /dev/null @@ -1,99 +0,0 @@ -package com.anjiplus.template.gaea.business.config; - -import com.alibaba.druid.pool.DruidDataSource; -import lombok.Data; -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.stereotype.Component; - -/** - * Created by raodeming on 2021/8/6. - */ -@Component -@ConfigurationProperties(prefix = "spring.druid") -@Data -public class DruidProperties { - /** - * 初始化时建立物理连接的个数。初始化发生在显示调用init方法,或者第一次getConnection时 - */ - private int initialSize; - - /** - * 最小连接池数量 - */ - private int minIdle; - - /** - * 最大连接池数量 - */ - private int maxActive; - - /** - * 获取连接时最大等待时间,单位毫秒。配置了maxWait之后,缺省启用公平锁,并发效率会有所下降,如果需要可以通过配置 - */ - private int maxWait; - - /** - * 关闭空闲连接的检测时间间隔.Destroy线程会检测连接的间隔时间,如果连接空闲时间大于等于minEvictableIdleTimeMillis则关闭物理连接。 - */ - private int timeBetweenEvictionRunsMillis; - - /** - * 连接的最小生存时间.连接保持空闲而不被驱逐的最小时间 - */ - private int minEvictableIdleTimeMillis; - - /** - * 申请连接时检测空闲时间,根据空闲时间再检测连接是否有效.建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRun - */ - private boolean testWhileIdle; - - /** - * 开启PSCache - */ - private boolean poolPreparedStatements; - - /** - * 设置PSCache值 - */ - private int maxPoolPreparedStatementPerConnectionSize; - - /** - * 连接出错后再尝试连接三次 - */ - private int connectionErrorRetryAttempts; - - /** - * 数据库服务宕机自动重连机制 - */ - private boolean breakAfterAcquireFailure; - - /** - * 连接出错后重试时间间隔 - */ - private int timeBetweenConnectErrorMillis; - - public DruidDataSource dataSource(String url, String username, String password, String driverClassName) { - DruidDataSource datasource = new DruidDataSource(); - datasource.setUrl(url); - datasource.setUsername(username); - datasource.setPassword(password); - datasource.setDriverClassName(driverClassName); - //configuration - datasource.setInitialSize(initialSize); - datasource.setMinIdle(minIdle); - datasource.setMaxActive(maxActive); - datasource.setMaxWait(maxWait); - - datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); - datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); - datasource.setTestWhileIdle(testWhileIdle); - - datasource.setPoolPreparedStatements(poolPreparedStatements); - datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize); - - datasource.setConnectionErrorRetryAttempts(connectionErrorRetryAttempts); - datasource.setBreakAfterAcquireFailure(breakAfterAcquireFailure); - datasource.setTimeBetweenConnectErrorMillis(timeBetweenConnectErrorMillis); - return datasource; - } -} diff --git a/report-core/src/main/java/com/anjiplus/template/gaea/business/config/HikariPoolProperties.java b/report-core/src/main/java/com/anjiplus/template/gaea/business/config/HikariPoolProperties.java new file mode 100644 index 00000000..91d3a5df --- /dev/null +++ b/report-core/src/main/java/com/anjiplus/template/gaea/business/config/HikariPoolProperties.java @@ -0,0 +1,34 @@ +package com.anjiplus.template.gaea.business.config; + +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.pool.HikariPool; +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +/** + * Created by raodeming on 2021/8/6. + */ +@Component +@ConfigurationProperties(prefix = "spring.datasource.hikari") +@Data +public class HikariPoolProperties extends HikariConfig { + + + public HikariPool dataSource(String url, String username, String password, String driverClassName) { + HikariConfig hikariConfig = new HikariConfig(); + hikariConfig.setJdbcUrl(url); + hikariConfig.setUsername(username); + hikariConfig.setPassword(password); + hikariConfig.setDriverClassName(driverClassName); + + hikariConfig.setConnectionTimeout(getConnectionTimeout()); + hikariConfig.setValidationTimeout(getValidationTimeout()); + hikariConfig.setIdleTimeout(getIdleTimeout()); + hikariConfig.setMaxLifetime(getMaxLifetime()); + hikariConfig.setMaximumPoolSize(getMaximumPoolSize()); + hikariConfig.setMinimumIdle(getMinimumIdle()); + HikariPool hikariPool = new HikariPool(hikariConfig); + return hikariPool; + } +} diff --git a/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/datasource/service/impl/JdbcServiceImpl.java b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/datasource/service/impl/JdbcServiceImpl.java index 016bad3f..c9c7f98b 100644 --- a/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/datasource/service/impl/JdbcServiceImpl.java +++ b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/datasource/service/impl/JdbcServiceImpl.java @@ -1,9 +1,9 @@ package com.anjiplus.template.gaea.business.modules.datasource.service.impl; -import com.alibaba.druid.pool.DruidDataSource; -import com.anjiplus.template.gaea.business.config.DruidProperties; +import com.anjiplus.template.gaea.business.config.HikariPoolProperties; import com.anjiplus.template.gaea.business.modules.datasource.controller.dto.DataSourceDto; import com.anjiplus.template.gaea.business.modules.datasource.service.JdbcService; +import com.zaxxer.hikari.pool.HikariPool; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -21,22 +21,22 @@ import java.util.concurrent.ConcurrentHashMap; public class JdbcServiceImpl implements JdbcService { @Autowired - private DruidProperties druidProperties; + private HikariPoolProperties hikariPoolProperties; /** * 所有数据源的连接池存在map里 */ - private Map map = new ConcurrentHashMap<>(); + private Map map = new ConcurrentHashMap<>(); private Object lock = new Object(); - public DruidDataSource getJdbcConnectionPool(DataSourceDto dataSource) { + public HikariPool getJdbcConnectionPool(DataSourceDto dataSource) { if (map.containsKey(dataSource.getId())) { return map.get(dataSource.getId()); } else { try { synchronized (lock) { if (!map.containsKey(dataSource.getId())) { - DruidDataSource pool = druidProperties.dataSource(dataSource.getJdbcUrl(), + HikariPool pool = hikariPoolProperties.dataSource(dataSource.getJdbcUrl(), dataSource.getUsername(), dataSource.getPassword(), dataSource.getDriverName()); map.put(dataSource.getId(), pool); log.info("创建连接池成功:{}", dataSource.getJdbcUrl()); @@ -57,7 +57,7 @@ public class JdbcServiceImpl implements JdbcService { @Override public void removeJdbcConnectionPool(Long id) { try { - DruidDataSource pool = map.get(id); + HikariPool pool = map.get(id); if (pool != null) { log.info("remove pool success, datasourceId:{}", id); map.remove(id); @@ -77,7 +77,7 @@ public class JdbcServiceImpl implements JdbcService { */ @Override public Connection getPooledConnection(DataSourceDto dataSource) throws SQLException{ - DruidDataSource pool = getJdbcConnectionPool(dataSource); + HikariPool pool = getJdbcConnectionPool(dataSource); return pool.getConnection(); } @@ -91,7 +91,7 @@ public class JdbcServiceImpl implements JdbcService { */ @Override public Connection getUnPooledConnection(DataSourceDto dataSource) throws SQLException { - DruidDataSource druidDataSource = druidProperties.dataSource(dataSource.getJdbcUrl(), + HikariPool druidDataSource = hikariPoolProperties.dataSource(dataSource.getJdbcUrl(), dataSource.getUsername(), dataSource.getPassword(), dataSource.getDriverName()); return druidDataSource.getConnection(); } diff --git a/report-core/src/main/resources/bootstrap.yml b/report-core/src/main/resources/bootstrap.yml index 2ef5beca..25f3deaf 100644 --- a/report-core/src/main/resources/bootstrap.yml +++ b/report-core/src/main/resources/bootstrap.yml @@ -22,6 +22,14 @@ spring: url: jdbc:mysql://10.108.26.197:3306/aj_report?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false username: root password: appuser@anji + type: com.zaxxer.hikari.HikariDataSource + hikari: + connection-timeout: 300000 # 连接超时时间 - 默认值:30秒。 + validation-timeout: 5000 # 连接被测试活动的最长时间 - 默认值:5秒。 + idle-timeout: 60000 # 连接池中允许闲置的最长时间 - 默认值:10分钟 + max-lifetime: 1800000 # 一个连接生命时长(毫秒),超时而没被使用则被释放 - 默认值:30分钟 + maximum-pool-size: 10 #连接池中允许的最大连接数,包括闲置和使用中的连接 - 默认值:10 + minimum-idle: 5 # 连接池中允许的最小空闲连接数 - 默认值:10。 #如果要使用redis,请参考report-core目录下的README.md文件中写的进行调整 # redis: @@ -29,22 +37,6 @@ spring: # port: 6379 # password: root # database: 1 - - #数据源连接池配置 - druid: - initial-size: 10 # 初始化时建立物理连接的个数。初始化发生在显示调用init方法,或者第一次getConnection时 - min-idle: 10 # 最小连接池数量 - maxActive: 200 # 最大连接池数量 - maxWait: 3000 # 获取连接时最大等待时间,单位毫秒。配置了maxWait之后,缺省启用公平锁,并发效率会有所下降,如果需要可以通过配置 - timeBetweenEvictionRunsMillis: 60000 # 关闭空闲连接的检测时间间隔.Destroy线程会检测连接的间隔时间,如果连接空闲时间大于等于minEvictableIdleTimeMillis则关闭物理连接。 - minEvictableIdleTimeMillis: 300000 # 连接的最小生存时间.连接保持空闲而不被驱逐的最小时间 - testWhileIdle: true # 申请连接时检测空闲时间,根据空闲时间再检测连接是否有效.建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRun - poolPreparedStatements: true # 开启PSCache - maxPoolPreparedStatementPerConnectionSize: 20 #设置PSCache值 - connectionErrorRetryAttempts: 3 # 连接出错后再尝试连接三次 - breakAfterAcquireFailure: true # 数据库服务宕机自动重连机制 - timeBetweenConnectErrorMillis: 300000 # 连接出错后重试时间间隔 - validationQuery: select 1 # 用来检测连接是否有效的sql flyway: enabled: true #是否开启flyway,默认true. baseline-on-migrate: true