標籤:target ruid tail l資料庫 ml2 replicate tor word image
一、前言
分布式環境下資料庫的讀寫分離策略是解決資料庫讀寫效能瓶頸的一個關鍵解決方案,更是最大限度了提高了應用中讀取 (Read)資料的速度和並發量。
在進行資料庫讀寫分離的時候,我們首先要進行資料庫的主從配置,最簡單的是一台Master和一台Slave(大型網站系統的話,當然會很複雜,這裡只是分析了最簡單的情況)。通過主從配置主從資料庫保持了相同的資料,我們在進行讀操作的時候訪問從資料庫Slave,在進行寫操作的時候訪問主要資料庫Master。這樣的話就減輕了一台伺服器的壓力。
在進行讀寫分離案例分析的時候。首先,設定資料庫的主從複製,下邊是兩種方法(任選其一即可):
1、MySQL5.6 資料庫主從(Master/Slave)同步安裝與配置詳解
2、使用mysqlreplicate命令快速搭建 Mysql 主從複製
當然,只是簡單的為了看一下如何用代碼的方式實現資料庫的讀寫分離,完全不必要去配置主從資料庫,只需要兩台安裝了 相同資料庫的機器就可以了。
二、實現讀寫分離的兩種方法
具體到開發中,實現讀寫分離常用的有兩種方式:
1、第一種方式是我們最常用的方式,就是定義2個資料庫連接,一個是MasterDataSource,另一個是SlaveDataSource。更新資料時我們讀取MasterDataSource,查詢資料時我們讀取SlaveDataSource。這種方式很簡單,我就不贅述了。
2、第二種方式動態資料源切換,就是在程式運行時,把資料來源動態織入到程式中,從而選擇讀取主庫還是從庫。主要使用的技術是:Annotation,Spring AOP ,反射。
下面會詳細的介紹實現方式。
三、Aop實現主從資料庫的讀寫分離案例
1、項目代碼地址
目前該Demo的項目地址在開源中國 碼雲 上邊:http://git.oschina.net/xuliugen/aop-choose-db-demo
或者CSDN免費下載:
http://download.csdn.net/detail/u010870518/9724872
2、項目結構
中,除了標記的代碼,其他的主要是配置代碼和業務代碼。
3、具體分析
該項目是SSM架構的一個demo,Spring、Spring MVC和MyBatis,具體的設定檔不在過多介紹。
(1)UserContoller類比讀寫資料
類比讀寫資料,調用IUserService 。
(2)spring-db.xml讀寫資料來源配置
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <beanid="statFilter" class="com.alibaba.druid.filter.stat.StatFilter" lazy-init="true"> <property name="logSlowSql" value="true"/> <property name="mergeSql"value="true"/> </bean> <!-- 資料庫連接 --> <bean id="readDataSource"class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close" init-method="init" lazy-init="true"> <property name="driverClassName"value="${driver}"/> <property name="url" value="${url1}"/> <propertyname="username" value="root"/> <property name="password"value="${password}"/> <!-- 省略部分內容 --> </bean> <beanid="writeDataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close" init-method="init" lazy-init="true"> <propertyname="driverClassName" value="${driver}"/> <property name="url"value="${url}"/> <property name="username" value="root"/> <propertyname="password" value="${password}"/> <!-- 省略部分內容 --> </bean> <!-- 配置動態分配的讀寫 資料來源 --> <bean id="dataSource"class="com.xuliugen.choosedb.demo.aspect.ChooseDataSource" lazy-init="true"> <property name="targetDataSources"> <map key-type="java.lang.String"value-type="javax.sql.DataSource"> <!-- write --> <entrykey="write" value-ref="writeDataSource"/> <!-- read --> <entrykey="read" value-ref="readDataSource"/> </map> </property> <property name="defaultTargetDataSource" ref="writeDataSource"/> <property name="methodType"> <map key-type="java.lang.String"> <!-- read --> <entry key="read" value=",get,select,count,list,query"/> <!-- write --> <entry key="write"value=",add,create,update,delete,remove,"/> </map> </property> </bean></beans>
上述配置中,配置了readDataSource和writeDataSource兩個資料來源,但是交給SqlSessionFactoryBean進行管理的只有dataSource,其中使用到了:com.xuliugen.choosedb.demo.aspect.ChooseDataSource 這個是進行資料庫選擇的。
(3)ChooseDataSource
(4)DataSourceAspect進行具體方法的AOP攔截
(5)DataSourceHandler,資料來源的Handler類
161920、使用Spring AOP實現MySQL資料庫讀寫分離案例分析