$ vim dbServers.xml
<?xml version="1.0"encoding="gbk"?>
<!DOCTYPE amoeba:dbServers SYSTEM "dbserver.dtd">
<amoeba:dbServers xmlns:amoeba="https://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="connectionManager">${defaultManager}</property>
<property name="sendBufferSize">64</property>
<property name="receiveBufferSize">128</property>
<!-- mysql port -->
<property name="port">3306</property> #设置Amoeba要连接的mysql数据库的端口,默认是3306 <!-- mysql schema -->
<property name="schema">test</property> #设置缺省的数据库,当连接amoeba时,操作表必须显式的指定数据库名,即采用dbname.tablename的方式,不支持 use dbname指定缺省库,因为操作会调度到各个后端dbserver <!-- mysql user -->
<property name="user">proxy</property> #设置amoeba连接后端数据库服务器的账号和密码,因此需要在所有后端数据库上创建该用户,并授权amoeba服务器可连接 <property name="password">123456.Com</property>
</factoryConfig>
<poolConfig class="com.meidusa.toolkit.common.poolable.PoolableObjectPool">
<property name="maxActive">500</property> #最大连接数,默认500 <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="master"parent="abstractServer"> #设置一个后端可写的dbServer,这里定义为master,这个名字可以任意命名,后面还会用到 <factoryConfig>
<!-- mysql ip -->
<property name="ipAddress">192.168.100.10</property>
</factoryConfig>
</dbServer>
<dbServer name="slave1"parent="abstractServer"> #设置后端可写slave1 <factoryConfig>
<!-- mysql ip -->
<property name="ipAddress">192.168.100.20</property>
</factoryConfig>
</dbServer>
<dbServer name="slave2"parent="abstractServer"> #设置后端可写slave2 <factoryConfig>
<!-- mysql ip -->
<property name="ipAddress">192.168.100.30</property>
</factoryConfig>
</dbServer>
<dbServer name="slave"virtual="true"> #设置定义一个虚拟的dbserver,实际上相当于一个dbserver组,这里将可读的数据库ip统一放到一个组中,将这个组的名字命名为slave <poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
<!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->
<property name="loadbalance">1</property> #选择调度算法,1表示复制均衡,2表示权重,3表示HA, 这里选择1 <!-- Separated by commas,such as: server1,server2,server1 -->
<property name="poolNames">slave1,slave2</property> #slave组成员 </poolConfig>
</dbServer>
</amoeba:dbServers>
$ mysql -h192.168.100.40 -P8066 -uroot -p123456
> select * from test.student;+----+--------+-----+-----+
| id | name | age | sex |+----+--------+-----+-----+
|1| 张三 |20| 男 ||2| 李四 |19| 男 |+----+--------+-----+-----+
192.168.100.10:
> show global status like 'Question%';+---------------+-------+
| Variable_name | Value |+---------------+-------+
| Questions |100|+---------------+-------+
192.168.100.20:
> show global status like 'Question%';+---------------+-------+
| Variable_name | Value |+---------------+-------+
| Questions |100|+---------------+-------+
192.168.100.30:
> show global status like 'Question%';+---------------+-------+
| Variable_name | Value |+---------------+-------+
| Questions |100|+---------------+-------+
每执行一次访问量增加1,确保两个从库访问量相同
192.168.100.10:
> show global status like 'Question%';+---------------+-------+
| Variable_name | Value |+---------------+-------+
| Questions |100|+---------------+-------+
192.168.100.20:
> show global status like 'Question%';+---------------+-------+
| Variable_name | Value |+---------------+-------+
| Questions |102|+---------------+-------+
192.168.100.30:
> show global status like 'Question%';+---------------+-------+
| Variable_name | Value |+---------------+-------+
| Questions |102|+---------------+-------+
从库值访问量各增加1,读取负载成功
192.168.100.10:
> show global status like 'Question%';+---------------+-------+
| Variable_name | Value |+---------------+-------+
| Questions |103|+---------------+-------+
192.168.100.20:
> show global status like 'Question%';+---------------+-------+
| Variable_name | Value |+---------------+-------+
| Questions |102|+---------------+-------+
192.168.100.30:
> show global status like 'Question%';+---------------+-------+
| Variable_name | Value |+---------------+-------+
| Questions |102|+---------------+-------+
主库访问量增加2,读写分离成功