使用amoeba实现mysql读写分离
转载马士兵连老师笔记
使用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
<?xmlversion="1.0" encoding="gbk"?>
<!DOCTYPE amoeba:dbServers SYSTEM "dbserver.dtd">
<amoeba:dbServersxmlns: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
-->
<dbServername="abstractServer"abstractive="true">
<factoryConfigclass="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">
<propertyname="connectionManager">${defaultManager}</property>
<propertyname="sendBufferSize">64</property>
<propertyname="receiveBufferSize">128</property>
<!-- mysql port -->
<propertyname="port">3306</property>
<!-- mysql schema -->
<propertyname="schema">msb</property>
<!-- mysql user -->
<propertyname="user">root</property>
<propertyname="password">123456</property>
</factoryConfig>
<poolConfigclass="com.meidusa.toolkit.common.poolable.PoolableObjectPool">
<propertyname="maxActive">500</property>
<propertyname="maxIdle">500</property>
<propertyname="minIdle">1</property>
<propertyname="minEvictableIdleTimeMillis">600000</property>
<propertyname="timeBetweenEvictionRunsMillis">600000</property>
<propertyname="testOnBorrow">true</property>
<propertyname="testOnReturn">true</property>
<propertyname="testWhileIdle">true</property>
</poolConfig>
</dbServer>
<dbServername="writedb" parent="abstractServer">
<factoryConfig>
<!-- mysql ip -->
<propertyname="ipAddress">192.168.85.11</property>
</factoryConfig>
</dbServer>
<dbServername="slave" parent="abstractServer">
<factoryConfig>
<!-- mysql ip -->
<propertyname="ipAddress">192.168.85.12</property>
</factoryConfig>
</dbServer>
<dbServername="myslave"virtual="true">
<poolConfigclass="com.meidusa.amoeba.server.MultipleServerPool">
<!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->
<propertyname="loadbalance">1</property>
<!-- Separated by commas,such as: server1,server2,server1 -->
<propertyname="poolNames">slave</property>
</poolConfig>
</dbServer>
</amoeba:dbServers>
amoeba.xml
<?xmlversion="1.0" encoding="gbk"?>
<!DOCTYPE amoeba:configuration SYSTEM "amoeba.dtd">
<amoeba:configurationxmlns:amoeba="http://amoeba.meidusa.com/">
<proxy>
<!-- service class must implements com.meidusa.amoeba.service.Service -->
<servicename="Amoeba for Mysql"class="com.meidusa.amoeba.mysql.server.MySQLService">
<!-- port -->
<propertyname="port">8066</property>
<!-- bind ipAddress -->
<!--
<property name="ipAddress">127.0.0.1</property>
-->
<propertyname="connectionFactory">
<beanclass="com.meidusa.amoeba.mysql.net.MysqlClientConnectionFactory">
<propertyname="sendBufferSize">128</property>
<propertyname="receiveBufferSize">64</property>
</bean>
</property>
<propertyname="authenticateProvider">
<beanclass="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator">
<propertyname="user">root</property>
<propertyname="password">123456</property>
<propertyname="filter">
<beanclass="com.meidusa.toolkit.net.authenticate.server.IPAccessController">
<propertyname="ipFile">${amoeba.home}/conf/access_list.conf</property>
</bean>
</property>
</bean>
</property>
</service>
<runtimeclass="com.meidusa.amoeba.mysql.context.MysqlRuntimeContext">
<!-- proxy server client process thread size -->
<propertyname="executeThreadSize">128</property>
<!-- per connection cache prepared statement size -->
<propertyname="statementCacheSize">500</property>
<!-- default charset -->
<propertyname="serverCharset">utf8</property>
<!-- query timeout( default: 60 second , TimeUnit:second) -->
<propertyname="queryTimeout">60</property>
</runtime>
</proxy>
<!--
Each ConnectionManager will start as thread
manager responsible for the Connection IO read , Death Detection
-->
<connectionManagerList>
<connectionManagername="defaultManager"class="com.meidusa.toolkit.net.MultiConnectionManagerWrapper">
<propertyname="subManagerClassName">com.meidusa.toolkit.net.AuthingableConnectionManager</property>
</connectionManager>
</connectionManagerList>
<!-- default using file loader -->
<dbServerLoaderclass="com.meidusa.amoeba.context.DBServerConfigFileLoader">
<propertyname="configFile">${amoeba.home}/conf/dbServers.xml</property>
</dbServerLoader>
<queryRouterclass="com.meidusa.amoeba.mysql.parser.MysqlQueryRouter">
<propertyname="ruleLoader">
<beanclass="com.meidusa.amoeba.route.TableRuleFileLoader">
<propertyname="ruleFile">${amoeba.home}/conf/rule.xml</property>
<propertyname="functionFile">${amoeba.home}/conf/ruleFunctionMap.xml</property>
</bean>
</property>
<propertyname="sqlFunctionFile">${amoeba.home}/conf/functionMap.xml</property>
<propertyname="LRUMapSize">1500</property>
<propertyname="defaultPool">writedb</property>
<propertyname="writePool">writedb</property>
<propertyname="readPool">myslave</property>
<propertyname="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上插入数据
insertinto user values(2,2);
--在master和slave上分别查看表中的数据
select * from user;
--将master上的mysql服务停止,继续插入数据会发现插入不成功,但是能够查询
--将master上的msyql服务开启,停止slave上的mysql,发现插入成功,但是不能够查询
以上是 使用amoeba实现mysql读写分离 的全部内容, 来源链接: utcz.com/z/518133.html