原文出自 http://www.abcdocker.com/abcdocker/81
1.AMOEBA是什么
Amoeba(变形虫)项目,该开源框架于2008年 开始发布一款 Amoeba for Mysql软件。这个软件致力于MySQL的分布式数据库前端代理层,它主要在应用层访问MySQL的 时候充当SQL路由功能,专注于分布式数据库代理层(Database Proxy)开发。座落与 Client、DB Server(s)之间,对客户端透明。具有负载均衡、高可用性、SQL 过滤、读写分离、可路由相关的到目标数据库、可并发请求多台数据库合并结果。 通过Amoeba你能够完成多数据源的高可用、负载均衡、数据切片的功能,目前Amoeba已在很多 企业的生产线上面使用。
2.AMOBA不能做什么
•目前还不支持事务
•暂时不支持存储过程(近期会支持)
•不适合从amoeba导数据的场景或者对大数据量查询的query并不合适(比如一次请求返回10w以上甚至更多数据的场合)
•暂时不支持分库分表,amoeba目前只做到分数据库实例,每个被切分的节点需要保持库表结构一致
3.安装AMOBA
安装JDK
[root@db01 ~]# tar -xf jdk-8u91-linux-i586.tar.gz -C /usr/lib/
[root@db01 ~]# ln -s /usr/lib/jdk1.8.0_91/ /usr/lib/java
[root@db01 ~]# cat >> /etc/profile <<EOF
[root@db01 ~]# echo '
export JAVA_HOME=/usr/lib/java
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH' >> /etc/profileEOF
[root@db01 ~]# source /etc/profile
[root@db01 ~]# java -version
java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
Java HotSpot(TM) Client VM (build 25.91-b14, mixed mode)
安装amoeba
[root@db01 ~]# wget http://ncu.dl.sourceforge.net/project/amoeba/Amoeba%20for%20mysql/2.x/amoeba-mysql-binary-2.1.0-RC5.tar.gz
[root@db01 ~]# mkdir -p /application/amoeba-2.1.0-rc5
[root@db01 ~]# tar -xf amoeba-mysql-binary-2.1.0-RC5.tar.gz /application/amoeba-2.1.0-rc5/
[root@db01 ~]# ln -s /application/amoeba-2.1.0-rc5/ /application/amoeba
[root@db01 ~]# echo 'PATH=/application/amoeba/bin/:$PATH' >> /etc/profile
[root@db01 ~]# source /etc/profile
4.AMOEBA基础配置介绍
Amoeba主要配置文件
dbServers.xml #主机IP,端口,Amoeba的用户名密码
rule.xml #切分规则相关
functionMap.xml #描述了函数名和函数处理的关系
ruleFunctionMap.xml #自定义函数
access_list.conf #可以访问和拒绝访问的列表
log4j.xml #日志文件相关配置
5.配置AMOEBA对一个数据库实例进行操作
测试mysql是否可以正常连接
[root@db01 ~]# mysql -uroot -poldboy123 -S /data/3306/mysql.sock
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.5.49-log 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>
配置一个DB节点
<?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="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">test</property>
<!-- mysql user -->
<property name="user">oldboy</property>
<!-- mysql password -->
<property name="password">123456</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>
<dbServer name="server1" parent="abstractServer">
<factoryConfig>
<!-- mysql ip -->
<property name="ipAddress">127.0.0.1</property>
</factoryConfig>
</dbServer>
<dbServer name="multiPool" virtual="true">
<poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
<!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=H
A-->
<property name="loadbalance">1</property>
<!-- Separated by commas,such as: server1,server2,server1 -->
<property name="poolNames">server1</property>
</poolConfig>
</dbServer>
</amoeba:dbServers>
6.AMOEBA读写分离
创建授权用户bzjxs
GRANT ALL ON *.* TO 'bzjxs'@'172.16.1.%' IDENTIFIED BY '123456'
配置dbServer.xml
<?xml version="1.0" encoding="gbk"?>
<!DOCTYPE amoeba:dbServers SYSTEM "dbserver.dtd">
<amoeba:dbServers xmlns:amoeba="http://amoeba.meidusa.com/">
...
<dbServer name="Master" parent="abstractServer">
<factoryConfig>
<!-- mysql ip -->
<property name="ipAddress">192.168.0.1</property>
</factoryConfig>
</dbServer>
<--配置mysql节点master-->
<dbServer name="Slave1" parent="abstractServer">
<factoryConfig>
<!-- mysql ip -->
<property name="ipAddress">192.168.0.2</property>
</factoryConfig>
</dbServer>
<--配置mysql节点slave1-->
<dbServer name="Slave2" parent="abstractServer">
<factoryConfig>
<!-- mysql ip -->
<property name="ipAddress">192.168.0.3</property>
</factoryConfig>
</dbServer>
<--配置mysql节点slave2-->
<dbServer name="virtualSlave" virtual="true">
<--配置mysql节点池-->
<poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
<!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->
<property name="loadbalance">1</property>
<--1代表轮询-->
<!-- Separated by commas,such as: server1,server2,server1 -->
<property name="poolNames">Slave1,Slave1,Slave2</property>
<--配置权重 代表轮询两次Slave1,轮询1次Slave2-->
</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/">
...
<queryRouter class=”com.meidusa.amoeba.mysql.parser.MysqlQueryRouter”>
<property name="LRUMapSize">1500</property>
<--amoeba缓存的语句的条数-->
<property name="defaultPool">Master</property>
<--默认访问的mysql节点 一些除了SELECT\UPDATE\INSERT\DELETE的语句都会在defaultPool执行-->
<property name="writePool">Master</property>
<--写入的服务器-->
<property name="readPool">virtualSlave</property>
<--读取的服务器可以是节点名也可以是节点池名-->
<property name="needParse">true</property>
</queryRouter>
...
</amoeba:configuration>
AMOEBA官方文档
http://docs.hexnova.com/amoeba/rw-splitting.html
本文转载老男孩28期李佳豪同学分享
喜欢本作者请关注我们的博客!