什么是Amoeba?

Amoeba 2008发布的第一款开源框架

amoeba for mysql,致力于mysql分布式数据库代理层

它主要在应用层访问mysql的时候充当sql路由功能,专注于分布式数据库代理层,坐落于client,DBserver之间,对客户端透明具有负载均衡,高可用,sql过滤,读写分离,可路由相到目标数据库,可并发请求多台数据库合并结果

通过amoeba您能够完成数据源的高可用,负载均衡,数据切换功能,

目前amoeba目前不支持事务

下面是实现这一方案的架构图:

wKiom1Yt14_i8SSlAAEJLSWgjGU617.jpg

mysql主从复制配置

因为读写分离,所以一台负责mysql的写操作,另一台负责mysql的读操作,所以我们这里使用mysql的主从复制再合适不过了。关于这一配置,请移步:                                                                 http://douya.blog.51cto.com/6173221/1596113

Amoeba实现读写分离:

2,java环境配置

Amoeba框架是基于Java SE1.5开发的,建议使用Java SE 1.5版本。目前Amoeba经验证在JavaTM SE 1.5和Java SE 1.6能正常运行,(可能包括其他未经验证的版本)。
Java SE 1.6下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk-6u32-downloads-1594644.html
我这里下载jdk-6u32-linux-i586-rpm.bin。
下载完成后执行sh jdk-6u32-linux-i586-rpm.bin开始安装,将会安装到/usr/java/jdk1.6.0_32目录。

我自己选择的jdk是根据统一环境使用的1.7

3Amoeba的安装

Amoeba下载地址:http://sourceforge.net/projects/amoeba/

下面是安装步骤:

1

2

3

4

cd/root

mkdir/usr/local/amoeba

wgethttp://softlayer.dl.sourceforge.net/project/amoeba/Amoeba%20for%20mysql/2.x/amoeba-mysql-binary-2.1.0-RC5.tar.gz

tarxzf amoeba-mysql-binary-2.1.0-RC5.tar.gz -C /usr/local/amoeba

配置用户环境变量

vim /etc/profile

设置为:

1

2

3

4

5

exportJAVA_HOME=/root/jdk1.7.0_79

exportCLASSPATH=$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/jre/lib/rt.jar

exportPATH=$PATH:$JAVA_HOME/bin

exportJAVA_HOME

exportPATH

立即生效:

1

source/etc/profile

4,Amoeba for mysql配置

配置Amoeba for mysql的读写分离主要涉及两个文件:

1、/usr/local/amoeba/conf/amoeba.xml此文件定义了Amoeba代理的相关配置。

2、/usr/local/amoeba/conf/dbServers.xml
此文件定义由Amoeba代理的数据库如何连接,比如最基础的:主机IP、端口、Amoeba使用的用户名和密码等等。

amoeba.xml配置

amoeba本地配置或者对外的配置:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

 <!--service classmust implements com.meidusa.amoeba.service.Service -->

 <service name="Amoeba for Mysql"class="com.meidusa.amoeba.net.ServerableConnectionManager">

<!--port -->

<propertyname="port">3306</property>   -改成3306比较好,开发直接实用默认3306端口

<!--bind ipAddress -->

<propertyname="ipAddress">192.168.0.48</property>

对外提供程序访问的IP地址,也就是amoeba机器对外访问的IP地址

<propertyname="manager">${clientConnectioneManager}</property>

<propertyname="connectionFactory">

<bean class="com.meidusa.amoeba.mysql.net.MysqlClientConnectionFactory">

<propertyname="sendBufferSize">128</property>

<propertyname="receiveBufferSize">64</property>

    </bean>

    </property>

amoeba对外连接验证配置:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

 <propertyname="authenticator">

  <bean class="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator">

  <propertyname="user">amoeba</property>    ****对外提供访问的用户

  <propertyname="password">Centos</property>  ****对外提供访问的密码

   <propertyname="filter">

   <bean class="com.meidusa.amoeba.server.IPAccessController">

   <propertyname="ipFile">${amoeba.home}/conf/access_list.conf</property>

   </bean>

   </property>

   </bean>

   </property>

这里定义连接amoeba时用来验证的用户及密码。

amoeba.xml读写分离配置:

1

2

3

4

 <propertyname="defaultPool">Master</property>

<propertyname="writePool">Master</property>

<propertyname="readPool">virtualSlave</property>

virtualSlave和dbServer.xml中配置的节点相关

defaultPool:配置了默认的数据库节点,一些除了SELECT\UPDATE\INSERT\DELETE的语句都会在defaultPool执行。
writePool :配置了数据库写库,通常配为Master,如这里就配置为之前定义的Master数据库。
readPool :配置了数据库读库,通常配为Slave或者Slave组成的数据库池,如这里就配置之前的virtualSlave数据库池。

(1)dbServers.xml文件配置:

本地的配置或者对外访问配置

 ***************表示需要修改部分

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

<dbServer name="abstractServer"abstractive="true">

 <factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">

           <propertyname="manager">${defaultManager}</property>

           <propertyname="sendBufferSize">64</property>

          <propertyname="receiveBufferSize">128</property>

          <!--mysql port -->

          <propertyname="port">3306</property>   

           <!--mysql schema -->

         <propertyname="schema">roudy</property> 必须是一个存在的database

          <!--mysql user -->

          <propertyname="user">amoeba</property>  ********

          <!--mysql password -->

          <propertyname="password">Centos</property> 默认被注释掉,需打开

          </factoryConfig>

      <poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool">

            <propertyname="maxActive">500</property>

            <propertyname="maxIdle">500</property>

            <propertyname="minIdle">10</property>

            <propertyname="minEvictableIdleTimeMillis">600000</property>

            <propertyname="timeBetweenEvictionRunsMillis">600000</property>

            <propertyname="testOnBorrow">true</property>

            <propertyname="testWhileIdle">true</property>

            </poolConfig>

        </dbServer>

此部分定义 登录mysql服务器的端口,数据库名称,mysql用户及密码。

接下来配置主从定义:

***************表示需要修改部分

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

<dbServer name="master"parent="abstractServer">

     <factoryConfig>

      <!-- mysql ip -->

     <property name="ipAddress">192.168.0.1</property>  ** master-IP

     </factoryConfig>

     </dbServer>

<dbServer name="Slave1"parent="abstractServer">

     <factoryConfig>

     <!-- mysql ip -->

     <property name="ipAddress">192.168.0.2</property> ****slave1的IP        

     </factoryConfig>

     </dbServer>

<dbServer name="Slave2"parent="abstractServer"> 

     <factoryConfig>

     <!-- mysql ip -->

     <property name="ipAddress">192.168.0.3</property>   ***slave2的IP

     </factoryConfig>

     </dbServer>

     <dbServer name="virtualSlave"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>

此部分定义主服务器,从服务器,及从服务器连接池。这里只定义数据库地址,它们的用户及密码就是上面的abstractServer里的设置。注意用来连接真实mysql服务器的用户必须拥有远程连接权限 ,这里是先安装,完了在授权

配置授权用户

主服务器

1

mysql> grantcreate,select,insert,update,deleteon*.* toamoeba@'192.168.0.%'identified by'Centos';

mysql> flush  privileges;

此时MySQL由于配置了主从同步,所以需要先停掉从的权限。

这里的amoeba,是amoeba.xml ,dbServer.xml中配置的amoeba用户

从服务器

1

2

 stop slave;

 grantselecton*.* toamoeba@'192.168.0.%'identified by'Centos';

amoeba启动

启动命令:

1

2

3

4

[root@localhost amobea]# bin/amoeba start

The stack sizespecified istoo small, Specify atleast 228k

Error: Could notcreatethe Java Virtual Machine.

Error: A fatal exception has occurred. Program will exit.

·

·

此命令以前台的方式启动,会输出启动时的信息,检查没有错误信息后,中断,并后台运行:

启动报错,修改启动脚本

1

2

3

[root@localhost amobea]# vim bin/amoeba

改为

DEFAULT_OPTS="-server -Xms256m -Xmx256m -Xss256k"

后台启动amoeba

/usr/local/amoeba/bin/amoeba &

检验启动是否成功(3306端口):

1

2

3

[root@localhost amoeba]# ps aux | grep amoeba

root     24580  0.2 19.2 408912 49264 pts/1Sl   12:52   0:11 /usr/java/jdk1.6/bin/java-server -Xms256m -Xmx256m -Xss128k -Damoeba.home=/usr/local/amoeba-Dclassworlds.conf=/usr/local/amoeba/bin/amoeba.classworlds -classpath /usr/local/amoeba/lib/classworlds-1.0.jar org.codehaus.classworlds.Launcher

[root@localhos amoeba]# netstat -lnp | grep java

最终测试:

链接测试 在任意客户端登录amoeba

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

[root@localhost ~]# mysql -u amoeba -p -h192.168.2.48

Enter password: 

Welcome tothe MySQL monitor.  Commands endwith; or\g.

Your MySQL connectionid is1982632907

Server version: 5.1.45-mysql-amoeba-proxy-2.1.0-RC5 MySQL Community Server (GPL)

Copyright (c) 2000, 2013, Oracle and/orits affiliates. Allrights reserved.

Oracle isa registered trademark ofOracle Corporation and/orits

affiliates. Other names may be trademarks oftheir respective

owners.

Type 'help;'or'\h'forhelp. Type '\c'toclear the currentinput statement.

mysql> show databases;

+--------------------+

| Database|

+--------------------+

| information_schema |

| amoeba_test        |

| mysql              |

| performance_schema |

| repl               |

| roudy              |

| test               |

+--------------------+

7 rowsinset(0.02 sec)

mysql>

如上表明amoeba配置完全正确。

测试读写是否正确:

此时读库是没有开启复制功能的,(stop  slave)

在master服务器上操作,新增一条数据

insert into luodi values('1','luodi','master1');

紧接着在master查询

1

2

3

4

5

6

mysql> select* fromluodi;

+------+-------+---------+

| id   | name| address |

+------+-------+---------+

|    1 | luodi | master  |

|    1 | luodi | master1 |

在salve服务器上操作,新增一条数据

insert into luodi values('1','luodi','slave1');

紧接着在salve查询:

1

2

3

4

5

6

mysql> select* fromluodi;

+------+-------+---------+

| id   | name| address  |

+------+-------+---------+

|    1 | luodi | slave   |

|    1 | luodi | slave1  |

现在在ameoba服务器查询:

1

2

3

4

5

6

mysql> select* fromluodi;

+------+-------+---------+

| id   | name| address  |

+------+-------+---------+

|    1 | luodi | slave   |

|    1 | luodi | slave1  |

没有master服务器的数据,那么在amoeba服务器上插入一条数据,再次验证

insert into luodi values('1','luodi','slave2');

登录master,salve查看

1

2

3

4

5

6

7

8

master

mysql> select* fromluodi;

+------+-------+---------+

| id   | name| address |

+------+-------+---------+

|    1 | luodi | master  |

|    1 | luodi | master1 |

|    1 | luodi | master2

salve:   此时slave任然没有开启复制

1

2

3

4

5

6

mysql> select* fromluodi;

+------+-------+---------+

| id   | name| address  |

+------+-------+---------+

|    1 | luodi | slave   |

|    1 | luodi | slave1  |

到目前为止amoeba基于-M-S结构的动静分离完全成功,然后start slave即可、