mysql中间件-amoeba
中间件:一种提供在不同技术、不同的软件之间共享资源的程序,更大化了利用了数据库的性能,可以无限扩展(注:真实环境中并非如此)
数据库的中间件:
mysql proxy (官方版本)性能低,需要lua 脚本
atlas 性能低,响应时间长
amoeba 陈思儒研发
一、 先搭建一个主从关系的服务器
在主、从服务器上安装mysql mysql-server
1. 开启二进制日志
[root@localhost ~]# vim /etc/my.cnf ... log-bin=mysql-bin server-id = 132 ...
[root@localhost ~]# vim /etc/my.cnf ...l og-bin=mysql-bin server-id = 134 ...
2. 在主服务器上授权,从服务器上保存授权信息,并开启从服务线程
mysql> grant replication slave on *.* to root@'192.168.80.134' identified by '123456'; mysql> show master status;
mysql> change master to -> master_user='root', -> master_password='123456', -> master_host='192.168.80.132', -> master_log_file='mysql-bin.000008', -> master_log_pos=260; Query OK, 0 rows affected (0.35 sec) mysql> start slave; Query OK, 0 rows affected (0.01 sec) mysql> show slave status\G;
二、配置读写分离
1. 安装 gcc 环境
[root@localhost ~]# yum -y install gcc*
2. 安装jdk
前往官网下载:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
[root@localhost ~]# tar -zxvf jdk-8u231-linux-x64.tar.gz /amoeba/ [root@localhost /amoeba]# ln -s jdk1.8.0_231/ jdk [root@localhost /amoeba]# vim /etc/profile JAVA_HOME=/amoeba/jdk export JAVA_HOME PATH=$JAVA_HOME/bin:$PATH export PATH [root@localhost /amoeba]# source /etc/profile
3. 安装amoeba
[root@localhost ~]# wget https://jaist.dl.sourceforge.net/project/amoeba/Amoeba%20for%20mysql/3.x/amoeba-mysql-3.0.5-RC-distribution.zip [root@localhost ~]# mkdir /amoeba [root@localhost ~]# unzip amoeba-mysql-3.0.5-RC-distribution.zip -d /amoeba
4. 配置amoeba
amoeba 的配置文件位于安装目录下的conf目录中,实现读写分离功能,只需修改两个配置文件,dbServers.xml和amoeba.xml。
[root@localhost /amoeba/amoeba-mysql-3.0.5-RC/conf]# cat dbServers.xml
<?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">123456</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="server1" parent="abstractServer"> #改名字可任意命名,后续有用 <factoryConfig> <!-- mysql ip --> <property name="ipAddress">192.168.80.132</property> #数据库ip </factoryConfig> </dbServer> <dbServer name="server2" parent="abstractServer"> <factoryConfig> <!-- mysql ip --> <property name="ipAddress">192.168.80.134</property> #数据库ip </factoryConfig> </dbServer> <dbServer name="write" virtual="true"> #定义一个dbserver 组名 <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">server1</property> #write组成员 </poolConfig> </dbServer> <dbServer name="read" 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">server1,server2</property> </poolConfig> </dbServer> </amoeba:dbServers>
[root@localhost /amoeba/amoeba-mysql-3.0.5-RC/conf]# cat 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> #amoeba监听端口,默认8066 <!-- bind ipAddress --> <property name="ipAddress">192.168.80.133</property> #amoeba安装地址 <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> #设置客户端连接amoeba所需的用户名和密码 <property name="password">123456</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">write</property> #设置amoeba默认池 <property name="writePool">write</property> #设置amoeba 写池,默认注释 <property name="readPool">read</property> #设置amoeba读池,默认注释 <property name="needParse">true</property> </queryRouter> </amoeba:configuration>
5. 在主从服务器上授权
mysql> grant all on *.* to 'root'@'192.168.80.133' identified by '123456';Query OK, 0 rows affected (0.00 sec)
6. 修改jvm.properties
[root@localhost /amoeba/amoeba-mysql-3.0.5-RC]# vim jvm.properties JVM_OPTIONS="-server -Xms1024m -Xmx1024m -Xss256k -XX:PermSize=16m -XX:MaxPermSize=96m"
7. 启动
[root@localhost /amoeba/amoeba-mysql-3.0.5-RC]# nohup bash +x /amoeba/amoeba-mysql-3.0.5-RC/bin/launcher &
[root@localhost /amoeba/amoeba-mysql-3.0.5-RC]# tail -f nohup.out 2019-12-24 18:34:02,897 INFO context.MysqlRuntimeContext - Amoeba for Mysql current versoin=5.1.45-mysql-amoeba-proxy-3.0.4-BETA log4j:WARN ip access config load completed from file:/amoeba/amoeba-mysql-3.0.5-RC/conf/access_list.conf 2019-12-24 18:34:03,331 INFO net.ServerableConnectionManager - Server listening on /192.168.80.133:8066. Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=16m; support was removed in 8.0 Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=96m; support was removed in 8.0 2019-12-24 18:59:15 [INFO] Project Name=Amoeba-MySQL, PID=7607 , starting... log4j:WARN log4j config load completed from file:/amoeba/amoeba-mysql-3.0.5-RC/conf/log4j.xml 2019-12-24 18:59:16,333 INFO context.MysqlRuntimeContext - Amoeba for Mysql current versoin=5.1.45-mysql-amoeba-proxy-3.0.4-BETA log4j:WARN ip access config load completed from file:/amoeba/amoeba-mysql-3.0.5-RC/conf/access_list.conf 2019-12-24 18:59:16,669 INFO net.ServerableConnectionManager - Server listening on /192.168.80.133:8066.
[root@localhost ~]# netstat -antp |grep 8066 tcp6 0 0 192.168.80.133:8066 :::* LISTEN 7607/java
[root@localhost ~]# firewall-cmd --zone=public --add-port=8066/tcp [root@localhost ~]# firewall-cmd --zone=public --add-port=8066/tcp --permanent
8. 测试
[root@localhost ~]# mysql -uroot -p123456 -P8066 -h192.168.80.133 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 151281375 Server version: 5.1.45-mysql-amoeba-proxy-3.0.4-BETA Source distribution Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> use test; Database changed mysql> select * from ceshi2; +--------+ | name | +--------+ | master | | amoeba | +--------+ 2 rows in set (0.01 sec) mysql> select * from ceshi2; +--------+ | name | +--------+ | master | | slave | | amoeba | +--------+ 3 rows in set (0.01 sec) mysql> select * from ceshi2; +--------+ | name | +--------+ | master | | amoeba | +--------+ 2 rows in set (0.01 sec) mysql> select * from ceshi2; +--------+ | name | +--------+ | master | | slave | | amoeba | +--------+ 3 rows in set (0.01 sec) mysql> select * from ceshi2; +--------+ | name | +--------+ | master | | amoeba | +--------+ 2 rows in set (0.01 sec) mysql> select * from ceshi2; +--------+ | name | +--------+ | master | | slave | | amoeba | +--------+ 3 rows in set (0.02 sec)
三、amoeba配置文件
主配置文件 $AMOEBA_HOME/conf/amoeba.xml,用来配置Amoeba服务的基本参数,如Amoeba主机地址、端口、认证方式、用于连接的用户名、密码、线程数、超时时间、其他配置文件的位置等。
数据库服务配置文件 $AMOEBA_HOME/conf/dbServers.xml,用来存储和配置Amoeba所代理的数据库服务器的信息,如:主机IP、端口、用户名、密码等。
切分规则配置文件 $AMOEBA_HOME/conf/rule.xml,用来配置切分规则。
数据库函数配置文件 $AMOEBA_HOME/conf/functionMap.xml,用来配置数据库函数的处理方法,Amoeba将使用该配置文件中的方法解析数据库函数。
切分规则函数配置文件 $AMOEBA_HOME/conf/ruleFunctionMap.xml,用来配置切分规则中使用的用户自定义函数的处理方法。
访问规则配置文件 $AMOEBA_HOME/conf/access_list.conf,用来授权或禁止某些服务器IP访问Amoeba。
日志规格配置文件 $AMOEBA_HOME/conf/log4j.xml,用来配置Amoeba输出日志的级别和方式。