Mysql基於Amoeba_讀寫分離搭架
一、Amoeba簡介
Amoeba是一個以MySQL為底層資料儲存,並對應用提供MySQL協議介面的proxy。它集中地響應應用的請求,依據使用者事先設定的規則,將SQL請求傳送到特定的資料庫上執行。基於此可以實現負載均衡、讀寫分離、高可用性等需求,
Amoeba相當於一個SQL請求的路由器,目的是為負載均衡、讀寫分離、高可用性提供機制,而不是完全實現它們。需要結合使用MySQL的 Replication等機制來實現副本同步等功能
二、Amoeba搭建過程
在搭建amoeba過程中,我們首先需要準備環境,這裡我們提供了三臺Linux伺服器,分別是:
Amoeba for MySQL:centos-node5(主機名)
master:centos-node6
slave:centos-node7
Amoeba 伺服器,master 主伺服器,slave 從伺服器,服務上分別都安裝了,Jdk1.7以上版本,相關jdk安裝參考
並且已經安裝了相關的主從資料庫,相關安裝參考:
- Linux 命令下載地址:
- Amoeba 安裝
命令建立amoeba資料夾,在/usr/local/amoeba目錄下 執行下載以上地址
mkdir /usr/local/amoeba
執行以下命令解壓在當前資料夾下
# tar xvf amoeba-mysql-binary-2.1.0-RC5.tar.gz
- 驗證Amoeba是否安裝成功的命令在 /usr/local/amoeba/bin/ 目錄下執行,
# ./amoeba
如上圖,表示安裝成功
- 修改配置檔案 dbServer.xml
dbServer.xml 檔案在 /usr/local/amoeba/conf/目錄下 命令編輯
# vim dbServer.xml
需要修改的 程式碼部分
<!-- 資料庫連線配置的公共部分 -->
<dbServer name="abstractServer" abstractive="true">
<factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">
<property name="manager">${defaultManager}</property>
<property name="sendBufferSize">64</property>
<property name="receiveBufferSize">128</property>
<!-- mysql port 埠號 -->
<property name="port">3306</property>
<!-- mysql schema amoeba 訪問主從資料庫真實庫-->
<property name="schema">test</property>
<!-- mysql user 主從資料庫分配給Amoeba訪問資料的使用者名稱 -->
<property name="user">proxyuser</property>
<!-- mysql password 主從資料庫分配給Amoeba訪問資料的密碼-->
<property name="password">123456</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="testWhileIdle">true</property>
</poolConfig>
</dbServer>
<!-- Master 的獨立部分,也就只有 IP 了這裡 寫了主機名 -->
<dbServer name="master" parent="abstractServer">
<factoryConfig>
<!-- mysql ip -->
<property name="ipAddress">centos-node6</property>
</factoryConfig>
</dbServer>
<!-- Slave 的獨立部分,也就只有 IP 了這裡 寫了主機名 ,如果有多個Slave伺服器,可以配置多個dbServer -->
<dbServer name="slave" parent="abstractServer">
<factoryConfig>
<!-- mysql ip -->
<property name="ipAddress">centos-node7</property>
</factoryConfig>
</dbServer>
<!-- 資料庫池,虛擬伺服器,實現讀取的負載均衡,如果有多個Slave,則<property name="poolNames">slave1,slave2</property>用逗號隔開 -->
<dbServer name="slaves" 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">slave</property>
</poolConfig>
</dbServer>
- 修改amoeba.xml檔案,設定讀寫分離
<proxy>
<!-- service class must implements com.meidusa.amoeba.service.Service -->
<service name="Amoeba for Mysql" class="com.meidusa.amoeba.net.ServerableConnectionManager">
<!-- Amoeba 埠號 ,客戶端client 連結amoeba埠號,不能和主從資料庫 衝突-->
<property name="port">8066</property>
<!-- bind ipAddress -->
<!--
<property name="ipAddress">127.0.0.1</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">
<!-- Amoeba 賬號 ,客戶端client 連結amoeba端 賬號-->
<property name="user">root</property>
<!-- Amoeba 賬號 ,客戶端client 連結amoeba端 密碼-->
<property name="password">root</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>
<!-- server class must implements com.meidusa.amoeba.service.Service -->
<service name="Amoeba Monitor Server" class="com.meidusa.amoeba.monitor.MonitorServer">
<!-- port -->
<!-- default value: random number
<property name="port">9066</property>
-->
<!-- bind ipAddress -->
<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">
<!-- proxy server net IO Read thread size -->
<property name="readThreadPoolSize">20</property>
<!-- proxy server client process thread size -->
<property name="clientSideThreadPoolSize">30</property>
<!-- mysql server data packet process thread size -->
<property name="serverSideThreadPoolSize">30</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="clientConnectioneManager" class="com.meidusa.amoeba.net.MultiConnectionManagerWrapper">
<property name="subManagerClassName">com.meidusa.amoeba.net.ConnectionManager</property>
<!--
default value is avaliable Processors
<property name="processors">5</property>
-->
</connectionManager>
<connectionManager name="defaultManager" class="com.meidusa.amoeba.net.MultiConnectionManagerWrapper">
<property name="subManagerClassName">com.meidusa.amoeba.net.AuthingableConnectionManager</property>
<!--
default value is avaliable Processors
<property name="processors">5</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>
<!-- 讀資料庫,dbServer.xml 中配置的 虛擬資料庫,資料庫池 -->
<property name="readPool">slaves</property>
<property name="needParse">true</property>
</queryRouter>
Amoeba 相關配置檔案結束;
三 Amoeba 讀寫分離 測試
- 啟動amoeba服務,命令進入amoeba/bin目錄執行
# ./amoeba start
啟動失敗了,原因 Amoeba 啟動 指定的堆疊大小太小,指定至少228k;解決辦法 :
開啟bin/amoeba,DEFAULT_OPTS=”-server -Xms256m -Xmx256m -Xss128k”改成:DEFAULT_OPTS=”-server -Xms512m -Xmx512m -Xmn100m -Xss1204k”
啟動成功如下:
測試 讀寫分離結果,我們在主資料庫 寫入5條資料,在連結amoeba連結的客戶端檢視,結果是查詢5條資料,管理 slave stop 服務,在從資料庫,寫入一條資料,再次查詢 結果多了一條資料,說明讀寫分離完成;