MySQL讀寫分離-Amoeba

1.下載amoebamysql

    https://pan.baidu.com/s/1qDV8pHAg9w7yDfnL7-Ew3gsql

2.上傳amoebashell

    在xshell中使用rz -E命令便可上傳文件數據庫

    或者使用其餘ftp/sftp工具上傳app

3.安裝amoeba負載均衡

    解壓 tar -xf amoeba-mysql-3.0.4-BETA.tar.gz框架

4.配置dbServers.xmlide

    

<?xml version="1.0" encoding="gbk"?>

<!DOCTYPE amoeba:dbServers SYSTEM "dbserver.dtd">
<amoeba:dbServers xmlns:amoeba="http://amoeba.meidusa.com/">

	<!--
		<dbServer>是核心標籤,用來配置物理數據庫等
	-->
	
	<!-- abstractServer 是全部dbServer的父親,就像Java中的繼承,pom中的依賴;若是存在數據端口或者密碼不一致,須要配置多個abstractServer -->
	<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>
			<!-- 數據庫的端口號 -->
			<property name="port">3306</property>
			<!-- 數據庫中的databaseId -->
			<property name="schema">test</property>
			<!-- 數據庫用戶名 -->
			<property name="user">root</property>
			<!-- 數據庫密碼 -->
			<property name="password">12340101</property>
		</factoryConfig>
		<!-- amoeba 維持的鏈接池,池中放的數據庫鏈接 -->
		<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>

	<!-- 從數據庫1配置 -->
	<dbServer name="slave1"  parent="abstractServer">
		<factoryConfig>
			<property name="ipAddress">192.168.245.112</property>
		</factoryConfig>
	</dbServer>
	<!-- 從數據庫2配置 -->
	<dbServer name="master"  parent="abstractServer">
		<factoryConfig>
			<property name="ipAddress">192.168.245.111</property>
		</factoryConfig>
	</dbServer>
	<!-- 主數據庫配置 -->
	<dbServer name="slave2"  parent="abstractServer">
		<factoryConfig>
			<property name="ipAddress">192.168.245.113</property>
		</factoryConfig>
	</dbServer>
	<!-- 一個數據庫池,在讀寫分離中是關鍵,能夠在amoeba.xml中指定讀池爲它,那麼amoeba就會輪詢裏面的數據庫,作到負載均衡 -->
	<dbServer name="multiPool" virtual="true">
		<poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
			<!--
				loadbalance=1 根據poolNames配置的dbServer進行輪詢
				loadbalance=2 權重
				loadbalance=3 高可用
			-->
			<property name="loadbalance">1</property>
			<property name="poolNames">slave1,master,slave2</property>
		</poolConfig>
	</dbServer>
</amoeba:dbServers>

5.配置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">
			<!-- amoeba 監聽的端口 ,程序鏈接的時候就要指定爲該端口 -->
			<property name="port">8066</property>
			
			<!-- 綁定的IP -->
			<!-- 
			<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>
			
			
			<!-- 鏈接amoeba的用戶名和密碼,以及訪問列表 -->
			<property name="authenticateProvider">
				<bean class="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator">
					
					<property name="user">root</property>
					
					<property name="password">12340101</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>
		<!--
			重點在這裏:
			writePool:修改操做走master數據庫
			readPool :查詢操做走multiPool數據庫池
			defaultPool:若是沒有配置writePool和readPool,就走它指定的數據庫或者數據庫池
		-->
		<property name="defaultPool">multiPool</property>
		<property name="writePool">master</property>
		<property name="readPool">multiPool</property>
		<property name="needParse">true</property>
	</queryRouter>
</amoeba:configuration>

6.啓動amoeba.net

進入amoeba的bin目錄

./launcher start &

去logs目錄查看啓動參數

ps -ef|grep launcher檢查進程是否存在

netstat -an|grep 8066 檢查端口是否處理監聽狀態

7.驗證amoeba

打開Navicat,新建一個指向amoeba的鏈接,並打開數據庫

amoeba優缺點

    優勢:支持讀寫分離、分庫分表、高可用

    缺點:高可用不穩定,分庫分表支撐不好,稍微複雜的關聯就不行了,開發團隊也已經解散

讀寫分離開源框架發展過程:

    amoeba-cobar-mycat

amoeba已經被淘汰,開發團隊也沒有在維護,如今很流行mycat,它的讀寫分離、高可用、分庫分表、數據庫支撐比較全

MySQL讀寫分離-mycat

相關文章
相關標籤/搜索