移除druid

Raod 2 years ago
parent a4ce8a71b0
commit 03be882ae4

@ -108,11 +108,6 @@
<artifactId>mysql-connector-java</artifactId> <artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version> <version>8.0.28</version>
</dependency> </dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.0</version>
</dependency>
<dependency> <dependency>
<groupId>org.flywaydb</groupId> <groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId> <artifactId>flyway-core</artifactId>

@ -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 {
/**
* initgetConnection
*/
private int initialSize;
/**
*
*/
private int minIdle;
/**
*
*/
private int maxActive;
/**
* maxWait
*/
private int maxWait;
/**
* .Destroy线minEvictableIdleTimeMillis
*/
private int timeBetweenEvictionRunsMillis;
/**
* .
*/
private int minEvictableIdleTimeMillis;
/**
* .truetimeBetweenEvictionRun
*/
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;
}
}

@ -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;
}
}

@ -1,9 +1,9 @@
package com.anjiplus.template.gaea.business.modules.datasource.service.impl; package com.anjiplus.template.gaea.business.modules.datasource.service.impl;
import com.alibaba.druid.pool.DruidDataSource; import com.anjiplus.template.gaea.business.config.HikariPoolProperties;
import com.anjiplus.template.gaea.business.config.DruidProperties;
import com.anjiplus.template.gaea.business.modules.datasource.controller.dto.DataSourceDto; import com.anjiplus.template.gaea.business.modules.datasource.controller.dto.DataSourceDto;
import com.anjiplus.template.gaea.business.modules.datasource.service.JdbcService; import com.anjiplus.template.gaea.business.modules.datasource.service.JdbcService;
import com.zaxxer.hikari.pool.HikariPool;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -21,22 +21,22 @@ import java.util.concurrent.ConcurrentHashMap;
public class JdbcServiceImpl implements JdbcService { public class JdbcServiceImpl implements JdbcService {
@Autowired @Autowired
private DruidProperties druidProperties; private HikariPoolProperties hikariPoolProperties;
/** /**
* map * map
*/ */
private Map<Long, DruidDataSource> map = new ConcurrentHashMap<>(); private Map<Long, HikariPool> map = new ConcurrentHashMap<>();
private Object lock = new Object(); private Object lock = new Object();
public DruidDataSource getJdbcConnectionPool(DataSourceDto dataSource) { public HikariPool getJdbcConnectionPool(DataSourceDto dataSource) {
if (map.containsKey(dataSource.getId())) { if (map.containsKey(dataSource.getId())) {
return map.get(dataSource.getId()); return map.get(dataSource.getId());
} else { } else {
try { try {
synchronized (lock) { synchronized (lock) {
if (!map.containsKey(dataSource.getId())) { if (!map.containsKey(dataSource.getId())) {
DruidDataSource pool = druidProperties.dataSource(dataSource.getJdbcUrl(), HikariPool pool = hikariPoolProperties.dataSource(dataSource.getJdbcUrl(),
dataSource.getUsername(), dataSource.getPassword(), dataSource.getDriverName()); dataSource.getUsername(), dataSource.getPassword(), dataSource.getDriverName());
map.put(dataSource.getId(), pool); map.put(dataSource.getId(), pool);
log.info("创建连接池成功:{}", dataSource.getJdbcUrl()); log.info("创建连接池成功:{}", dataSource.getJdbcUrl());
@ -57,7 +57,7 @@ public class JdbcServiceImpl implements JdbcService {
@Override @Override
public void removeJdbcConnectionPool(Long id) { public void removeJdbcConnectionPool(Long id) {
try { try {
DruidDataSource pool = map.get(id); HikariPool pool = map.get(id);
if (pool != null) { if (pool != null) {
log.info("remove pool success, datasourceId:{}", id); log.info("remove pool success, datasourceId:{}", id);
map.remove(id); map.remove(id);
@ -77,7 +77,7 @@ public class JdbcServiceImpl implements JdbcService {
*/ */
@Override @Override
public Connection getPooledConnection(DataSourceDto dataSource) throws SQLException{ public Connection getPooledConnection(DataSourceDto dataSource) throws SQLException{
DruidDataSource pool = getJdbcConnectionPool(dataSource); HikariPool pool = getJdbcConnectionPool(dataSource);
return pool.getConnection(); return pool.getConnection();
} }
@ -91,7 +91,7 @@ public class JdbcServiceImpl implements JdbcService {
*/ */
@Override @Override
public Connection getUnPooledConnection(DataSourceDto dataSource) throws SQLException { 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()); dataSource.getUsername(), dataSource.getPassword(), dataSource.getDriverName());
return druidDataSource.getConnection(); return druidDataSource.getConnection();
} }

@ -22,6 +22,14 @@ spring:
url: jdbc:mysql://10.108.26.197:3306/aj_report?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false url: jdbc:mysql://10.108.26.197:3306/aj_report?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false
username: root username: root
password: appuser@anji 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请参考report-core目录下的README.md文件中写的进行调整
# redis: # redis:
@ -29,22 +37,6 @@ spring:
# port: 6379 # port: 6379
# password: root # password: root
# database: 1 # 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: flyway:
enabled: true #是否开启flyway默认true. enabled: true #是否开启flyway默认true.
baseline-on-migrate: true baseline-on-migrate: true

Loading…
Cancel
Save