mycat分表扩展之范围约定

mycat分表扩展之范围约定
1.修改schema.xml配置文件

<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
	<schema name="orders1" checkSQLschema="true" sqlMaxLimit="100" dataNode="dn1">
		<table name="customer" dataNode="dn2" ></table>
		<table name="orders" dataNode="dn1,dn2" rule="mod_rule" autoIncrement="true" fetchStoreNodeByJdbc="true">
			<!--orders_detail与orders表根据order_id关联-->
			<childTable name="orders_detail" primaryKey="id" joinKey="order_id" parentKey="id" />
		</table>
		<!--定义全局表-->
	   <table name="dict_order_type" dataNode="dn1,dn2" type="global" ></table>
	   <!--分片枚举-->
	   <table name="orders_ware_info" dataNode="dn1,dn2" rule="sharding_by_intfile" ></table>
	   <!--范围约定-->
	   <table name="payment_info" dataNode="dn1,dn2" rule="auto_sharding_long" ></table>
	</schema>

	<dataNode name="dn1" dataHost="host1" database="orders1" />
	<dataNode name="dn2" dataHost="host2" database="orders1" />
	
	<dataHost name="host1" maxCon="1000" minCon="10" balance="0"
			  writeType="0" dbType="mysql" dbDriver="jdbc" switchType="1"  slaveThreshold="100">
		<heartbeat>select user()</heartbeat>
		<writeHost host="hostM1" url="jdbc:mysql://192.168.2.130:3306" user="root"
				   password="kht123">
		</writeHost>
	</dataHost>
	
	<dataHost name="host2" maxCon="1000" minCon="10" balance="0"
			  writeType="0" dbType="mysql" dbDriver="jdbc" switchType="1"  slaveThreshold="100">
		<heartbeat>select user()</heartbeat>
		<writeHost host="hostM2" url="jdbc:mysql://192.168.2.131:3306" user="root"
				   password="kht123">
		</writeHost>
	</dataHost>
</mycat:schema>

2.修改rule.xml配置文件

	<tableRule name="auto_sharding_long">
		<rule>
		  <columns>order_id</columns>
		  <algorithm>rang-long</algorithm>
		</rule>
	</tableRule>

	<!--
	# columns:分片字段,algorithm:分片函数
	# mapFile:标识配置文件名称,type:0为int型、非0为String,
	#defaultNode:默认节点:小于 0 表示不设置默认节点,大于等于 0 表示设置默认节点,
	#设置默认节点如果碰到不识别的枚举值,就让它路由到默认节点,如不设置不识别就报错
	-->
	<function name="rang-long"
			  class="io.mycat.route.function.AutoPartitionByLong">
		<property name="mapFile">autopartition-long.txt</property>
		<property name="defaultNode">0</property>
	</function>

3.修改autopartition-long.txt配置文件

0-102=0
103-200=1
#支付信息表
CREATE TABLE payment_info
(
`id` INT AUTO_INCREMENT comment '编号',
`order_id` INT comment '订单编号',
`payment_status` INT comment '支付状态',
PRIMARY KEY(id)
);
#插入数据
INSERT INTO payment_info (id,order_id,payment_status) VALUES (1,101,0);
INSERT INTO payment_info (id,order_id,payment_status) VALUES (2,102,1);
INSERT INTO payment_info (id,order_id ,payment_status) VALUES (3,103,0);
INSERT INTO payment_info (id,order_id,payment_status) VALUES (4,104,1);

4.查询Mycat、dn1、dn2可以看到数据分片效果

posted @ 2021-09-09 09:24  kht  阅读(70)  评论(0编辑  收藏  举报