水平分片Demo - xinwu-yang/cube-java GitHub Wiki

功能描述

使用本地时间戳主键,水平拆分系统的业务为,db1和db2按时间分别存放用户。

表结构

db1:

DROP TABLE IF EXISTS `mc`;
CREATE TABLE `mc` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `sharding_id` int(5) NOT NULL,
  `create_time` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4;

db2:

DROP TABLE IF EXISTS `mc`;
CREATE TABLE `mc` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `sharding_id` int(5) NOT NULL,
  `create_time` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4;

schema.xml

<schema name="TESTDB" checkSQLschema="true" sqlMaxLimit="100" dataNode="dn2">
    <table name="mc" primaryKey="id" dataNode="dn1,dn2" rule="sharding-by-intfile" autoIncrement="true" />
</schema>
<dataNode name="dn1" dataHost="25.30.10.224" database="test" />
<dataNode name="dn2" dataHost="25.30.10.224" database="test1"/>
<dataHost name="25.30.10.224" maxCon="1000" minCon="5" balance="0" writeType="0" dbType="mysql" dbDriver="jdbc" switchType="1" slaveThreshold="100">
    <heartbeat>select user()</heartbeat>
    <writeHost host="hostM1" url="jdbc:mysql://25.30.10.224:3306?characterEncoding=UTF-8" user="root" password="chengxun" />
</dataHost>

rule.xml

<tableRule name="sharding-by-month">
    <rule>
        <columns>sharding_id</columns>
        <algorithm>hash-int</algorithm>
    </rule>
</tableRule>
<function name="hash-int" class="io.mycat.route.function.PartitionByFileMap">
    <property name="mapFile">partition-hash-int.txt</property>
</function>

partition-hash-int.txt

0=0 #sharding_id=0的指向0节点
1=1 #sharding_id=1的指向1节点
⚠️ **GitHub.com Fallback** ⚠️