在做之前,首先要实现 MYSQL--之--主从复制同步 .
首先介绍一下 amoeba :
1. 安装JDK环境 (略过,网上太多文章,自己搜一下)
2. 安装amoeba-mysql
下载amoeba-mysql,目前最新版本为amoeba-mysql-1.3.1-BETA.zip
mkdir /usr/local/amoeba/
wget http://sourceforge.net/projects/amoeba/files/latest
unzip amoeba-mysql-1.3.1-BETA.zip
配置文件位于conf目录下,执行文件位于bin目录下,解压后发现bin目录下的启动文件没有可执行权限,请执行:chmod -R +x /usr/local/amoeba/bin/
Amoeba For MySQL 的使用非常简单,所有的配置文件都是标准的XML 文件,总共有四个配置文件。分别为:
◆ amoeba.xml:主配置文件,配置所有数据源以及Amoeba 自身的参数设置;实现主从的话配置这个文件就可以了;
◆ rule.xml:配置所有Query 路由规则的信息;
◆ functionMap.xml:配置用于解析Query 中的函数所对应的Java 实现类;
◆ rullFunctionMap.xml:配置路由规则中需要使用到的特定函数的实现类;
3. 下面我们就来通过更改amoeba.xml配置文件实现mysql主从读写分离,我的配置如下:
<?xml version="1.0"encoding="gbk"?>
<!DOCTYPEamoeba:configuration SYSTEM "amoeba.dtd"><amoeba:configurationxmlns:amoeba="http://amoeba.meidusa.com/">
<server>
<!-- proxy server绑定的端口 -->
<propertyname="port">8066</property>
<!-- proxy server绑定的IP -->
<propertyname="ipAddress">192.168.0.4</property>
<!-- proxy server net IO Readthread size -->
<propertyname="readThreadPoolSize">100</property>
<!-- proxy server clientprocess thread size -->
<propertyname="clientSideThreadPoolSize">80</property>
<!-- mysql server data packetprocess thread size -->
<propertyname="serverSideThreadPoolSize">100</property>
<!-- socket Send and receiveBufferSize(unit:K)
-->
<propertyname="netBufferSize">128</property>
<!-- Enable/disableTCP_NODELAY (disable/enable Nagle's algorithm). -->
<propertyname="tcpNoDelay">true</property>
<!-- 对外验证的用户名 -->
<propertyname="user">root</property>
<!-- 对外验证的密码 -->
<propertyname="password">51cto</property>
<!-- query timeout( default:60 second , TimeUnit:second) -->
<propertyname="queryTimeout">60</property>
</server>
<!--
每个ConnectionManager都将作为一个线程启动。
manager负责Connection IO读写/死亡检测
-->
<connectionManagerList>
<connectionManagername="defaultManager"class="com.meidusa.amoeba.net.MultiConnectionManagerWrapper">
<propertyname="subManagerClassName">com.meidusa.amoeba.net.AuthingableConnectionManager</property>
<!--
default value is avaliable Processors
<propertyname="processors">5</property>
-->
</connectionManager>
</connectionManagerList>
<dbServerList>
<!--
一台mysqlServer 需要配置一个pool,
如果多台 平等的mysql需要进行loadBalance,
平台已经提供一个具有负载均衡能力的objectPool:com.meidusa.amoeba.mysql.server.MultipleServerPool
简单的配置是属性加上virtual="true",该Pool 不允许配置factoryConfig
或者自己写一个ObjectPool。
-->
<dbServername="server1">
<!--PoolableObjectFactory实现类 -->
<factoryConfigclass="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">
<propertyname="manager">defaultManager</property>
<!-- 真实mysql数据库端口-->
<propertyname="port">3306</property>
<!-- 真实mysql数据库IP-->
<propertyname="ipAddress">10.1.1.1</property>
<propertyname="schema">jafy</property>
<!-- 用于登陆mysql的用户名-->
<propertyname="user">jafy</property>
<!-- 用于登陆mysql的密码-->
<propertyname="password">51cto</property>
</factoryConfig>
<!-- ObjectPool实现类-->
<poolConfigclass="com.meidusa.amoeba.net.poolable.PoolableObjectPool">
<propertyname="maxActive">200</property>
<propertyname="maxIdle">200</property>
<property name="minIdle">10</property>
<propertyname="minEvictableIdleTimeMillis">600000</property>
<propertyname="timeBetweenEvictionRunsMillis">600000</property>
<propertyname="testOnBorrow">true</property>
<propertyname="testWhileIdle">true</property>
</poolConfig>
</dbServer>
<dbServername="server2">
<!--PoolableObjectFactory实现类 -->
<factoryConfigclass="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">
<propertyname="manager">defaultManager</property>
<!-- 真实mysql数据库端口-->
<propertyname="port">3306</property>
<!--真实mysql数据库IP -->
<propertyname="ipAddress">10.1.1.2</property>
<propertyname="schema">jafy</property>
<!-- 用于登陆mysql的用户名-->
<propertyname="user">jafy</property>
<!-- 用于登陆mysql的密码-->
<propertyname="password">51cto</property>
</factoryConfig>
<!-- ObjectPool实现类-->
<poolConfigclass="com.meidusa.amoeba.net.poolable.PoolableObjectPool">
<propertyname="maxActive">200</property>
<propertyname="maxIdle">200</property>
<propertyname="minIdle">10</property>
<propertyname="minEvictableIdleTimeMillis">600000</property>
<propertyname="timeBetweenEvictionRunsMillis">600000</property>
<propertyname="testOnBorrow">true</property>
<propertyname="testWhileIdle">true</property>
</poolConfig>
</dbServer>
<dbServername="master" virtual="true">
<poolConfigclass="com.meidusa.amoeba.server.MultipleServerPool">
<!-- 负载均衡参数1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->
<propertyname="loadbalance">1</property>
<!-- 参与该pool负载均衡的poolName列表以逗号分割-->
<propertyname="poolNames">server1</property>
</poolConfig>
</dbServer>
<dbServer name="slave"virtual="true">
<poolConfigclass="com.meidusa.amoeba.server.MultipleServerPool">
<!-- 负载均衡参数1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->
<propertyname="loadbalance">1</property>
<!-- 参与该pool负载均衡的poolName列表以逗号分割-->
<propertyname="poolNames">server2</property>
</poolConfig>
</dbServer>
</dbServerList>
<queryRouterclass="com.meidusa.amoeba.mysql.parser.MysqlQueryRouter">
<property name="ruleConfig">${amoeba.home}/conf/rule.xml</property>
<propertyname="functionConfig">${amoeba.home}/conf/functionMap.xml</property>
<propertyname="ruleFunctionConfig">${amoeba.home}/conf/ruleFunctionMap.xml</property>
<propertyname="LRUMapSize">1500</property>
<propertyname="defaultPool">master</property>
<propertyname="writePool">master</property>
<propertyname="readPool">slave</property>
<propertyname="needParse">true</property>
</queryRouter></amoeba:configuration>
4. 启动amoeba
/usr/local/amoeba/bin/amoeba&
检验启动是否成功(使用的是默认的8066端口):
[root@Centos2 amoeba]# ps aux | grep amoeba
root 24580 0.2 19.2 408912 49264 pts/1 Sl 12:52 0:11 /usr/java/jdk1.6/bin/java -server -Xms256m -Xmx256m -Xss128k -Damoeba.home=/usr/local/amoeba -Dclassworlds.conf=/usr/local/amoeba/bin/amoeba.classworlds -classpath /usr/local/amoeba/lib/classworlds-1.0.jar org.codehaus.classworlds.Launcher
[root@Centos2 amoeba]# netstat -lnp | grep java
tcp 0 0 ::ffff:192.168.1.159:8066 :::* LISTEN 24580/java
5、测试
测试之前先要保证amoeba-server有访问两个主从服务器jafy库的权限,在主从mysql上都执行:
grant all on jafy.* to jafy@'10.1.1.%' identified by '51cto';
测试的时候和我们平时使用一样,amoeba-mysql对我们应用透明,就是个mysql的代理了!
登录mysql使用如下命令(用户名密码和上面配置的要一致):
mysql -u root -p -h 192.168.0.4 -P 8066
测试之前可以停掉 主从复制同步, 在从服务器mysql2上
mysql> stop slave;
写入服务器: mysql1
读取服务器: mysql2
从代理服务器mysql-proxy 登入后 :
自己随便发挥吧, insert 数据 会插入到 mysql1,
读取会从mysql2 .
先到此为止吧.