首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

【转】运用spring的动态路由实现数据库负载均衡

2013-03-22 
【转】使用spring的动态路由实现数据库负载均衡使用spring的动态路由实现数据库负载均衡?系统中存在的多台服

【转】使用spring的动态路由实现数据库负载均衡

使用spring的动态路由实现数据库负载均衡

?

系统中存在的多台服务器是“地位相当”的,不过,同一时间他们都处于活动(Active)状态,处于负载均衡等因素考虑,数据访问请求需要在这几台数据库服务器之间进行合理分配, 这个时候,通过统一的一个DataSource来屏蔽这种请求分配的需求,从而屏蔽数据访问类与具体DataSource的耦合;

系统中存在的多台数据库服务器现在地位可能相当也可能不相当,但数据访问类在系统启动时间无法明确到底应该使用哪一个数据源进行数据访问,而必须在系统运行期间通过某种条件来判定到底应该使用哪一个数据源,这个时候,我们也得使用这种“合纵连横”的方式向数据访问类暴露一个统一的DataSource,由该DataSource来解除数据访问类与具体数据源之间的过紧耦合;
更多场景需要读者根据具体的应用来判定,不过,并非所有的应用要做这样的处理,如果能够保持简单,那尽量保持简单.要实现这种“合纵连横”的多数据源管理方式,总的指导原则就是实现一个自定义的DataSource,让该DataSource来管理系统中存在的多个与具体数据库挂钩的数据源, 数据访问类只跟这个自定义的DataSource打交道即可。在spring2.0.1发布之前,各个项目中可能存在多种针对这种情况下的多数据源管理方式, 不过,spring2.0.1发布之后,引入了AbstractRoutingDataSource,使用该类可以实现普遍意义上的多数据源管理功能。

假设我们有三台数据库用来实现负载均衡,所有的数据访问请求最终需要平均的分配到这三台数据库服务器之上,那么,我们可以通过继承AbstractRoutingDataSource来快速实现一个满足这样场景的原型(Prototype):

?

[java]?view plaincopy
  1. public?class?PrototypeLoadBalanceDataSource?extends?AbstractRoutingDataSource??{??
  2. ????private?Lock?lock?=?new?ReentrantLock();??
  3. ????private?int?counter?=?0;??
  4. ????private?int?dataSourceNumber?=?3;??
  5. ????@Override??
  6. ????protected?Object?determineCurrentLookupKey()?{??
  7. ????????lock.lock();??
  8. ????????try{??
  9. ????????????counter++;??
  10. ????????????int?lookupKey?=?counter?%?getDataSourceNumber();??
  11. ????????????return?new?Integer(lookupKey);??
  12. ????????}finally{??
  13. ????????????lock.unlock();??
  14. ????????}??
  15. ????}??
  16. ????//?...??
  17. }??

我们在介绍AbstractRoutingDataSource的时候说过,要继承该类,通常只需要给出determineCurrentLookupKey()方法的逻辑即可。 下面是针对PrototypeLoadBalanceDataSource的配置:

[html]?view plaincopy
  1. <bean?id="dataSourc1"?class="org.apache.commons.dbcp.BasicDataSource"?destroy-method="close">??
  2. ????<property?name="url"?value=".."/>??
  3. ????<property?name="driverClassName"?value=".."/>??
  4. ????<property?name="username"?value=".."/>??
  5. ????<property?name="password"?value=".."/>??
  6. ????<!--?other?property?settings?-->??
  7. </bean>??
  8. <bean?id="dataSource2"?class="org.apache.commons.dbcp.BasicDataSource"?destroy-method="close">??
  9. ????<property?name="url"?value=".."/>??
  10. ????<property?name="driverClassName"?value=".."/>??
  11. ????<property?name="username"?value=".."/>??
  12. ????<property?name="password"?value=".."/>??
  13. ????<!--?other?property?settings?-->??
  14. </bean>??
  15. <bean?id="dataSource3"?class="org.apache.commons.dbcp.BasicDataSource"?destroy-method="close">??
  16. ????<property?name="url"?value=".."/>??
  17. ????<property?name="driverClassName"?value=".."/>??
  18. ????<property?name="username"?value=".."/>??
  19. ????<property?name="password"?value=".."/>??
  20. ????<!--?other?property?settings?-->??
  21. </bean>??
  22. <util:map?id="dataSources">??
  23. ????<entry?key="0"?value-ref="dataSource1"/>??
  24. ????<entry?key="1"?value-ref="dataSource2"/>??
  25. ????<entry?key="2"?value-ref="dataSource3"/>??
  26. </util:map>??
  27. <bean?id="dataSourceLookup"?class="org.springframework.jdbc.datasource.lookup.MapDataSourceLookup">??
  28. ????<constructor-arg>??
  29. ????????<ref?bean="dataSources"/>??
  30. ????</constructor-arg>??
  31. </bean>??
  32. <bean?id="dataSource"?class="..PrototypeLoadBalanceDataSource">??
  33. ????<property?name="defaultTargetDataSource"?ref="dataSourc1"/>??
  34. ????<property?name="targetDataSources"?ref="dataSources"/>??
  35. ????<property?name="dataSourceLookup"?ref=""/>??
  36. </bean>??
  37. <bean?id="jdbcTemplate"?class="org.springframework.jdbc.core.JdbcTemplate">??
  38. ????<property?name="dataSource"?ref="dataSource"/>??
  39. </bean>??
  40. <bean?id="someDao"?class="...">??
  41. ????<property?name=""jdbcTemplate""?ref=""jdbcTemplate""/>??
  42. ????<!--?other?property?settings?-->??
  43. </bean>??

热点排行