mysql基于mysql-proxy和amoeba的读写分离
以及tcpdump+wireshark工具验证(下)
【基于amoeba的读写分离】
此处依然,借助上一个节点的主从不再配置。
lftp 172.16.0.1:/pub/Sources/6.x86_64/amoeba> mgetamoeba-mysql-binary-2.2.0.tar.gz [root@node2 local ]# mkdir amoeba #####因为amoeba解压之后是多个文件,我们这里给建文件夹 [root@node2 local ]#cd [root@node2 ~ ]# tar xf amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba/ [root@node2 ~ ]# cd /usr/local/amoeba/ [root@node2 amoeba ]# ls benchmark bin changelogs.txt conf lib LICENSE.txt README.html
[root@node2 ~ ]# rpm -ivh jdk-7u67-linux-x64.rpm ####因为amoeba是JAVA程序,所以需要先先安装JDK。 [root@node2 profile.d ]# vim java.sh #####配置JDK的环境变量 export JAVA_HOME=/usr/java/latest export PATH=$JAVA_HOME/bin:$PATH [root@node2 profile.d ]# java -version #######JDK正常 java version "1.7.0_67" Java(TM) SE Runtime Environment (build 1.7.0_67-b01) Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)
#######################################配置的dbServers.xml#####################################
##########################看配置文件的名字就知道这是定义后端的配置文件#########################
[root@node2 conf ]# vim dbServers.xml 1 <?xml version="1.0"encoding="gbk"?> 2 3 <!DOCTYPE amoeba:dbServers SYSTEM"dbserver.dtd"> 4 <amoeba:dbServersxmlns:amoeba="http://amoeba.meidusa.com/"> 5 6 <!-- 7 Each dbServer needs to be configured into a Pool, 8 If you need to configure multiple dbServer with load balancing that can be simplified by the following co nfiguration: 9 add attribute with name virtual = "true" in dbServer, but theconfiguration does not allow the element w ith name factoryConfig 10 such as 'multiPool' dbServer 11 --> 12 13 <dbServer name="abstractServer"abstractive="true"> 14 <factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory"> 15 <propertyname="manager">${defaultManager}</property> 16 <property name="sendBufferSize">64</property> 17 <property name="receiveBufferSize">128</property> 18 19 <!-- mysql port --> ####定义链接后端的MySQL端口 20 <property name="port">3306</property> 21 22 <!-- mysql schema --> ####定义连接的后端数据库 23 <property name="schema">test</property> 24 25 <!-- mysql user --> ####定义连接后端使用的用户( 26 <propertyname="user">admin</property> 27 28 <!-- mysql password --> ####链接后端使用的密码 29 <property name="password">admin</property> 30 31 </factoryConfig> 32 <!-- 数据库连接配置的公共部分 --> 33 <poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool"> 34 <property name="maxActive">500</property> ####最大连接数,默认500 35 <property name="maxIdle">500</property> ####最大空闲连接数 36 <propertyname="minIdle">10</property> ####最小空闲连接数 37 <propertyname="minEvictableIdleTimeMillis">600000</property> 38 <property name="timeBetweenEvictionRunsMillis">600000</property> 39 <property name="testOnBorrow">true</property> 40 <property name="testOnReturn">true</property> 41 <property name="testWhileIdle">true</property> 42 </poolConfig> 43 </dbServer> 44 45 <dbServer name="writedb" parent="abstractServer"> #########定义一个写的主机 46 <factoryConfig> 47 <!-- mysql ip --> 48 <propertyname="ipAddress">172.16.11.3</property> #####写主机的IP 49 </factoryConfig> 50 </dbServer> 51 52 <dbServer name="readdb" parent="abstractServer"> #####定义一个读主机 53 <factoryConfig> 54 <!-- mysql ip --> 55 <propertyname="ipAddress">172.16.249.141</property> ####读主机的IP 56 </factoryConfig> 57 </dbServer> 58 <!-- 数据库池,虚拟服务器,实现读取的负载均衡 --> 59 <dbServer name="slaves" virtual="true"> ####定义虚拟主机组 60 <poolConfig class="com.meidusa.amoeba.server.MultipleServerPool"> 61 <!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED ,3=HA--> 62 <property name="loadbalance">1</property> ##1是轮询,2是加权轮询,3高可用 63 64 <!-- Separated by commas,such as: server1,server2,server1 --> 65 <propertyname="poolNames">readdb</property> ##虚拟组里面包含的后端主机 66 </poolConfig> 67 </dbServer> 68 69 </amoeba:dbServers>
#####################################配置amoeba的配置文件#############################
[root@node2 conf ]#vim amoeba.xml 1 <?xml version="1.0"encoding="gbk"?> 2 3 <!DOCTYPE amoeba:configuration SYSTEM"amoeba.dtd"> 4 <amoeba:configurationxmlns:amoeba="http://amoeba.meidusa.com/"> 5 6 <proxy> 7 8 <!-- service class must implements com.meidusa.amoeba.service.Service--> 9 <service name="Amoeba for Mysql"class="com.meidusa.amoeba.net.ServerableConnectionManager"> 10 <!-- port --> 11 <propertyname="port">8066</property> ####amoeba的监听端口 12 13 <!-- bind ipAddress --> 14 <!-- 15 <propertyname="ipAddress">127.0.0.1</property> ####地址是本机 16 --> 17 18 <propertyname="manager">${clientConnectioneManager}</property> 19 20 <property name="connectionFactory"> 21 <beanclass="com.meidusa.amoeba.mysql.net.MysqlClientConnectionFactory"> 22 <propertyname="sendBufferSize">128</property> 23 <propertyname="receiveBufferSize">64</property> 24 </bean> 25 </property> 26 27 <property name="authenticator"> 28 <beanclass="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator"> 29 30 <propertyname="user">admin</property> #####连接后端用户的账号 31 32 <propertyname="password">admin</property> ####连接后端用户的密码 33 34 <property name="filter"> 35 <beanclass="com.meidusa.amoeba.server.IPAccessController"> 36 <propertyname="ipFile">${amoeba.home}/conf/access_list.conf</property> 37 </bean> 38 </property> 39 </bean> 40 </property> 41 42 </service> 43 44 <!-- server class must implements com.meidusa.amoeba.service.Service --> 45 <service name="Amoeba Monitor Server"class="com.meidusa.amoeba.monitor.MonitorServer"> 46 <!-- port --> 47 <!-- default value: randomnumber 48 <property name="port">9066</property> 49 --> 50 <!-- bind ipAddress --> 51 <propertyname="ipAddress">127.0.0.1</property> 52 <property name="daemon">true</property> 53 <propertyname="manager">${clientConnectioneManager}</property> 54 <property name="connectionFactory"> 55 <beanclass="com.meidusa.amoeba.monitor.net.MonitorClientConnectionFactory"></bean> 56 </property> 57 58 </service> 59 60 <runtimeclass="com.meidusa.amoeba.mysql.context.MysqlRuntimeContext"> 61 <!-- proxy server net IO Read thread size --> 62 <property name="readThreadPoolSize">20</property> 63 64 <!-- proxy server client process thread size --> 65 <property name="clientSideThreadPoolSize">30</property> 66 67 <!-- mysql server data packet process thread size --> 68 <property name="serverSideThreadPoolSize">30</property> 69 70 <!-- per connection cache prepared statement size --> 71 <property name="statementCacheSize">500</property> 72 73 <!-- query timeout( default: 60 second , TimeUnit:second) --> 74 <property name="queryTimeout">60</property> 75 </runtime> 76 77 </proxy> 78 79 <!-- 80 Each ConnectionManager will start as thread 81 manager responsible for the Connection IO read , Death Detection 82 --> 83 <connectionManagerList> 84 <connectionManager name="clientConnectioneManager" class="com.meidusa.amoeba.net.MultiConnectionManagerWrappe r"> 85 <property name="subManagerClassName">com.meidusa.amoeba.net.ConnectionManager</property> 86 <!-- 87 default value is avaliableProcessors 88 <property name="processors">5</property> 89 --> 90 </connectionManager> 91 <connectionManager name="defaultManager"class="com.meidusa.amoeba.net.MultiConnectionManagerWrapper"> 92 <propertyname="subManagerClassName">com.meidusa.amoeba.net.AuthingableConnectionManager</property> 93 94 <!-- 95 default value is avaliableProcessors 96 <property name="processors">5</property> 97 --> 98 </connectionManager> 99 </connectionManagerList> 100 101 <!-- default using file loader--> 102 <dbServerLoaderclass="com.meidusa.amoeba.context.DBServerConfigFileLoader"> 103 <propertyname="configFile">${amoeba.home}/conf/dbServers.xml</property> 104 </dbServerLoader> 105 106 <queryRouterclass="com.meidusa.amoeba.mysql.parser.MysqlQueryRouter"> 107 <propertyname="ruleLoader"> 108 <beanclass="com.meidusa.amoeba.route.TableRuleFileLoader"> 109 <propertyname="ruleFile">${amoeba.home}/conf/rule.xml</property> 110 <propertyname="functionFile">${amoeba.home}/conf/ruleFunctionMap.xml</property> 111 </bean> 112 </property> 113 <propertyname="sqlFunctionFile">${amoeba.home}/conf/functionMap.xml</property> 114 <propertyname="LRUMapSize">1500</property> 115 <propertyname="defaultPool">writedb</property> ####默认的后端数据库 116 117 <!-- --> 118 <propertyname="writePool">writedb</property> ####后端的写主机的虚拟主机 119 <propertyname="readPool">slaves</property> ####后端的读主机的虚拟主机 120 121 <propertyname="needParse">true</property> 122 </queryRouter> 123</amoeba:configuration> [root@node2 bin ]#pwd /usr/local/amoeba/bin [root@node2 bin ]# ./amoeba start ############启动amoeba
[root@node2 conf ]# mysql -uadmin -padmin -h172.16.249.106 -P8066 ####连入测试 mysql> create database haha; Query OK, 1 row affected (0.04 sec)
使用tcpdump抓包测试结果
[root@node3 ~]#tcpdump -i eth0 -w mysql.pkg port 3306