[Open Source] SpringBoot 3-Compatible Multi-Data-Source Dynamic Routing, Read/Write Separation, and Distributed Transactions
![[Open Source] SpringBoot 3-Compatible Multi-Data-Source Dynamic Routing, Read/Write Separation, and Distributed Transactions](https://yy123.ink/api/img/1782591249107-kggojz.jpg)
1. Project Overview

dynamic-datasource-spring-boot-starter is a SpringBoot-based starter for quickly integrating multiple data sources.
It supports Jdk 1.7+, SpringBoot 1.5.x 2.x.x 3.x.x。
A SpringBoot-based multi-data-source dynamic routing solution with read/write separation, quick startup, and distributed transaction support.
2. Open Source License
Licensed under Apache-2.0
3. Screenshots

4. Feature Overview
Features
- Supports Data source grouping , suitable for various scenarios, including multiple databases, read/write separation, one primary with multiple replicas, and hybrid modes.
- Supports encryption of sensitive database configuration information Encryption (customizable) ENC()。
- Supports independent initialization of table structures (schema) and databases (database) for each database.
- Supports startup without a data source and lazy loading of data sources (connections are created only when needed).
- Supports Custom annotations , requiring inheritance from DS (3.2.0+).
- Provides simplified, rapid integration with Druid, HikariCp, BeeCp, and Dbcp2.
- Provides integration solutions for components such as Mybatis-Plus, Quartz, ShardingJdbc, P6sy, and Jndi.
- Provides Custom data source providers solutions (for example, loading all data sources from a database).
- Provides solutions for Dynamically adding and removing data sources solution.
- Provides Pure read/write separation solution.
- Provides support for using SpEL dynamic parameters to resolve data sources. Built-in support for SpEL, session, and header, with custom extensions supported.
- Supports Multi-level nested data source switching 。(ServiceA >>> ServiceB >>> ServiceC)。
- Provides Seata-based distributed transaction solution 。
- Provides Local multi-data-source transaction solution.
Conventions
- This framework focuses solely on switching data sources as its core function, anddoes not restrict your specific operations. Once a data source is switched, you can perform any CRUD operations.
- For data sources in the configuration file, the prefix before the underscore _ is the prefix that identifies the group name. Data sources with the same group name are placed in the same group.
- You can switch data sources by either group name or specific data source name. When switching by group name, a load-balancing algorithm is used.
- The default data source name is master , which you can change using spring.datasource.dynamic.primary.
- Annotations on methods take precedence over annotations on classes.
- DS supports inheritance from abstract classes annotated with DS, but inheritance from interfaces annotated with DS is not currently supported.
5. Tech Stack
Usage
1. Add dynamic-datasource-spring-boot-starter.
spring-boot 1.5.x 2.x.x
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>${version}</version>
</dependency>
Spring Boot 3 and above
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot3-starter</artifactId>
<version>${version}</version>
</dependency>
2. Configure the data sources.
spring:
datasource:
dynamic:
primary: master #Set the default data source or data source group. The default value is master.
strict: false #Strictly match data sources. Default is false. If true, an exception is thrown when the specified data source is not found. If false, the default data source is used.
datasource:
master:
url: jdbc:mysql://xx.xx.xx.xx:3306/dynamic
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver #SPI support is available from 3.2.0, so this configuration can be omitted.
slave_1:
url: jdbc:mysql://xx.xx.xx.xx:3307/dynamic
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
slave_2:
url: ENC(xxxxx) #Built-in encryption. See the detailed documentation for usage instructions.
username: ENC(xxxxx)
password: ENC(xxxxx)
driver-class-name: com.mysql.jdbc.Driver
#......omitted
#The above configuration defines one default database, master, and a slave group containing two databases: slave_1 and slave_2
# Multiple Primary and Multiple Replica Databases Pure Multiple Databases (Remember to Set primary) Hybrid Configuration
spring: spring: spring:
datasource: datasource: datasource:
dynamic: dynamic: dynamic:
datasource: datasource: datasource:
master_1: mysql: master:
master_2: oracle: slave_1:
slave_1: sqlserver: slave_2:
slave_2: postgresql: oracle_1:
slave_3: h2: oracle_2:
3. Use @DS to switch data sources.
@DS can be applied to methods or classes.When both are present, the nearest annotation takes precedence: method-level annotations override class-level annotations.。
Annotation
Result
No @DS
Default data source
@DS("dsName")
dsName can be either a group name or the name of a specific database.
@Service
@DS("slave")
public class UserServiceImpl implements UserService {
@Autowired
private JdbcTemplate jdbcTemplate;
public List selectAll() {
return jdbcTemplate.queryForList("select * from user");
}
@Override
@DS("slave_1")
public List selectByCondition() {
return jdbcTemplate.queryForList("select * from user where age >10");
}
}