Mysql 基于 Amoeba 的 读写分离(2)

<?xml version="1.0" encoding="gbk"?>
<!DOCTYPE amoeba:configuration SYSTEM "amoeba.dtd">
<amoeba:configuration xmlns:amoeba="http://amoeba.meidusa.com/">
	<proxy>
	
		<!-- service class must implements com.meidusa.amoeba.service.Service -->
		<service name="Amoeba for Mysql" class="com.meidusa.amoeba.net.ServerableConnectionManager">
			<!-- Amoeba 端口号 -->
			<property name="port">8066</property>
			
			<!-- bind ipAddress -->
			<!-- 
			<property name="ipAddress">127.0.0.1</property>
			 -->
			
			<property name="manager">${clientConnectioneManager}</property>
			
			<property name="connectionFactory">
				<bean class="com.meidusa.amoeba.mysql.net.MysqlClientConnectionFactory">
					<property name="sendBufferSize">128</property>
					<property name="receiveBufferSize">64</property>
				</bean>
			</property>
			
			<property name="authenticator">
				<bean class="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator">
					<!-- Amoeba 账号,密码 -->
					<property name="user">root</property>
					
					<property name="password">root</property>
					
					<property name="filter">
						<bean class="com.meidusa.amoeba.server.IPAccessController">
							<property name="ipFile">${amoeba.home}/conf/access_list.conf</property>
						</bean>
					</property>
				</bean>
			</property>
			
		</service>
		
		<!-- server class must implements com.meidusa.amoeba.service.Service -->
		<service name="Amoeba Monitor Server" class="com.meidusa.amoeba.monitor.MonitorServer">
			<!-- port -->
			<!--  default value: random number
			<property name="port">9066</property>
			-->
			<!-- bind ipAddress -->
			<property name="ipAddress">127.0.0.1</property>
			<property name="daemon">true</property>
			<property name="manager">${clientConnectioneManager}</property>
			<property name="connectionFactory">
				<bean class="com.meidusa.amoeba.monitor.net.MonitorClientConnectionFactory"></bean>
			</property>
			
		</service>
		
		<runtime class="com.meidusa.amoeba.mysql.context.MysqlRuntimeContext">
			<!-- proxy server net IO Read thread size -->
			<property name="readThreadPoolSize">20</property>
			
			<!-- proxy server client process thread size -->
			<property name="clientSideThreadPoolSize">30</property>
			
			<!-- mysql server data packet process thread size -->
			<property name="serverSideThreadPoolSize">30</property>
			
			<!-- per connection cache prepared statement size  -->
			<property name="statementCacheSize">500</property>
			
			<!-- query timeout( default: 60 second , TimeUnit:second) -->
			<property name="queryTimeout">60</property>
		</runtime>
		
	</proxy>
	
	<!-- 
		Each ConnectionManager will start as thread
		manager responsible for the Connection IO read , Death Detection
	-->
	<connectionManagerList>
		<connectionManager name="clientConnectioneManager" class="com.meidusa.amoeba.net.MultiConnectionManagerWrapper">
			<property name="subManagerClassName">com.meidusa.amoeba.net.ConnectionManager</property>
			<!-- 
			  default value is avaliable Processors 
			<property name="processors">5</property>
			 -->
		</connectionManager>
		<connectionManager name="defaultManager" class="com.meidusa.amoeba.net.MultiConnectionManagerWrapper">
			<property name="subManagerClassName">com.meidusa.amoeba.net.AuthingableConnectionManager</property>
			
			<!-- 
			  default value is avaliable Processors 
			<property name="processors">5</property>
			 -->
		</connectionManager>
	</connectionManagerList>
	
		<!-- default using file loader -->
	<dbServerLoader class="com.meidusa.amoeba.context.DBServerConfigFileLoader">
		<property name="configFile">${amoeba.home}/conf/dbServers.xml</property>
	</dbServerLoader>
	
	<queryRouter class="com.meidusa.amoeba.mysql.parser.MysqlQueryRouter">
		
		<property name="ruleLoader">
			<bean class="com.meidusa.amoeba.route.TableRuleFileLoader">
				<property name="ruleFile">${amoeba.home}/conf/rule.xml</property>
				<property name="functionFile">${amoeba.home}/conf/ruleFunctionMap.xml</property>
			</bean>
		</property>
		<property name="sqlFunctionFile">${amoeba.home}/conf/functionMap.xml</property>
		
		<property name="LRUMapSize">1500</property>
		<!-- 默认数据库,主数据库 -->
		<property name="defaultPool">master</property>
		
		<!-- 写数据库 -->
		<property name="writePool">master</property>
		<!-- 读数据库,dbServer.xml 中配置的 虚拟数据库,数据库池 -->
		<property name="readPool">slaves</property>
		
		<property name="needParse">true</property>
	</queryRouter>
</amoeba:configuration>
rule.xml
Java代码
  1. <?xml version="1.0" encoding="gbk"?>
  2. <!DOCTYPE amoeba:rule SYSTEM "rule.dtd">
  3. <amoeba:rule xmlns:amoeba="http://amoeba.meidusa.com/">
  4. <tableRule name="message" schema="test" defaultPools="server1">
  5. </tableRule>
  6. </amoeba:rule>
<?xml version="1.0" encoding="gbk"?>
<!DOCTYPE amoeba:rule SYSTEM "rule.dtd">
<amoeba:rule xmlns:amoeba="http://amoeba.meidusa.com/">
	<tableRule name="message" schema="test" defaultPools="server1">
	</tableRule>
</amoeba:rule>
不需要 数据库分片时,不用配置。 但是不能没有 tableRule 元素, 否则报错。 随便写个空规则就行了。
3. 测试读写分离
a. 在 Master , Slave1 , Slave2 中分别查看 日志文件: mysql.log
Shell代码
  1. tail -f ./log/mysql.log
tail -f ./log/mysql.log
b. 启动 Amoeba, 使用 Mysql GUI Tools 连接 Amoeba
Mysql 基于 Amoeba 的 读写分离(2)_第1张图片

Mysql 基于 Amoeba 的 读写分离(2)_第2张图片
执行以上几个命令。 查看日志内容。
Master mysql.log
Shell代码
  1. [mysql@prx1 mysql]$ tail -f log/mysql.log
  2. 370 Query SET SESSION sql_mode=''
  3. 370 Query SET NAMES utf8
  4. 370 Query SHOW FULL TABLES
  5. 370 Query SHOW COLUMNS FROM `t_message`
  6. 370 Query SHOW COLUMNS FROM `t_user`
  7. 370 Query SHOW PROCEDURE STATUS
  8. 370 Query SHOW FUNCTION STATUS
  9. 110813 15:21:11 370 Query SHOW VARIABLES LIKE 'character_set_server'
  10. 370 Query SHOW FULL COLUMNS FROM `test`.`t_message`
  11. 110813 15:21:12 370 Query SHOW CREATE TABLE `test`.`t_message`
  12. 110813 15:22:40 374 Connect test_user@192.168.14.129 on test
  13. 375 Connect test_user@192.168.14.129 on test
  14. 376 Connect test_user@192.168.14.129 on test
  15. 110813 15:23:40 370 Query insert into t_message values(1, 'c1')
  16. 110813 15:24:07 377 Connect test_user@192.168.14.129 on test
  17. 378 Connect test_user@192.168.14.129 on test
  18. 379 Connect test_user@192.168.14.129 on test
  19. 110813 15:24:15 370 Query insert into t_user values(8, 'n8', 'p8')
  20. 110813 15:24:24 370 Query SHOW FULL COLUMNS FROM `test`.`t_user`
  21. 370 Query SHOW CREATE TABLE `test`.`t_user`
  22. 110813 15:24:35 370 Query SHOW FULL COLUMNS FROM `test`.`t_message`
  23. 370 Query SHOW CREATE TABLE `test`.`t_message`
[mysql@prx1 mysql]$ tail -f log/mysql.log
                  370 Query     SET SESSION sql_mode=''
                  370 Query     SET NAMES utf8
                  370 Query     SHOW FULL TABLES
                  370 Query     SHOW COLUMNS FROM `t_message`
                  370 Query     SHOW COLUMNS FROM `t_user`
                  370 Query     SHOW PROCEDURE STATUS
                  370 Query     SHOW FUNCTION STATUS
110813 15:21:11   370 Query     SHOW VARIABLES LIKE 'character_set_server'
                  370 Query     SHOW FULL COLUMNS FROM `test`.`t_message`
110813 15:21:12   370 Query     SHOW CREATE TABLE `test`.`t_message`
110813 15:22:40   374 Connect   [email protected] on test
                  375 Connect   [email protected] on test
                  376 Connect   [email protected] on test
110813 15:23:40   370 Query     insert into t_message values(1, 'c1')
110813 15:24:07   377 Connect   [email protected] on test
                  378 Connect   [email protected] on test
                  379 Connect   [email protected] on test
110813 15:24:15   370 Query     insert into t_user values(8, 'n8', 'p8')
110813 15:24:24   370 Query     SHOW FULL COLUMNS FROM `test`.`t_user`
                  370 Query     SHOW CREATE TABLE `test`.`t_user`
110813 15:24:35   370 Query     SHOW FULL COLUMNS FROM `test`.`t_message`
                  370 Query     SHOW CREATE TABLE `test`.`t_message`
Slave1 mysql.log
Shell代码
  1. [mysql@prx2 mysql]$ tail -f log/mysql.log
  2. 317 Connect test_user@192.168.14.129 on test
  3. 318 Connect test_user@192.168.14.129 on test
  4. 110813 15:35:30 315 Query SELECT @@sql_mode
  5. 110813 15:35:32 315 Query SELECT @@sql_mode
  6. 110813 15:35:44 315 Query SELECT @@SQL_MODE
  7. 110813 15:35:46 315 Query SELECT @@SQL_MODE
  8. 110813 15:37:18 319 Connect test_user@192.168.14.129 on test
  9. 320 Connect test_user@192.168.14.129 on test
  10. 110813 15:37:19 321 Connect test_user@192.168.14.129 on test
  11. 110813 15:37:26 246 Quit
  12. 110813 15:38:21 315 Query SELECT @@SQL_MODE
  13. 110813 15:38:22 42 Query BEGIN
  14. 42 Query insert into t_message values(1, 'c1')
  15. 42 Query COMMIT /* implicit, from Xid_log_event */
  16. 110813 15:38:50 322 Connect test_user@192.168.14.129 on test
  17. 323 Connect test_user@192.168.14.129 on test
  18. 324 Connect test_user@192.168.14.129 on test
  19. 110813 15:38:58 42 Query BEGIN
  20. 42 Query insert into t_user values(8, 'n8', 'p8')
  21. 42 Query COMMIT /* implicit, from Xid_log_event */
  22. 110813 15:39:08 315 Query SELECT @@SQL_MODE
  23. 110813 15:39:19 315 Query SELECT @@SQL_MODE
  24. 110813 15:44:08 325 Connect test_user@192.168.14.129 on test
  25. 326 Connect test_user@192.168.14.129 on test
  26. 327 Connect test_user@192.168.14.129 on test
[mysql@prx2 mysql]$ tail -f log/mysql.log
                  317 Connect   [email protected] on test
                  318 Connect   [email protected] on test
110813 15:35:30   315 Query     SELECT @@sql_mode
110813 15:35:32   315 Query     SELECT @@sql_mode
110813 15:35:44   315 Query     SELECT @@SQL_MODE
110813 15:35:46   315 Query     SELECT @@SQL_MODE
110813 15:37:18   319 Connect   [email protected] on test
                  320 Connect   [email protected] on test
110813 15:37:19   321 Connect   [email protected] on test
110813 15:37:26   246 Quit
110813 15:38:21   315 Query     SELECT @@SQL_MODE
110813 15:38:22    42 Query     BEGIN
                   42 Query     insert into t_message values(1, 'c1')
                   42 Query     COMMIT /* implicit, from Xid_log_event */
110813 15:38:50   322 Connect   [email protected] on test
                  323 Connect   [email protected] on test
                  324 Connect   [email protected] on test
110813 15:38:58    42 Query     BEGIN
                   42 Query     insert into t_user values(8, 'n8', 'p8')
                   42 Query     COMMIT /* implicit, from Xid_log_event */
110813 15:39:08   315 Query     SELECT @@SQL_MODE
110813 15:39:19   315 Query     SELECT @@SQL_MODE
110813 15:44:08   325 Connect   [email protected] on test
                  326 Connect   [email protected] on test
                  327 Connect   [email protected] on test
Slave2 mysql.log
Shell代码
  1. [mysql@prx3 mysql]$ tail -f log/mysql.log
  2. 110813 15:35:08 305 Connect test_user@192.168.14.129 on test
  3. 306 Connect test_user@192.168.14.129 on test
  4. 307 Connect test_user@192.168.14.129 on test
  5. 110813 15:35:31 304 Query SELECT @@sql_mode
  6. 304 Query SELECT @@sql_mode
  7. 110813 15:35:44 304 Query SELECT @@SQL_MODE
  8. 110813 15:35:46 304 Query SELECT * FROM t_message t
  9. 110813 15:37:18 308 Connect test_user@192.168.14.129 on test
  10. 309 Connect test_user@192.168.14.129 on test
  11. 310 Connect test_user@192.168.14.129 on test
  12. 110813 15:38:21 8 Query BEGIN
  13. 8 Query insert into t_message values(1, 'c1')
  14. 8 Query COMMIT /* implicit, from Xid_log_event */
  15. 110813 15:38:50 311 Connect test_user@192.168.14.129 on test
  16. 312 Connect test_user@192.168.14.129 on test
  17. 313 Connect test_user@192.168.14.129 on test
  18. 110813 15:38:58 304 Query SELECT @@SQL_MODE
  19. 8 Query BEGIN
  20. 8 Query insert into t_user values(8, 'n8', 'p8')
  21. 8 Query COMMIT /* implicit, from Xid_log_event */
  22. 110813 15:39:08 304 Query select * from t_user
  23. 110813 15:39:19 304 Query select * from t_message
  24. 110813 15:44:08 314 Connect test_user@192.168.14.129 on test
  25. 315 Connect test_user@192.168.14.129 on test
  26. 316 Connect test_user@192.168.14.129 on test
[mysql@prx3 mysql]$ tail -f log/mysql.log
110813 15:35:08   305 Connect   [email protected] on test
                  306 Connect   [email protected] on test
                  307 Connect   [email protected] on test
110813 15:35:31   304 Query     SELECT @@sql_mode
                  304 Query     SELECT @@sql_mode
110813 15:35:44   304 Query     SELECT @@SQL_MODE
110813 15:35:46   304 Query     SELECT * FROM t_message t
110813 15:37:18   308 Connect   [email protected] on test
                  309 Connect   [email protected] on test
                  310 Connect   [email protected] on test
110813 15:38:21     8 Query     BEGIN
                    8 Query     insert into t_message values(1, 'c1')
                    8 Query     COMMIT /* implicit, from Xid_log_event */
110813 15:38:50   311 Connect   [email protected] on test
                  312 Connect   [email protected] on test
                  313 Connect   [email protected] on test
110813 15:38:58   304 Query     SELECT @@SQL_MODE
                    8 Query     BEGIN
                    8 Query     insert into t_user values(8, 'n8', 'p8')
                    8 Query     COMMIT /* implicit, from Xid_log_event */
110813 15:39:08   304 Query     select * from t_user
110813 15:39:19   304 Query     select * from t_message
110813 15:44:08   314 Connect   [email protected] on test
                  315 Connect   [email protected] on test
                  316 Connect   [email protected] on test
从日志中可以看出。
在 Master mysql.log 中,只执行了 insert into 命令。
在 Slave1 中,只是复制了 insert into 命令, 是主从复制的结果
在 Slave2 中, 复制了 insert into 命令,同时还执行了 select 命令。
说明,主从分离已经成功。

你可能感兴趣的:(mysql,amoeba)