????? 當(dāng)系統(tǒng)數(shù)據(jù)量發(fā)展到一定程度后,往往需要進(jìn)行數(shù)據(jù)庫的垂直切分和水平切分,以實(shí)現(xiàn)負(fù)載均衡和性能提升,而數(shù)據(jù)切分后隨之會(huì)帶來多數(shù)據(jù)源整合等等問題。如果僅僅從應(yīng)用程序的角度去解決這類問題,無疑會(huì)加重應(yīng)用程度的復(fù)雜度,因此需要一個(gè)成熟的第三方解決方案。
Amoeba正是解決此類問題的一個(gè)開源方案,Amoeba位于應(yīng)用程序和數(shù)據(jù)庫服 務(wù)器之間,相當(dāng)于提供了一個(gè)代理,使得應(yīng)用程序只要連接一個(gè)Amoeba,相當(dāng)于只是在操作一個(gè)單獨(dú)的數(shù)據(jù)庫服務(wù)器,而實(shí)際上卻是在操作多個(gè)數(shù)據(jù)庫服務(wù) 器,這中間的工作全部交由Amoeba去完成。
本文針對基于MySQL的水平切分的實(shí)現(xiàn)機(jī)制,講解Amoeba For MySQL的簡單應(yīng)用。
?
一、背景介紹
使用數(shù)據(jù)庫:MySQL
數(shù)據(jù)庫節(jié)點(diǎn)1:127.0.0.1
數(shù)據(jù)庫節(jié)點(diǎn)2:10.167.157.176
?
數(shù)據(jù)庫名:yunzhu
切分的表:用戶表(user_info)
切分的參數(shù):用戶ID(USERID)
?
切分規(guī)則:
用戶ID小于等于100的數(shù)據(jù)存到數(shù)據(jù)庫節(jié)點(diǎn)1,
用戶ID大于100的數(shù)據(jù)存到數(shù)據(jù)庫節(jié)點(diǎn)2
?
user_info表結(jié)構(gòu)如下:
- CREATE ? TABLE ?`user_info`?(??
- ????`USERID`? INT (10)? NOT ? NULL ? DEFAULT ? '0' ,??
- ????`USERNAME`? VARCHAR (50)? NULL ? DEFAULT ? NULL ,??
- ???? PRIMARY ? KEY ?(`USERID`)??
- )??
數(shù)據(jù)庫節(jié)點(diǎn)1中user_info表中的數(shù)據(jù):
+--------+-----------+ | USERID | USERNAME? | +--------+-----------+ |???? 73 | Chen Feng | |???? 88 | China???? | +--------+-----------+
數(shù)據(jù)庫節(jié)點(diǎn)1中user_info表中的數(shù)據(jù):
+--------+----------+ | USERID | USERNAME | +--------+----------+ |??? 108 | Jiang Su | |??? 200 | NanJing? | +--------+----------+
?
Amoeba版本:
amoeba-mysql-binary-2.2.0
?
下載地址:
下載后直接解壓即可使用
?
二、配置Amoeba
配置文件全部位于conf目錄下
1、amoeba.xml
配置連接Amoeba程序的用戶名和密碼:
- < property ? name = "user" > root </ property > ??
- < property ? name = "password" > chenfeng123 </ property > ??
2、dbServers.xml
先配置一個(gè)抽象的父節(jié)點(diǎn),定義多個(gè)數(shù)據(jù)庫節(jié)點(diǎn)的共通的信息,包括數(shù)據(jù)庫節(jié)點(diǎn)的端口、schema、用戶名和密碼:
- < dbServer ? name = "abstractServer" ? abstractive = "true" > ??
- ???? < factoryConfig ? class = "com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory" > ??
- ????????......??
- ??????????????
- ???????? <!--?mysql?port?--> ??
- ???????? < property ? name = "port" > 3306 </ property > ??
- ??????????
- ???????? <!--?mysql?schema?--> ??
- ???????? < property ? name = "schema" > yunzhu </ property > ??
- ??????????
- ???????? <!--?mysql?user?--> ??
- ???????? < property ? name = "user" > root </ property > ??
- ??????????
- ???????? <!--??mysql?password?--> ??
- ???????? < property ? name = "password" > chenfeng </ property > ??
- ???? </ factoryConfig > ??
- ??????......??
- </ dbServer > ??
? 再配置兩個(gè)數(shù)據(jù)庫節(jié)點(diǎn),繼承上面的父節(jié)點(diǎn),然后配置各自的IP地址即可:
- < dbServer ? name = "server1" ?? parent = "abstractServer" > ??
- ???? < factoryConfig > ??
- ???????? <!--?mysql?ip?--> ??
- ???????? < property ? name = "ipAddress" > 127.0.0.1 </ property > ??
- ???? </ factoryConfig > ??
- </ dbServer > ??
- ??
- < dbServer ? name = "server2" ?? parent = "abstractServer" > ??
- ???? < factoryConfig > ??
- ???????? <!--?mysql?ip?--> ??
- ???????? < property ? name = "ipAddress" > 10.167.157.176 </ property > ??
- ???? </ factoryConfig > ??
- </ dbServer > ??
3、rule.xml
? 配置切分規(guī)則:
???? 1、schema指定數(shù)據(jù)庫名,name指定表名,defaultPools指定關(guān)聯(lián)的數(shù)據(jù)庫節(jié)點(diǎn)(指定哪幾個(gè)節(jié)點(diǎn)就從哪幾個(gè)節(jié)點(diǎn)里面查數(shù)據(jù))
???? 2、切分規(guī)則:
???????? 1)用戶ID小于100的數(shù)據(jù)存到數(shù)據(jù)庫節(jié)點(diǎn)1
???????? 2)用戶ID大于100的數(shù)據(jù)存到數(shù)據(jù)庫節(jié)點(diǎn)2
- < amoeba:rule ? xmlns:amoeba = "http://amoeba.meidusa.com/" > ??
- ???? < tableRule ? name = "user_info" ? schema = "yunzhu" ? defaultPools = "server1,server2" > ??
- ???????? < rule ? name = "rule1" > ??
- ???????????? < parameters > USERID </ parameters > ??
- ???????????? < expression > <![CDATA[?USERID?<=?100]]> </ expression > ??
- ???????????? < defaultPools > server1 </ defaultPools > ??
- ???????????? < readPools > server1 </ readPools > ??
- ???????????? < writePools > server1 </ writePools > ??
- ???????? </ rule > ??
- ???????? < rule ? name = "rule2" > ??
- ???????????? < parameters > USERID </ parameters > ??
- ???????????? < expression > <![CDATA[?USERID?>?100?]]> </ expression > ??
- ???????????? < defaultPools > server2 </ defaultPools > ??
- ???????????? < writePools > server2 </ writePools > ??
- ???????????? < readPools > server2 </ readPools > ??
- ???????? </ rule > ??
- ???? </ tableRule > ????????
- </ amoeba:rule > ??
?
三、運(yùn)行及驗(yàn)證
1、啟動(dòng)Amoeba
通過bin目錄下的amoeba.bat啟動(dòng):
?
- amoeba?start??
?
啟動(dòng)后控制臺(tái)打印如下信息,可以看到:
- log4j:WARN?log4j?config?load?completed?from?file:D:\JavaTools\amoeba-mysql-binar??
- y- 2.2 . 0 \bin\..\conf\log4j.xml??
- 2013 - 01 - 08 ? 09 : 32 : 27 , 765 ?INFO??context.MysqlRuntimeContext?-?Amoeba?for?Mysql?cur??
- rent?versoin= 5.1 . 45 -mysql-amoeba-proxy- 2.2 . 0 ??
- log4j:WARN?ip?access?config?load?completed?from?file:D:\JavaTools\amoeba-mysql-b??
- inary- 2.2 . 0 \bin\../conf/access_list.conf??
- 2013 - 01 - 08 ? 09 : 32 : 27 , 921 ?INFO??net.ServerableConnectionManager?-?Amoeba?for?Mysql??
- ?listening?on? 0.0 . 0.0 / 0.0 . 0.0 : 8066 .??
- 2013 - 01 - 08 ? 09 : 32 : 27 , 921 ?INFO??net.ServerableConnectionManager?-?Amoeba?Monitor?S??
- erver?listening?on?/ 127.0 . 0.1 : 40170 .??
2、連接Amoeba
通過mysql客戶端連接Amoeba,端口指定為8066,然后還像以前操作MySQL一樣進(jìn)行操作:
- D:\>mysql?-P8066?-uroot?-pchenfeng123??
- Welcome?to?the?MySQL?monitor.??Commands?end?with?;?or?\g.??
- Your?MySQL?connection?id?is? 21616774 ?to?server?version:? 5.1 . 45 -mysql-amoeba-prox??
- y- 2.2 . 0 ??
- ??
- Type? 'help;' ?or? '\h' ?for?help.?Type? '\c' ?to?clear?the?buffer.??
- ??
- mysql>??
3、驗(yàn)證數(shù)據(jù)的查詢
查詢yunzhu庫下的user_info表的數(shù)據(jù),如下:
- mysql>? select ?*? from ?yunzhu.user_info;??
- + --------+-----------+ ??
- |?USERID?|?USERNAME??|??
- + --------+-----------+ ??
- |????108?|?Jiang?Su??|??
- |????200?|?NanJing???|??
- |?????73?|?Chen?Feng?|??
- |?????88?|?China?????|??
- + --------+-----------+ ??
- 4? rows ? in ? set ?(0.02?sec)??
可以看到,現(xiàn)在查到了兩個(gè)數(shù)據(jù)庫節(jié)點(diǎn)中的user_info表中的所有記錄。
?
4、驗(yàn)證數(shù)據(jù)的插入
這里插入兩條數(shù)據(jù),一條USERID為55,另一條USERID為155,如下:
- mysql>?insert?into?yunzhu.user_info(USERID,USERNAME)?values( 55 , 'test55' );??
- Query?OK,? 1 ?row?affected?( 0.13 ?sec)??
- ??
- mysql>?insert?into?yunzhu.user_info(USERID,USERNAME)?values( 155 , 'test155' );??
- Query?OK,? 1 ?row?affected?( 0.05 ?sec)??
?
查詢數(shù)據(jù)庫節(jié)點(diǎn)1:
- D:\>mysql?-uroot?-pchenfeng??
- Welcome?to?the?MySQL?monitor.??Commands?end?with?;?or?\g.??
- Your?MySQL?connection?id?is? 33 ?to?server?version:? 5.0 . 18 -nt??
- ??
- Type? 'help;' ?or? '\h' ?for?help.?Type? '\c' ?to?clear?the?buffer.??
- ??
- mysql>?select?*?from?yunzhu.user_info;??
- +--------+-----------+??
- |?USERID?|?USERNAME??|??
- +--------+-----------+??
- |????? 55 ?|?test55????|??
- |????? 73 ?|?Chen?Feng?|??
- |????? 88 ?|?China?????|??
- +--------+-----------+??
- 3 ?rows?in?set?( 0.00 ?sec)??
?
查詢數(shù)據(jù)庫節(jié)點(diǎn)2:
- D:\>mysql?-uroot?-pchenfeng?-h10. 167.157 . 176 ??
- Welcome?to?the?MySQL?monitor.??Commands?end?with?;?or?\g.??
- Your?MySQL?connection?id?is? 34 ?to?server?version:? 5.0 . 18 -nt??
- ??
- Type? 'help;' ?or? '\h' ?for?help.?Type? '\c' ?to?clear?the?buffer.??
- ??
- mysql>?select?*?from?yunzhu.user_info;??
- +--------+----------+??
- |?USERID?|?USERNAME?|??
- +--------+----------+??
- |???? 108 ?|?Jiang?Su?|??
- |???? 155 ?|?test155??|??
- |???? 200 ?|?NanJing??|??
- +--------+----------+??
- 3 ?rows?in?set?( 0.00 ?sec)??
可以發(fā)現(xiàn)USERID為55的記錄插入到了數(shù)據(jù)庫節(jié)點(diǎn)1中,USERID為155的記錄插入到了數(shù)據(jù)庫節(jié)點(diǎn)2中。
因?yàn)楦鶕?jù)rule.xml中的切分規(guī)則,USERID小于等于100的的記錄存在數(shù)據(jù)庫節(jié)點(diǎn)1中,而大于100的則存在數(shù)據(jù)庫節(jié)點(diǎn)2中。
?
四、注意一些限制
這是我在實(shí)踐中發(fā)現(xiàn)的,剛開始不知道存在這樣的限制,以致于浪費(fèi)了很多時(shí)間,以為配置有問題,搞了很久才發(fā)現(xiàn)原來是因?yàn)檫@些限制才導(dǎo)致沒有出現(xiàn)預(yù)期的結(jié)果,所以必須要注意:
1、不管是查詢和插入,每條都必須顯式地指定數(shù)據(jù)庫名(yunzhu),否則只會(huì)從一個(gè)數(shù)據(jù)庫節(jié)點(diǎn)中查詢數(shù)據(jù),或者所有數(shù)據(jù)全部會(huì)插入一個(gè)數(shù)據(jù)庫節(jié)點(diǎn)中。
2、插入數(shù)據(jù)時(shí),必須顯式地指定列名,如“insert into yunzhu.user_info(USERID,USERNAME)”,否則切分規(guī)則不會(huì)生效,所有記錄都會(huì)插入到一個(gè)數(shù)據(jù)庫節(jié)點(diǎn)中。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
