Spring 注解动态数据源设计实践
Spring 动态数据源
动态数据源是什么?解决了什么问题?
在实际的开发中,同一个项目中使用多个数据源是很常见的场景。比如,一个读写分离的项目存在主数据源与读数据源。
所谓动态数据源,就是通过Spring的一些配置来自动控制某段数据操作逻辑是走哪一个数据源。举个读写分离的例子,项目中引用了两个数据源,master、slave。通过Spring配置或扩展能力来使得一个接口中调用了查询方法会自动使用slave数据源。
一般实现这种效果可以通过:
- 使用@MapperScan注解指定某个包下的所有方法走固定的数据源(这个比较死板些,会产生冗余代码,到也可以达到效果,可以作为临时方案使用);
- 使用注解+AOP+AbstractRoutingDataSource的形式来指定某个方法下的数据库操作是走那个数据源。
关键核心类
这里主要介绍通过注解+AOP+AbstractRoutingDataSource的联动来实现动态数据源的方式。
一切的起点是AbstractRoutingDataSource这个类,此类实现了 DataSource 接口
public abstract class AbstractRoutingDataSource extends AbstractDataSource implements InitializingBean {
// .... 省略 ...
@Nullable
private Map<Object, Object> targetDataSources;
@Nullable
private Map<Object, DataSource> resolvedDataSources;
public void setTargetDataSources(Map<Object, Object> targetDataSources) {
this.targetDataSources = targetDataSources;
}
public void setDefaultTargetDataSource(Object defaultTargetDataSource) {
this.defaultTargetDataSource = defaultTargetDataSource;
}
@Override
public void afterPropertiesSet() {
// 初始化 targetDataSources、resolvedDataSources
if (this.targetDataSources == null) {
throw new IllegalArgumentException("Property 'targetDataSources' is required");
}
this.resolvedDataSources = new HashMap<>(this.targetDataSources.size());
this.targetDataSources.forEach((key, value) -> {
Object lookupKey = resolveSpecifiedLookupKey(key);
DataSource dataSource = resolveSpecifiedDataSource(value);
this.resolvedDataSources.put(lookupKey, dataSource);
});
if (this.defaultTargetDataSource != null) {
this.resolvedDefaultDataSource = resolveSpecifiedDataSource(this.defaultTargetDataSource);
}
}
@Override
public Connection getConnection() throws SQLException {
return determineTargetDataSource().getConnection();
}
@Override
public Connection getConnection(String username, String password) throws SQLException {
return determineTargetDataSource().getConnection(username, password);
}
/**
* Retrieve the current target DataSource. Determines the
* {@link #determineCurrentLookupKey() current lookup key}, performs
* a lookup in the {@link #setTargetDataSources targetDataSources} map,
* falls back to the specified
* {@link #setDefaultTargetDataSource default target DataSource} if necessary.
* @see #determineCurrentLookupKey()
*/
protected DataSource determineTargetDataSource() {
Assert.notNull(this.resolvedDataSources, "DataSource router not initialized");
// @1 start
Object lookupKey = determineCurrentLookupKey();
DataSource dataSource = this.resolvedDataSources.get(lookupKey);
// @1 end
if (dataSource == null && (this.lenientFallback || lookupKey == null)) {
dataSource = this.resolvedDefaultDataSource;
}
if (dataSource == null) {
throw new IllegalStateException("Cannot determine target DataSource for lookup key [" + lookupKey + "]");
}
return dataSource;
}
/**
* 返回一个key,这个key用来从 resolvedDataSources 数据源中获取具体的数据源对象 见 @1
*/
@Nullable
protected abstract Object determineCurrentLookupKey();
}
可以看到 AbstractRoutingDataSource 中有个可扩展抽象方法 determineCurrentLookupKey(),利用这个方法可以来实现动态数据源效果。
从0写一个简单动态数据源组件
从上一个part我们知道可以通过实现AbstractRoutingDataSource的 determineCurrentLookupKey() 方法动态设置一个key,然后
在配置类下通过setTargetDataSources()方法设置我们提前准备好的DataSource Map。
注解、常量定义、ThreadLocal 准备
/**
* @author axin
* @Summary 动态数据源注解定义
*/
public @interface MyDS {
String value() default "default";
}
/**
* @author axin
* @Summary 动态数据源常量
*/
public interface DSConst {
String 默认 = "default";
String 主库 = "master";
String 从库 = "slave";
String 统计 = "stat";
}
/**
* @author axin
* @Summary 动态数据源 ThreadLocal 工具
*/
public class DynamicDataSourceHolder {
//保存当前线程所指定的DataSource
private static final ThreadLocal<String> THREAD_DATA_SOURCE = new ThreadLocal<>();
public static String getDataSource() {
return THREAD_DATA_SOURCE.get();
}
public static void setDataSource(String dataSource) {
THREAD_DATA_SOURCE.set(dataSource);
}
public static void removeDataSource() {
THREAD_DATA_SOURCE.remove();
}
}
自定一个 AbstractRoutingDataSource 类
/**
* @author axin
* @Summary 动态数据源
*/
public class DynamicDataSource extends AbstractRoutingDataSource {
/**
* 从数据源中获取目标数据源的key
* @return
*/
@Override
protected Object determineCurrentLookupKey() {
// 从ThreadLocal中获取key
String dataSourceKey = DynamicDataSourceHolder.getDataSource();
if (StringUtils.isEmpty(dataSourceKey)) {
return DSConst.默认;
}
return dataSourceKey;
}
}
AOP 实现
/**
* @author axin
* @Summary 数据源切换AOP
*/
@Slf4j
@Aspect
@Service
public class DynamicDataSourceAOP {
public DynamicDataSourceAOP() {
log.info("/*---------------------------------------*/");
log.info("/*---------- ----------*/");
log.info("/*---------- 动态数据源初始化... ----------*/");
log.info("/*---------- ----------*/");
log.info("/*---------------------------------------*/");
}
/**
* 切点
*/
@Pointcut(value = "@annotation(xxx.xxx.MyDS)")
private void method(){}
/**
* 方法执行前,切换到指定的数据源
* @param point
*/
@Before("method()")
public void before(JoinPoint point) {
MethodSignature methodSignature = (MethodSignature) point.getSignature();
//获取被代理的方法对象
Method targetMethod = methodSignature.getMethod();
//获取被代理方法的注解信息
CultureDS cultureDS = AnnotationUtils.findAnnotation(targetMethod, CultureDS.class);
// 方法链条最外层的动态数据源注解优先级最高
String key = DynamicDataSourceHolder.getDataSource();
if (!StringUtils.isEmpty(key)) {
log.warn("提醒:动态数据源注解调用链上出现覆盖场景,请确认是否无问题");
return;
}
if (cultureDS != null ) {
//设置数据库标志
DynamicDataSourceHolder.setDataSource(MyDS.value());
}
}
/**
* 释放数据源
*/
@AfterReturning("method()")
public void doAfter() {
DynamicDataSourceHolder.removeDataSource();
}
}
DataSourceConfig 配置
通过以下代码来将动态数据源配置到 SqlSession 中去
/**
* 数据源的一些配置,主要是配置读写分离的sqlsession,这里没有使用mybatis annotation
*
@Configuration
@EnableTransactionManagement
@EnableAspectJAutoProxy
class DataSourceConfig {
/** 可读写的SQL Session */
public static final String BEANNAME_SQLSESSION_COMMON = "sqlsessionCommon";
/** 事务管理器的名称,如果有多个事务管理器时,需要指定beanName */
public static final String BEANNAME_TRANSACTION_MANAGER = "transactionManager";
/** 主数据源,必须配置,spring启动时会执行初始化数据操作(无论是否真的需要),选择查找DataSource class类型的数据源 配置通用数据源,可读写,连接的是主库 */
@Bean
@Primary
@ConfigurationProperties(prefix = "datasource.common")
public DataSource datasourceCommon() {
// 数据源配置 可更换为其他实现方式
return DataSourceBuilder.create().build();
}
/**
* 动态数据源
* @returnr
*/
@Bean
public DynamicDataSource dynamicDataSource() {
DynamicDataSource dynamicDataSource = new DynamicDataSource();
LinkedHashMap<Object, Object> hashMap = Maps.newLinkedHashMap();
hashMap.put(DSConst.默认, datasourceCommon());
hashMap.put(DSConst.主库, datasourceCommon());
hashMap.put(DSConst.从库, datasourceReadOnly());
hashMap.put(DSConst.统计, datasourceStat());
// 初始化数据源 Map
dynamicDataSource.setTargetDataSources(hashMap);
dynamicDataSource.setDefaultTargetDataSource(datasourceCommon());
return dynamicDataSource;
}
/**
* 配置事务管理器
*/
@Bean(name = BEANNAME_TRANSACTION_MANAGER)
public DataSourceTransactionManager createDataSourceTransactionManager() {
DataSource dataSource = this.datasourceCommon();
DataSourceTransactionManager manager = new DataSourceTransactionManager(dataSource);
return manager;
}
/**
* 配置读写sqlsession
*/
@Primary
@Bean(name = BEANNAME_SQLSESSION_COMMON)
public SqlSession readWriteSqlSession() throws Exception {
SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
// 设置动态数据源
factory.setDataSource(this.dynamicDataSource());
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
factory.setConfigLocation(resolver.getResource("mybatis/mybatis-config.xml"));
factory.setMapperLocations(resolver.getResources("mybatis/mappers/**/*.xml"));
return new SqlSessionTemplate(factory.getObject());
}
}
总结
综上,实现了一个简单的Spring动态数据源功能,使用的时候,仅需要在目标方法上加上 @MyDS 注解即可。许多开源组件,会在现有的基础上增加一个扩展功能,比如路由策略等等。
Spring 注解动态数据源设计实践的更多相关文章
- Spring实现动态数据源,支持动态加入、删除和设置权重及读写分离
当项目慢慢变大,訪问量也慢慢变大的时候.就难免的要使用多个数据源和设置读写分离了. 在开题之前先说明下,由于项目多是使用Spring,因此下面说到某些操作可能会依赖于Spring. 在我经历过的项目中 ...
- Spring Boot 动态数据源(Spring 注解数据源)
本文实现案例场景:某系统除了需要从自己的主要数据库上读取和管理数据外,还有一部分业务涉及到其他多个数据库,要求可以在任何方法上可以灵活指定具体要操作的数据库. 为了在开发中以最简单的方法使用,本文基于 ...
- Spring配置动态数据源-读写分离和多数据源
在现在互联网系统中,随着用户量的增长,单数据源通常无法满足系统的负载要求.因此为了解决用户量增长带来的压力,在数据库层面会采用读写分离技术和数据库拆分等技术.读写分离就是就是一个Master数据库,多 ...
- spring boot动态数据源方案
动态数据源 1.背景 动态数据源在实际的业务场景下需求很多,而且想要沟通多数据库确实需要封装这种工具,针对于bi工具可能涉及到从不同的业务库或者数据仓库中获取数据,动态数据源就更加有意义. 2.依赖 ...
- 43. Spring Boot动态数据源(多数据源自动切换)【从零开始学Spring Boot】
[视频&交流平台] àSpringBoot视频 http://study.163.com/course/introduction.htm?courseId=1004329008&utm ...
- Spring Boot 动态数据源(多数据源自己主动切换)
本文实现案例场景: 某系统除了须要从自己的主要数据库上读取和管理数据外.另一部分业务涉及到其它多个数据库,要求能够在不论什么方法上能够灵活指定详细要操作的数据库. 为了在开发中以最简单的方法使用,本文 ...
- (43). Spring Boot动态数据源(多数据源自动切换)【从零开始学Spring Boot】
在上一篇我们介绍了多数据源,但是我们会发现在实际中我们很少直接获取数据源对象进行操作,我们常用的是jdbcTemplate或者是jpa进行操作数据库.那么这一节我们将要介绍怎么进行多数据源动态切换.添 ...
- Spring Boot 动态数据源(多数据源自动切换)
本文实现案例场景: 某系统除了需要从自己的主要数据库上读取和管理数据外,还有一部分业务涉及到其他多个数据库,要求可以在任何方法上可以灵活指定具体要操作的数据库. 为了在开发中以最简单的方法使用,本文基 ...
- SaaS 系统架构,Spring Boot 动态数据源实现!
这段时候在准备从零开始做一套SaaS系统,之前的经验都是开发单数据库系统并没有接触过SaaS系统,所以接到这个任务的时候也有也些头疼,不过办法部比困难多,难得的机会. 在网上找了很多关于SaaS的资料 ...
随机推荐
- 微信二维码引擎OpenCV开源研究
<微信二维码引擎OpenCV开源研究> 一.编译和Test测试 opencv_wechat_qrcode的编译需要同时下载opencv(https://github.com/ ...
- [源码解析] 并行分布式任务队列 Celery 之 消费动态流程
[源码解析] 并行分布式任务队列 Celery 之 消费动态流程 目录 [源码解析] 并行分布式任务队列 Celery 之 消费动态流程 0x00 摘要 0x01 来由 0x02 逻辑 in komb ...
- QT程序发布
1.新建一个脚本文件,后缀为.bat 2.查看自己qt的windeployqt.exe路径,一般在QT安装的bin目录,而且脚本程序中需要去掉其后缀, 前面部分是windeployqt.exe的路径以 ...
- 《SQL必知必会》学习笔记整理
简介 本笔记目前已包含 <SQL必知必会>中的所有章节. 我在整理笔记时所考虑的是:在笔记记完后,当我需要查找某个知识点时,不需要到书中去找,只需查看笔记即可找到相关知识点.因此在整理笔记 ...
- Redis 与 Python 交互
1. Python 库安装 2. 交互代码范例 3. Redis 操作封装 4. 应用范例:用户登录 1. Python 库安装 联网安装 pip install redis 使用源码安装 到中文官网 ...
- OO博客总结——OO落下帷幕
OO博客总结--OO落下帷幕 凡此过往,皆为序章. 不知不觉OO课程即将落下帷幕,一路坎坎坷坷磕磕绊绊,可算是要结束了,心里终于松了一口气,也有小小的不甘和遗憾.凡此过往,皆为序章.特殊的线上OO课程 ...
- NNLM原理及Pytorch实现
NNLM NNLM:Neural Network Language Model,神经网络语言模型.源自Bengio等人于2001年发表在NIPS上的<A Neural Probabilistic ...
- Linux内核软中断
1 软中断概述 软中断是实现中断下半部的一种手段,与2.5以前版本的下半段机制不同.软中断可以同时运行在不同的CPU上. 1.1 软中断的表示 内核中用结构体softirq_action表示一个软中断 ...
- 032- for循环语句
问题 请在控制台里面打印10次HelloWorld. 这时可能你会这么写: System.out.println("helloworld"); System.out.println ...
- 08- adb常用命令以及模拟器链接adb命令
adb 命令简介 ADB即 Android debug bridge.是Android下面一个通用的调试工具. 熟练使用adb命令会大大增加开发效率,作为测试人员,熟练掌握adb,我们可以管理设备或手 ...