Amoeba是一个以MySQL为底层数据存储,并对应用提供MySQL协议接口的proxy。它集中地响应应用的请求,依据用户事先设置的规则,将SQL请求发送到特定的数据库上执行。基于此可以实现负载均衡、读写分离、高可用性等需求。

## 安装java环境 因为amoeba需要java环境,所以首先安装java环境,请参考之前的文档 ## 下载Amoeba 访问[https://nchc.dl.sourceforge.net/project/](https://nchc.dl.sourceforge.net/project/)查找最新版amoeba ``` wget https://nchc.dl.sourceforge.net/project/amoeba/Amoeba%20for%20mysql/3.x/amoeba-mysql-3.0.5-RC-distribution.zip yum install unzip unzip amoeba-mysql-3.0.5-RC-distribution.zip ln -s /data/amoeba-mysql-3.0.5-RC /usr/local/amoeba ``` ## 修改配置dbServers.xml ``` [root@mysql-amoeba conf]# cat /usr/local/amoeba/conf/dbServers.xml
<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 -->
        <property name="port">3306</property>

        <!-- mysql schema -->
        <property name="schema">zabbix</property>

        <!-- mysql user -->
        <property name="user">zabbix</property>

        <property name="password">zabbix</property>
    </factoryConfig>

    <poolConfig class="com.meidusa.toolkit.common.poolable.PoolableObjectPool">
        <property name="maxActive">500</property>
        <property name="maxIdle">500</property>
        <property name="minIdle">1</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="master"  parent="abstractServer">
    <factoryConfig>
        <!-- mysql ip -->
        <property name="ipAddress">172.31.101.22</property>
    </factoryConfig>
</dbServer>

<dbServer name="slave1"  parent="abstractServer">
    <factoryConfig>
        <!-- mysql ip -->
        <property name="ipAddress">172.31.101.23</property>
    </factoryConfig>
</dbServer>

<dbServer name="multiPool" 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">slave1</property>
    </poolConfig>
</dbServer>

/amoeba:dbServers

## 修改配置amoeba.xml

[root@mysql-amoeba conf]# cat /usr/local/amoeba/conf/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 -->
        <property name="port">8066</property>

        <!-- bind ipAddress -->
        <!--
        <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">

                <property name="user">zabbix</property>

                <property name="password">zabbix</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">

        <!-- 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>
    <property name="defaultPool">master</property>

    <!--
    <property name="writePool">master</property>
    <property name="readPool">slave1</property>
    -->
    <property name="needParse">true</property>
</queryRouter>

/amoeba:configuration ```

修改java参数

打开/usr/local/amoeba/jvm.properties文件,修改下面这段参数,主要是分配JVM内存大小。默认值有点小。 JVM_OPTIONS="-server -Xms1024m -Xmx1024m -Xss256k -XX:PermSize=16m -XX:MaxPermSize=96m"

基础命令

  • /usr/local/amoeba/bin/launcher > /var/log/amoeba.log 2>&1 & #后台启动
  • /usr/local/amoeba/bin/shutdown #关闭

注意

日志巨大,日志巨大,日志巨大

当读写频繁时,会产生较大的日志,建议做定期删除。