当前位置: 首页 > 工具软件 > Amoeba > 使用案例 >

MySQL-Amoeba读写分离

龚玄天
2023-12-01

MySQL-Amoeba读写分离

Amoeba是基于Java语言编写的软件,所以要首先构建Java环境

实验环境

前提:一主两从已经建立

192.168.4.111 主库

192.168.4.112 从库

192.168.4.115 从库

192.168.4.144 安装中间件amoeba

192.168.4.113 安装mysql客户端,登录amoeba测试

挂载挂载镜像文件

mount -o loop amoeba-n.iso /iso
cd iso && ll
-r-xr-xr-x 1 root root   3302165 6月  12 2015 amoeba-mysql-1.3.1-BETA.zip  #amoeba软件包
-r-xr-xr-x 1 root root 138021223 6月  12 2015 jdk-7u40-linux-x64.gz  #Java语言环境jdk软件包

安装Java环境

tar -zxvf jdk-7u40-linux-x64.gz -C /usr/local/  #解压并放到local路径下
mv /usr/local/jdk1.7.0_40 jdk  #更改名称为jdk
vim /etc/profile  #增加以下内容
JAVA_HOME=/usr/local/jdk
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$CLASSPATH
export CLASSPATH

. /etc/profile  #刷新环境变量
java -version  #查看java的版本

安装amoeba服务器

1.解压amoeba压缩包

 unzip amoeba-mysql-1.3.1-BETA.zip -d /usr/local/amoeba  #安装到/usr/local下

2.修改amoeba配置文件

配置文件结构

#配置文件其实就是三个区域
server>
这个区域声明的是客户端,如php连接amoeba中间件的用户名称、密码、访问端口
</server>

<dbServerList>  #库列表
<dbServer name="server1"> #声明每一个库
这个区域声明的是一个主库/从库,包括数据库的ip、端口、数据库授权amoeba连接的用户及密码,以及共享的库名称。每个dbserver只声明一个库,如果有多个库,不论主从,则需要在声明多个dbserver,每一个dbserver的name值不能相同。
</dbServer>
<dbServer name="master/slave" virtual="true"> #声明主库池和从库池
这个区域声明的是主库池和从库池,每个库都是上边声明好的,主库写到一个池里,从库写到一个池里,为读写分离做分组准备
</dbServer>
</dbServerList>

<queryRouter class="com.meidusa.amoeba.mysql.parser.MysqlQueryRouter"> #查询的路由表信息
这里设置的是master(上边已经声的),执行的是写操作。slave(上边已经声明的),执行的是读操作,从而实现读写分离
</queryRouter>

配置文件解析

vim /usr/local/amoeba/conf/amoebaxml
    <?xml version="1.0" encoding="gbk"?>
<!DOCTYPE amoeba:configuration SYSTEM "amoeba.dtd">
<amoeba:configuration xmlns:amoeba="http://amoeba.meidusa.com/">
	<server>
		<!-- proxy server�樵Ķ˿� -->
		<property name="port">8066</property>  #php连接amoeba中间件的端口,可以修改
		
		<!-- proxy server�樵�IP -->
		<!-- 
		<property name="ipAddress">127.0.0.1</property>  #默认注释了,监听的地址,修改为amoeba端ip
		 -->
		<!-- 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>
		
		<!-- socket Send and receive BufferSize(unit:K)  -->
		<property name="netBufferSize">128</property>
		
		<!-- Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm). -->
		<property name="tcpNoDelay">true</property>
		
		<!-- ������֤���û��� -->
		<property name="user">root</property>  #客户端php连接到amoeba的用户名
		
		<!-- ������֤������ -->
		<!--  #打开注释
		<property name="password">123</property>  #客户端php连接到amoeba的用户密码,自己修改,默认注释
		-->
		
		<!-- query timeout( default: 60 second , TimeUnit:second) -->
		<property name="queryTimeout">60</property>
	</server>
	
	<!-- 
		ÿ��ConnectionManager������Ϊһ���߳������
		manager����Connection IO��д/�������
	-->
	<connectionManagerList>
		<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>
	
	<dbServerList>  #数据库的列表
		<!-- 
			һ̨mysqlServer ��Ҫ����һ��pool��
			�����̨ ƽ�ȵ�mysql��Ҫ����loadBalance�� 
			ƽ̨�Ѿ��ṩһ�����и��ؾ���������objectPool��com.meidusa.amoeba.mysql.server.MultipleServerPool
			�奵����������Լ��� virtual="true",��Pool ����������factoryConfig
			�����Լ�дһ��ObjectPool��
		-->
		<dbServer name="server1">  #主库的设定区域,一个server代表一个库,编号不能冲突
			
			<!-- PoolableObjectFactoryʵ���� -->
			<factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">
				<property name="manager">defaultManager</property>
				<!-- ��ʵmysql���ݿ�˿� -->
				<property name="port">3306</property> #bmoeba连接的主库的端口
				<!-- ��ʵmysql���ݿ�IP -->
				<property name="ipAddress">192.168.4.111</property>  #主库的ip
				<property name="schema">test</property>  #需要同步的库名称
				<!-- ���ڵ�½mysql���û��� -->
				<property name="user">laow</property>  #amoeba连接到数据库的用户,需要在主库授权用户
				<!-- ���ڵ�½mysql������ -->
				<!--  #取消注释
				<property name="password">123</property>  #授权用户连接amoeba的密码
				-->
			</factoryConfig>
			<!-- ObjectPoolʵ���� -->
			<poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool">
				<property name="maxActive">200</property>
				<property name="maxIdle">200</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>
		
		<dbServer name="server2">  #从库2的设定区域,一个server代表一个库,编号不能冲突
			
			<!-- PoolableObjectFactoryʵ���� -->
			<factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">
				<property name="manager">defaultManager</property>
				<!-- ��ʵmysql���ݿ�˿� -->
				<property name="port">3306</property> #bmoeba连接的从库1的端口
				<!-- ��ʵmysql���ݿ�IP -->
				<property name="ipAddress">192.168.4.112</property>  #从库1的ip
				<property name="schema">test</property>  #需要同步的库名称
				<!-- ���ڵ�½mysql���û��� -->
				<property name="user">laow</property>  #amoeba连接到数据库的用户,需要在从库库授权用户
				<!-- ���ڵ�½mysql������ -->
				<!--  #取消注释
				<property name="password">123</property>  #授权用户连接amoeba的密码
				-->
			</factoryConfig>
			<!-- ObjectPoolʵ���� -->
			<poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool">
				<property name="maxActive">200</property>
				<property name="maxIdle">200</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>
		
		<dbServer name="server3">  #从库2的设定区域,一个server代表一个库,编号不能冲突
			
			<!-- PoolableObjectFactoryʵ���� -->
			<factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">
				<property name="manager">defaultManager</property>
				<!-- ��ʵmysql���ݿ�˿� -->
				<property name="port">3306</property> #bmoeba连接从库2从库的端口
				<!-- ��ʵmysql���ݿ�IP -->
				<property name="ipAddress">192.168.4.113</property>  #从库2的ip
				<property name="schema">test</property>  #需要同步的库名称(主从库设定一致)
				<!-- ���ڵ�½mysql���û��� -->
				<property name="user">laow</property>  #amoeba连接到数据库的用户,需要在从库2授权用户
				<!-- ���ڵ�½mysql������ -->
				<!--  #取消注释
				<property name="password">123</property>  #授权用户连接amoeba的密码
				-->
			</factoryConfig>
			<!-- ObjectPoolʵ���� -->
			<poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool">
				<property name="maxActive">200</property>
				<property name="maxIdle">200</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>
		<dbServer name="master" virtual="true"> #数据库声明池,主库的声明池,server1加入到了master池
			<poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
				<!-- ���ؾ������ 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->
				<property name="loadbalance">1</property>
				<!-- �����pool���ؾ����poolName�б��Զ��ŷָ� -->
				<property name="poolNames">server1</property>  #声明的是主库server1
			</poolConfig>
		</dbServer>
		<dbServer name="salve" virtual="true"> #数据库声明池,从库的声明池,server2和server3加入到slave池
			<poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
				<!-- ���ؾ������ 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->
				<property name="loadbalance">1</property>
				<!-- �����pool���ؾ����poolName�б��Զ��ŷָ� -->
				<property name="poolNames">server2,,server3</property>  #声明的是从库server2,server3,如果写server1,server2,server2,server3,server3,server3则主库:从库1:从库2读的分配比例为1:2:3
			</poolConfig>
		</dbServer>
	</dbServerList>
	
	<queryRouter class="com.meidusa.amoeba.mysql.parser.MysqlQueryRouter">  #查询路由区域
		<property name="ruleConfig">${amoeba.home}/conf/rule.xml</property>
		<property name="functionConfig">${amoeba.home}/conf/functionMap.xml</property>
		<property name="ruleFunctionConfig">${amoeba.home}/conf/ruleFunctionMap.xml</property>
		<property name="LRUMapSize">1500</property>
		<property name="defaultPool">master</property>  #默认的池是主库master池的声明区域,默认池允许读写
		<!--  #取消注释
		<property name="writePool">master</property>  #允许写声明的池是master池,master池声明的是主库
		<property name="readPool">slave</property>  #允许写声明的池是slave池,slave池声明的是两个从库
		-->
		<property name="needParse">true</property>
	</queryRouter>
</amoeba:configuration>

3.修改/usr/local/amoeba/bin/amoeba命令的启动内存

DEFAULT_OPTS="-server -Xms256m-Xmx256m-Xss128k"  #将128修改为256,因为在64位操作系统下,128k的启动内存不足

4.启动amoeba

#第一种启动方式
nohup bash -x /usr/local/amoeba/bin/amoeba &  #amoeba启动命令
[root@localhost conf]# nohup bash -x /usr/local/amoeba/bin/amoeba &
[1] 5617 
[root@localhost conf]# nohup: 忽略输入并把输出追加到"nohup.out"
[root@localhost conf]#   #启动成功

配置文件修改错误时启动
[root@localhost conf]# nohup bash -x /usr/local/amoeba/bin/amoeba &
[1] 5379
[root@localhost conf]# nohup: 忽略输入并把输出追加到"nohup.out"
[1]+  Exit 255                nohup bash -x /usr/local/amoeba/bin/amoeba  #启动失败,失败时会自动退出,说明配置文件有问题

#第二种启动方式
chmod a+x /usr/local/amoeba/bin/amoeba  #给amoeba增加执行权限
cd /usr/local/amoeba/bin/
./amoeba start &  #开启amoeba
[root@localhost bin]# ./amoeba start &
[1] 2000
[root@localhost bin]# log4j:WARN log4j config load completed from file:/usr/local/amoeba/conf/log4j.xml
log4j:WARN ip access config load completed from file:/usr/local/amoeba/conf/access_list.conf
2020-06-17 07:36:30,167 INFO  net.ServerableConnectionManager - Server listening on /192.168.4.114:8066.
#上边为正常开启

[root@localhost bin]# ./amoeba start &
[1] 1918
[root@localhost bin]# log4j:WARN log4j config load completed from file:/usr/local/amoeba/conf/log4j.xml
Exception in thread "main" com.meidusa.amoeba.config.ConfigurationException: Error loading configuration file /usr/local/amoeba/conf/amoeba.xml
    with nested exception 
com.meidusa.amoeba.config.ConfigurationException: Caught exception while loading file /usr/local/amoeba/conf/amoeba.xml
    with nested exception 
[1]+  Exit 1                  ./amoeba start  #配置文件有问题,开启失败

客户端登录测试

[root@localhost ~]# yum install -y mysql #安装mysql客户端
[root@localhost ~]# mysql -h 192.168.4.114 -P8066 -uroot -p123  #登录amoeba测试
 类似资料: