Spring Boot 动态数据源

本文介绍了如何在Spring Boot项目中利用AbstractRoutingDataSource实现动态数据源切换。通过创建多个数据源,存储在Map中,并在执行数据库操作时根据指定Key选择合适的数据源。文章详细讲解了配置、数据源加载、切换、注解解析以及测试的过程,确保在多线程环境下数据的安全性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

利用AOP来实现多数据源的动态切换功能。

1.前言

AbstractRoutingDataSource是Spring2.0.1版本引入的一个抽象类,它提供了多数据源的支持能力。AbstractRoutingDataSource抽象类定义了抽象的determineCurrentLookupKey方法,子类只需实现此方法,进而动态确定要使用的数据源。

AbstractRoutingDataSource

public abstract class AbstractRoutingDataSource extends AbstractDataSource implements InitializingBean {
   
    @Nullable
    private Map<Object, Object> targetDataSources;
    @Nullable
    private Object defaultTargetDataSource;
    private boolean lenientFallback = true;
    private DataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
    @Nullable
    private Map<Object, DataSource> resolvedDataSources;
    @Nullable
    private DataSource resolvedDefaultDataSource;
    
    ......

    protected DataSource determineTargetDataSource() {
   
        Assert.notNull(this.resolvedDataSources, "DataSource router not initialized");
        Object lookupKey = this.determineCurrentLookupKey();
        DataSource dataSource = (DataSource)this.resolvedDataSources.get(lookupKey);
        if (dataSource == null && (this.lenientFallback || lookupKey == null)) {
   
            dataSource = this.resolvedDefaultDataSource;
        }

        if (dataSource == null) {
   
            throw new IllegalStateException("Cannot determine target DataSource for lookup key [" + lookupKey + "]");
        } else {
   
            return dataSource;
        }
    }

    @Nullable
    protected abstract Object determineCurrentLookupKey();
}
  1. 调用determineCurrentLookupKey方法来获取数据源名称key
  2. 从resolvedDataSources属性中得到对应的DataSource对象。
  3. 如果找不到DataSource对象或者数据源名称key不存在则使用resolvedDefaultDataSource。

2.实现思路

  1. 提前准备好多个数据源
  2. 将其存入一个Map中 (Map的Key是对应数据源的名称,而Value则是对应的数据源)
  3. 将Map设置到AbstractRoutingDataSource对象的resolvedDataSources属性中
  4. 当执行数据库操作的时候就通过一个Key来从Map中获取对应的数据源实例
  5. 执行对应的数据库操作

3.项目实战

3.1 项目初始化

新建一个spring的项目:multi-source

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cn.flowboot</groupId>
    <artifactId>multi-source</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>multi-source</name>
    <description>multi-source</description>
    <properties>
        <java.version>1.8</java.version>
        <!-- mybatis plus 依赖 -->
        <mybatis-plus.version>3.5.2</mybatis-plus.version>
        <!-- druid 依赖 -->
        <druid.version>1.2.6</druid.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
     
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值