mycat实现读写分离(一)

1、实现读写分离的方式

  1.1 应用层实现

    比如使用SpringJDBC/myBatis/Hibernate访问数据库时配置多数据源,这些组件会通过算法把请求分流到不同的数据源,本文不做过多说明。

  1.2 代理实现

    这种方式是在应用层和数据库集群之间添加一个代理服务,应用层访问代理,代理根据请求类型(读/写)自动分流到不同的数据库服务器。各种分布式数据库中间件应运而生,比如Amoeba、Atlas、Cobar、Mycat、Mysql-proxy等,Mycat继承并发扬了Cobar,是                      相对成熟开源社区较为活跃的一款数据库中间件。

2、mycat读写分离配置

  2.1 下载mycat

    2016年,mycat发布了1.5版本,这是目前为止比较稳定的版本。生产环境官方还是建议使用1.5,但是本文搭建实验环境选择了较新的mycat1.6。下载地址:https://github.com/MyCATApache/Mycat-download

  2.2  安装mycat

    将下载的Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz上传至mycat服务器,执行tar -zxvf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz,解压后如图得到mycat根目录。

    

    mycat目录结构:

     

    bin               mycat命令,启动、重启、停止等命令

    catlet           mycat的扩展功能

           conf             mycat相关配置文件所在目录

    lib                mycat引用的jar包,mycat是Java开发的,(上述服务器中已经配置好jdk)

    logs             mycat日志文件所在目录主要mycat.pid、mycat.log、wrapper.log

  2.3 配置文件:

    server.xml 

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <!-- - - Licensed under the Apache License, Version 2.0 (the "License"); 
  3     - you may not use this file except in compliance with the License. - You 
  4     may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 
  5     - - Unless required by applicable law or agreed to in writing, software - 
  6     distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT 
  7     WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the 
  8     License for the specific language governing permissions and - limitations 
  9     under the License. -->
 10 <!DOCTYPE mycat:server SYSTEM "server.dtd">
 11 <mycat:server xmlns:mycat="http://io.mycat/">
 12     <system>
 13     <property name="useSqlStat">0</property>  <!-- 1为开启实时统计、0为关闭 -->
 14     <property name="useGlobleTableCheck">0</property>  <!-- 1为开启全加班一致性检测、0为关闭 -->
 15 
 16         <property name="sequnceHandlerType">2</property>
 17       <!--  <property name="useCompression">1</property>--> <!--1为开启mysql压缩协议-->
 18         <!--  <property name="fakeMySQLVersion">5.6.20</property>--> <!--设置模拟的MySQL版本号-->
 19     <!-- <property name="processorBufferChunk">40960</property> -->
 20     <!-- 
 21     <property name="processors">1</property> 
 22     <property name="processorExecutor">32</property> 
 23      -->
 24         <!--默认为type 0: DirectByteBufferPool | type 1 ByteBufferArena-->
 25         <property name="processorBufferPoolType">0</property>
 26         <!--默认是65535 64K 用于sql解析时最大文本长度 -->
 27         <!--<property name="maxStringLiteralLength">65535</property>-->
 28         <!--<property name="sequnceHandlerType">0</property>-->
 29         <!--<property name="backSocketNoDelay">1</property>-->
 30         <!--<property name="frontSocketNoDelay">1</property>-->
 31         <!--<property name="processorExecutor">16</property>-->
 32         <!--
 33             <property name="serverPort">8066</property> <property name="managerPort">9066</property> 
 34             <property name="idleTimeout">300000</property> <property name="bindIp">0.0.0.0</property> 
 35             <property name="frontWriteQueueSize">4096</property> <property name="processors">32</property> -->
 36         <!--分布式事务开关,0为不过滤分布式事务,1为过滤分布式事务(如果分布式事务内只涉及全局表,则不过滤),2为不过滤分布式事务,但是记录分布式事务日志-->
 37         <property name="handleDistributedTransactions">0</property>
 38         
 39             <!--
 40             off heap for merge/order/group/limit      1开启   0关闭
 41         -->
 42         <property name="useOffHeapForMerge">1</property>
 43 
 44         <!--
 45             单位为m
 46         -->
 47         <property name="memoryPageSize">1m</property>
 48 
 49         <!--
 50             单位为k
 51         -->
 52         <property name="spillsFileBufferSize">1k</property>
 53 
 54         <property name="useStreamOutput">0</property>
 55 
 56         <!--
 57             单位为m
 58         -->
 59         <property name="systemReserveMemorySize">384m</property>
 60 
 61 
 62         <!--是否采用zookeeper协调切换  -->
 63         <property name="useZKSwitch">true</property>
 64 
 65 
 66     </system>
 67     
 68     <!-- 全局SQL防火墙设置 -->
 69     <!-- 
 70     <firewall> 
 71        <whitehost>
 72           <host host="127.0.0.1" user="mycat"/>
 73           <host host="127.0.0.2" user="mycat"/>
 74        </whitehost>
 75        <blacklist check="false">
 76        </blacklist>
 77     </firewall>
 78     -->
 79     
 80     <user name="root">
 81         <property name="password">mysql</property>
 82         <property name="schemas">testdb</property>
 83         <property name="readOnly">false</property>
 84         
 85         <!-- 表级 DML 权限设置 -->
 86         <!--         
 87         <privileges check="false">
 88             <schema name="TESTDB" dml="0110" >
 89                 <table name="tb01" dml="0000"></table>
 90                 <table name="tb02" dml="1111"></table>
 91             </schema>
 92         </privileges>        
 93          -->
 94     </user>
 95 
 96     <!--
 97     <user name="user">
 98         <property name="password">user</property>
 99         <property name="schemas">TESTDB</property>
100         <property name="readOnly">true</property>
101     </user>
102     -->
103 
104 </mycat:server>

  schema.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <!DOCTYPE mycat:schema SYSTEM "schema.dtd">
 3 
 4 <mycat:schema xmlns:mycat="http://io.mycat/">  
 5   <!-- 数据库配置,与server.xml中的数据库对应 -->  
 6   <schema name="testdb" checkSQLschema="true" sqlMaxLimit="100"> 
 7     <table name="travel_record" primaryKey="id" autoIncrement="true" dataNode="dn1"/> 
 8   </schema>  
 9   <!-- 分片配置 -->  
10  
11   <dataNode name="dn1" dataHost="host1" database="testdb"/>  
12   <!--
13   <dataNode name="dn2" dataHost="host2" database="testdb"/>  
14   <dataNode name="dn3" dataHost="host3" database="testdb"/>  
15   -->
16   <!-- 物理数据库配置 -->  
17   <dataHost name="host1" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native"> 
18     <heartbeat>select user();</heartbeat>  
19     <writeHost host="M1" url="192.168.153.130:3306" user="root" password="mysql">
20        <readHost host="S1" url="192.168.153.131:3306" user="root" password="mysql"/>
21     </writeHost> 
22     <writeHost host="M2" url="192.168.153.133:3306" user="root" password="mysql"/>
23   </dataHost>
24   <!--  
25   <dataHost name="host2" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native"> 
26     <heartbeat>select user();</heartbeat>  
27     <writeHost host="M2" url="192.168.153.131:3306" user="root" password="mysql"/> 
28   </dataHost>  
29   <dataHost name="host3" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native"> 
30     <heartbeat>select user();</heartbeat>  
31     <writeHost host="M3" url="192.168.153.133:3306" user="root" password="mysql"/> 
32   </dataHost>
33   --> 
34 </mycat:schema>

  2.4 配置文件说明

   server.xml中user标签指定逻辑库的用户名,密码,库名,访问权限等相关配置。

   schema.xml   

        schema标签指定server.xml中指定的逻辑库,schema标签中通过table定义逻辑表,并指定数据节点。

             dataNode定义数据节点指定节点名称,物理节点host,物理库名。

        dataHost定义读写节点信息。

  2.5 启动mycat

    通过bin目录下mycat脚本启动,执行./mycat start。通过管理端9066端口查看数据源:mysql -uroot -pmysql -P9066 -h127.0.0.1(8066用于操作数据库)

    

    

  注意:在配置mycat读写分离时,只会对写节点进行写入,mycat不负责任何数据同步问题,因此需提前配置好mysql的主从复制数据自动同步。

                          

    

猜你喜欢

转载自www.cnblogs.com/ouhouki/p/9628837.html