我们通过路由选择来决定操作时访问那个数据库,而路由的选择方式不外乎以下几种:
1) SpringAOP方式:spring底层配置多个数据源,配置路由(面向切面编程)手工写很多代码(废除)
2) MySql proxy编程lua(脚本语言)(几乎没人直接使用)
3) Amoeba国人基于mysql proxy。不用编程,配置两个xml文件即可,对程序没有侵入性。
4) MyCat阿里,基于mysqlProxy
这里主要应用Amoeba。
Amoeba读写分离,通过配置xml文件来声明有几个数据库服务器。通过配置来决定哪个机器用来写操作,哪几个的机器用来读操作。根据配置就把用户请求人工分开。让写的操作都写到master的服务器,slave的各个节点都自动同步msater上的数据。这样当海量用户过来,大量查询都转向slave节点服务器,从而减轻主上的访问压力。对主的操作就更加的快捷,没有查询的干扰。同时复制的速度很快。让系统性能飞速提升,大概30%。
因为amoeba用户请求都要通过amoeba代理,这时对其访问压力非常大,给其充分资源,尽量单独找一台服务器安装amoeba。
优势:
Amoeba主要解决以下问题:
a). 数据切分后复杂数据源整合
b). 提供数据切分规则并降低数据切分规则给数据库带来的影响
c). 降低数据库与客户端连接
d). 读写分离路由
安装配置:
1、Amoeba是java写的,所以要想运行amoeba必须先要安装jdk,配置好jdk的环境变量(详见随笔jdk的配置)
2、安装Amoeba
3、配置
3.1 dbServers.xml
修改mysql的端口 默认端口是3306
修改mysql的用户名密码,要求所有节点的端口和用户名密码一致
修改主从节点的名字和ip地址
修改负载方式
详细配置如下:
<?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">root</property> <!-- mysql password --> <property name="password">root</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="testOnReturn">true</property> <property name="testWhileIdle">true</property> </poolConfig> </dbServer> <dbServer name="master" parent="abstractServer"> <factoryConfig> <!-- mysql ip --> <property name="ipAddress">192.168.170.43</property> </factoryConfig> </dbServer> <dbServer name="slave" parent="abstractServer"> <factoryConfig> <!-- mysql ip --> <property name="ipAddress">192.168.170.85</property> </factoryConfig> </dbServer> <dbServer name="multiPool" 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">master,slave</property> </poolConfig> </dbServer> </amoeba:dbServers>
3.2 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.net.ServerableConnectionManager"> <!-- port --> <property name="port">8066</property> <!-- bind ipAddress --> <!-- <property name="ipAddress">127.0.0.1</property> --> <property name="manager">${clientConnectioneManager}</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="authenticator"> <bean class="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator"> <property name="user">root</property> <property name="password">root</property> <property name="filter"> <bean class="com.meidusa.amoeba.server.IPAccessController"> <property name="ipFile">${amoeba.home}/conf/access_list.conf</property> </bean> </property> </bean> </property> </service> <!-- server class must implements com.meidusa.amoeba.service.Service --> <service name="Amoeba Monitor Server" class="com.meidusa.amoeba.monitor.MonitorServer"> <!-- port --> <!-- default value: random number <property name="port">9066</property> --> <!-- bind ipAddress --> <property name="ipAddress">127.0.0.1</property> <property name="daemon">true</property> <property name="manager">${clientConnectioneManager}</property> <property name="connectionFactory"> <bean class="com.meidusa.amoeba.monitor.net.MonitorClientConnectionFactory"></bean> </property> </service> <runtime class="com.meidusa.amoeba.mysql.context.MysqlRuntimeContext"> <!-- proxy server net IO Read thread size --> <property name="readThreadPoolSize">20</property> <!-- proxy server client process thread size --> <property name="clientSideThreadPoolSize">30</property> <!-- mysql server data packet process thread size --> <property name="serverSideThreadPoolSize">30</property> <!-- per connection cache prepared statement size --> <property name="statementCacheSize">500</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="clientConnectioneManager" class="com.meidusa.amoeba.net.MultiConnectionManagerWrapper"> <property name="subManagerClassName">com.meidusa.amoeba.net.ConnectionManager</property> <!-- default value is avaliable Processors <property name="processors">5</property> --> </connectionManager> <connectionManager name="defaultManager" class="com.meidusa.amoeba.net.MultiConnectionManagerWrapper"> <property name="subManagerClassName">com.meidusa.amoeba.net.AuthingableConnectionManager</property> <!-- default value is avaliable Processors <property name="processors">5</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">master</property> <property name="writePool">master</property> <property name="readPool">multiPool</property> <property name="needParse">true</property> </queryRouter> </amoeba:configuration>
启动Amoeba
./bin/amoeba start 或 ./bin/amoeba start &
./bin/laucher start 或 ./bin/laucher start &
如果有&则进行后台启动,没有&本窗口启动,且启动后占用本窗口不能再进行其他操作
停止Amoeba
./bin/amoeba stop
./bin/laucher stop
配置注意事项:
1)amoeba要求所有的节点的端口和用户名密码一致
2)验证数据库是否能正确的访问,链接test库
3)amoeba支持负载均衡,默认:轮询,权重
4)需要打开8806端口
/sbin/iptables -I INPUT -p tcp --dport 8066 -j ACCEPT
/etc/rc.d/init.d/iptables save #修改生效
/etc/init.d/iptables status #查看配置
三种负载均衡算法:轮询,权重,HA高可用
读写分离:写master,读slave01,slave02,master
电商项目:读的操作占量非常大,所有写的服务器资源严重浪费,所有读时让写的服务器负责少量读取操作。
总结:
1) 安装amoeba,配置两个xml
2) dbServers.xml 配置哪个服务器负责写操作,哪些(多个)服务器负责写操作。
3) 业务如果写操作量也比较大,只写的操作配置master,如果读的操作非常多,master不能完全利用,资源有所闲置,这时也把它配置的读的负载均衡策略中
4) amoeba核心配置文件,8066端口,所有节点的用户名,密码一致,加载dbServers.xml,执行