随着业务增长,一般会对数据进行垂直切分与水平切分, 一般会有一个应用对应多个数据源的需求。
本文基于Spring 3.1.1.RELEASE 和 iBatis 实现多数据源配置, 有以下两种方法。
配置多个SqlMapClientTemplate
一个SqlMapClientTemplate 对应一个数据源
<!-- global 数据源配置 -->
<bean id="globalDataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="${redisa.database.global.url}"/>
<property name="username" value="${redisa.database.global.username}"/>
<property name="password" value="${redisa.database.global.password}" />
</bean>
<bean id="globalSqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient">
<bean class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>classpath:ibatis/sqlmap-config.xml</value>
</property>
<property name="dataSource">
<ref local="globalDataSource"/>
</property>
</bean>
</property>
<property name="dataSource">
<ref local="globalDataSource"/>
</property>
</bean>
<!-- // -->
<!-- region 数据源配置 -->
<bean id="regionDataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="${redisa.database.region.url}"/>
<property name="username" value="${redisa.database.region.username}"/>
<property name="password" value="${redisa.database.region.password}" />
</bean>
<bean id="regionSqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient">
<bean class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>classpath:ibatis/sqlmap-config.xml</value>
</property>
<property name="dataSource">
<ref local="regionDataSource"/>
</property>
</bean>
</property>
<property name="dataSource">
<ref local="regionDataSource"/>
</property>
</bean>
设置好 sqlMapClientTemplate 后,在不同的DAO 中引用对应的sqlMapClientTemplate
<bean id="redisaConfigDao" class="com.aliyun.redisa.phoenix.dao.impl.RedisaConfigDaoImpl">
<property name="sqlMapClientTemplate" ref="globalSqlMapClientTemplate" />
</bean>
<bean id="redisaInstanceDAO" class="com.aliyun.redisa.phoenix.dao.impl.RedisaInstanceDAOImpl">
<property name="regionSqlMapClientTemplate" ref="regionSqlMapClientTemplate" />
</bean>
重写AbstractRoutingDataSource 的determineCurrentLookupKey 方法
- 使用ThreadLocal 来保存当前线程使用的数据源
public class DynamicDataSourceHolder {
/**
* 数据源标识保存在线程变量中,避免多线程操作数据源时互相干扰
*/
private static final ThreadLocal<String> THREAD_DATA_SOURCE = new ThreadLocal<String>();
public static String getDataSource() {
return THREAD_DATA_SOURCE.get();
}
public static void setDataSource(String dataSource) {
THREAD_DATA_SOURCE.set(dataSource);
}
public static void clearDataSource() {
THREAD_DATA_SOURCE.remove();
}
}
- 编写DynamicDataSource.java 类重写AbstractRoutingDataSource 的determineCurrentLookupKey 方法
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
// 从自定义的位置获取数据源标识
return DynamicDataSourceHolder.getDataSource();
}
}
- 配置数据源
<!-- apsaradb phoenix global config begin -->
<bean id="globalDataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="${adb.database.global.url}"/>
<property name="username" value="${adb.database.global.username}"/>
<property name="password" value="${adb.database.global.password}"/>
</bean>
<!-- apsaradb phoenix global config end-->
<!-- product database begin-->
<!-- rds database config -->
<bean id="rdsDataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="${rds.database.url}"/>
<property name="username" value="${rds.database.username}"/>
<property name="password" value="${rds.database.password}"/>
</bean>
<!-- product datasource end-->
<!-- multi datasource config -->
<util:map id="dataSources">
<!-- 指定lookupKey和与之对应的数据源 -->
<entry key="global" value-ref="globalDataSource" />
<entry key="rds" value-ref="rdsDataSource" />
</util:map>
<bean id="dataSourceLookup" class="org.springframework.jdbc.datasource.lookup.MapDataSourceLookup">
<constructor-arg ref="dataSources"/>
</bean>
<bean id="dynamicDataSource" class="com.aliyun.adb.dao.datasource.DynamicDataSource">
<property name="targetDataSources" ref="dataSources"/>
<!-- 这里可以指定默认的数据源 -->
<property name="defaultTargetDataSource" ref="globalDataSource" />
<property name="dataSourceLookup" ref="dataSourceLookup" />
</bean>
<!-- multi datasource config end-->
<!-- sqlMapClient config-->
<bean id="sqlMapClientFactory" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource" ref="dynamicDataSource"/>
<property name="configLocation" value="classpath:ibatis/sqlmap-config.xml"/>
</bean>
<bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient" ref="sqlMapClientFactory" />
<property name="dataSource">
<ref local="dynamicDataSource"/>
</property>
</bean>
<!-- -->
- 在各个DAO引用sqlMapClientTemplate
<bean id="rdsInstanceDao" class="com.aliyun.adb.dao.impl.RdsInstanceDAOImpl">
<property name="sqlMapClientTemplate" ref="sqlMapClientTemplate" />
</bean>
- 在使用DAO 来进行查询之前设置成应该使用的数据源
public InstanceDO getByInstanceId(NewBaseRequest request) {
DynamicDataSourceHolder.setDataSource("rds");
InstanceDO instance = instanceDAO.getByInstanceId(request.getDbInstanceId());
return instance;
}