一、Amoeba简介
Amoeba是一个以MySQL为底层数据存储,并对应用提供MySQL协议接口的proxy。它集中地响应应用的请求,依据用户事先设置的规则,将SQL请求发送到特定的数据库上执行。基于此可以实现负载均衡、读写分离、高可用性等需求,
Amoeba相当于一个SQL请求的路由器,目的是为负载均衡、读写分离、高可用性提供机制,而不是完全实现它们。需要结合使用MySQL的 Replication等机制来实现副本同步等功能
二、Amoeba搭建过程
在搭建amoeba过程中,我们首先需要准备环境,这里我们提供了三台Linux服务器,分别是:
Amoeba for MySQL:centos-node5(主机名)
master:centos-node6
slave:centos-node7
Amoeba 服务器,master 主服务器,slave 从服务器,服务上分别都安装了,Jdk1.7以上版本,相关jdk安装参考
http://blog.csdn.net/liulihui1988/article/details/51985517;
并且已经安装了相关的主从数据库,相关安装参考:
http://blog.csdn.net/liulihui1988/article/details/79066755
wget http://nchc.dl.sourceforge.net/project/amoeba/Amoeba%20for%20mysql/2.x/amoeba-mysql-binary-2.1.0-RC5.tar.gz
命令创建amoeba文件夹,在/usr/local/amoeba目录下 执行下载以上地址
mkdir /usr/local/amoeba
执行以下命令解压在当前文件夹下
# tar xvf amoeba-mysql-binary-2.1.0-RC5.tar.gz
# ./amoeba
如上图,表示安装成功
dbServer.xml 文件在 /usr/local/amoeba/conf/目录下 命令编辑
# vim dbServer.xml
需要修改的 代码部分
<dbServer name="abstractServer" abstractive="true">
<factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">
<property name="manager">${defaultManager}property>
<property name="sendBufferSize">64property>
<property name="receiveBufferSize">128property>
<property name="port">3306property>
<property name="schema">testproperty>
<property name="user">proxyuserproperty>
<property name="password">123456property>
factoryConfig>
<poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool">
<property name="maxActive">500property>
<property name="maxIdle">500property>
<property name="minIdle">10property>
<property name="minEvictableIdleTimeMillis">600000property>
<property name="timeBetweenEvictionRunsMillis">600000property>
<property name="testOnBorrow">trueproperty>
<property name="testWhileIdle">trueproperty>
poolConfig>
dbServer>
<dbServer name="master" parent="abstractServer">
<factoryConfig>
<property name="ipAddress">centos-node6property>
factoryConfig>
dbServer>
<dbServer name="slave" parent="abstractServer">
<factoryConfig>
<property name="ipAddress">centos-node7property>
factoryConfig>
dbServer>
<dbServer name="slaves" virtual="true">
<poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
<property name="loadbalance">1property>
<property name="poolNames">slaveproperty>
poolConfig>
dbServer>
<proxy>
<service name="Amoeba for Mysql" class="com.meidusa.amoeba.net.ServerableConnectionManager">
<property name="port">8066property>
<property name="manager">${clientConnectioneManager}property>
<property name="connectionFactory">
<bean class="com.meidusa.amoeba.mysql.net.MysqlClientConnectionFactory">
<property name="sendBufferSize">128property>
<property name="receiveBufferSize">64property>
bean>
property>
<property name="authenticator">
<bean class="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator">
<property name="user">rootproperty>
<property name="password">rootproperty>
<property name="filter">
<bean class="com.meidusa.amoeba.server.IPAccessController">
<property name="ipFile">${amoeba.home}/conf/access_list.confproperty>
bean>
property>
bean>
property>
service>
<service name="Amoeba Monitor Server" class="com.meidusa.amoeba.monitor.MonitorServer">
<property name="ipAddress">127.0.0.1property>
<property name="daemon">trueproperty>
<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">
<property name="readThreadPoolSize">20property>
<property name="clientSideThreadPoolSize">30property>
<property name="serverSideThreadPoolSize">30property>
<property name="statementCacheSize">500property>
<property name="queryTimeout">60property>
runtime>
proxy>
<connectionManagerList>
<connectionManager name="clientConnectioneManager" class="com.meidusa.amoeba.net.MultiConnectionManagerWrapper">
<property name="subManagerClassName">com.meidusa.amoeba.net.ConnectionManagerproperty>
connectionManager>
<connectionManager name="defaultManager" class="com.meidusa.amoeba.net.MultiConnectionManagerWrapper">
<property name="subManagerClassName">com.meidusa.amoeba.net.AuthingableConnectionManagerproperty>
connectionManager>
connectionManagerList>
<dbServerLoader class="com.meidusa.amoeba.context.DBServerConfigFileLoader">
<property name="configFile">${amoeba.home}/conf/dbServers.xmlproperty>
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.xmlproperty>
<property name="functionFile">${amoeba.home}/conf/ruleFunctionMap.xmlproperty>
bean>
property>
<property name="sqlFunctionFile">${amoeba.home}/conf/functionMap.xmlproperty>
<property name="LRUMapSize">1500property>
<property name="defaultPool">masterproperty>
<property name="writePool">masterproperty>
<property name="readPool">slavesproperty>
<property name="needParse">trueproperty>
queryRouter>
Amoeba 相关配置文件结束;
三 Amoeba 读写分离 测试
# ./amoeba start
启动失败了,原因 Amoeba 启动 指定的堆栈大小太小,指定至少228k;解决办法 :
打开bin/amoeba,DEFAULT_OPTS=”-server -Xms256m -Xmx256m -Xss128k”改成:DEFAULT_OPTS=”-server -Xms512m -Xmx512m -Xmn100m -Xss1204k”
启动成功如下:
测试 读写分离结果,我们在主数据库 写入5条数据,在链接amoeba链接的客户端查看,结果是查询5条数据,管理 slave stop 服务,在从数据库,写入一条数据,再次查询 结果多了一条数据,说明读写分离完成;
相关的参考文档:http://blog.csdn.net/liulihui1988/article/details/51985517;
http://blog.csdn.net/liulihui1988/article/details/79066755
http://www.iteye.com/topic/1113437