乐趣区

关于mysql:mysql基础之七mysql读写分离之amoeba

应用 amoeba 实现 mysql 读写拆散

1、什么是 amoeba?

Amoeba(变形虫) 我的项目,专一 分布式数据库 proxy 开发。座落与 Client、DB Server(s) 之间。对客户端通明。具备负载平衡、高可用性、sql 过滤、读写拆散、可路由相干的 query 到指标数据库、可并发申请多台数据库合并后果。

次要解决:

• 升高 数据切分带来的简单多数据库构造

• 提供切分规定并升高 数据切分规定 给利用带来的影响

• 升高 db 与客户端的连接数

• 读写拆散

2、为什么要用 Amoeba

目前要实现 mysql 的主从读写拆散,次要有以下几种计划:

1、通过程序实现,网上很多现成的代码,比较复杂,如果增加从服务器要更改多台服务器的代码。

2、通过 mysql-proxy 来实现,因为 mysql-proxy 的主从读写拆散是通过 lua 脚本来实现,目前 lua 的脚本的开发跟不上节奏,而写没有完满的现成的脚本,因而导致用于生产环境的话危险比拟大,据网上很多人说 mysql-proxy 的性能不高。

3、本人开发接口实现,这种计划门槛高,开发成本高,不是个别的小公司能承当得起。

4、利用阿里巴巴的开源我的项目 Amoeba 来实现,具备负载平衡、高可用性、sql 过滤、读写拆散、可路由相干的 query 到指标数据库,并且装置配置非常简单。国产的开源软件,应该反对,目前正在应用,不发表太多论断,所有等测试完再发表论断吧,哈哈!

3、amoeba 装置

1、首先装置 jdk,间接应用 rpm 包装置即可
2、下载 amoeba 对应的版本 https://sourceforge.net/projects/amoeba/,间接解压即可
3、配置 amoeba 的配置文件

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 configu
ration: add attribute with name virtual = “true” in dbServer, but the configuration does not allow the element with n
ame 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”>msb</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=”writedb”  parent=”abstractServer”>
<factoryConfig>
<!– mysql ip –>
<property name=”ipAddress”>192.168.85.11</property>
</factoryConfig>
</dbServer>

<dbServer name=”slave”  parent=”abstractServer”>
<factoryConfig>
<!– mysql ip –>
<property name=”ipAddress”>192.168.85.12</property>
</factoryConfig>
</dbServer>
<dbServer name=”myslave” 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</property>
</poolConfig>
</dbServer>
</amoeba:dbServers>

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>

<!– bind ipAddress –>
<!–
<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>

<property name=”authenticateProvider”>
<bean class=”com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator”>

<property name=”user”>root</property>

<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”>writedb</property>

<property name=”writePool”>writedb</property>
<property name=”readPool”>myslave</property>
<property name=”needParse”>true</property>
</queryRouter>
</amoeba:configuration>

4、启动 amoeba

/root/amoeba-mysql-3.0.5-RC/bin/launcher

4、测试 amoeba

– 测试的 sql
– 在装置 amoeba 的服务器上登录 mysql
mysql -h192.168.85.13 -uroot -p123 -P8066
– 别离在 master、slave、amoeba 上登录 mysql
use msb
select * from user;
– 在 amoeba 上插入数据
insert into user values(2,2);
– 在 master 和 slave 上别离查看表中的数据
select * from user;
– 将 master 上的 mysql 服务进行,持续插入数据会发现插入不胜利,然而可能查问
– 将 master 上的 msyql 服务开启,进行 slave 上的 mysql,发现插入胜利,然而不可能查问

退出移动版