Amoeba變形蟲(chóng)(阿米巴)入門(mén)級(jí)代理服務(wù)器
Amoeba是一個(gè)以MySQL為底層數(shù)據(jù)存儲(chǔ),并對(duì)應(yīng)用提供MySQL協(xié)議接口的proxy焚刺。它集中地響應(yīng)應(yīng)用的請(qǐng)求门烂,依據(jù)用戶事先設(shè)置的規(guī)則,將SQL請(qǐng)求發(fā)送到特定的數(shù)據(jù)庫(kù)上執(zhí)行蔓姚。基于此可以實(shí)現(xiàn)負(fù)載均衡坡脐、讀寫(xiě)分離房揭、高可用性等需求。與MySQL官方的MySQL Proxy相比恬砂,作者強(qiáng)調(diào)的是amoeba配置的方便(基于XML的配置文件蓬痒,用SQLJEP語(yǔ)法書(shū)寫(xiě)規(guī)則,比基于lua腳本的MySQL Proxy簡(jiǎn)單)梧奢。
搭建步驟
1配置jdk環(huán)境
上傳jdk
解壓jdk
配置環(huán)境變量:vim /etc/profile
4.重寫(xiě)加載 source /etc/profile(手動(dòng)刷新環(huán)境變量-沒(méi)報(bào)任何錯(cuò)就是配置好了)
2安裝Amoeba
[root@localhost src]# ls
amoeba-mysql-3.0.4-BETA.tar.gz java
[root@localhost src]# tar -xvf amoeba-mysql-3.0.4-BETA.tar.gz
[root@localhost src]# ls
amoeba-mysql-3.0.4-BETA amoeba-mysql-3.0.4-BETA.tar.gz java
[root@localhost src]# rm amoeba-mysql-3.0.4-BETA.tar.gz
rm: remove regular file `amoeba-mysql-3.0.4-BETA.tar.gz'? y
[root@localhost src]# mv amoeba-mysql-3.0.4-BETA amoeba
[root@localhost src]# ls
amoeba java
[root@localhost src]# cd amoeba/
[root@localhost amoeba]# ls
benchmark bin conf jvm.properties lib
[root@localhost amoeba]# cd conf
[root@localhost conf]# ls
access_list.conf dbserver.dtd functionMap.xml rule.dtd
amoeba.dtd dbServers.xml log4j.dtd ruleFunctionMap.xml
amoeba.xml function.dtd log4j.xml rule.xml
更改dbServer
connectionManager連接管理器
sendBufferSize緩沖區(qū)大小
maxActive最大活躍數(shù)量
minIdle最小活躍數(shù)量
<!-- mysql port -->
<property name="port">3306</property>
定義公共連接端口
修改如下配置
<!-- mysql user -->
<property name="user">root</property>
<property name="password">root</property>
<!--配置主庫(kù)-->
<dbServer name="master" parent="abstractServer">
<factoryConfig>
<!-- mysql ip -->
<property name="ipAddress">192.168.161.132</property>
</factoryConfig>
</dbServer>
<!--配置從庫(kù)-->
<dbServer name="slave01" parent="abstractServer">
<factoryConfig>
<!-- mysql ip -->
<property name="ipAddress">192.168.161.133</property>
</factoryConfig>
</dbServer>
<!--實(shí)現(xiàn)負(fù)載均衡-->
<!--主讀操作-->
<dbServer name="multiPool" virtual="true">
<poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
<!--負(fù)載均衡方式
WEIGHTBASED 請(qǐng)求發(fā)往負(fù)載較少的機(jī)器-->
<!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->
<property name="loadbalance">1</property>
<!--三次訪問(wèn)僅一次訪問(wèn)主庫(kù)-->
<!-- Separated by commas,such as: server1,server2,server1 -->
<property name="poolNames">slave01,master,slave01</property>
</poolConfig>
</dbServer>
完整版
<?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 -->
<property name="port">3306</property>
<!-- mysql schema -->
<property name="schema">test</property>
<!-- mysql user -->
<property name="user">root</property>
<property name="password">root</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>
<!--每次獲取連接時(shí)都會(huì)測(cè)試瓶埋,如果測(cè)試不同換下一個(gè)連接-->
<property name="testOnBorrow">true</property>
<property name="testOnReturn">true</property>
<!--連接不夠時(shí),創(chuàng)建連接-->
<property name="testWhileIdle">true</property>
</poolConfig>
</dbServer>
<!--配置主庫(kù)-->
<dbServer name="master" parent="abstractServer">
<factoryConfig>
<!-- mysql ip -->
<property name="ipAddress">192.168.161.132</property>
</factoryConfig>
</dbServer>
<!--配置從庫(kù)-->
<dbServer name="slave01" parent="abstractServer">
<factoryConfig>
<!-- mysql ip -->
<property name="ipAddress">192.168.161.133</property>
</factoryConfig>
</dbServer>
<!--實(shí)現(xiàn)負(fù)載均衡-->
<!--主讀操作-->
<dbServer name="multiPool" virtual="true">
<poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
<!--負(fù)載均衡方式
WEIGHTBASED 請(qǐng)求發(fā)往負(fù)載較少的機(jī)器-->
<!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->
<property name="loadbalance">1</property>
<!--三次訪問(wèn)僅一次訪問(wèn)主庫(kù)-->
<!-- Separated by commas,such as: server1,server2,server1 -->
<property name="poolNames">slave01,master,slave01</property>
</poolConfig>
</dbServer>
</amoeba:dbServers>
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.161.134:8066/jtdb2?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
jdbc.username=root
jdbc.password=123456
amoeba.xml
<bean class="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator">
<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>
放開(kāi)
<property name="writePool">server1</property>
<property name="readPool">server1</property>
修改
<property name="writePool">master</property>
<property name="readPool">multiPool</property>
完整版
<?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 服務(wù)器連接Amoeba的端口-->
<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">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">
<!-- proxy server client process thread size -->
<property name="executeThreadSize">128</property>
<!-- per connection cache prepared statement size -->
<property name="statementCacheSize">500</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">multiPool</property>
<property name="writePool">master</property>
<property name="readPool">multiPool</property>
<property name="needParse">true</property>
</queryRouter>
</amoeba:configuration>
修改jvm內(nèi)存
jvm.properties
# JVM相關(guān)的參數(shù),包括內(nèi)存配置晕粪、垃圾回收策略
JVM_OPTIONS="-server -Xms256m -Xmx1024m -Xss196k -XX:PermSize=16m -XX:MaxPermSize=96m"
完整版
# app名字
APP_NAME=Amoeba-MySQL
# app版本號(hào)
APP_VERSION=3.0.0-beta
# 日志輸出路徑挤悉,log4j中可引用參數(shù) ${project.output}
APP_OUTPUT_PATH=$PROJECT_HOME/logs
# 應(yīng)用程序的PID文件存放路徑, 默認(rèn)存放在: ${project.home}/${APP_NAME}.pid
#APP_PID_PATH=/temp/logs/$APP_NAME
# 控制臺(tái)輸出到日志文件
APP_CONSOLE_LOG=$APP_OUTPUT_PATH/console.log
# 程序相關(guān)的配置參數(shù)
#APP_OPTIONS="-DmyParam=value1 -DmyParam2=value2"
# 啟動(dòng)參數(shù)
#APP_ARGS="args0 "
# JVM相關(guān)的參數(shù),包括內(nèi)存配置巫湘、垃圾回收策略
JVM_OPTIONS="-server -Xms256m -Xmx1024m -Xss256k -XX:PermSize=16m -XX:MaxPermSize=96m"
# 應(yīng)用程序忽略的信號(hào)列表装悲,以逗號(hào)分割昏鹃,程序shutdown的信號(hào)為15(可用 kill -15 pid 可讓程序文明的shutdown,請(qǐng)不要在這兒填15)
IGNORE_SIGNALS=1,2
-Xms256m 初始內(nèi)存大小256m
-Xmx1024m 最大內(nèi)存 1g
-Xss196k 每個(gè)線程的大小196kb
這個(gè)要改
Amoeba對(duì)線程有要求至少需要217kb
我們?nèi)≌麛?shù)改成256
啟動(dòng)Amoeba
1關(guān)閉防火墻
benchmark.classpath launcher.bat mkdirhier
benchmark.bat launcher launcher.classpath shutdown
[root@localhost bin]# pwd
/usr/local/src/amoeba/bin
[root@localhost bin]# service iptables stop
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Flushing firewall rules: [ OK ]
iptables: Unloading modules: [ OK ]
[root@localhost bin]# ./launcher
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.161.134:8066/jtdb2?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
jdbc.username=root
jdbc.password=root