Amoeba 依赖jdk centons7安装jdk8
目前Amoeba for Mysql最新版本为amoeba-mysql-3.0.5-RC-distribution.zip。
安装过程很简单,只需要将zip压缩包解压至/usr/local/即可。若没有安装zip和unzip,可以通过centOS yum安装。
接下来,解压Amoeba压缩包。
[root@chenllcentos~]#unzipamoeba-mysql-3.0.5-RC-distribution.zip [root@chenllcentos~]#cp-rfamoeba-mysql-3.0.5-RC/usr/local
启动Amoeba。
[root@chenllcentos~]#/usr/local/amoeba-mysql-3.0.5-RC/bin/launcher
但是提示出现fatal exception:
Thestacksizespecifiedistoosmall,Specifyatleast228kError:CouldnotcreatetheJavaVirtualMachine.Error:Afatalexceptionhasoccurred.Programwillexit.
从错误文字上看,应该是由于stack size太小,导致JVM启动失败,要如何修改呢?
其实Amoeba已经考虑到这个问题,并将JVM参数配置写在属性文件里。现在,让我们通过该属性文件修改JVM参数。
修改jvm.properties文件JVM_OPTIONS参数。
[root@chenllcentos~]#vi/usr/local/amoeba-mysql-3.0.5-RC/jvm.properties
将内容:
JVM_OPTIONS="-server-Xms256m-Xmx1024m-Xss196k-XX:PermSize=16m-XX:MaxPermSize=96m"
替换为:
JVM_OPTIONS="-server-Xms1024m-Xmx1024m-Xss256k-XX:PermSize=16m-XX:MaxPermSize=96m"
再次启动Amoeba。
[root@chenllcentos~]#/usr/local/amoeba-mysql-3.0.5-RC/bin/launcher
若使用Amoeba完成读写分离,需要分别对dbServers.xml和amoeba.xml两个配置文件进行配置。与在应用层实现读写分离不同,使用Amoeba实现读写分离只需要修改配置文件,并不会产生硬编码耦合,有利于系统扩展和维护。
首先是配置dbServers.xml,主要是配置真实Mysql数据库连接信息。
<?xml version="1.0" encoding="gbk"?><!DOCTYPE amoeba:dbServers SYSTEM "dbserver.dtd"><amoeba:dbServers xmlns:amoeba="/"><!-- 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 factoryConfigsuch as 'multiPool' dbServer --><dbServer name="abstractServer" abstractive="true"><factoryConfig class="com.meidusa..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">itcast</property><!-- mysql user --><property name="user">root</property><property name="password">root</property></factoryConfig><poolConfig class="com.mon.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="master" parent="abstractServer"><factoryConfig><!-- mysql ip --><property name="ipAddress">192.168.96.130</property></factoryConfig></dbServer><dbServer name="slave1" parent="abstractServer"><factoryConfig><!-- mysql ip --><property name="ipAddress">192.168.96.131</property></factoryConfig></dbServer><dbServer name="slave2" parent="abstractServer"><factoryConfig><!-- mysql ip --><property name="ipAddress">192.168.96.132</property></factoryConfig></dbServer><dbServer name="readPool" 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">slave1,slave2</property></poolConfig></dbServer></amoeba:dbServers>
可以看出,对dbServers.xml文件的配置,主要就是对dbServer节点的配置。其中,readPool节点需要特别注意,因为Amoeba实现读写分离就是根据它来实现。接下来是amoeba.xml,主要是配置代理数据库连接信息。
<span style="font-size:18px;"><?xml version="1.0" encoding="gbk"?><!DOCTYPE amoeba:configuration SYSTEM "amoeba.dtd"><amoeba:configuration xmlns:amoeba="/"><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><!-- bind ipAddress --><!-- <property name="ipAddress">127.0.0.1</property>--><property name="connectionFactory"><bean class="com.meidusa..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..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 threadmanager responsible for the Connection IO read , Death Detection--><connectionManagerList><connectionManager name="defaultManager" class="com..MultiConnectionManagerWrapper"><property name="subManagerClassName">com..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">master</property><property name="writePool">master</property><property name="readPool">readPool</property><property name="needParse">true</property></queryRouter></amoeba:configuration></span>
在amoeba.xml中,主要完成连接信息和SQL路由配置。在queryRouter节点中,通过配置writePool和readPool可以实现读写分离。
配置完成后,重启Amoeba。
[root@chenllcentos~]#/usr/local/amoeba-mysql-3.0.5-RC/bin/shutdown[root@chenllcentos~]#/usr/local/amoeba-mysql-3.0.5-RC/bin/launcher
至此,Mysql主从复制和使用Amoeba实现数据库读写分离全部配置完成。
读写分离验证
接下来,进行简单测试,验证以上配置是否能够正确运行。
登录master主数据库。
[root@chenllcentos~]# mysql -uroot -proot -h192.168.96.130 -P8066
额外说明下,此处的yourpassword是连接Amoeba的密码,也就是在amoeba.xml配置文件中配置的密码,与Mysql密码不同,需要注意。
登陆后,此时会提示以下信息。
Serverversion:5.1.45-mysql-amoeba-proxy-3.0.4-BETASourcedistribution
说明已经成功连接Mysql代理Amoeba。
为了验证Amoeba读写分离配置是否生效,我们做一个简单的测试。
先在181服务器master服务器上创建一个表。
mysql>createtablesxit(idint(10),namevarchar(10));
而后,分别停止服务器B和服务器C两个从数据库的主从复制,便于数据库操作观察。
登陆服务器B从数据库。
[root@chenllcentos~]#mysql-uroot-proot
停止从数据库主从复制。
mysql>slavestop;
登陆服务器C从数据库。
[root@chenllcentos~]#mysql-uroot-proot
停止从数据库主从复制。
mysql>slavestop;
在主数据库插入。
mysql>insertintosxitvalues('1','zhangsan');
在从数据库B插入。
mysql>insertintosxitvalues('2','lisi');
在从数据库C插入。
mysql>insertintosxitvalues('3','john');
登陆到amoeba服务器,进行读写分离的测试:
[root@chenllcentos~]#mysql-uroot-proot -h192.168.96.130 -P8066mysql>usetest;mysql>select*fromsxit;+------+------+|id|name|+------+------+|2|lisi|+------+------+mysql>select*fromsxit;+------+------+|id|name|+------+------+|3|john|+------+------+
重复执行多次,发现始终只显示从数据库的数据,说明如果进行数据库读操作,Amoeba只将读数据SQL命令路由至从数据库。
登录主数据库。
[root@chenllcentos~]#mysql-uroot-prootmysql>usetest;mysql>select*fromsxit;+------+----------+|id|name|+------+----------+|1|zhangsan|+------+----------+
可以验证,使用Amoeba对Mysql读写分离成功。若此时开启从数据库主从复制,则可以进行Mysql集群和负载均衡。