先简单介绍下amoeba
Amoeba(变形虫)项目,该开源框架于2008年 开始发布一款 Amoeba for Mysql软件。这个软件致力于MySQL的分布式数据库前端代理层,它主要在应用层访问MySQL的 时候充当SQL路由功能,专注于分布式数据库代理层(Database Proxy)开发。座落与 Client、DB Server(s)之间,对客户端透明。具有负载均衡、高可用性、SQL 过滤、读写分离、可路由相关的到目标数据库、可并发请求多台数据库合并结果。 通过Amoeba你能够完成多数据源的高可用、负载均衡、数据切片的功能。
初次使用,amoeba是基于java开发的,运行时需要java环境。
amoeba分为4个只要的配置文件,
分别位于/conf 文件夹下
amoeba.XML 是基本的配置信息,主要是外部访问amoeba时的一些验证及地址端口的配置。DBserver.XML这个是mysqlserver list ,将所有的数据库服务器写入到此文件。
rule.xml 这个是读写分离,负载均衡、数据切片的一些规则。
access_list.conf 是访问amoeba机器的控制。
以上4个配置文件为常用的几个,其他的文件就不一一说明了,可以看一下amoeba的使用说明http://docs.hexnova.com/amoeba/index.html
安装:
http://sourceforge.net/projects/amoeba/files/ 为amoeba的下载地址。
下载完成直接加压到相对应的目录即可,我解压到d:/下目录为D:\amoeba。
然后下cmd下 进入到D:\amoeba\bin,输入amoeba验证是否正确(jdk的运行环境安装就不在这里说了)
配置:
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.net.ServerableConnectionManager"> <!-- port --> <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"> <property name="user">monkey</property> #对外部提供的用户名 <property name="password">monkey@123</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">192.168.0.165</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">mysql_master</property> #我这里写的mysql_master是在dbserver.xml定义过的 ,看下面dbserver.xml的配置 <property name="writePool">mysql_master</property> #mysql_master为写服务器 <property name="readPool">virtualPool</property>#virtualPool是我定义的虚拟组,提供读的服务器池。 <property name="needParse">true</property> </queryRouter> </amoeba:configuration>
dbserver.xml配置
<?xml version="1.0" encoding="gbk"?> <!DOCTYPE amoeba:dbServers SYSTEM "dbserver.dtd"> <amoeba:dbServers xmlns:amoeba="http://amoeba.meidusa.com/"> <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 --> <property name="schema">mysql</property> <!-- mysql user --> <property name="user">monkey</property> <property name="password">monkey@123</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> #上面的信息定义了mysqllist的验证信息 <dbServer name="mysql_master" parent="abstractServer"> <factoryConfig> <!-- mysql ip --> <property name="ipAddress">192.168.0.165</property> </factoryConfig> </dbServer> <dbServer name="slave_162" parent="abstractServer"> <factoryConfig> <!-- mysql ip --> <property name="ipAddress">192.168.0.162</property> </factoryConfig> </dbServer> <dbServer name="slave_147" parent="abstractServer"> <factoryConfig> <!-- mysql ip --> <property name="ipAddress">192.168.0.147</property> </factoryConfig> </dbServer> #上面信息定义了mysql的服务器地址 <dbServer name="virtualPool" 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_162,slave_147,slave_162</property> </poolConfig> </dbServer> #这部分我将几个读取的服务器加入到virtualPool池中,并使用ROUNDROBIN (轮询)的机制进行读取的路由。 </amoeba:dbServers>
上面的两个配置文件已经实现了读写的分离,在amoeba.xml里面已经定义好那个是读和那个是写,所以就无需在rule里面进行规制话了。
接下来就是验证了,我就不写了。。。。。。