IT资讯

Mycat核心开发者带你轻松掌握Mycat路由转发!!

作者:admin 2021-04-11 我要评论

作者个人研发的在高并发场景下,提供的简单、稳定、可扩展的延迟消息队列框架,具有精准的定时任务和延迟队列处理功能。自开源半年多以来,已成功为十几家中小型...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)

作者个人研发的在高并发场景下,提供的简单、稳定、可扩展的延迟消息队列框架,具有精准的定时任务和延迟队列处理功能。自开源半年多以来,已成功为十几家中小型企业提供了精准定时调度方案,经受住了生产环境的考验。为使更多童鞋受益,现给出开源框架地址:https://github.com/sunshinelyz/mykit-delay

写在前面

熟悉Mycat的小伙伴都知道,Mycat一个很重要的功能就是路由转发,那么,这篇文章就带着大家一起来看看Mycat是如何进行路由转发的,好了,不多说了,我们直接进入主题。

环境准备

软件版本

操作系统:CentOS-6.8

JDK版本:jdk1.8

Mycat版本:Mycat-server-1.6

MySQL:5.7

注意:这里,我将Mycat和MySQL安装在同一台虚拟机(IP:192.168.209.140 主机名为:binghe140),大家也可以将Mycat和MySQL安装到不同的主机上,测试效果是一样的。

创建物理库

  1. mysql -uroot -proot -h192.168.209.140 -P3306 
  2.  
  3. drop database if exists db1; 
  4. create database db1; 
  5. drop database if exists db2; 
  6. create database db2; 
  7. drop database if exists db3; 
  8. create database db3; 

配置Mycat

schema.xml配置

  1. <?xml version="1.0"?> 
  2. <!DOCTYPE mycat:schema SYSTEM "schema.dtd"
  3. <mycat:schema xmlns:mycat="http://org.opencloudb/" > 
  4.   
  5.  <schema name="binghe" checkSQLschema="false" sqlMaxLimit="100"
  6.   <table name="travelrecord" dataNode="dn1,dn2,dn3" rule="auto-sharding-long"></table
  7.  </schema
  8.  <!-- <dataNode name="dn1$0-743" dataHost="localhost1" database="db$0-743"  
  9.   /> --> 
  10.  <dataNode name="dn1" dataHost="localhost1" database="db1" /> 
  11.  <dataNode name="dn2" dataHost="localhost1" database="db2" /> 
  12.  <dataNode name="dn3" dataHost="localhost1" database="db3" /> 
  13.  <!--<dataNode name="dn4" dataHost="sequoiadb1" database="SAMPLE" /> 
  14.   <dataNode name="jdbc_dn1" dataHost="jdbchost" database="db1" />  
  15.  <dataNode name="jdbc_dn2" dataHost="jdbchost" database="db2" />  
  16.  <dataNode name="jdbc_dn3"  dataHost="jdbchost" database="db3" /> --> 
  17.  <dataHost name="localhost1" maxCon="1000" minCon="10" balance="0" 
  18.   writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100"
  19.   <heartbeat>select user()</heartbeat> 
  20.   <!-- can have multi write hosts --> 
  21.   <writeHost host="hostM1" url="127.0.0.1:3306" user="root" password="root"></writeHost> 
  22.    
  23.   <writeHost host="hostM2" url="127.0.0.1:3306" user="root" password="root"></writeHost> 
  24.   <!--<writeHost host="hostS1" url="localhost:3316" user="root"--> 
  25.    <!--password="123456" />--> 
  26.   <!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> --> 
  27.  </dataHost> 
  28. </mycat:schema

server.xml配置

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <!DOCTYPE mycat:server SYSTEM "server.dtd"
  3. <mycat:server xmlns:mycat="http://org.opencloudb/"
  4.  <system> 
  5.  <property name="defaultSqlParser">druidparser</property> 
  6.  </system> 
  7.  <user name="binghe"
  8.   <property name="password">binghe.123</property> 
  9.   <property name="schemas">binghe</property> 
  10.  </user
  11.  <user name="test"
  12.   <property name="password">test</property> 
  13.   <property name="schemas">binghe</property> 
  14.   <property name="readOnly">true</property> 
  15.  </user
  16. </mycat:server> 

rule.xml配置

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <!DOCTYPE mycat:rule SYSTEM "rule.dtd"
  3. <mycat:rule xmlns:mycat="http://org.opencloudb/"
  4.  <tableRule name="rule1"
  5.   <rule
  6.    <columns>id</columns> 
  7.    <algorithm>func1</algorithm> 
  8.   </rule
  9.  </tableRule> 
  10.   
  11.  <tableRule name="rule2"
  12.   <rule
  13.    <columns>user_id</columns> 
  14.    <algorithm>func1</algorithm> 
  15.   </rule
  16.  </tableRule> 
  17.   
  18.  <tableRule name="sharding-by-intfile"
  19.   <rule
  20.    <columns>sharding_id</columns> 
  21.    <algorithm>hash-int</algorithm> 
  22.   </rule
  23.  </tableRule> 
  24.  <tableRule name="auto-sharding-long"
  25.   <rule
  26.    <columns>id</columns> 
  27.    <algorithm>rang-long</algorithm> 
  28.   </rule
  29.  </tableRule> 
  30.  <tableRule name="mod-long"
  31.   <rule
  32.    <columns>id</columns> 
  33.    <algorithm>mod-long</algorithm> 
  34.   </rule
  35.  </tableRule> 
  36.  <tableRule name="sharding-by-murmur"
  37.   <rule
  38.    <columns>id</columns> 
  39.    <algorithm>murmur</algorithm> 
  40.   </rule
  41.  </tableRule> 
  42.  <tableRule name="sharding-by-month"
  43.   <rule
  44.    <columns>create_date</columns> 
  45.    <algorithm>partbymonth</algorithm> 
  46.   </rule
  47.  </tableRule> 
  48.  <tableRule name="latest-month-calldate"
  49.   <rule
  50.    <columns>calldate</columns> 
  51.    <algorithm>latestMonth</algorithm> 
  52.   </rule
  53.  </tableRule> 
  54.   
  55.  <tableRule name="auto-sharding-rang-mod"
  56.   <rule
  57.    <columns>id</columns> 
  58.    <algorithm>rang-mod</algorithm> 
  59.   </rule
  60.  </tableRule> 
  61.   
  62.  <tableRule name="jch"
  63.   <rule
  64.    <columns>id</columns> 
  65.    <algorithm>jump-consistent-hash</algorithm> 
  66.   </rule
  67.  </tableRule> 
  68.   
  69.  <function name="murmur" 
  70.   class="org.opencloudb.route.function.PartitionByMurmurHash"
  71.   <property name="seed">0</property> 
  72.   <property name="count">2</property> 
  73.   <property name="virtualBucketTimes">160</property> 
  74.  </function
  75.  <function name="hash-int" 
  76.   class="org.opencloudb.route.function.PartitionByFileMap"
  77.   <property name="mapFile">partition-hash-int.txt</property> 
  78.  </function
  79.  <function name="rang-long" 
  80.   class="org.opencloudb.route.function.AutoPartitionByLong"
  81.   <property name="mapFile">autopartition-long.txt</property> 
  82.  </function
  83.  <function name="mod-long" class="org.opencloudb.route.function.PartitionByMod"
  84.   <!-- how many data nodes --> 
  85.   <property name="count">3</property> 
  86.  </function
  87.   
  88.  <function name="func1" class="org.opencloudb.route.function.PartitionByLong"
  89.   <property name="partitionCount">8</property> 
  90.   <property name="partitionLength">128</property> 
  91.  </function
  92.  <function name="latestMonth" 
  93.   class="org.opencloudb.route.function.LatestMonthPartion"
  94.   <property name="splitOneDay">24</property> 
  95.  </function
  96.  <function name="partbymonth" 
  97.   class="org.opencloudb.route.function.PartitionByMonth"
  98.   <property name="dateFormat">yyyy-MM-dd</property> 
  99.   <property name="sBeginDate">2020-01-01</property> 
  100.  </function
  101.   
  102.  <function name="rang-mod" class="org.opencloudb.route.function.PartitionByRangeMod"
  103.          <property name="mapFile">partition-range-mod.txt</property> 
  104.  </function
  105.   
  106.  <function name="jump-consistent-hash" class="org.opencloudb.route.function.PartitionByJumpConsistentHash"
  107.   <property name="totalBuckets">3</property> 
  108.  </function
  109. </mycat:rule

登录Mycat

登录Mycat

命令行输入以下命令登录Mycat

  1. D:\>mysql -ubinghe -pbinghe.123 -h192.168.209.140 -P8066 
  2. Welcome to the MySQL monitor.  Commands end with ; or \g. 
  3. Your MySQL connection id is 2 
  4. Server version: 5.5.8-mycat-1.6.1-RELEASE-20170807215126 MyCat Server (OpenCloundDB) 
  5.   
  6. Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. 
  7.   
  8. Oracle is a registered trademark of Oracle Corporation and/or its 
  9. affiliates. Other names may be trademarks of their respective 
  10. owners. 
  11.   
  12. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 
  13.   
  14. mysql> 

创建表测试

输入以下命令查看创建表的路由

  1. create table travelrecord (id bigint not null primary key,user_id varchar(100),traveldate DATE, fee decimal,days int);  

结果如下:

  1. mysql> explain create table travelrecord (id bigint not null primary key,user_id varchar(100),traveldate DATE, fee decimal,days int); 
  2. +-----------+-----------------------------------------------------------------------------------------------------------------------+ 
  3. | DATA_NODE | SQL                                                                                                                   | 
  4. +-----------+-----------------------------------------------------------------------------------------------------------------------+ 
  5. | dn1       | create table travelrecord (id bigint not null primary key,user_id varchar(100),traveldate DATE, fee decimal,days int) | 
  6. | dn2       | create table travelrecord (id bigint not null primary key,user_id varchar(100),traveldate DATE, fee decimal,days int) | 
  7. | dn3       | create table travelrecord (id bigint not null primary key,user_id varchar(100),traveldate DATE, fee decimal,days int) | 
  8. +-----------+-----------------------------------------------------------------------------------------------------------------------+ 
  9. rows in set (0.00 sec) 
  10.   
  11. mysql> 

说明创建表的SQL语句被Mycat路由到dn1,dn2,dn3三个节点上,也就是说在3个节点上都执行了创建表的SQL。

我们输入建表语句:

  1. mysql> create table travelrecord (id bigint not null primary key,user_id varchar(100),traveldate DATE, fee decimal,days int); 
  2. Query OK, 0 rows affected (0.18 sec) 

此时,将会在dn1,dn2,dn3三个节点上创建travelrecord表。

录入数据测试

录入到dn1节点

我们在命令行输入如下SQL语句

  1. explain insert into travelrecord (id,user_id,traveldate,fee,days) values(100001,'binghe','2020-11-10',510.5,3); 

结果如下:

  1. mysql> explain insert into travelrecord (id,user_id,traveldate,fee,days) values(100001,'binghe','2017-08-07',510.5,3); 
  2. +-----------+-------------------------------------------------------------------------------------------------------------+ 
  3. | DATA_NODE | SQL                                                                                                         | 
  4. +-----------+-------------------------------------------------------------------------------------------------------------+ 
  5. | dn1       | insert into travelrecord (id,user_id,traveldate,fee,days) values(100001,'binghe','2017-08-07',510.5,3) | 
  6. +-----------+-------------------------------------------------------------------------------------------------------------+ 
  7. 1 row in set (0.00 sec) 

说明Mycat将SQL路由到了dn1节点。

我们执行插入语句:

  1. mysql> insert into travelrecord (id,user_id,traveldate,fee,days) values(100001,'binghe','2020-11-10',510.5,3); 
  2. Query OK, 1 row affected, 1 warning (0.01 sec) 
  3.   
  4. mysql> 

录入到dn2节点

我们在命令行输入如下语句:

  1. explain insert into travelrecord (id,user_id,traveldate,fee,days) values(8000004,'binghe','2017-08-07',510.5,3); 

结果如下:

  1. mysql> explain insert into travelrecord (id,user_id,traveldate,fee,days) values(8000004,'binghe','2020-11-10',510.5,3); 
  2. +-----------+--------------------------------------------------------------------------------------------------------------+ 
  3. | DATA_NODE | SQL                                                                                                          | 
  4. +-----------+--------------------------------------------------------------------------------------------------------------+ 
  5. | dn2       | insert into travelrecord (id,user_id,traveldate,fee,days) values(8000004,'binghe','2020-11-10',510.5,3) | 
  6. +-----------+--------------------------------------------------------------------------------------------------------------+ 
  7. 1 row in set (0.00 sec) 

说明Mycat将SQL路由到了dn2节点,我们执行插入语句:

  1. mysql> insert into travelrecord (id,user_id,traveldate,fee,days) values(8000004,'binghe','2017-08-07',510.5,3); 
  2. Query OK, 1 row affected, 1 warning (0.06 sec) 

路由到dn3节点

我们在命令行输入如下语句

  1. explain insert into travelrecord (id,user_id,traveldate,fee,days) values(10000004,'binghe','2017-08-07',510.5,3); 

结果为:

  1. mysql> explain insert into travelrecord (id,user_id,traveldate,fee,days) values(10000004,'binghe','2020-11-10',510.5,3); 
  2. +-----------+---------------------------------------------------------------------------------------------------------------+ 
  3. | DATA_NODE | SQL                                                                                                           | 
  4. +-----------+---------------------------------------------------------------------------------------------------------------+ 
  5. | dn3       | insert into travelrecord (id,user_id,traveldate,fee,days) values(10000004,'binghe','2020-11-10',510.5,3) | 
  6. +-----------+---------------------------------------------------------------------------------------------------------------+ 
  7. 1 row in set (0.00 sec) 

说明Mycat将SQL路由到了dn3节点,我们同样执行插入语句的操作

  1. mysql>  insert into travelrecord (id,user_id,traveldate,fee,days) values(10000004,'binghe','2017-08-07',510.5,3); 
  2. Query OK, 1 row affected, 1 warning (0.01 sec) 

查询测试

查询所有数据

在命令行执行如下语句:

  1. explain select * from travelrecord; 

结果为:

  1. mysql> explain select * from travelrecord; 
  2. +-----------+--------------------------------------+ 
  3. | DATA_NODE | SQL                                  | 
  4. +-----------+--------------------------------------+ 
  5. | dn1       | SELECT * FROM travelrecord LIMIT 100 | 
  6. | dn2       | SELECT * FROM travelrecord LIMIT 100 | 
  7. | dn3       | SELECT * FROM travelrecord LIMIT 100 | 
  8. +-----------+--------------------------------------+ 
  9. rows in set (0.01 sec) 

说明查询所有的数据,Mycat是将SQL语句路由到了所有的数据分片,即dn1,dn2,dn3节点上。

根据id查询指定数据

我们分别在命令行中执行如下SQL:

  1. explain select * from travelrecord where id = 1000004; 
  2. explain select * from travelrecord where id = 8000004; 
  3. explain select * from travelrecord where id = 10000004; 

得到的结果依次如下:

  1. mysql> explain select * from travelrecord where id = 1000004; 
  2. +-----------+---------------------------------------------------------+ 
  3. | DATA_NODE | SQL                                                     | 
  4. +-----------+---------------------------------------------------------+ 
  5. | dn1       | SELECT * FROM travelrecord WHERE id = 1000004 LIMIT 100 | 
  6. +-----------+---------------------------------------------------------+ 
  7. 1 row in set (0.06 sec) 
  8.   
  9. mysql> explain select * from travelrecord where id = 8000004; 
  10. +-----------+---------------------------------------------------------+ 
  11. | DATA_NODE | SQL                                                     | 
  12. +-----------+---------------------------------------------------------+ 
  13. | dn2       | SELECT * FROM travelrecord WHERE id = 8000004 LIMIT 100 | 
  14. +-----------+---------------------------------------------------------+ 
  15. 1 row in set (0.00 sec) 
  16.   
  17. mysql> explain select * from travelrecord where id = 10000004; 
  18. +-----------+----------------------------------------------------------+ 
  19. | DATA_NODE | SQL                                                      | 
  20. +-----------+----------------------------------------------------------+ 
  21. | dn3       | SELECT * FROM travelrecord WHERE id = 10000004 LIMIT 100 | 
  22. +-----------+----------------------------------------------------------+ 
  23. 1 row in set (0.00 sec) 

说明:按照分片字段查询,Mycat只会将SQL路由到指定的数据分片。

删表测试

在命令行输入如下SQL:

  1. explain drop table travelrecord; 

结果如下

  1. mysql> explain drop table travelrecord; 
  2. +-----------+-------------------------+ 
  3. | DATA_NODE | SQL                     | 
  4. +-----------+-------------------------+ 
  5. | dn1       | drop table travelrecord | 
  6. | dn2       | drop table travelrecord | 
  7. | dn3       | drop table travelrecord | 
  8. +-----------+-------------------------+ 
  9. rows in set (0.00 sec) 

有结果可知,删表操作和创建表操作一样,Mycat在本实例中都会将SQL路由到所有的数据分片。

注意:本文的Mycat路由结果针对本文的配置实例,其他配置下,Mycat的路由结果可能会有不同。

好了,我们今天就到这儿吧,我是冰河,我们下期见~~

本文转载自微信公众号「冰河技术」,可以通过以下二维码关注。转载本文请联系冰河技术公众号


本文转载自网络,原文链接:https://mp.weixin.qq.com/s/xk1Sx8aVst36k4tIkiXJig

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • 20款小众宝藏APP,工作、生活全不误,

    20款小众宝藏APP,工作、生活全不误,

  • 谷歌开发 MicroDroid,用于虚拟机的精

    谷歌开发 MicroDroid,用于虚拟机的精

  • 微信 8.0 专属红包灰度测试:可指定发

    微信 8.0 专属红包灰度测试:可指定发

  • 如何隐藏你的热更新 Bundle 文件?

    如何隐藏你的热更新 Bundle 文件?

腾讯云代理商
海外云服务器