赞
踩
保证原有项目中在执行数据库操作时,默认使用原有数据源,新数据源做特定操作
引入多数据源切换依赖:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.6.1</version>
</dependency>
使用的数据库连接池依赖:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.15</version>
</dependency>
<!-- 使用这个也没有问题,都是druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
以下相关代码编写基于的配置文件:
mysql: &db-mysql
username: 'xxxx'
password: 'xxxxx'
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/xxxxx?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useUnicode=true&useSSL=false&autoReconnect=true&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&nullCatalogMeansCurrent=true
spring:
datasource:
# << 用法解释:引用变量的作用 引用已提前定义好的配置:db-mysql 即最上面的配置
# 使用后的配置为:
# db-type: mysql
# validation-query: SELECT 'x'
# filters: stat,wall
# username: 'xxxx'
# password: 'xxxxx'
# driverClassName: com.mysql.cj.jdbc.Driver
# url: jdbc:mysql://localhost:3306/xxxxx?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useUnicode=true&useSSL=false&autoReconnect=true&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&nullCatalogMeansCurrent=true
<<: *db-mysql
动态数据源配置类
@Configuration
@EnableConfigurationProperties({DynamicDataSourceProperties.class})
@AutoConfigureBefore({DynamicDataSourceAutoConfiguration.class, SpringBootConfiguration.class})
public class DataSourceConfiguration {
private Logger logger = LoggerFactory.getLogger(this.getClass());
/**
* 动态数据源配置项
*/
@Autowired
private DynamicDataSourceProperties properties;
@Autowired
private DataSourceProperties dataSourceProperties;
@Bean
public DynamicDataSourceProvider dynamicDataSourceProvider() {
//todo 可以做更多的事情,例如从spring容器中获取已创建好的数据源对象,来由动态数据源管理,等等
//默认使用spring提供的数据源信息来创建默认数据源
DataSourceProperty masterProperty = new DataSourceProperty();
masterProperty.setType(dataSourceProperties.getType())
.setDriverClassName(dataSourceProperties.getDriverClassName())
.setUrl(dataSourceProperties.getUrl())
.setUsername(dataSourceProperties.getUsername()).setPassword(dataSourceProperties.getPassword());
//多数据源的数据源信息配置是否存在,如果存在则保存
Map<String, DataSourceProperty> datasource = properties.getDatasource();
if(!datasource.isEmpty()){
//如果存在指定了默认数据源,则替换spring的数据源信息
DataSourceProperty dynamicMasterProperty = datasource.get(properties.getPrimary());
if(dynamicMasterProperty != null){
masterProperty.setType(dynamicMasterProperty.getType())
.setDriverClassName(dynamicMasterProperty.getDriverClassName())
.setUrl(dynamicMasterProperty.getUrl()).setUsername(dynamicMasterProperty.getUsername())
.setPassword(dynamicMasterProperty.getPassword());
}
}
return new CustomDataSourceProvider(masterProperty,properties);
}
/**
* 将动态数据源设置为首选的
* 当spring存在多个数据源时, 自动注入的是首选的对象
* 设置为主要的数据源之后,就可以支持shardingjdbc原生的配置方式了
* @return
*/
@Primary
@Bean
public DataSource dataSource() {
DynamicRoutingDataSource dataSource = new DynamicRoutingDataSource();
dataSource.setPrimary(properties.getPrimary());
dataSource.setStrict(properties.getStrict());
dataSource.setStrategy(properties.getStrategy());
dataSource.setP6spy(properties.getP6spy());
dataSource.setSeata(properties.getSeata());
return dataSource;
}
}
继承AbstractDataSourceProvider
类,实现loadDataSources
方法
@AllArgsConstructor
public class CustomDataSourceProvider extends AbstractDataSourceProvider {
private final DataSourceProperty masterProperty;
private final DynamicDataSourceProperties dynamicDataSourceProperties;
@Override
public Map<String, DataSource> loadDataSources() {
Map<String, DataSourceProperty> map = new HashMap(16);
masterProperty.setDruid(this.dynamicDataSourceProperties.getDruid());
map.put(this.dynamicDataSourceProperties.getPrimary(), masterProperty);
Map<String, DataSourceProperty> datasource = this.dynamicDataSourceProperties.getDatasource();
if (!datasource.isEmpty()) {
map.putAll(datasource);
}
Map<String, DataSource> dataSourceMap = this.createDataSourceMap(map);
return dataSourceMap;
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。