繼續上面的操做
<dbServer name="server2">
<!-- PoolableObjectFactory
實現類 -->
<factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">
<property name="manager">defaultManager</property>
<!--
真實mysql數據庫端口 -->
<property name="port">3306</property>
<!--
真實mysql數據庫IP -->
<property name="ipAddress">10.10.0.77</property>
<property name="schema">test</property>
<!--
用於登錄mysql的用戶名 -->
<property name="user">test</property>
<!--
用於登錄mysql的密碼 -->
<property name="password">123456</property>
</factoryConfig>
<!-- ObjectPool
實現類 -->
<poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool">
<property name="maxActive">200</property>
<property name="maxIdle">200</property>
<property name="minIdle">10</property>
<property name="minEvictableIdleTimeMillis">600000</property>
<property name="timeBetweenEvictionRunsMillis">600000</property>
<property name="testOnBorrow">true</property>
<property name="testWhileIdle">true</property>
</poolConfig>
</dbServer>
<dbServer name="master" virtual="true">
<poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
<!--
負載均衡參數 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->
<property name="loadbalance">1</property>
<!--
參與該pool負載均衡的poolName列表以逗號分割 -->
<property name="poolNames">server1</property>
</poolConfig>
</dbServer>
<dbServer name="slave" virtual="true">
<poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
<!--
負載均衡參數 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->
<property name="loadbalance">1</property>
<!--
參與該pool負載均衡的poolName列表以逗號分割 -->
property name="poolNames">server1,server2</property>
</poolConfig>
</dbServer>
</dbServerList>
<queryRouter class="com.meidusa.amoeba.mysql.parser.MysqlQueryRouter">
<property name="ruleConfig">${amoeba.home}/conf/rule.xml</property>
<property name="functionConfig">${amoeba.home}/conf/functionMap.xml</property>
<property name="ruleFunctionConfig">${amoeba.home}/conf/ruleFunctionMap.xml</property>
<property name="LRUMapSize">1500</property>
<property name="defaultPool">master</property>
<property name="writePool">master</property>
<property name="readPool">slave</property>
<property name="needParse">true</property>
</queryRouter>
</amoeba:configuration>
# /usr/local/amoeba/bin/amoeba & //啓動amoeba服務
# ps aux |grep /usr/local/amoeba
root 9116 0.0 1.0 460460 43028 pts/0 Sl 12:50 0:00 /usr/java/jdk1.5.0_16/bin/java -server -Xms256m -Xmx256m -Xss128k -Damoeba.home=/usr/local/amoeba -Dclassworlds.conf=/usr/local/amoeba/bin/amoeba.classworlds -classpath /usr/local/amoeba/lib/classworlds-1.0.jar org.codehaus.classworlds.Launcher
# netstat -lntpu |grep 8066
tcp 0 0 10.10.0.87:8066 :::*
看到進程以及端口說明amoeba服務啓動正常
測試mysql的讀寫分離
測試以前先要保證amoeba-server有訪問兩個主從服務器test庫的權限,在主從mysql上都執行:
mysql> flush privileges;
登陸amoeba服務器使用以下命令(用戶名密碼和上面配置的要一致):
# mysql -u root -psxit -h10.10.0.87 -P8066 //若是能夠登錄而且沒有任何錯誤就說
明是OK的.
登陸上去後,爲了測試讀和寫必須先把mysql的主從複製停掉,才能更清楚地看出讀寫的服務器是哪臺,在從上使用stop slave;登陸到amoeba-mysql上,使用命令mysql -uroot -psxit -h10.10.0.87 -P8066,而後執行寫和讀操做,查看寫的是哪臺服務器,讀的是哪臺服務器,實驗結果顯示:寫只在主上進行,讀在主和從都進行,比率是1:1
在沒有中止同步以前先在master服務器上建立一個表
mysql> create table sxit (id int(10) ,name varchar(10));
而後在slave服務器上中止數據同步
mysql> stop slave;
在主從上各插入一條不一樣數據(供測試讀的時候用):
在主上插入:insert into sxit values('1','zhangsan');
在從上插入:insert into sxit values('2','lisi');
登錄到amoeba服務器,進行讀寫分離的測試:
# mysql -u root -psxit -h10.10.0.87 -P8066
mysql> use test;
mysql> select * from sxit; //第一次執行select是在master服務器上查詢的
+------+----------+
| id | name |
+------+----------+
| 1 | zhangsan |
+------+----------+
1 row in set (0.00 sec)
mysql> select * from sxit; //第二次執行select是在slave服務器上查詢的
+------+------+
| id | name |
+------+------+
| 2 | lisi |
+------+------+
1 row in set (0.01 sec)
mysql> insert into sxit values('3','wangmazi'); //插入一條數據,而後在select
Query OK, 1 row affected (0.05 sec)
mysql> select * from sxit; //能夠看到上面新插入的語句在master上了
+------+----------+
| id | name |
+------+----------+
| 1 | zhangsan |
| 3 | wangmazi |
+------+----------+
2 rows in set (0.00 sec)
mysql> select * from sxit; //slave服務器上是沒有這條新的語句的,
+------+------+
| id | name |
+------+------+
| 2 | lisi |
+------+------+
1 row in set (0.00 sec)
權重的設置
在這裏可能會想到,兩臺數據庫服務器,一臺主,一臺從,按照上面的配置只能是主和從的讀取比率是1:1,而寫又所有在主上進行,這樣主的壓力就很大了,因此若是能讓主和從的讀設置權重,好比設置成1:3,這樣就能夠很好的解決主從不壓力均衡的問題!經過研究確實能夠!
配置就是將上面的讀的池的配置更改一下:
將<property name="poolNames">server1,server2</property>更改爲
<property name="poolNames">server1,server2,server2,server2</property>
mysql> select * from sxit;
+------+------+
| id | name |
+------+------+
| 2 | lisi |
+------+------+
1 row in set (0.00 sec)
mysql> select * from sxit;
+------+------+
| id | name |
+------+------+
| 2 | lisi |
+------+------+
1 row in set (0.01 sec)
mysql> select * from sxit;
+------+------+
| id | name |
+------+------+
| 2 | lisi |
+------+------+
1 row in set (0.00 sec)
mysql> select * from sxit;
+------+----------+
| id | name |
+------+----------+
| 1 | zhangsan |
| 3 | wangmazi |
+------+----------+
2 rows in set (0.00 sec)
//從以上的查詢來看,select確實是安裝1:3的比例來進行查詢的.
關於amoeba的其餘功能你們能夠到官方上去看看,裏面有關於垂直和水平切割方面的知識,相關信息以下: