作为一名小白,在发现项目中环境崩溃的时候我果断选择了各种暴力删除和重启,经过不断的尝试终于把Amoeba所在的环境彻底搞废了。。。
项目场景
1、主数据库服务器10.10.20.111作为写库,从数据库服务器10.10.20.116作为读库
2、Amoeba安装在主数据库服务器上
准备工作
1、安装java环境
Amoeba是基于jdk1.5开发的,所以需要先安装java环境,安装步骤戳这里,jdk下载戳这里
2、安装Amoeba
Amoeba下载戳这里,我下载的是amoeba-mysql-3.0.5-RC-distribution.zip。下载完成后将压缩文件解压到/usr/local/amoeba目录下
Amoeba配置
Amoeba配置文件位于/usr/local/amoeba/conf目录下,配置读写分离我们只需关注dbServers.xml和amoeba.xml
1、dbServers.xml
<?xml version="1.0" encoding="gbk"?>
<!DOCTYPE amoeba:dbServers SYSTEM "dbserver.dtd">
<amoeba:dbServers xmlns:amoeba="http://amoeba.meidusa.com/">
<!--
Each dbServer needs to be configured into a Pool,
If you need to configure multiple dbServer with load balancing that can be simplified by the following configuration:
add attribute with name virtual = "true" in dbServer, but the configuration does not allow the element with name factoryConfig
such as 'multiPool' dbServer
-->
<dbServer name="abstractServer" abstractive="true">
<factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">
<property name="connectionManager">${defaultManager}</property>
<property name="sendBufferSize">64</property>
<property name="receiveBufferSize">128</property>
<!-- mysql port -->
#设置Amoeba要连接的mysql数据库的端口,默认是3306
<property name="port">3306</property>
<!-- mysql schema -->
#设置缺省的数据库,当连接amoeba时,操作表必须显式的指定数据库名,即采用dbname.tablename的方式,不支持 use dbname指定缺省库,因为操作会调度到各个后端数据库
<property name="schema">db</property>
<!-- mysql user -->
#设置amoeba连接后端数据库服务器的账号和密码,因此需要在所有后端数据库上创建该用户,并授权amoeba服务器可连接
<property name="user">amoeba</property>
<property name="password">1234</property>
</factoryConfig>
<poolConfig class="com.meidusa.toolkit.common.poolable.PoolableObjectPool">
#最大连接数,默认500
<property name="maxActive">500</property>
#最大空闲连接数
<property name="maxIdle">500</property>
#最小空闲连接数
<property name="minIdle">10</property>
<property name="minEvictableIdleTimeMillis">600000</property>
<property name="timeBetweenEvictionRunsMillis">600000</property>
<property name="testOnBorrow">true</property>
<property name="testWhileIdle">true</property>
</poolConfig>
</dbServer>
#设置写库信息,名字可以任意命名
<dbServer name="master" parent="abstractServer">
<factoryConfig>
<!-- mysql ip -->
#写库既主数据库服务器地址
<property name="ipAddress">10.10.20.111</property>
</factoryConfig>
</dbServer>
#设置读库信息,名字可以任意命名
<dbServer name="slave1" parent="abstractServer">
<factoryConfig>
<!-- mysql ip -->
#读库既从数据库服务器地址
<property name="ipAddress">10.10.20.116</property>
</factoryConfig>
</dbServer>
</amoeba:dbServers>
2、amoeba.xml
<?xml version="1.0" encoding="gbk"?>
<!DOCTYPE amoeba:configuration SYSTEM "amoeba.dtd">
<amoeba:configuration xmlns:amoeba="http://amoeba.meidusa.com/">
<proxy>
<!-- service class must implements com.meidusa.amoeba.service.Service -->
<service name="Amoeba for Mysql" class="com.meidusa.amoeba.mysql.server.MySQLService">
<!-- port -->
#设置amoeba监听的端口,默认是8066
<property name="port">8066</property>
<!-- bind ipAddress -->
#设置amoeba监听的地址,如果不设置,默认监听所有地址
<!--
<property name="ipAddress">127.0.0.1</property>
-->
<property name="connectionFactory">
<bean class="com.meidusa.amoeba.mysql.net.MysqlClientConnectionFactory">
<property name="sendBufferSize">128</property>
<property name="receiveBufferSize">64</property>
</bean>
</property>
<property name="authenticateProvider">
<bean class="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator">
<!-- 登录Amoeba的账号密码(这里的账号密码和amoeba连接后端数据库服务器的密码无关) -->
<property name="user">root</property>
<property name="password">root</property>
<property name="filter">
<bean class="com.meidusa.toolkit.net.authenticate.server.IPAccessController">
<property name="ipFile">${amoeba.home}/conf/access_list.conf</property>
</bean>
</property>
</bean>
</property>
</service>
<runtime class="com.meidusa.amoeba.mysql.context.MysqlRuntimeContext">
<property name="readThreadPoolSize">20</property>
<property name="clientSideThreadPoolSize">30</property>
<property name="serverSideThreadPoolSize">30</property>
<!-- proxy server client process thread size -->
<property name="executeThreadSize">128</property>
<!-- per connection cache prepared statement size -->
<property name="statementCacheSize">500</property>
<!-- default charset -->
<property name="serverCharset">utf8</property>
<!-- query timeout( default: 60 second , TimeUnit:second) -->
<property name="queryTimeout">60</property>
</runtime>
</proxy>
<!--
Each ConnectionManager will start as thread
manager responsible for the Connection IO read , Death Detection
-->
<connectionManagerList>
<connectionManager name="defaultManager" class="com.meidusa.toolkit.net.MultiConnectionManagerWrapper">
<property name="subManagerClassName">com.meidusa.toolkit.net.AuthingableConnectionManager</property>
</connectionManager>
</connectionManagerList>
<!-- default using file loader -->
<dbServerLoader class="com.meidusa.amoeba.context.DBServerConfigFileLoader">
<property name="configFile">${amoeba.home}/conf/dbServers.xml</property>
</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.xml</property>
<property name="functionFile">${amoeba.home}/conf/ruleFunctionMap.xml</property>
</bean>
</property>
<property name="sqlFunctionFile">${amoeba.home}/conf/functionMap.xml</property>
<property name="LRUMapSize">1500</property>
#设置amoeba默认的池,这里设置为master
<property name="defaultPool">master</property>
#设置读写池,在上一个配置文件中已定义好
<property name="writePool">master</property>
<property name="readPool">slave1</property>
<property name="needParse">true</property>
</queryRouter>
</amoeba:configuration>
3、授权Amoeba
分别在主数据库和从数据库为Amoeba授权
mysql>grant all on db.* to 'amoeba'@'10.10.20.111' identified by '1234';
mysql>flush privileges;
4、启动Amoeba
cd /usr/local/amoeba/bin
./launcher start
由于Amoeba的运行是基于Java的,所以在初次运行时由于stack size太小导致jvm启动失败。我们可以修改jvm.properties文件中的参数,然后重新启动
vi /usr/local/amoeba/jvm.properties
#JVM_OPTIONS="-server -Xms256m -Xmx1024m -Xss196k -XX:PermSize=16m -XX:MaxPermSize=96m"
JVM_OPTIONS="-server -Xms256m -Xmx1024m -Xss512k"
测试读写分离
1、关闭MySQL主从同步功能,在从数据库服务器中执行
mysql>stop slave;
2、登录Amoeba,插入数据
mysql -uroot -proot -P8066
mysql>use db;
mysql>insert into test(name) values('amoeba');
mysql>select * from test;
并没有查到新增的数据
3、登录主数据库服务器
mysql -uroot -proot
mysql>use db;
mysql>select * from test;
可以看到刚才新增的数据
4、从数据库服务器开启MySQL主从同步
mysql>start slave;
5、在Amoeba中重新查询
mysql>select * from test
现在可以查到新增的数据了,说明写是在主数据库执行,查是在从数据库执行