作者个人研发的在高并发场景下,提供的简单、稳定、可扩展的延迟消息队列框架,具有精准的定时任务和延迟队列处理功能。自开源半年多以来,已成功为十几家中小型企业提供了精准定时调度方案,经受住了生产环境的考验。为使更多童鞋受益,现给出开源框架地址: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安装到不同的主机上,测试效果是一样的。
创建物理库
- mysql -uroot -proot -h192.168.209.140 -P3306
-
- drop database if exists db1;
- create database db1;
- drop database if exists db2;
- create database db2;
- drop database if exists db3;
- create database db3;
配置Mycat
schema.xml配置
- "1.0"?>
- schema SYSTEM "schema.dtd">
schema xmlns:mycat="http://org.opencloudb/" > -
- <schema name="binghe" checkSQLschema="false" sqlMaxLimit="100">
- <table name="travelrecord" dataNode="dn1,dn2,dn3" rule="auto-sharding-long">table>
- schema>
- --
- /> -->
-
name ="dn1" dataHost="localhost1" database="db1" /> -
name ="dn2" dataHost="localhost1" database="db2" /> -
name ="dn3" dataHost="localhost1" database="db3" /> - --
-
name ="jdbc_dn1" dataHost="jdbchost" database="db1" /> -
name ="jdbc_dn2" dataHost="jdbchost" database="db2" /> -
name ="jdbc_dn3" dataHost="jdbchost" database="db3" /> --> -
name ="localhost1" maxCon="1000" minCon="10" balance="0" - writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
-
select user() - -- can have multi write hosts -->
-
"hostM1" url="127.0.0.1:3306" user="root" password="root"> -
-
"hostM2" url="127.0.0.1:3306" user="root" password="root"> - --
- --password="123456" />-->
- --
--> -
- schema>
server.xml配置
- "1.0" encoding="UTF-8"?>
- "server.dtd">
"http://org.opencloudb/" > -
-
name ="defaultSqlParser">druidparser -
- <user name="binghe">
-
name ="password">binghe.123 -
name ="schemas">binghe - user>
- <user name="test">
-
name ="password">test -
name ="schemas">binghe -
name ="readOnly">true - user>
-
rule.xml配置
- "1.0" encoding="UTF-8"?>
- rule SYSTEM "rule.dtd">
rule xmlns:mycat="http://org.opencloudb/"> -
name="rule1"> - <rule>
-
id -
func1 - rule>
-
-
-
name="rule2"> - <rule>
-
user_id -
func1 - rule>
-
-
-
name="sharding-by-intfile"> - <rule>
-
sharding_id -
hash-int - rule>
-
-
name="auto-sharding-long"> - <rule>
-
id -
rang-long - rule>
-
-
name="mod-long"> - <rule>
-
id -
mod-long - rule>
-
-
name="sharding-by-murmur"> - <rule>
-
id -
murmur - rule>
-
-
name="sharding-by-month"> - <rule>
-
create_date -
partbymonth - rule>
-
-
name="latest-month-calldate"> - <rule>
-
calldate -
latestMonth - rule>
-
-
-
name="auto-sharding-rang-mod"> - <rule>
-
id -
rang-mod - rule>
-
-
-
name="jch"> - <rule>
-
id -
jump-consistent-hash - rule>
-
-
- <function name="murmur"
- class="org.opencloudb.route.function.PartitionByMurmurHash">
-
name ="seed">0 -
name ="count">2 -
name ="virtualBucketTimes">160 - function>
- <function name="hash-int"
- class="org.opencloudb.route.function.PartitionByFileMap">
-
name ="mapFile">partition-hash-int.txt - function>
- <function name="rang-long"
- class="org.opencloudb.route.function.AutoPartitionByLong">
-
name ="mapFile">autopartition-long.txt - function>
- <function name="mod-long" class="org.opencloudb.route.function.PartitionByMod">
- -- how many data nodes -->
-
name ="count">3 - function>
-
- <function name="func1" class="org.opencloudb.route.function.PartitionByLong">
-
name ="partitionCount">8 -
name ="partitionLength">128 - function>
- <function name="latestMonth"
- class="org.opencloudb.route.function.LatestMonthPartion">
-
name ="splitOneDay">24 - function>
- <function name="partbymonth"
- class="org.opencloudb.route.function.PartitionByMonth">
-
name ="dateFormat">yyyy-MM-dd -
name ="sBeginDate">2020-01-01 - function>
-
- <function name="rang-mod" class="org.opencloudb.route.function.PartitionByRangeMod">
-
name ="mapFile">partition-range-mod.txt - function>
-
- <function name="jump-consistent-hash" class="org.opencloudb.route.function.PartitionByJumpConsistentHash">
-
name ="totalBuckets">3 - function>
- rule>
登录Mycat
登录Mycat
命令行输入以下命令登录Mycat
- D:\>mysql -ubinghe -pbinghe.123 -h192.168.209.140 -P8066
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 2
- Server version: 5.5.8-mycat-1.6.1-RELEASE-20170807215126 MyCat Server (OpenCloundDB)
-
- Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
-
- Oracle is a registered trademark of Oracle Corporation and/or its
- affiliates. Other names may be trademarks of their respective
- owners.
-
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
-
- mysql>
创建表测试
输入以下命令查看创建表的路由
- create table travelrecord (id bigint not null primary key,user_id varchar(100),traveldate DATE, fee decimal,days int);
结果如下:
- mysql> explain create table travelrecord (id bigint not null primary key,user_id varchar(100),traveldate DATE, fee decimal,days int);
- +-----------+-----------------------------------------------------------------------------------------------------------------------+
- | DATA_NODE | SQL |
- +-----------+-----------------------------------------------------------------------------------------------------------------------+
- | dn1 | create table travelrecord (id bigint not null primary key,user_id varchar(100),traveldate DATE, fee decimal,days int) |
- | dn2 | create table travelrecord (id bigint not null primary key,user_id varchar(100),traveldate DATE, fee decimal,days int) |
- | dn3 | create table travelrecord (id bigint not null primary key,user_id varchar(100),traveldate DATE, fee decimal,days int) |
- +-----------+-----------------------------------------------------------------------------------------------------------------------+
- 3 rows in set (0.00 sec)
-
- mysql>
说明创建表的SQL语句被Mycat路由到dn1,dn2,dn3三个节点上,也就是说在3个节点上都执行了创建表的SQL。
我们输入建表语句:
- mysql> create table travelrecord (id bigint not null primary key,user_id varchar(100),traveldate DATE, fee decimal,days int);
- Query OK, 0 rows affected (0.18 sec)
此时,将会在dn1,dn2,dn3三个节点上创建travelrecord表。
录入数据测试
录入到dn1节点
我们在命令行输入如下SQL语句
- explain insert into travelrecord (id,user_id,traveldate,fee,days) values(100001,'binghe','2020-11-10',510.5,3);
结果如下:
- mysql> explain insert into travelrecord (id,user_id,traveldate,fee,days) values(100001,'binghe','2017-08-07',510.5,3);
- +-----------+-------------------------------------------------------------------------------------------------------------+
- | DATA_NODE | SQL |
- +-----------+-------------------------------------------------------------------------------------------------------------+
- | dn1 | insert into travelrecord (id,user_id,traveldate,fee,days) values(100001,'binghe','2017-08-07',510.5,3) |
- +-----------+-------------------------------------------------------------------------------------------------------------+
- 1 row in set (0.00 sec)
说明Mycat将SQL路由到了dn1节点。
我们执行插入语句:
- mysql> insert into travelrecord (id,user_id,traveldate,fee,days) values(100001,'binghe','2020-11-10',510.5,3);
- Query OK, 1 row affected, 1 warning (0.01 sec)
-
- mysql>
录入到dn2节点
我们在命令行输入如下语句:
- explain insert into travelrecord (id,user_id,traveldate,fee,days) values(8000004,'binghe','2017-08-07',510.5,3);
结果如下:
- mysql> explain insert into travelrecord (id,user_id,traveldate,fee,days) values(8000004,'binghe','2020-11-10',510.5,3);
- +-----------+--------------------------------------------------------------------------------------------------------------+
- | DATA_NODE | SQL |
- +-----------+--------------------------------------------------------------------------------------------------------------+
- | dn2 | insert into travelrecord (id,user_id,traveldate,fee,days) values(8000004,'binghe','2020-11-10',510.5,3) |
- +-----------+--------------------------------------------------------------------------------------------------------------+
- 1 row in set (0.00 sec)
说明Mycat将SQL路由到了dn2节点,我们执行插入语句:
- mysql> insert into travelrecord (id,user_id,traveldate,fee,days) values(8000004,'binghe','2017-08-07',510.5,3);
- Query OK, 1 row affected, 1 warning (0.06 sec)
路由到dn3节点
我们在命令行输入如下语句
- explain insert into travelrecord (id,user_id,traveldate,fee,days) values(10000004,'binghe','2017-08-07',510.5,3);
结果为:
- mysql> explain insert into travelrecord (id,user_id,traveldate,fee,days) values(10000004,'binghe','2020-11-10',510.5,3);
- +-----------+---------------------------------------------------------------------------------------------------------------+
- | DATA_NODE | SQL |
- +-----------+---------------------------------------------------------------------------------------------------------------+
- | dn3 | insert into travelrecord (id,user_id,traveldate,fee,days) values(10000004,'binghe','2020-11-10',510.5,3) |
- +-----------+---------------------------------------------------------------------------------------------------------------+
- 1 row in set (0.00 sec)
说明Mycat将SQL路由到了dn3节点,我们同样执行插入语句的操作
- mysql> insert into travelrecord (id,user_id,traveldate,fee,days) values(10000004,'binghe','2017-08-07',510.5,3);
- Query OK, 1 row affected, 1 warning (0.01 sec)
查询测试
查询所有数据
在命令行执行如下语句:
- explain select * from travelrecord;
结果为:
- mysql> explain select * from travelrecord;
- +-----------+--------------------------------------+
- | DATA_NODE | SQL |
- +-----------+--------------------------------------+
- | dn1 | SELECT * FROM travelrecord LIMIT 100 |
- | dn2 | SELECT * FROM travelrecord LIMIT 100 |
- | dn3 | SELECT * FROM travelrecord LIMIT 100 |
- +-----------+--------------------------------------+
- 3 rows in set (0.01 sec)
说明查询所有的数据,Mycat是将SQL语句路由到了所有的数据分片,即dn1,dn2,dn3节点上。
根据id查询指定数据
我们分别在命令行中执行如下SQL:
- explain select * from travelrecord where id = 1000004;
- explain select * from travelrecord where id = 8000004;
- explain select * from travelrecord where id = 10000004;
得到的结果依次如下:
- mysql> explain select * from travelrecord where id = 1000004;
- +-----------+---------------------------------------------------------+
- | DATA_NODE | SQL |
- +-----------+---------------------------------------------------------+
- | dn1 | SELECT * FROM travelrecord WHERE id = 1000004 LIMIT 100 |
- +-----------+---------------------------------------------------------+
- 1 row in set (0.06 sec)
-
- mysql> explain select * from travelrecord where id = 8000004;
- +-----------+---------------------------------------------------------+
- | DATA_NODE | SQL |
- +-----------+---------------------------------------------------------+
- | dn2 | SELECT * FROM travelrecord WHERE id = 8000004 LIMIT 100 |
- +-----------+---------------------------------------------------------+
- 1 row in set (0.00 sec)
-
- mysql> explain select * from travelrecord where id = 10000004;
- +-----------+----------------------------------------------------------+
- | DATA_NODE | SQL |
- +-----------+----------------------------------------------------------+
- | dn3 | SELECT * FROM travelrecord WHERE id = 10000004 LIMIT 100 |
- +-----------+----------------------------------------------------------+
- 1 row in set (0.00 sec)
说明:按照分片字段查询,Mycat只会将SQL路由到指定的数据分片。
删表测试
在命令行输入如下SQL:
- explain drop table travelrecord;
结果如下
- mysql> explain drop table travelrecord;
- +-----------+-------------------------+
- | DATA_NODE | SQL |
- +-----------+-------------------------+
- | dn1 | drop table travelrecord |
- | dn2 | drop table travelrecord |
- | dn3 | drop table travelrecord |
- +-----------+-------------------------+
- 3 rows in set (0.00 sec)
有结果可知,删表操作和创建表操作一样,Mycat在本实例中都会将SQL路由到所有的数据分片。
注意:本文的Mycat路由结果针对本文的配置实例,其他配置下,Mycat的路由结果可能会有不同。
好了,我们今天就到这儿吧,我是冰河,我们下期见~~
本文转载自微信公众号「冰河技术」,可以通过以下二维码关注。转载本文请联系冰河技术公众号。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
软考中级精品资料免费领
- 历年真题答案解析
- 备考技巧名师总结
- 高频考点精准押题
- 资料下载
- 历年真题
193.9 KB下载数265
191.63 KB下载数245
143.91 KB下载数1148
183.71 KB下载数642
644.84 KB下载数2756
相关文章
发现更多好内容- Java 如何获取某个接口的实现类?(Java怎么获取某个接口的实现类)
- Java中如何正确使用 collections.shuffle 方法?(Java中collections.shuffle怎么使用)
- Java Swing 中常用的布局有哪些?(java swing常用布局有哪些)
- 如何通过 Java Reflection 获取泛型信息?(java reflection如何获取泛型信息)
- 如何自定义 Java 泛型通配符?(java泛型通配符怎么自定义)
- Java Spring 注解与 XML 配置的差异究竟有哪些?(java spring注解与XML配置的区别是什么)
- Java 动态线程池对性能究竟有哪些影响呢?(Java动态线程池对性能的影响)
- 在 Java 中,Guava 究竟有哪些作用呢?(java中guava的作用是什么)
- 软考高项证书能个税抵扣吗?软考高项证书个税啥时候填报?
- 在 JavaScript 中,offsetWidth 的作用究竟是什么?(javascript中offsetwidth作用是什么)
咦!没有更多了?去看看其它编程学习网 内容吧