1.1 前言
在我們的Java后端研發(fā)工作中, 有時候由于業(yè)務的快速迭代和數(shù)據(jù)的安全隔離性,往往會為不同的 API業(yè)務線分配不同的數(shù)據(jù)庫,即一個微服務經(jīng)常需要和多個數(shù)據(jù)源打交道。
1.2 動態(tài)數(shù)據(jù)源開源庫簡介
-
dynamic-datasource-spring-boot-starter
是一個基于springboot的快速集成多數(shù)據(jù)源的啟動器。 - 其支持 Jdk 1.7+, SpringBoot 1.5.x 2.x.x 3.x.x。
- 點擊查看詳細文檔
PS: 一般免費的文檔部分就夠用了,不需付費
1.3 特性
- 支持 數(shù)據(jù)源分組,適用于多種場景 純粹多庫 讀寫分離 一主多從 混合模式。
- 支持數(shù)據(jù)庫敏感配置信息 加密(可自定義) ENC()。
- 支持每個數(shù)據(jù)庫獨立初始化表結(jié)構(gòu)schema和數(shù)據(jù)庫database。
- 支持無數(shù)據(jù)源啟動,支持懶加載數(shù)據(jù)源(需要的時候再創(chuàng)建連接)。
- 支持 自定義注解 ,需繼承DS(3.2.0+)。
- 提供并簡化對Druid,HikariCp,BeeCp,Dbcp2的快速集成。
- 提供對Mybatis-Plus,Quartz,ShardingJdbc,P6sy,Jndi等組件的集成方案。
- 提供 自定義數(shù)據(jù)源來源 方案(如全從數(shù)據(jù)庫加載)。
- 提供項目啟動后 動態(tài)增加移除數(shù)據(jù)源 方案。
- 提供Mybatis環(huán)境下的 純讀寫分離 方案。
- 提供使用 spel動態(tài)參數(shù) 解析數(shù)據(jù)源方案。內(nèi)置spel,session,header,支持自定義。
- 支持 多層數(shù)據(jù)源嵌套切換 。(ServiceA >>> ServiceB >>> ServiceC)。
- 提供 基于seata的分布式事務方案 。
- 提供 本地多數(shù)據(jù)源事務方案。
優(yōu)點:功能強大,使用簡潔,配合 mybatis plus 更能如虎添翼。
1.4 用法示例
1.4.1 添加依賴
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.6.0</version>
</dependency>
查看最新版本
1.4.2 配置數(shù)據(jù)源
spring:
datasource:
dynamic:
primary: master #設置默認的數(shù)據(jù)源或者數(shù)據(jù)源組,默認值即為master
strict: false #嚴格匹配數(shù)據(jù)源,默認false. true未匹配到指定數(shù)據(jù)源時拋異常,false使用默認數(shù)據(jù)源
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) # 內(nèi)置加密,使用請查看詳細文檔
username: ENC(xxxxx)
password: ENC(xxxxx)
driver-class-name: com.mysql.jdbc.Driver
#......省略
#以上會配置一個默認庫master,一個組slave下有兩個子庫slave_1,slave_2
1.4.3 使用 @DS 注解切換數(shù)據(jù)源
雖然官網(wǎng)示例在 service層
@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 最佳實踐
我建議使用在 Dao 層上,比如如下是我項目脫敏抽離出來的一個示例:
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
# 動態(tài)數(shù)據(jù)源 dynamic-datasource-spring-boot-starter
datasource:
dynamic:
# 設置默認的數(shù)據(jù)源或者數(shù)據(jù)源組,默認值即為master
primary: default-datasource
# 嚴格匹配數(shù)據(jù)源,默認false. true未匹配到指定數(shù)據(jù)源時拋異常,false使用默認數(shù)據(jù)源
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
注解的數(shù)據(jù)源名稱需在application.yml 中配置文章來源:http://www.zghlxwxcb.cn/news/detail-530706.html
本篇完~文章來源地址http://www.zghlxwxcb.cn/news/detail-530706.html
到了這里,關于分享一個優(yōu)秀的動態(tài)數(shù)據(jù)源開源庫-dynamic-datasource-spring-boot-starter的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!