mysql主從復(fù)制配置見(jiàn): http://www.aygfsteel.com/howard-he/
項(xiàng)目中使用mysql 主從復(fù)制,但是用程序?qū)崿F(xiàn)的讀寫(xiě)分離,代碼片段如下:
1
public DataSource getDataSource(MethodType methodType)
{
2
if (methodType == MethodType.WRITE)
{
3
return getDataSource(MasterDataSources);
4
} else
{
5
return getDataSource(SlaveDataSources);
6
}
7
}



2



3

4



5

6

7

獲取數(shù)據(jù)源,首先你要確定MethodType 類(lèi)型,一個(gè)是讀,一個(gè)是寫(xiě)
1
public enum MethodType
{
2
READ, WRITE
3
}



2

3

讀是獲取從庫(kù)數(shù)據(jù)源,寫(xiě)是獲取主庫(kù)數(shù)據(jù)源。
這樣,就需要在jdbc配置配置兩個(gè)數(shù)據(jù)源(一主一從)
還有另一種實(shí)現(xiàn)方式,不用程序來(lái)控制。mysql 驅(qū)動(dòng)包提供相應(yīng)的實(shí)現(xiàn) com.mysql.jdbc.ReplicationDriver.
我寫(xiě)了一個(gè)簡(jiǎn)單的例子:
1
package com.howard.loadbalance;
2
3
import java.sql.Connection;
4
import java.sql.ResultSet;
5
import java.sql.SQLException;
6
import java.sql.Statement;
7
import java.util.Properties;
8
import java.util.concurrent.ExecutorService;
9
import java.util.concurrent.Executors;
10
11
import org.slf4j.Logger;
12
import org.slf4j.LoggerFactory;
13
14
import com.mysql.jdbc.ReplicationDriver;
15
16
public class MysqlTest
{
17
18
public static class QueryRunnable implements Runnable
{
19
20
protected final static Logger logger = LoggerFactory
21
.getLogger(QueryRunnable.class);
22
23
@Override
24
public void run()
{
25
logger.info("user size: " + this.query());
26
}
27
28
public int query()
{
29
int count = 0;
30
try
{
31
ReplicationDriver driver = new ReplicationDriver();
32
Properties props = new Properties();
33
props.put("roundRobinLoadBalance", "true");
34
props.put("autoReconnect", "true");
35
props.put("user", "core");
36
props.put("password", "core");
37
Connection conn = null;
38
Statement stat = null;
39
ResultSet res = null;
40
try
{
41
conn = driver
42
.connect(
43
//注意url串中間不要有空格,因?yàn)閙ysql源碼對(duì)多個(gè)地址split時(shí)沒(méi)有trim.
44
"jdbc:mysql:replication://127.0.0.1:3309,127.0.0.1:3306/core",
45
props);
46
//讀寫(xiě)分離標(biāo)記
47
//當(dāng)設(shè)置true時(shí),只會(huì)在從庫(kù)查詢(xún)數(shù)據(jù)
48
//當(dāng)設(shè)置false時(shí),會(huì)在主庫(kù)做更新刪除操作
49
// conn.setReadOnly(true);
50
stat = conn.createStatement();
51
res = stat.executeQuery("select count(1) from c_user");
52
while (res.next())
53
count = res.getInt(1);
54
} finally
{
55
if (res != null)
56
res.close();
57
if (stat != null)
58
stat.close();
59
if (conn != null)
60
conn.close();
61
}
62
} catch (SQLException e)
{
63
e.printStackTrace();
64
}
65
return count;
66
}
67
}
68
69
public static void main(String[] args)
{
70
// 創(chuàng)建線程池測(cè)試負(fù)載軍衡
71
ExecutorService service = Executors.newCachedThreadPool();
72
for (int i = 0; i < 10; i++)
{
73
service.execute(new QueryRunnable());
74
}
75
service.shutdown();
76
}
77
78
}
79

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

62



63

64

65

66

67

68

69



70

71

72



73

74

75

76

77

78

79
