MySQL读写分离之amoeba

MySQL读写分离之amoeba
主从复制的搭建环境参考:http://www.cnblogs.com/fansik/p/5270334.html
amoeba依赖于jdk环境:
jdk环境搭建参考:http://www.cnblogs.com/fansik/p/5406827.html
amoeba的具体参数说明参考:http://blog.chinaunix.net/uid-20639775-id-154600.html
1、安装
mkdir /usr/local/amoeba
tar xf amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba
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 name="Amoeba for Mysql" class="com.meidusa.amoeba.net.ServerableConnectionManager">
            <property name="port">8066</property>
            <property name="ipAddress">0.0.0.0</property>
            <property name="manager">${clientConnectioneManager}</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="authenticator">
                <bean class="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator">
                    <property name="user">fansik</property>
                    <property name="password">fanjinbao</property>
                    <property name="filter">
                        <bean class="com.meidusa.amoeba.server.IPAccessController">
                            <property name="ipFile">${amoeba.home}/conf/access_list.conf</property>
                        </bean>
                    </property>
                </bean>
            </property>
        </service>
        <service name="Amoeba Monitor Server" class="com.meidusa.amoeba.monitor.MonitorServer">
            <property name="port">9066</property>
            <property name="ipAddress">127.0.0.1</property>
            <property name="daemon">true</property>
            <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">20</property>
            <property name="clientSideThreadPoolSize">30</property>
            <property name="serverSideThreadPoolSize">30</property>
            <property name="statementCacheSize">500</property>
            <property name="queryTimeout">60</property>
        </runtime>
        
    </proxy>
    <connectionManagerList>
        <connectionManager name="clientConnectioneManager" class="com.meidusa.amoeba.net.MultiConnectionManagerWrapper">
            <property name="subManagerClassName">com.meidusa.amoeba.net.ConnectionManager</property>
        </connectionManager>
        <connectionManager name="defaultManager" class="com.meidusa.amoeba.net.MultiConnectionManagerWrapper">
            <property name="subManagerClassName">com.meidusa.amoeba.net.AuthingableConnectionManager</property>
        </connectionManager>
    </connectionManagerList>
    
    <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>
        <property name="defaultPool">master</property>
        <property name="writePool">master</property>
        <property name="readPool">multiPool</property>
        <property name="needParse">true</property>
    </queryRouter>
</amoeba:configuration>

3、dbServer.xml配置信息

<?xml version="1.0" encoding="gbk"?>
<!DOCTYPE amoeba:dbServers SYSTEM "dbserver.dtd">
<amoeba:dbServers xmlns:amoeba="http://amoeba.meidusa.com/">
    <dbServer name="master">
        <factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">
                        <property name="manager">${defaultManager}</property>
                        <property name="sendBufferSize">64</property>
                        <property name="receiveBufferSize">128</property>
            <property name="ipAddress">192.168.1.1.210</property>
                        <property name="port">3401</property>
                        <property name="schema">fansik</property>
                        <property name="user">fansik</property>
                        <property name="password">fansik</property>
                </factoryConfig>
                <poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool">
                        <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="testOnReturn">true</property>
                        <property name="testWhileIdle">true</property>
                </poolConfig>
    </dbServer>
    <dbServer name="slave">
        <factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">
                        <property name="manager">${defaultManager}</property>
                        <property name="sendBufferSize">64</property>
                        <property name="receiveBufferSize">128</property>
            <property name="ipAddress">192.168.1.1.210</property>
                        <property name="port">3402</property>
                        <property name="schema">fansik</property>
                        <property name="user">fansik</property>
                        <property name="password">fansik</property>
                </factoryConfig>
                <poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool">
                        <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="testOnReturn">true</property>
                        <property name="testWhileIdle">true</property>
                </poolConfig>
    </dbServer>
    <dbServer name="multiPool" virtual="true">
        <poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
            <property name="loadbalance">1</property>
            <property name="poolNames">master,slave</property>
        </poolConfig>
    </dbServer>
</amoeba:dbServers>

4、System-V方式启动amoeba,脚本如下:

#!/bin/sh
# chkconfig: 12345 62 62
# Author: fansik
# description: amoeba 2.2.0 AutoRun Servimces # /etc/init.d/amoeba # # Run-level Startup script for the Oracle Instance, Listener, and # Web Interface export JAVA_HOME=/usr/local/jdk1.8 export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin NAME=Amoeba AMOEBA_BIN=/usr/local/amoeba/bin PIDFILE=/usr/local/amoeba/Amoeba-MySQL.pid SCRIPTNAME=/etc/init.d/amoeba case "$1" in start) echo -n "Starting $NAME... " $AMOEBA_BIN/amoeba start &>/dev/null & echo -e "\t[ \033[1;32mok\033[0m ]" ;; stop) echo -n "Stoping $NAME... " $AMOEBA_BIN/amoeba stop &>/dev/null echo -e "\t[ \033[1;32mok\033[0m ]" ;; restart) echo -n "Restarting $NAME..." $AMOEBA_BIN/amoeba stop &>/dev/null sleep 1 $AMOEBA_BIN/amoeba start &>/dev/null & echo -e "\t[ \033[1;32mok\033[0m ]" ;; *) echo "Usage: $SCRIPTNAME {start|stop|restart}" exit 1 ;; esac

5、开机自启动设置
chmod +x /etc/init.d/amoeba
chkconfig --add amoeba
chkconfig amoeba on
6、搭建过程中遇到的问题
    在测试读写分离的时候不能开启从库复制,也就是要登录slave数据库执行stop slave,否则无法验证而且在主库停掉服务后,也是无法读的,
因为主库停掉以后,从库的状态会变成Slave_IO_Running: No。

   在启动amoeba的时候需要将Xss:每个线程的堆栈大小增加,否则无法启动服务,默认大小是128,调整为256即可,配置位置就是amoeba的bin目录下,一个叫做amoeba的文件。

posted @ 2017-07-14 09:36  fansik  阅读(309)  评论(0编辑  收藏  举报