Spring + iBatis 的多库横向切分简易解决思路

1.引言

笔者最近在做一个互联网的“类SNS”应用,应用中用户数量巨大(约4000万)左右,因此,简单的使用传统单一数据库存储肯定是不行的。

参考了业内广泛使用的分库分表,以及使用DAL数据访问层等的做法,笔者决定使用一种最简单的数据源路由选择方式来解决问题。

严格的说,目前的实现不能算是一个解决方案,只能是一种思路的简易实现,笔者也仅花了2天时间来完成(其中1.5天是在看资料和Spring/ibatis的源码)。这里也只是为各位看官提供一个思路参考,顺便给自己留个笔记

2.系统的设计前提

我们的系统使用了16个数据库实例(目前分布在2台物理机器上,后期将根据系统负荷的增加,逐步移库到16台物理机器上)。16个库是根据用户的UserID进行简单的hash分配。这里值得一说的是,我们既然做了这样的横向切分设计,就已经考虑了系统需求的特性,

  • 1.不会发生经常性的跨库访问。

  • 2.主要的业务逻辑都是围绕UserID为核心的,在一个单库事务内即可完成。

    在系统中,我们使用Spring和iBatis。Spring负责数据库的事务管理AOP,以及Bean间的IOC。选择iBatis的最大原因是对Sql的性能优化,以及后期如果有分表要求的时,可以很容易实现对sql表名替换。

3.设计思路

首先,要说明一下笔者的思路,其实很简单,即“在每次数据库操作前,确定当前要选择的数据库对象”而后就如同访问单库一样的访问当前选中的数据库即可。

其次,要在每次DB访问前选择数据库,需要明确几个问题,1.iBatis在什么时候从DataSource中取得具体的数据库Connection的,2.对取得的Connection,iBatis是否进行缓存,因为在多库情况下Connection被缓存就意味着无法及时改变数据库链接选择。3.由于我们使用了Spring来管理DB事务,因此必须搞清Spring对DB Connction的开关拦截过程是否会影响多DataSource的情况。

幸运的是,研究源码的结果发现,iBatis和Spring都是通过标准的DataSource接口来控制

Connection的,这就为我们省去了很多的麻烦,只需要实现一个能够支持多个数据库的DataSource,就能达到我们的目标。

4.代码与实现

多数据库的DataSource实现:MultiDataSource.class

Java代码

  1. import java.io.PrintWriter;
  2. import java.sql.Connection;
  3. import java.sql.SQLException;
  4. import java.util.ArrayList;
  5. import java.util.Collection;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. import javax.sql.DataSource;
  9. import org.apache.log4j.Logger;
  10. import com.xxx.sql.DataSourceRouter.RouterStrategy;
  11. /**
  12. * 复合多数据源(Alpha)
  13. * @author linliangyi2005@gmail.com
  14. * Jul 15, 2010
  15. */
  16. public class MultiDataSource implements DataSource {
  17. static Logger logger = Logger.getLogger(MultiDataSource.class);
  18. //当前线程对应的实际DataSource
  19. private ThreadLocal currentDataSourceHolder = new ThreadLocal();
  20. //使用Key-Value映射的DataSource
  21. private Map<String , DataSource> mappedDataSources;
  22. //使用横向切分的分布式DataSource
  23. private ArrayList clusterDataSources;
  24. public MultiDataSource(){
  25. ​ mappedDataSources = new HashMap<String , DataSource>(4);
  26. ​ clusterDataSources = new ArrayList(4);
  27. }
  28. /**
  29. * 数据库连接池初始化
  30. * 该方法通常在web 应用启动时调用
  31. */
  32. public void initialMultiDataSource(){
  33. for(DataSource ds : clusterDataSources){
  34. if(ds != null){
  35. ​ Connection conn = null;
  36. try {
  37. ​ conn = ds.getConnection();
  38. ​ } catch (SQLException e) {
  39. ​ e.printStackTrace();
  40. ​ } finally{
  41. if(conn != null){
  42. try {
  43. ​ conn.close();
  44. ​ } catch (SQLException e) {
  45. ​ e.printStackTrace();
  46. ​ }
  47. ​ conn = null;
  48. ​ }
  49. ​ }
  50. ​ }
  51. ​ }
  52. ​ Collection dsCollection = mappedDataSources.values();
  53. for(DataSource ds : dsCollection){
  54. if(ds != null){
  55. ​ Connection conn = null;
  56. try {
  57. ​ conn = ds.getConnection();
  58. ​ } catch (SQLException e) {
  59. ​ e.printStackTrace();
  60. ​ } finally{
  61. if(conn != null){
  62. try {
  63. ​ conn.close();
  64. ​ } catch (SQLException e) {
  65. ​ e.printStackTrace();
  66. ​ }
  67. ​ conn = null;
  68. ​ }
  69. ​ }
  70. ​ }
  71. ​ }
  72. }
  73. /**
  74. * 获取当前线程绑定的DataSource
  75. * @return
  76. */
  77. public DataSource getCurrentDataSource() {
  78. ​ //如果路由策略存在,且更新过,则根据路由算法选择新的DataSource
  79. ​ RouterStrategy strategy = DataSourceRouter.currentRouterStrategy.get();
  80. if(strategy == null){
  81. throw new IllegalArgumentException("DataSource RouterStrategy No found.");
  82. ​ }
  83. if(strategy != null && strategy.isRefresh()){
  84. if(RouterStrategy.SRATEGY_TYPE_MAP.equals(strategy.getType())){
  85. this.choiceMappedDataSources(strategy.getKey());
  86. ​ }else if(RouterStrategy.SRATEGY_TYPE_CLUSTER.equals(strategy.getType())){
  87. this.routeClusterDataSources(strategy.getRouteFactor());
  88. ​ }
  89. ​ strategy.setRefresh(false);
  90. ​ }
  91. return currentDataSourceHolder.get();
  92. }
  93. public Map<String, DataSource> getMappedDataSources() {
  94. return mappedDataSources;
  95. }
  96. public void setMappedDataSources(Map<String, DataSource> mappedDataSources) {
  97. this.mappedDataSources = mappedDataSources;
  98. }
  99. public ArrayList getClusterDataSources() {
  100. return clusterDataSources;
  101. }
  102. public void setClusterDataSources(ArrayList clusterDataSources) {
  103. this.clusterDataSources = clusterDataSources;
  104. }
  105. /**
  106. * 使用Key选择当前的数据源
  107. * @param key
  108. */
  109. public void choiceMappedDataSources(String key){
  110. ​ DataSource ds = this.mappedDataSources.get(key);
  111. if(ds == null){
  112. throw new IllegalStateException("No Mapped DataSources Exist!");
  113. ​ }
  114. this.currentDataSourceHolder.set(ds);
  115. }
  116. /**
  117. * 使用取模算法,在群集数据源中做路由选择
  118. * @param routeFactor
  119. */
  120. public void routeClusterDataSources(int routeFactor){
  121. int size = this.clusterDataSources.size();
  122. if(size == 0){
  123. throw new IllegalStateException("No Cluster DataSources Exist!");
  124. ​ }
  125. int choosen = routeFactor % size;
  126. ​ DataSource ds = this.clusterDataSources.get(choosen);
  127. if(ds == null){
  128. throw new IllegalStateException("Choosen DataSources is null!");
  129. ​ }
  130. ​ logger.debug("Choosen DataSource No." + choosen+ " : " + ds.toString());
  131. this.currentDataSourceHolder.set(ds);
  132. }
  133. /* (non-Javadoc)
  134. * @see javax.sql.DataSource#getConnection()
  135. */
  136. public Connection getConnection() throws SQLException {
  137. if(getCurrentDataSource() != null){
  138. return getCurrentDataSource().getConnection();
  139. ​ }
  140. return null;
  141. }
  142. /* (non-Javadoc)
  143. * @see javax.sql.DataSource#getConnection(java.lang.String, java.lang.String)
  144. */
  145. public Connection getConnection(String username, String password)
  146. throws SQLException {
  147. if(getCurrentDataSource() != null){
  148. return getCurrentDataSource().getConnection(username , password);
  149. ​ }
  150. return null;
  151. }
  152. /* (non-Javadoc)
  153. * @see javax.sql.CommonDataSource#getLogWriter()
  154. */
  155. public PrintWriter getLogWriter() throws SQLException {
  156. if(getCurrentDataSource() != null){
  157. return getCurrentDataSource().getLogWriter();
  158. ​ }
  159. return null;
  160. }
  161. /* (non-Javadoc)
  162. * @see javax.sql.CommonDataSource#getLoginTimeout()
  163. */
  164. public int getLoginTimeout() throws SQLException {
  165. if(getCurrentDataSource() != null){
  166. return getCurrentDataSource().getLoginTimeout();
  167. ​ }
  168. return 0;
  169. }
  170. /* (non-Javadoc)
  171. * @see javax.sql.CommonDataSource#setLogWriter(java.io.PrintWriter)
  172. */
  173. public void setLogWriter(PrintWriter out) throws SQLException {
  174. if(getCurrentDataSource() != null){
  175. ​ getCurrentDataSource().setLogWriter(out);
  176. ​ }
  177. }
  178. /* (non-Javadoc)
  179. * @see javax.sql.CommonDataSource#setLoginTimeout(int)
  180. */
  181. public void setLoginTimeout(int seconds) throws SQLException {
  182. if(getCurrentDataSource() != null){
  183. ​ getCurrentDataSource().setLoginTimeout(seconds);
  184. ​ }
  185. }
  186. /* (non-Javadoc)
  187. * 该接口方法since 1.6
  188. * 不是所有的DataSource都实现有这个方法
  189. * @see java.sql.Wrapper#isWrapperFor(java.lang.Class)
  190. */
  191. public boolean isWrapperFor(Class<?> iface) throws SQLException {
  192. // if(getCurrentDataSource() != null){
  193. // return getCurrentDataSource().isWrapperFor(iface);
  194. // }
  195. return false;
  196. }
  197. /* (non-Javadoc)
  198. * 该接口方法since 1.6
  199. * 不是所有的DataSource都实现有这个方法
  200. * @see java.sql.Wrapper#unwrap(java.lang.Class)
  201. */
  202. public T unwrap(Class iface) throws SQLException {
  203. // if(getCurrentDataSource() != null){
  204. // return getCurrentDataSource().unwrap(iface);
  205. // }
  206. return null;
  207. }
  208. }

这个类实现了DataSource的标准接口,而最核心的部分是getConnection()方法的重载。下面具体阐述:

  • 1.实例变量 clusterDataSources 是一个DataSource 的 ArrayList它存储了多个数据库的DataSource实例,我们使用Spring的IOC功能,将多个DataSource注入到这个list中。
  • 2.实例变量 mappedDataSources 是一个DataSource 的Map,它与clusterDataSources 一样用来存储多个数据库的DataSource实例,不同的是,它可以使用key直接获取DataSource。我们一样会使用Spring的IOC功能,将多个DataSource注入到这个Map中。
  • 3.实例变量currentDataSourceHolder ,他是一个ThreadLocal变量,保存与当前线程相关的且已经取得的DataSource实例。这是为了在同一线程中,多次访问同一数据库时,不需要再重新做路由选择。
  • 4.当外部类调用getConnection()方法时,方法将根据上下文的路由规则,从clusterDataSources 或者 mappedDataSources 选择对应DataSource,并返回其中的Connection。

(PS:关于DataSource的路由选择规则,可以根据应用场景的不同,自行设计。笔者这里提供两种简单的思路,1.根据HashCode,在上述例子中可以是UserId,进行取模运算,来定位数据库。2.根据上下文设置的关键字key,从map中选择映射的DataSource)

DataSourceRouter.class:

  1. /**
  2. * @author linliangyi2005@gmail.com
  3. * Jul 15, 2010
  4. */
  5. public class DataSourceRouter {
  6. public static ThreadLocal currentRouterStrategy =
  7. new ThreadLocal();
  8. /**
  9. * 设置MultiDataSource的路由策略
  10. * @param type
  11. * @param key
  12. * @param routeFactor
  13. */
  14. public static void setRouterStrategy(String type , String key , int routeFactor){
  15. if(type == null){
  16. throw new IllegalArgumentException("RouterStrategy Type must not be null");
  17. ​ }
  18. ​ RouterStrategy rs = currentRouterStrategy.get();
  19. if(rs == null){
  20. ​ rs = new RouterStrategy();
  21. ​ currentRouterStrategy.set(rs);
  22. ​ }
  23. ​ rs.setType(type);
  24. ​ rs.setKey(key);
  25. ​ rs.setRouteFactor(routeFactor);
  26. }
  27. /**
  28. * 数据源路由策略
  29. * @author linliangyi2005@gmail.com
  30. * Jul 15, 2010
  31. */
  32. public static class RouterStrategy{
  33. public static final String SRATEGY_TYPE_MAP = "MAP";
  34. public static final String SRATEGY_TYPE_CLUSTER = "CLUSTER";
  35. ​ /*
  36. ​ * 可选值 “MAP” , “CLUSTER”
  37. ​ * MAP : 根据key从DataSourceMap中选中DS
  38. ​ * CLUSTER : 根据routeFactor参数,通过算法获取群集
  39. ​ */
  40. private String type;
  41. ​ /*
  42. ​ * “MAP” ROUTE 中的key
  43. ​ *
  44. ​ */
  45. private String key;
  46. ​ /*
  47. ​ * "CLUSTER" ROUTE时的参数
  48. ​ */
  49. private int routeFactor;
  50. ​ /*
  51. ​ * True表示RouterStrategy更新过
  52. ​ * False表示没有更新
  53. ​ */
  54. private boolean refresh;
  55. public String getType() {
  56. return type;
  57. ​ }
  58. public void setType(String type) {
  59. if(this.type != null && !this.type.equals(type)){
  60. this.type = type;
  61. this.refresh = true;
  62. ​ }else if(this.type == null && type != null){
  63. this.type = type;
  64. this.refresh = true;
  65. ​ }
  66. ​ }
  67. public String getKey() {
  68. return key;
  69. ​ }
  70. public void setKey(String key) {
  71. if(this.key != null && !this.key.equals(key)){
  72. this.key = key;
  73. this.refresh = true;
  74. ​ }else if(this.key == null && key != null){
  75. this.key = key;
  76. this.refresh = true;
  77. ​ }
  78. ​ }
  79. public int getRouteFactor() {
  80. return routeFactor;
  81. ​ }
  82. public void setRouteFactor(int routeFactor) {
  83. if(this.routeFactor != routeFactor){
  84. this.routeFactor = routeFactor;
  85. this.refresh = true;
  86. ​ }
  87. ​ }
  88. public boolean isRefresh() {
  89. return refresh;
  90. ​ }
  91. public void setRefresh(boolean refresh) {
  92. this.refresh = refresh;
  93. ​ }
  94. }
  95. }

5.将MultiDataSource与Spring,iBatis结合

在完成了上述的编码过程后,就是将这个MultiDataSource与现有Spring和iBatis结合起来配置。

STEP 1。配置多个数据源

笔者这里使用了C3P0作为数据库连接池,这一步和标准的Spring配置一样,唯一不同的是,以前只配置一个,现在要配置多个

Xml代码

  1. <bean id="c3p0_dataSource_1" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
  2. <property name="driverClass">
  3. ${jdbc.driverClass}
  4. <property name="jdbcUrl">
  5. ${mysql.url_1}
  6. <property name="user">
  7. ${jdbc.username}
  8. <property name="password">
  9. ${jdbc.password}
  10. <property name="minPoolSize">
  11. ${c3p0.minPoolSize}
  12. <property name="maxPoolSize">
  13. ${c3p0.maxPoolSize}
  14. <property name="initialPoolSize">
  15. ${c3p0.initialPoolSize}
  16. <property name="idleConnectionTestPeriod">
  17. ${c3p0.idleConnectionTestPeriod}
  18. <bean id="c3p0_dataSource_2" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
  19. <property name="driverClass">
  20. ${jdbc.driverClass}
  21. <property name="jdbcUrl">
  22. ${mysql.url_2}
  23. <property name="user">
  24. ${jdbc.username}
  25. <property name="password">
  26. ${jdbc.password}
  27. <property name="minPoolSize">
  28. ${c3p0.minPoolSize}
  29. <property name="maxPoolSize">
  30. ${c3p0.maxPoolSize}
  31. <property name="initialPoolSize">
  32. ${c3p0.initialPoolSize}
  33. <property name="idleConnectionTestPeriod">
  34. ${c3p0.idleConnectionTestPeriod}
  35. ......

STEP 2。将多个数据源都注入到MultiDataSource中

Xml代码

  1. <bean id="multiDataSource" class="com.xxx.sql.MultiDataSource">
  2. <property name="clusterDataSources">
  3. <ref bean="c3p0_dataSource_1" />
  4. <ref bean="c3p0_dataSource_2" />
  5. <ref bean="c3p0_dataSource_3" />
  6. <ref bean="c3p0_dataSource_4" />
  7. <ref bean="c3p0_dataSource_5" />
  8. <ref bean="c3p0_dataSource_6" />
  9. <ref bean="c3p0_dataSource_7" />
  10. <ref bean="c3p0_dataSource_8" />
  11. <property name="mappedDataSources">
  12. <entry key="system" value-ref="c3p0_dataSource_system" />

STEP 3。像使用标准的DataSource一样,使用MultiDataSource

Xml代码

  1. <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
  2. <property name="configLocation" value="classpath:SqlMapConfig.xml"/>
  3. <property name="dataSource" ref="multiDataSource">
  4. <bean id="jdbc_TransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  5. <property name="dataSource" ref="multiDataSource">

至此,我们的程序就可以让Spring来管理多库访问了,但请注意,数据库事务仍然限于单库范围(之前已经说过,这里的应用场景不存在跨库的事务)。

6.Java代码使用例子

首先要说明的是,这里我们只是提供了一个简单的使用范例,在范例中,我们还必须手动的调用API,以确定DataSource的路由规则,在实际的应用中,您可以针对自己的业务特点,对此进行封装,以实现相对透明的路由选择

Java代码

  1. public boolean addUserGameInfo(UserGameInfo userGameInfo){
  2. //1.根据UserGameInfo.uid 进行数据源路由选择
  3. DataSourceRouter.setRouterStrategy(
  4. ​ RouterStrategy.SRATEGY_TYPE_CLUSTER ,
  5. null,
  6. ​ userGameInfo.getUid());
  7. //2.数据库存储
  8. try {
  9. ​ userGameInfoDAO.insert(userGameInfo);
  10. return true;
  11. } catch (SQLException e) {
  12. ​ e.printStackTrace();
  13. ​ logger.debug("Insert UserGameInfo failed. " + userGameInfo.toString());
  14. }
  15. return false;
  16. }
分类:
后端
标签: