Mycat之——路由转发实例解析

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/l1028386804/article/details/76862419

转载请注明出处:http://blog.csdn.net/l1028386804/article/details/76862419

今天,给大家带来一篇通过一个配置实例来讲解Mycat路由转发的文章,熟悉Mycat的同学都知道,Mycat一个很重要的功能就是路由转发,那么,这篇文章就带着大家一起来看看Mycat是如何进行路由转发的,好了,不多说了,我们直接进入主题。

一、环境准备

1、软件版本

操作系统:CentOS-6.5-x86_64-bin-DVD1.iso
JDK版本:jdk1.7.0_80
Mycat版本:Mycat-server-1.5.1-RELEASE-20170717215510-Linux.tar.gz (下载源码自行编译的)
MySQL:5.6.32

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

2、创建物理库

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;

二、配置

1、schema.xml配置

<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://org.opencloudb/" >

	<schema name="lyz" checkSQLschema="false" sqlMaxLimit="100">
		<table name="travelrecord" dataNode="dn1,dn2,dn3" rule="auto-sharding-long"></table>
	</schema>
	<!-- <dataNode name="dn1$0-743" dataHost="localhost1" database="db$0-743" 
		/> -->
	<dataNode name="dn1" dataHost="localhost1" database="db1" />
	<dataNode name="dn2" dataHost="localhost1" database="db2" />
	<dataNode name="dn3" dataHost="localhost1" database="db3" />
	<!--<dataNode name="dn4" dataHost="sequoiadb1" database="SAMPLE" />
	 <dataNode name="jdbc_dn1" dataHost="jdbchost" database="db1" /> 
	<dataNode	name="jdbc_dn2" dataHost="jdbchost" database="db2" /> 
	<dataNode name="jdbc_dn3" 	dataHost="jdbchost" database="db3" /> -->
	<dataHost name="localhost1" maxCon="1000" minCon="10" balance="0"
		writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
		<heartbeat>select user()</heartbeat>
		<!-- can have multi write hosts -->
		<writeHost host="hostM1" url="127.0.0.1:3306" user="root" password="root"></writeHost>
		
		<writeHost host="hostM2" url="127.0.0.1:3306" user="root" password="root"></writeHost>
		<!--<writeHost host="hostS1" url="localhost:3316" user="root"-->
			<!--password="123456" />-->
		<!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> -->
	</dataHost>
</mycat:schema>

2、server.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<!-- - - Licensed under the Apache License, Version 2.0 (the "License"); 
	- you may not use this file except in compliance with the License. - You 
	may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 
	- - Unless required by applicable law or agreed to in writing, software - 
	distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT 
	WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the 
	License for the specific language governing permissions and - limitations 
	under the License. -->
<!DOCTYPE mycat:server SYSTEM "server.dtd">
<mycat:server xmlns:mycat="http://org.opencloudb/">
	<system>
	<property name="defaultSqlParser">druidparser</property>
      <!--  <property name="useCompression">1</property>--> <!--1为开启mysql压缩协议-->
	<!-- <property name="processorBufferChunk">40960</property> -->
	<!-- 
	<property name="processors">1</property> 
	<property name="processorExecutor">32</property> 
	 -->
		<!--默认是65535 64K 用于sql解析时最大文本长度 -->
		<!--<property name="maxStringLiteralLength">65535</property>-->
		<!--<property name="sequnceHandlerType">0</property>-->
		<!--<property name="backSocketNoDelay">1</property>-->
		<!--<property name="frontSocketNoDelay">1</property>-->
		<!--<property name="processorExecutor">16</property>-->
		<!-- 
			<property name="mutiNodeLimitType">1</property> 0:开启小数量级(默认) ;1:开启亿级数据排序
	    	<property name="mutiNodePatchSize">100</property> 亿级数量排序批量
			<property name="processors">32</property> <property name="processorExecutor">32</property> 
			<property name="serverPort">8066</property> <property name="managerPort">9066</property> 
			<property name="idleTimeout">300000</property> <property name="bindIp">0.0.0.0</property> 
			<property name="frontWriteQueueSize">4096</property> <property name="processors">32</property> -->
	</system>
	<user name="lyz">
		<property name="password">lyz.123</property>
		<property name="schemas">lyz</property>
	</user>

	<user name="test">
		<property name="password">test</property>
		<property name="schemas">lyz</property>
		<property name="readOnly">true</property>
	</user>
	<!-- 
	<quarantine> 
	   <whitehost>
	      <host host="127.0.0.1" user="mycat"/>
	      <host host="127.0.0.2" user="mycat"/>
	   </whitehost>
       <blacklist check="false"></blacklist>
	</quarantine>
	-->

</mycat:server>

3、rule.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<!-- - - Licensed under the Apache License, Version 2.0 (the "License"); 
	- you may not use this file except in compliance with the License. - You 
	may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 
	- - Unless required by applicable law or agreed to in writing, software - 
	distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT 
	WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the 
	License for the specific language governing permissions and - limitations 
	under the License. -->
<!DOCTYPE mycat:rule SYSTEM "rule.dtd">
<mycat:rule xmlns:mycat="http://org.opencloudb/">
	<tableRule name="rule1">
		<rule>
			<columns>id</columns>
			<algorithm>func1</algorithm>
		</rule>
	</tableRule>

	<tableRule name="rule2">
		<rule>
			<columns>user_id</columns>
			<algorithm>func1</algorithm>
		</rule>
	</tableRule>

	<tableRule name="sharding-by-intfile">
		<rule>
			<columns>sharding_id</columns>
			<algorithm>hash-int</algorithm>
		</rule>
	</tableRule>
	<tableRule name="auto-sharding-long">
		<rule>
			<columns>id</columns>
			<algorithm>rang-long</algorithm>
		</rule>
	</tableRule>
	<tableRule name="mod-long">
		<rule>
			<columns>id</columns>
			<algorithm>mod-long</algorithm>
		</rule>
	</tableRule>
	<tableRule name="sharding-by-murmur">
		<rule>
			<columns>id</columns>
			<algorithm>murmur</algorithm>
		</rule>
	</tableRule>
	<tableRule name="sharding-by-month">
		<rule>
			<columns>create_date</columns>
			<algorithm>partbymonth</algorithm>
		</rule>
	</tableRule>
	<tableRule name="latest-month-calldate">
		<rule>
			<columns>calldate</columns>
			<algorithm>latestMonth</algorithm>
		</rule>
	</tableRule>
	
	<tableRule name="auto-sharding-rang-mod">
		<rule>
			<columns>id</columns>
			<algorithm>rang-mod</algorithm>
		</rule>
	</tableRule>
	
	<tableRule name="jch">
		<rule>
			<columns>id</columns>
			<algorithm>jump-consistent-hash</algorithm>
		</rule>
	</tableRule>

	<function name="murmur"
		class="org.opencloudb.route.function.PartitionByMurmurHash">
		<property name="seed">0</property><!-- 默认是0 -->
		<property name="count">2</property><!-- 要分片的数据库节点数量,必须指定,否则没法分片 -->
		<property name="virtualBucketTimes">160</property><!-- 一个实际的数据库节点被映射为这么多虚拟节点,默认是160倍,也就是虚拟节点数是物理节点数的160倍 -->
		<!-- <property name="weightMapFile">weightMapFile</property> 节点的权重,没有指定权重的节点默认是1。以properties文件的格式填写,以从0开始到count-1的整数值也就是节点索引为key,以节点权重值为值。所有权重值必须是正整数,否则以1代替 -->
		<!-- <property name="bucketMapPath">/etc/mycat/bucketMapPath</property> 
			用于测试时观察各物理节点与虚拟节点的分布情况,如果指定了这个属性,会把虚拟节点的murmur hash值与物理节点的映射按行输出到这个文件,没有默认值,如果不指定,就不会输出任何东西 -->
	</function>
	<function name="hash-int"
		class="org.opencloudb.route.function.PartitionByFileMap">
		<property name="mapFile">partition-hash-int.txt</property>
	</function>
	<function name="rang-long"
		class="org.opencloudb.route.function.AutoPartitionByLong">
		<property name="mapFile">autopartition-long.txt</property>
	</function>
	<function name="mod-long" class="org.opencloudb.route.function.PartitionByMod">
		<!-- how many data nodes -->
		<property name="count">3</property>
	</function>

	<function name="func1" class="org.opencloudb.route.function.PartitionByLong">
		<property name="partitionCount">8</property>
		<property name="partitionLength">128</property>
	</function>
	<function name="latestMonth"
		class="org.opencloudb.route.function.LatestMonthPartion">
		<property name="splitOneDay">24</property>
	</function>
	<function name="partbymonth"
		class="org.opencloudb.route.function.PartitionByMonth">
		<property name="dateFormat">yyyy-MM-dd</property>
		<property name="sBeginDate">2015-01-01</property>
	</function>
	
	<function name="rang-mod" class="org.opencloudb.route.function.PartitionByRangeMod">
        	<property name="mapFile">partition-range-mod.txt</property>
	</function>
	
	<function name="jump-consistent-hash" class="org.opencloudb.route.function.PartitionByJumpConsistentHash">
		<property name="totalBuckets">3</property>
	</function>
</mycat:rule>

三、测试

1、登录Mycat

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

mysql -ulyz -plyz.123 -h192.168.209.140 -P8066
D:\>mysql -ulyz -plyz.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.5.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>

2、创建表测试

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

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表。

3、录入数据测试

3.1录入到dn1节点

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

explain insert into travelrecord (id,user_id,traveldate,fee,days) values(100001,'liuyazhuang','2017-08-07',510.5,3);
结果如下:

mysql> explain insert into travelrecord (id,user_id,traveldate,fee,days) values(100001,'liuyazhuang','2017-08-07',510.5,3);
+-----------+-------------------------------------------------------------------------------------------------------------+
| DATA_NODE | SQL                                                                                                         |
+-----------+-------------------------------------------------------------------------------------------------------------+
| dn1       | insert into travelrecord (id,user_id,traveldate,fee,days) values(100001,'liuyazhuang','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,'liuyazhuang','2017-08-07',510.5,3);
Query OK, 1 row affected, 1 warning (0.01 sec)

mysql>

3.2录入到dn2节点

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

explain insert into travelrecord (id,user_id,traveldate,fee,days) values(8000004,'liuyazhuang','2017-08-07',510.5,3);
结果如下:

mysql> explain insert into travelrecord (id,user_id,traveldate,fee,days) values(8000004,'liuyazhuang','2017-08-07',510.5,3);
+-----------+--------------------------------------------------------------------------------------------------------------+
| DATA_NODE | SQL                                                                                                          |
+-----------+--------------------------------------------------------------------------------------------------------------+
| dn2       | insert into travelrecord (id,user_id,traveldate,fee,days) values(8000004,'liuyazhuang','2017-08-07',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,'liuyazhuang','2017-08-07',510.5,3);
Query OK, 1 row affected, 1 warning (0.06 sec)
3.3路由到dn3节点

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

explain insert into travelrecord (id,user_id,traveldate,fee,days) values(10000004,'liuyazhuang','2017-08-07',510.5,3);
结果为:

mysql> explain insert into travelrecord (id,user_id,traveldate,fee,days) values(10000004,'liuyazhuang','2017-08-07',510.5,3);
+-----------+---------------------------------------------------------------------------------------------------------------+
| DATA_NODE | SQL                                                                                                           |
+-----------+---------------------------------------------------------------------------------------------------------------+
| dn3       | insert into travelrecord (id,user_id,traveldate,fee,days) values(10000004,'liuyazhuang','2017-08-07',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,'liuyazhuang','2017-08-07',510.5,3);
Query OK, 1 row affected, 1 warning (0.01 sec)

4、查询测试

4.1查询所有数据

在命令行执行如下语句:

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节点上。

4.2根据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路由到指定的数据分片。

5、删表测试

在命令行输入如下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的路由结果可能会有不同。


阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页