本文主要是介绍Springboot 集成 dynamic-datasource-spring-boot-starter,实现项目中原有的数据源作为主数据源,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Springboot 集成 dynamic-datasource-spring-boot-starter,实现项目中原有的数据源作为主数据源
保证原有项目中在执行数据库操作时,默认使用原有数据源,新数据源做特定操作
引入多数据源切换依赖:
<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-mysqlusername: 'xxxx'password: 'xxxxx'driverClassName: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/xxxxx?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useUnicode=true&useSSL=false&autoReconnect=true&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&nullCatalogMeansCurrent=truespring: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());/*** 动态数据源配置项*/@Autowiredprivate DynamicDataSourceProperties properties;@Autowiredprivate DataSourceProperties dataSourceProperties;@Beanpublic 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@Beanpublic 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;@Overridepublic 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;}
}
这篇关于Springboot 集成 dynamic-datasource-spring-boot-starter,实现项目中原有的数据源作为主数据源的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!