一.amoeba介绍
网址:http://docs.hexnova.com/amoeba/
二.安装java se1.5
三.安装amoeba2.2.0
1.下载地址:http://sourceforge.net/projects/amoeba/files/,下载amoeba-mysql-binary-2.2.0.tar.gz
2.解压后直接放入/usr/local/amoeba(该路径随意)
四.配置amoeba
*注意以下配置中,请记得将xml的注释去掉,密码项默认是注释的
1.amoeba配置
#vi /usr/local/amoeba/conf/amoeba.xml
配置端口,服务ip地址,登录用户名和密码
<property name="port">8066</property>
<property name="ipAddress">192.168.1.84</property>
<property name="user">testuser</property>
<property name="password">password</property>
2.测试
#mysql -u testuser -p -h 192.168.1.84 -P 8066
3.配置dbServers.xml
#vi /usr/local/amoeba/conf/dbServers.xml
配置数据库,账号和密码(该账号和密码是mysql的)
<property name="schema">dba_db</property>
<property name="user">test</property>
<property name="password">123456</property>
4.修改dbServers.xml,配置mysql服务器的ip地址和别名:
<dbServer name="master" parent="abstractServer">
<factoryConfig>
<!-- mysql ip -->
<property name="ipAddress">192.168.1.126</property>
</factoryConfig>
</dbServer>
<dbServer name="slave" parent="abstractServer">
<factoryConfig>
<!-- mysql ip -->
<property name="ipAddress">127.0.0.1</property>
<property name="user">mytest</property>
<property name="password">pwd</property>
</factoryConfig>
</dbServer>
因为slave的账号和密码不同,所以在slave节点下再次设置账号密码
5.修改dbServers.xml,设置ROUNDROBIN(轮询策略)
<dbServer name="virtualSlave" virtual="true">
<poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
<!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->
<property name="loadbalance">1</property>
<!-- Separated by commas,such as: server1,server2,server1 -->
<property name="poolNames">slave,slave,master,master</property>
</poolConfig>
</dbServer>
6.修改amoeba.xml,设置读写分离
在<queryRouter class="com.meidusa.amoeba.mysql.parser.MysqlQueryRouter">段设置
<property name="defaultPool">master</property>
<property name="writePool">master</property>
<property name="readPool">virtualSlave</property>
<property name="needParse">true</property>
7.重启amoeba
#/usr/local/amoeba/bin/amoeba start
8.测试
#mysql -u testuser -p -h 192.168.1.84 -P 8066
五.优化
1.修改log4j.xml 取消日志文件生成(太大了,磁盘很容易满),<param name="file" value="amoeba.home/logs/project.log"/>改成:<paramname="file"value="<![CDATA[{amoeba.home}/logs/project.log>/dev/null]]>"/>
2.性能优化,打开bin/amoeba,DEFAULT_OPTS="-server -Xms256m -Xmx256m -Xss128k"改成:DEFAULT_OPTS="-server -Xms512m -Xmx512m -Xmn100m -Xss1204k"
3.loadbalance元素设置了loadbalance策略的选项,这里选择第一个“ROUNDROBIN”轮询策略,该配置提供负载均衡、failOver、故障恢复功能。poolNames定义了其中的数据库节点配置(当然也可以是虚拟的节点)。此外对于轮询策略,poolNames还定义了其轮询规则,比如设置成“Slave1,Slave1,Slave2”那么Amoeba将会以两次Slave1,一次Slave2的顺序循环对这些数据库节点转发请求。
六.性能测试
#/usr/local/amoeba/bin/benchmark -P 8066 -c 1000 -f ../benchmark/query.xml -h 192.168.1.84 -n 100000 -u testuser -p password
query.xml如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties version="1.0">
<entry key="class">com.meidusa.amoeba.mysql.net.packet.QueryCommandPacket</entry>
<entry key="command">3</entry>
<entry key="query"><![CDATA[
SELECT * FROM dba_db.test WHERE id=191
]]>
</entry>
</properties>
参考文章:http://www.cnblogs.com/lhj588/archive/2012/11/19/2777897.html