一、創(chuàng)建基于ThreadLocal的動(dòng)態(tài)數(shù)據(jù)源容器,保證數(shù)據(jù)源的線程安全性
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package com.bounter.mybatis.extension; /** * 基于ThreadLocal實(shí)現(xiàn)的動(dòng)態(tài)數(shù)據(jù)源容器,保證DynamicDataSource的線程安全性 * @author simon * */ public class DynamicDataSourceHolder { private static final ThreadLocal<String> dataSourceHolder = new ThreadLocal<>(); public static void setDataSource(String dataSourceKey) { dataSourceHolder.set(dataSourceKey); } public static String getDataSource() { return dataSourceHolder.get(); } public static void clearDataSource() { dataSourceHolder.remove(); } } |
二、定義Spring動(dòng)態(tài)數(shù)據(jù)源擴(kuò)展類,用來(lái)實(shí)現(xiàn)Master、Slave數(shù)據(jù)源動(dòng)態(tài)切換
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
package com.bounter.mybatis.extension; import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; /** * 自定義的Spring 動(dòng)態(tài)數(shù)據(jù)源擴(kuò)展類,用來(lái)實(shí)現(xiàn)Master、Slave數(shù)據(jù)源動(dòng)態(tài)切換 * @author simon * */ public class DynamicDataSource extends AbstractRoutingDataSource { @Override protected Object determineCurrentLookupKey() { //使用DynamicDataSourceHolder保證線程安全 return DynamicDataSourceHolder.getDataSource(); } } |
三、配置Master、Slave數(shù)據(jù)源
1. db.properties配置Master、Slave數(shù)據(jù)信息
1
2
3
4
5
6
7
8
9
10
11
|
# Master DB db.master.url=jdbc:mysql://192.168.168.110:3306/bounter?useUnicode= true &characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowMultiQueries= true &serverTimezone=PRC&useSSL= false db.master.username=bounter # AES encrypt,Base64 encode db.master. password =ZNhnEjauk3pecZxxS84ofA== # Slave DB db.slave.url=jdbc:mysql://192.168.168.111:3306/ database ?useUnicode= true &characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowMultiQueries= true &serverTimezone=PRC&useSSL= false db.slave.username=bounter # AES encrypt,Base64 encode db.slave. password =jFYmt2f57RHhzItYDhWiSA== |
2. Spring 配置文件配置Master、Slave連接池,動(dòng)態(tài)數(shù)據(jù)源
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
<!-- Master數(shù)據(jù)源 --> <bean id= "masterDataSource" class = "com.alibaba.druid.pool.DruidDataSource" init-method= "init" destroy-method= "close" > <!-- 基本屬性 url、user、password --> <property name= "url" value= "${db.master.url}" /> <property name= "username" value= "${db.master.username}" /> <property name= "password" value= "${db.master.password}" /> <!-- 配置初始化大小、最小、最大 --> <property name= "initialSize" value= "20" /> <property name= "minIdle" value= "1" /> <property name= "maxActive" value= "40" /> <!-- 配置獲取連接等待超時(shí)的時(shí)間 --> <property name= "maxWait" value= "60000" /> <!-- 配置間隔多久才進(jìn)行一次檢測(cè),檢測(cè)需要關(guān)閉的空閑連接,單位是毫秒 --> <property name= "timeBetweenEvictionRunsMillis" value= "60000" /> <!-- 配置一個(gè)連接在池中最小生存的時(shí)間,單位是毫秒 --> <property name= "minEvictableIdleTimeMillis" value= "300000" /> <property name= "validationQuery" value= "SELECT 'x'" /> <property name= "testWhileIdle" value= "true" /> <property name= "testOnBorrow" value= "false" /> <property name= "testOnReturn" value= "false" /> <!-- 配置監(jiān)控統(tǒng)計(jì)攔截的filters --> <property name= "filters" value= "stat" /> </bean> <!-- Slave數(shù)據(jù)源 --> <bean id= "slaveDataSource" class = "com.alibaba.druid.pool.DruidDataSource" init-method= "init" destroy-method= "close" > <!-- 基本屬性 url、user、password --> <property name= "url" value= "${db.slave.url}" /> <property name= "username" value= "${db.slave.username}" /> <property name= "password" value= "${db.slave.password}" /> <!-- 配置初始化大小、最小、最大 --> <property name= "initialSize" value= "20" /> <property name= "minIdle" value= "1" /> <property name= "maxActive" value= "40" /> <!-- 配置獲取連接等待超時(shí)的時(shí)間 --> <property name= "maxWait" value= "60000" /> <!-- 配置間隔多久才進(jìn)行一次檢測(cè),檢測(cè)需要關(guān)閉的空閑連接,單位是毫秒 --> <property name= "timeBetweenEvictionRunsMillis" value= "60000" /> <!-- 配置一個(gè)連接在池中最小生存的時(shí)間,單位是毫秒 --> <property name= "minEvictableIdleTimeMillis" value= "300000" /> <property name= "validationQuery" value= "SELECT 'x'" /> <property name= "testWhileIdle" value= "true" /> <property name= "testOnBorrow" value= "false" /> <property name= "testOnReturn" value= "false" /> <!-- 配置監(jiān)控統(tǒng)計(jì)攔截的filters --> <property name= "filters" value= "stat" /> </bean> <!-- 自定義動(dòng)態(tài)數(shù)據(jù)源 --> <bean id= "dataSource" class = "com.bounter.mybatis.extension.DynamicDataSource" > <property name= "targetDataSources" > <map key-type= "java.lang.String" > <!-- 配置讀寫數(shù)據(jù)源 --> <entry value-ref= "masterDataSource" key= "write" ></entry> <entry value-ref= "slaveDataSource" key= "read" ></entry> </map> </property> <property name= "defaultTargetDataSource" ref= "masterDataSource" ></property> </bean> |
四、創(chuàng)建數(shù)據(jù)源切面,通過AOP實(shí)現(xiàn)根據(jù)Dao層方法前綴動(dòng)態(tài)選取讀、寫數(shù)據(jù)源
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
package com.bounter.mybatis.aop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.context.annotation.EnableAspectJAutoProxy; import org.springframework.stereotype.Component; import com.bounter.mybatis.extension.DynamicDataSourceHolder; /** * 數(shù)據(jù)源切面,通過dao方法前綴決定訪問讀、寫數(shù)據(jù)源 * @author simon * */ @Component @Aspect @EnableAspectJAutoProxy (proxyTargetClass = true ) public class DataSourceAspect { //讀庫(kù)數(shù)據(jù)源key private static final String DATASOURCE_KEY_READ = "read" ; //查詢方法清單 String[] queryMethods = { "find" , "get" , "query" , "count" , "select" }; /** * dao層方法執(zhí)行前選擇數(shù)據(jù)源 * @param point */ @Before ( "execution(* com.bounter.mybatis.dao..*.*(..))" ) public void before(JoinPoint point) { // 獲取到當(dāng)前執(zhí)行的方法名 String methodName = point.getSignature().getName(); //匹配查詢方法 for (String queryMethod : queryMethods) { if (methodName.startsWith(queryMethod)) { //查詢方法設(shè)置數(shù)據(jù)源為讀庫(kù) DynamicDataSourceHolder.setDataSource(DATASOURCE_KEY_READ); break ; } } } /** * dao層方法執(zhí)行完后清空數(shù)據(jù)源選擇 * @param point */ @After ( "execution(* com.bounter.mybatis.dao..*.*(..))" ) public void after(JoinPoint point) { DynamicDataSourceHolder.clearDataSource(); } } |
github源碼地址:https://github.com/13babybear/bounter-mybatis
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。