Share an excellent dynamic data source open source library-dynamic-datasource-spring-boot-starter
1.1 Preface
In our Java backend R&D work, sometimes due to the rapid iteration of business and the security isolation of data, different databases are often allocated for different API business lines, that is, a microservice often needs to deal with multiple data sources.
1.2 Introduction to Dynamic Data Source Open Source Library
dynamic-datasource-spring-boot-starter
It is a starter that quickly integrates multiple data sources based on springboot.- It supports Jdk 1.7+, SpringBoot 1.5.x 2.xx 3.xx.
- Click to view detailed documents
PS: Generally, the free documentation part is enough, no need to pay
1.3 Features
- Supports data source grouping, which is suitable for a variety of scenarios purely multi-database read-write separation one-master multi-slave mixed mode.
- Support database sensitive configuration information encryption (customizable) ENC().
- Support independent initialization of table structure schema and database database for each database.
- Support no data source startup, support lazy loading data source (create connection when needed).
- Support custom annotations, need to inherit DS (3.2.0+).
- Provide and simplify the fast integration of Druid, HikariCp, BeeCp, Dbcp2.
- Provide integration solutions for Mybatis-Plus, Quartz, ShardingJdbc, P6sy, Jndi and other components.
- Provide custom data source source solutions (such as loading from the database).
- Provide a solution to dynamically increase and remove data sources after the project is started.
- Provide a pure read-write separation solution in the Mybatis environment.
- Provides a solution for parsing data sources using spel dynamic parameters. Built-in spel, session, header, support customization.
- Supports nested switching of multi-layer data sources. (ServiceA >>> ServiceB >>> ServiceC).
- Provides a distributed transaction solution based on seata.
- Provides a local multi-data source transaction solution.
Advantages: powerful, easy to use, even more powerful with mybatis plus.
1.4 Usage example
1.4.1 Add dependencies
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.6.0</version>
</dependency>
1.4.2 Configure data source
spring:
datasource:
dynamic:
primary: master #设置默认的数据源或者数据源组,默认值即为master
strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
datasource:
master:
url: jdbc:mysql://xx.xx.xx.xx:3306/dynamic
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver # 3.2.0开始支持SPI可省略此配置
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) # 内置加密,使用请查看详细文档
username: ENC(xxxxx)
password: ENC(xxxxx)
driver-class-name: com.mysql.jdbc.Driver
#......省略
#以上会配置一个默认库master,一个组slave下有两个子库slave_1,slave_2
1.4.3 Use @DS
annotations to switch data sources
Although the official website example is in the service layer
@Service
@DS("slave_1")
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");
}
}
1.5 Best Practices
I recommend using it on the Dao layer. For example, the following is an example of desensitization and extraction of my project:
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
/***
* @author qingfeng.zhao
* @date 2022/4/27
* @apiNote
*/
@DS(value = "default-datasource")
@Mapper
@Repository
public interface VueElementAdminUserRoleMapper extends BaseMapper<VueElementAdminUserRoleEntity> {
}
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
@DS(value = "second-data-source")
@Repository
@Mapper
public interface MyProductMapper extends BaseMapper<MyProductInfoEntity> {
}
spring:
application:
name: xxxxxxx
# 动态数据源 dynamic-datasource-spring-boot-starter
datasource:
dynamic:
# 设置默认的数据源或者数据源组,默认值即为master
primary: default-datasource
# 严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
strict: false
datasource:
default-datasource:
url: jdbc:mysql://192.168.xxx.xxx:3306/myFirstDb?serverTimezone=Asia/Shanghai&useUnicode=true&useSSL=false&allowPublicKeyRetrieval=true&characterEncoding=UTF-8
username: xxxxxx
password: xxxxxx
driver-class-name: com.mysql.cj.jdbc.Driver
second-data-source:
url: jdbc:mysql://192.168.xxx.xxx:3306/mySecondDb?serverTimezone=Asia/Shanghai&useUnicode=true&useSSL=false&allowPublicKeyRetrieval=true&characterEncoding=UTF-8
username: xxxxxx
password: xxxxxx
driver-class-name: com.mysql.cj.jdbc.Driver
@DS
The data source name of the annotation needs to be configured in application.yml
This article is over~