Amoeba:读写分离

1、安装jdk
  1. root@proxy:~# java -version
  2. java version "1.6.0_45"
  3. Java(TM) SE Runtime Environment (build 1.6.0_45-b06)
  4. Java HotSpot(TM) 64-Bit Server VM (build 20.45-b01, mixed mode)
2、配置dbServer.xml
  1. root@proxy:/usr/local/amoeba/conf# cat dbServers.xml
  2. <?xml version="1.0" encoding="gbk"?>

  3. <!DOCTYPE amoeba:dbServers SYSTEM "dbserver.dtd">
  4. <amoeba:dbServers xmlns:amoeba="http://amoeba.meidusa.com/">

  5.                 <!--
  6.                         Each dbServer needs to be configured into a Pool,
  7.                         If you need to configure multiple dbServer with load balancing that can be simplified by the following configuration:
  8.                          add attribute with name virtual = "true" in dbServer, but the configuration does not allow the element with name factoryConfig
  9.                          such as 'multiPool' dbServer
  10.                 -->

  11.         <dbServer name="abstractServer" abstractive="true">
  12.                 <factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">
  13.                         <property name="manager">${defaultManager}</property>
  14.                         <property name="sendBufferSize">64</property>
  15.                         <property name="receiveBufferSize">128</property>

  16.                         <!-- mysql port -->
  17.                         <property name="port">3306</property>

  18.                         <!-- mysql schema -->
  19.                         <property name="schema">test</property>

  20.                         <!-- mysql user -->
  21.                         <property name="user">root</property>

  22.                         <!-- mysql password -->
  23.                         <property name="password">111</property>

  24.                 </factoryConfig>

  25.                 <poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool">
  26.                         <property name="maxActive">500</property>
  27.                         <property name="maxIdle">500</property>
  28.                         <property name="minIdle">10</property>
  29.                         <property name="minEvictableIdleTimeMillis">600000</property>
  30.                         <property name="timeBetweenEvictionRunsMillis">600000</property>
  31.                         <property name="testOnBorrow">true</property>
  32.                         <property name="testOnReturn">true</property>
  33.                         <property name="testWhileIdle">true</property>
  34.                 </poolConfig>
  35.         </dbServer>

  36.         <dbServer name="master1" parent="abstractServer">
  37.                 <factoryConfig>
  38.                         <!-- mysql ip -->
  39.                         <property name="ipAddress">10.10.10.40</property>
  40.                 </factoryConfig>
  41.         </dbServer>

  42.         <dbServer name="master2" parent="abstractServer">
  43.                 <factoryConfig>
  44.                         <!-- mysql ip -->
  45.                         <property name="ipAddress">10.10.10.50</property>
  46.                 </factoryConfig>
  47.         </dbServer>

  48.         <dbServer name="slave1" parent="abstractServer">
  49.                 <factoryConfig>
  50.                         <!-- mysql ip -->
  51.                         <property name="ipAddress">10.10.10.41</property>
  52.                 </factoryConfig>
  53.         </dbServer>

  54.         <dbServer name="slave2" parent="abstractServer">
  55.                 <factoryConfig>
  56.                         <!-- mysql ip -->
  57.                         <property name="ipAddress">10.10.10.42</property>
  58.                 </factoryConfig>
  59.         </dbServer>
  60. ####rp池配置的是salve1和slave2节点
  61.         <dbServer name="rp" virtual="true">
  62.                 <poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
  63.                         <!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->
  64.                         <property name="loadbalance">1</property>

  65.                         <!-- Separated by commas,such as: server1,server2,server1 -->
  66.                         <property name="poolNames">slave1,slave2</property>
  67.                 </poolConfig>
  68.         </dbServer>
  69. ####wp池配置的是master1和master2节点
  70.         <dbServer name="wp" virtual="true">
  71.                 <poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
  72.                         <!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->
  73.                         <property name="loadbalance">1</property>

  74.                         <!-- Separated by commas,such as: server1,server2,server1 -->
  75.                         <property name="poolNames">master1,master2</property>
  76.                 </poolConfig>
  77.         </dbServer>

  78. </amoeba:dbServers>
3、配置 amoeba.xml
  1. root@proxy:/usr/local/amoeba/conf# cat amoeba.xml
  2. <?xml version="1.0" encoding="gbk"?>

  3. <!DOCTYPE amoeba:configuration SYSTEM "amoeba.dtd">
  4. <amoeba:configuration xmlns:amoeba="http://amoeba.meidusa.com/">

  5.         <proxy>

  6.                 <!-- service class must implements com.meidusa.amoeba.service.Service -->
  7.                 <service name="Amoeba for Mysql" class="com.meidusa.amoeba.net.ServerableConnectionManager">
  8.                         <!-- port -->
  9.                         <property name="port">8066</property>

  10.                         <!-- bind ipAddress -->
  11.                         <!--
  12.                         <property name="ipAddress">127.0.0.1</property>
  13.                          -->

  14.                         <property name="manager">${clientConnectioneManager}</property>

  15.                         <property name="connectionFactory">
  16.                                 <bean class="com.meidusa.amoeba.mysql.net.MysqlClientConnectionFactory">
  17.                                         <property name="sendBufferSize">128</property>
  18.                                         <property name="receiveBufferSize">64</property>
  19.                                 </bean>
  20.                         </property>

  21.                         <property name="authenticator">
  22.                                 <bean class="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator">

  23.                                         <property name="user">root</property>

  24.                                         <property name="password">root</property>

  25.                                         <property name="filter">
  26.                                                 <bean class="com.meidusa.amoeba.server.IPAccessController">
  27.                                                         <property name="ipFile">${amoeba.home}/conf/access_list.conf</property>
  28.                                                 </bean>
  29.                                         </property>
  30.                                 </bean>
  31.                         </property>

  32.                 </service>

  33.                 <!-- server class must implements com.meidusa.amoeba.service.Service -->
  34.                 <service name="Amoeba Monitor Server" class="com.meidusa.amoeba.monitor.MonitorServer">
  35.                         <!-- port -->
  36.                         <!-- default value: random number
  37.                         <property name="port">9066</property>
  38.                         -->
  39.                         <!-- bind ipAddress -->
  40.                         <property name="ipAddress">127.0.0.1</property>
  41.                         <property name="daemon">true</property>
  42.                         <property name="manager">${clientConnectioneManager}</property>
  43.                         <property name="connectionFactory">
  44.                                 <bean class="com.meidusa.amoeba.monitor.net.MonitorClientConnectionFactory"></bean>
  45.                         </property>

  46.                 </service>

  47.                 <runtime class="com.meidusa.amoeba.mysql.context.MysqlRuntimeContext">
  48.                         <!-- proxy server net IO Read thread size -->
  49.                         <property name="readThreadPoolSize">20</property>

  50.                         <!-- proxy server client process thread size -->
  51.                         <property name="clientSideThreadPoolSize">30</property>

  52.                         <!-- mysql server data packet process thread size -->
  53.                         <property name="serverSideThreadPoolSize">30</property>

  54.                         <!-- per connection cache prepared statement size -->
  55.                         <property name="statementCacheSize">500</property>

  56.                         <!-- query timeout( default: 60 second , TimeUnit:second) -->
  57.                         <property name="queryTimeout">60</property>
  58.                 </runtime>

  59.         </proxy>

  60.         <!--
  61.                 Each ConnectionManager will start as thread
  62.                 manager responsible for the Connection IO read , Death Detection
  63.         -->
  64.         <connectionManagerList>
  65.                 <connectionManager name="clientConnectioneManager" class="com.meidusa.amoeba.net.MultiConnectionManagerWrapper">
  66.                         <property name="subManagerClassName">com.meidusa.amoeba.net.ConnectionManager</property>
  67.                         <!--
  68.                           default value is avaliable Processors
  69.                         <property name="processors">5</property>
  70.                          -->
  71.                 </connectionManager>
  72.                 <connectionManager name="defaultManager" class="com.meidusa.amoeba.net.MultiConnectionManagerWrapper">
  73.                         <property name="subManagerClassName">com.meidusa.amoeba.net.AuthingableConnectionManager</property>

  74.                         <!--
  75.                           default value is avaliable Processors
  76.                         <property name="processors">5</property>
  77.                          -->
  78.                 </connectionManager>
  79.         </connectionManagerList>

  80.                 <!-- default using file loader -->
  81.         <dbServerLoader class="com.meidusa.amoeba.context.DBServerConfigFileLoader">
  82.                 <property name="configFile">${amoeba.home}/conf/dbServers.xml</property>
  83.         </dbServerLoader>

  84.         <queryRouter class="com.meidusa.amoeba.mysql.parser.MysqlQueryRouter">
  85.                 <property name="ruleLoader">
  86.                         <bean class="com.meidusa.amoeba.route.TableRuleFileLoader">
  87.                                 <property name="ruleFile">${amoeba.home}/conf/rule.xml</property>
  88.                                 <property name="functionFile">${amoeba.home}/conf/ruleFunctionMap.xml</property>
  89.                         </bean>
  90.                 </property>
  91. ####default池只配置了master1节点
  92.                 <property name="sqlFunctionFile">${amoeba.home}/conf/functionMap.xml</property>
  93.                 <property name="LRUMapSize">1500</property>
  94.                 <property name="defaultPool">master1</property>

  95.                 <property name="writePool">wp</property>
  96.                 <property name="readPool">rp</property>

  97.                 <property name="needParse">true</property>
  98.         </queryRouter>
  99. </amoeba:configuration>
4、启动amoeba
  1. root@proxy:/usr/local/amoeba# bin/amoeba start &
  2. [1] 2511
  3. root@proxy:/usr/local/amoeba#
  4. The stack size specified is too small, Specify at least 160k
  5. Could not create the Java virtual machine.

  6. [1]+ Exit 1 bin/amoeba start
更改bin/amoeba文件
  1. DEFAULT_OPTS="-server -Xms256m -Xmx256m -Xss128k"
  2. 更改为
  3. DEFAULT_OPTS="-server -Xms256m -Xmx256m -Xss256k"
启动正常之后,连接amoeba
  1. root@proxy:~# mysql -uroot -p -h10.10.10.39 -P8066
  2. Enter password:
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 307988519
  5. Server version: 5.1.45-mysql-amoeba-proxy-2.2.0

  6. Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.

  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

  11. java.util.NoSuchElementException: Could not create a validated object, cause: ValidateObject failed
  12. [root@10.10.10.39:(none)][11:46:57pm] MySQL-> at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:1191)
  13.         at com.meidusa.amoeba.net.poolable.GenericObjectPool.borrowObject(GenericObjectPool.java:381)
  14.         at com.meidusa.amoeba.mysql.handler.CommandMessageHandler.startSession(CommandMessageHandler.java:633)
  15.         at com.meidusa.amoeba.mysql.handler.MySqlCommandDispatcher.handleMessage(MySqlCommandDispatcher.java:123)
  16.         at com.meidusa.amoeba.mysql.net.MysqlClientConnection$2.run(MysqlClientConnection.java:291)
  17.         at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
  18.         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
  19.         at java.lang.Thread.run(Thread.java:662)
连接报错,查看root.log的输出
  1. 2015-08-29 16:01:03,299 INFO net.BackendConnectionFactory - open socket channel to server[10.10.10.40:3306]
  2. 2015-08-29 16:01:03,303 ERROR net.MysqlServerConnection - handShake with /10.10.10.40:3306 error:Access denied for user 'root'@'10.10.10.39' (using password: NO),hashCode=661272757
  3. 2015-08-29 16:01:03,324 INFO net.BackendConnectionFactory - open socket channel to server[10.10.10.50:3306]
  4. 2015-08-29 16:01:03,327 ERROR net.MysqlServerConnection - handShake with /10.10.10.50:3306 error:Access denied for user 'root'@'10.10.10.39' (using password: NO),hashCode=505588567
  5. 2015-08-29 16:01:03,331 INFO net.BackendConnectionFactory - open socket channel to server[10.10.10.41:3306]
  6. 2015-08-29 16:01:03,334 ERROR net.MysqlServerConnection - handShake with /10.10.10.41:3306 error:Access denied for user 'root'@'10.10.10.39' (using password: NO),hashCode=530654357
  7. 2015-08-29 16:01:03,335 INFO net.BackendConnectionFactory - open socket channel to server[10.10.10.42:3306]
  8. 2015-08-29 16:01:03,338 ERROR net.MysqlServerConnection - handShake with /10.10.10.42:3306 error:Access denied for user 'root'@'10.10.10.39' (using password: NO),hashCode=848649429
查看数据库连接权限
  1. [root@localhost:mysql][11:55:29pm] MySQL-> select host,user from user;
  2. +-----------+----------+
  3. | host | user |
  4. +-----------+----------+
  5. | % | rep_user |
  6. | % | root |
  7. | 127.0.0.1 | root |
  8. | ::1 | root |
  9. | localhost | |
  10. | localhost | root |
  11. | ubuntu | |
  12. | ubuntu | root |
  13. +-----------+----------+
  14. 8 rows in set (0.00 sec)
数据库权限正常,使用客户端连接测试
  1. root@proxy:~# mysql -uroot -p -h10.10.10.40
  2. Enter password:
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 417
  5. Server version: 5.6.19-enterprise-commercial-advanced-log MySQL Enterprise Server - Advanced Edition (Commercial)

  6. Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.

  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

  11. [root@10.10.10.40:(none)][11:59:09pm] MySQL-> use test;
  12. Reading table information for completion of table and column names
  13. You can turn off this feature to get a quicker startup with -A

  14. Database changed
  15. [root@10.10.10.40:test][11:59:12pm] MySQL-> show tables;
  16. +----------------+
  17. | Tables_in_test |
  18. +----------------+
  19. | t1 |
  20. +----------------+
  21. 1 row in set (0.00 sec)
客户端连接也正常,猜想应该是amoeba配置有问题
  1.                         <!-- mysql password
  2.                         <property name="password">111</property>
  3.                         -->
发现密码被注释了,更改
  1.                         <!-- mysql password -->
  2.                         <property name="password">111</property>
启动一切正常

  1. root@proxy:~# mysql -uroot -p -h10.10.10.39 -P8066
  2.     Enter password:
  3.     Welcome to the MySQL monitor. Commands end with ; or \g.
  4.     Your MySQL connection id is 1113757163
  5.     Server version: 5.1.45-mysql-amoeba-proxy-2.2.0 MySQL Enterprise Server - Advanced Edition (Commercial)

  6.     Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

  7.     Oracle is a registered trademark of Oracle Corporation and/or its
  8.     affiliates. Other names may be trademarks of their respective
  9.     owners.

  10.     Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

  11.     [root@10.10.10.39:(none)][12:06:15am] MySQL-> use test;
  12.     Database changed
  13.     [root@10.10.10.39:test][12:06:18am] MySQL->
  14.     [root@10.10.10.39:test][12:06:18am] MySQL-> show tables;
  15.     +----------------+
  16.     | Tables_in_test |
  17.     +----------------+
  18.     | t1 |
  19.     +----------------+
  20.     1 row in set (0.00 sec)
5、测试读写分离
停止master2,slave1,slave2节点的复制
  1. [root@localhost:(none)][12:27:38am] MySQL-> stop slave;
  2. Query OK, 0 rows affected (0.04 sec)

  3. [root@localhost:(none)][12:27:53am] MySQL-> show slave status \G
  4. *************************** 1. row ***************************
  5.                Slave_IO_State:
  6.                   Master_Host: 10.10.10.40
  7.                   Master_User: rep_user
  8.                   Master_Port: 3306
  9.                 Connect_Retry: 60
  10.               Master_Log_File: master1-bin.000007
  11.           Read_Master_Log_Pos: 442
  12.                Relay_Log_File: slave-relay-bin.000008
  13.                 Relay_Log_Pos: 607
  14.         Relay_Master_Log_File: master1-bin.000007
  15.              Slave_IO_Running: No
  16.             Slave_SQL_Running: No
使用amoeba连接,往测试表t1中插入测试数据
  1. root@proxy:~# mysql -uroot -p -h10.10.10.39 -P8066
  2. Enter password:
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 750410715
  5. Server version: 5.1.45-mysql-amoeba-proxy-2.2.0 MySQL Enterprise Server - Advanced Edition (Commercial)

  6. Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.

  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

  11. [root@10.10.10.39:(none)][12:25:49am] MySQL-> use test;
  12. Database changed
  13. [root@10.10.10.39:test][12:26:51am] MySQL-> insert into t1 values (null,'aaaaa');
  14. Query OK, 1 row affected (0.05 sec)
master1查看表t1

  1. [root@localhost:mysql][11:27:16pm] MySQL-> select * from test.t1;
  2. +----+-------+
  3. | id | name |
  4. +----+-------+
  5. | 1 | aaaaa |
  6. +----+-------+
  7. 1 row in set (0.00 sec)
在master2,slave1,slave2上查看表t1

  1. [root@localhost:(none)][12:28:28am] MySQL-> use test;
  2. Reading table information for completion of table and column names
  3. You can turn off this feature to get a quicker startup with -A

  4. Database changed
  5. [root@localhost:test][12:29:17am] MySQL-> select * from t1;
  6. Empty set (0.00 sec)
在proxy上查看表t1表的数据

  1. [root@10.10.10.39:test][12:30:01am] MySQL-> select * from t1;
  2. Empty set (0.05 sec)
在proxy上无法查看插入的数据,说明读是在rp池里,而rp池只有slave1和slave2,数据没有同步到从节点。

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12219480/viewspace-1786466/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/12219480/viewspace-1786466/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值