Springboot 集成 dynamic-datasource-spring-boot-starter,实现项目中原有的数据源作为主数据源

本文主要是介绍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,实现项目中原有的数据源作为主数据源的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/876069

相关文章

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依

利用python实现对excel文件进行加密

《利用python实现对excel文件进行加密》由于文件内容的私密性,需要对Excel文件进行加密,保护文件以免给第三方看到,本文将以Python语言为例,和大家讲讲如何对Excel文件进行加密,感兴... 目录前言方法一:使用pywin32库(仅限Windows)方法二:使用msoffcrypto-too

SpringBoot整合OpenFeign的完整指南

《SpringBoot整合OpenFeign的完整指南》OpenFeign是由Netflix开发的一个声明式Web服务客户端,它使得编写HTTP客户端变得更加简单,本文为大家介绍了SpringBoot... 目录什么是OpenFeign环境准备创建 Spring Boot 项目添加依赖启用 OpenFeig

Java Spring 中 @PostConstruct 注解使用原理及常见场景

《JavaSpring中@PostConstruct注解使用原理及常见场景》在JavaSpring中,@PostConstruct注解是一个非常实用的功能,它允许开发者在Spring容器完全初... 目录一、@PostConstruct 注解概述二、@PostConstruct 注解的基本使用2.1 基本代

C#使用StackExchange.Redis实现分布式锁的两种方式介绍

《C#使用StackExchange.Redis实现分布式锁的两种方式介绍》分布式锁在集群的架构中发挥着重要的作用,:本文主要介绍C#使用StackExchange.Redis实现分布式锁的... 目录自定义分布式锁获取锁释放锁自动续期StackExchange.Redis分布式锁获取锁释放锁自动续期分布式

springboot使用Scheduling实现动态增删启停定时任务教程

《springboot使用Scheduling实现动态增删启停定时任务教程》:本文主要介绍springboot使用Scheduling实现动态增删启停定时任务教程,具有很好的参考价值,希望对大家有... 目录1、配置定时任务需要的线程池2、创建ScheduledFuture的包装类3、注册定时任务,增加、删

SpringBoot整合mybatisPlus实现批量插入并获取ID详解

《SpringBoot整合mybatisPlus实现批量插入并获取ID详解》这篇文章主要为大家详细介绍了SpringBoot如何整合mybatisPlus实现批量插入并获取ID,文中的示例代码讲解详细... 目录【1】saveBATch(一万条数据总耗时:2478ms)【2】集合方式foreach(一万条数

IntelliJ IDEA 中配置 Spring MVC 环境的详细步骤及问题解决

《IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决》:本文主要介绍IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决,本文分步骤结合实例给大... 目录步骤 1:创建 Maven Web 项目步骤 2:添加 Spring MVC 依赖1、保存后执行2、将新的依赖

SpringBoot中配置文件的加载顺序解读

《SpringBoot中配置文件的加载顺序解读》:本文主要介绍SpringBoot中配置文件的加载顺序,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot配置文件的加载顺序1、命令⾏参数2、Java系统属性3、操作系统环境变量5、项目【外部】的ap

使用Python实现矢量路径的压缩、解压与可视化

《使用Python实现矢量路径的压缩、解压与可视化》在图形设计和Web开发中,矢量路径数据的高效存储与传输至关重要,本文将通过一个Python示例,展示如何将复杂的矢量路径命令序列压缩为JSON格式,... 目录引言核心功能概述1. 路径命令解析2. 路径数据压缩3. 路径数据解压4. 可视化代码实现详解1