一、后台配置文件
# 连接地址 url: jdbc:mysql://127.0.0.1:8066/CHUNK?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&rewriteBatchedStatements=true # 用户名 username: root # 密码 password: bluetooth@wy2019
8066:mycat默认端口
root:mycat配置文件设置、
password:mycat配置文件配置
二、mycat配置
2.1 schema.xml配置
xml version="1.0"?> DOCTYPE mycat:schema SYSTEM "schema.dtd"> <mycat:schema xmlns:mycat="http://io.mycat/"> <schema name="CHUNK" checkSQLschema="false" sqlMaxLimit="10000"> <table name="cn_chunk" primaryKey="id" autoIncrement="true" dataNode="dn01,dn02,dn03" rule="rule1"> <childTable name="cn_chunk_lockset" primaryKey="id" joinKey="chunkId" parentKey="id" />table> schema> <dataNode name="dn01" dataHost="localhost" database="chunk001" /> <dataNode name="dn02" dataHost="localhost" database="chunk002" /> <dataNode name="dn03" dataHost="localhost" database="chunk003" /> <dataHost name="localhost" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native"> <heartbeat>select user()heartbeat> <writeHost host="server1" url="127.0.0.1:3306" user="root" password="root"/> dataHost> mycat:schema>
2.2 rule.xml配置
xml version="1.0" encoding="UTF-8"?> DOCTYPE mycat:rule SYSTEM "rule.dtd"> <mycat:rule xmlns:mycat="http://io.mycat/"> <tableRule name="rule1"> <rule> <columns>idcolumns> <algorithm>mod-longalgorithm> rule> tableRule> <function name="mod-long" class="io.mycat.route.function.PartitionByMod"> <property name="count">3property> function> mycat:rule>
2.3 server.xml配置
xml version="1.0" encoding="UTF-8"?> DOCTYPE mycat:server SYSTEM "server.dtd"> <mycat:server xmlns:mycat="http://io.mycat/"> <system> <property name="useSqlStat">0property> <property name="useGlobleTableCheck">0property> <property name="sequnceHandlerType">1property> <property name="processorBufferPoolType">0property> <property name="handleDistributedTransactions">0property> <property name="useOffHeapForMerge">1property> <property name="memoryPageSize">1mproperty> <property name="spillsFileBufferSize">1kproperty> <property name="useStreamOutput">0property> <property name="systemReserveMemorySize">384mproperty> <property name="useZKSwitch">trueproperty> system> <user name="root"> <property name="password">bluetooth@wy2019property> <property name="schemas">CHUNKproperty> user> <user name="user"> <property name="password">userproperty> <property name="schemas">CHUNKproperty> <property name="readOnly">trueproperty> user> mycat:server>
2.4 sequence_db_conf.properties配置
#sequence stored in datanode
GLOBAL=dn1
COMPANY=dn1
CUSTOMER=dn1
ORDERS=dn1
#逻辑库的分片函数在对应个库下
CN_CHUNK=dn01
三、建表语句
#创建表语句区块与子表区块锁
#添加表 区块
DROP TABLE IF EXISTS `cn_chunk`;
CREATE TABLE `cn_chunk` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`createdAt` datetime DEFAULT NULL COMMENT '创建时间',
`modifiedAt` datetime DEFAULT NULL COMMENT '更新时间',
`createdBy` varchar(36) DEFAULT NULL COMMENT '创建人',
`modifiedBy` varchar(36) DEFAULT NULL COMMENT '更新人',
`version` int(11) DEFAULT NULL COMMENT '版本号',
`isDelete` int(11) DEFAULT NULL COMMENT '是否删除',
`keyInfoId` varchar(36) DEFAULT NULL COMMENT '钥匙id',
`chunkOne` int(11) DEFAULT NULL COMMENT '区块1',
`chunkTwo` int(11) DEFAULT NULL COMMENT '区块2',
`chunkThree` int(11) DEFAULT NULL COMMENT '区块3',
`chunkFour` int(11) DEFAULT NULL COMMENT '区块4',
`chunkFive` int(11) DEFAULT NULL COMMENT '区块5',
`chunkSix` int(11) DEFAULT NULL COMMENT '区块6',
`chunkSeven` int(11) DEFAULT NULL COMMENT '区块7',
`chunkEight` int(11) DEFAULT NULL COMMENT '区块8',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
#添加表 区块锁
DROP TABLE IF EXISTS `cn_chunk_lockset`;
CREATE TABLE `cn_chunk_lockset` (
`id` varchar(36) NOT NULL,
`createdAt` datetime DEFAULT NULL COMMENT '创建时间',
`modifiedAt` datetime DEFAULT NULL COMMENT '更新时间',
`createdBy` varchar(36) DEFAULT NULL COMMENT '创建人',
`modifiedBy` varchar(36) DEFAULT NULL COMMENT '更新人',
`version` int(11) DEFAULT NULL COMMENT '版本号',
`isDelete` int(11) DEFAULT NULL COMMENT '是否删除',
`taskId` varchar(36) DEFAULT NULL COMMENT '任务id',
`keyInfoId` varchar(36) DEFAULT NULL COMMENT '钥匙id',
`startTime` datetime DEFAULT NULL COMMENT '起始时间',
`endTime` datetime DEFAULT NULL COMMENT '截止时间',
`chunkLocation` int(11) DEFAULT NULL COMMENT '对应区块(1-8)',
`lockSetId` varchar(36) DEFAULT NULL COMMENT '锁具信息id',
`lockId` varchar(50) DEFAULT NULL COMMENT '锁具Id',
`lockSetName` varchar(50) DEFAULT NULL COMMENT '锁具名称',
`principalId` varchar(36) DEFAULT NULL COMMENT '执行人id',
`lockIdDecimal` int(11) DEFAULT NULL COMMENT '锁具Id十进制',
`chunkId` bigint(20) DEFAULT NULL COMMENT '区块id',
`thisUpdate` int(11) DEFAULT NULL COMMENT '本次更新(0:否,1:是)',
`sequence` int(11) DEFAULT NULL COMMENT '区块锁序列号',
PRIMARY KEY (`id`),
KEY `fk_cn_chunk_lockset_cn_chunk` (`chunkId`),
CONSTRAINT `cn_chunk_lockset_ibfk_1` FOREIGN KEY (`chunkId`) REFERENCES `cn_chunk` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
四、对应实体类
@Getter @Setter @Entity @Table(name = "cn_chunk") public class Chunk extends ChunkBaseEntity { /** 钥匙id*/ @ApiModelProperty(value = "钥匙id") private String keyInfoId; /** 区块1*/ @ApiModelProperty(value = "区块1") private Integer chunkOne = 0; /** 区块2*/ @ApiModelProperty(value = "区块2") private Integer chunkTwo = 0; /** 区块3*/ @ApiModelProperty(value = "区块3") private Integer chunkThree = 0; /** 区块4*/ @ApiModelProperty(value = "区块4") private Integer chunkFour = 0; /** 区块5*/ @ApiModelProperty(value = "区块5") private Integer chunkFive = 0; /** 区块6*/ @ApiModelProperty(value = "区块6") private Integer chunkSix = 0; /** 区块7*/ @ApiModelProperty(value = "区块7") private Integer chunkSeven = 0; /** 区块8*/ @ApiModelProperty(value = "区块8") private Integer chunkEight = 0; }
@Getter @Setter @Entity @Table(name = "cn_chunk_lockset") public class ChunkLockSet extends BaseEntity { /** 任务id*/ @ApiModelProperty(value = "任务id") @Column(length = 36) private String taskId; /** 钥匙id*/ @ApiModelProperty(value = "钥匙id") private String keyInfoId; /** 起始时间*/ @ApiModelProperty(value = "起始时间") private LocalDateTime startTime; /** 截止时间*/ @ApiModelProperty(value = "截止时间") private LocalDateTime endTime; /** 区块*/ @ApiModelProperty(value = "区块") @ManyToOne @JoinColumn(name = "chunkId", updatable = false, insertable = false) @JsonIgnore private Chunk chunk; /** 区块id*/ @ApiModelProperty(value = "区块id") @Column(name = "chunkId", updatable = false, length = 36) private Long chunkId; /** 对应区块(1-8)*/ @ApiModelProperty(value = "对应区块(1-8)") private Integer chunkLocation; /** 锁具信息id*/ @ApiModelProperty(value = "锁具信息id") @Column(name = "lockSetId", length = 36) private String lockSetId; /** 锁具Id*/ @ApiModelProperty(value = "锁具Id") @Column(length = 50) private String lockId; /** 锁具Id十进制*/ @ApiModelProperty(value = "锁具Id十进制") private Integer lockIdDecimal; /** 锁具名称*/ @ApiModelProperty(value = "锁具名称") @Column(length = 50) private String lockSetName; /** 执行人id*/ @ApiModelProperty(value = "执行人id") @Column(name = "principalId", length = 36) private String principalId; /** 是否已上传(0:否,1:是)*/ @ApiModelProperty(value = "是否已上传(0:否,1:是)") private Integer thisUpdate = BooleanEnum.FALSE.ordinal(); /** 区块锁序列号*/ @ApiModelProperty(value = "区块锁序列号") private Integer sequence; }
@Getter @Setter @MappedSuperclass @EntityListeners(AuditingEntityListener.class) @Slf4j public abstract class ChunkBaseEntity implements Serializable { /** * 主键id * GenericGenerator 注解 strategy 参数设置参考如下 * @see org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory * 参考网站:https://blog.csdn.net/mafian/article/details/53968878 */ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id", updatable = false) @ApiModelProperty(value = "主键id", position = 1) private Long id; /** 创建时间*/ @CreatedDate @ApiModelProperty(value = "创建时间", position = 2) @JsonFormat @DateTimeFormat private LocalDateTime createdAt; /** 更新时间*/ @LastModifiedDate @ApiModelProperty(value = "更新时间", position = 3) @JsonFormat @DateTimeFormat private LocalDateTime modifiedAt; /** 创建人*/ @Column(length = 36) @CreatedBy @ApiModelProperty(value = "创建人", position = 4) private String createdBy; /** 更新人*/ @Column(length = 36) @LastModifiedBy @ApiModelProperty(value = "更新人", position = 5) private String modifiedBy; /** 版本(乐观锁)*/ @Version @ApiModelProperty(value = "版本(乐观锁)", position = 6) private Integer version; /** 逻辑删除*/ @ApiModelProperty(value = "逻辑删除", position = 7) private Integer isDelete = BooleanEnum.FALSE.ordinal(); }
五、mycat分片函数
配置文件:sequence_db_conf.properties
设置节点:CN_CHUNK=dn01
该参数设置在哪个节点,则就在对应的数据库内执行
#函数自增 实现新增数据自增长以1开始 #添加表 DROP TABLE IF EXISTS MYCAT_SEQUENCE; CREATE TABLE MYCAT_SEQUENCE ( NAME VARCHAR (50) NOT NULL, current_value INT NOT NULL, increment INT NOT NULL DEFAULT 100, PRIMARY KEY (NAME) ) ENGINE = INNODB ; INSERT INTO MYCAT_SEQUENCE(name,current_value,increment) VALUES ('GLOBAL', 10000000, 100); DROP FUNCTION IF EXISTS `mycat_seq_currval`; DELIMITER ;; CREATE FUNCTION `mycat_seq_currval`(seq_name VARCHAR(50)) RETURNS varchar(64) CHARSET utf8 DETERMINISTIC BEGIN DECLARE retval VARCHAR(64); SET retval="-999999999,null"; SELECT concat(CAST(current_value AS CHAR),",",CAST(increment AS CHAR) ) INTO retval FROM MYCAT_SEQUENCE WHERE name = seq_name; RETURN retval ; END ;; DELIMITER ; DROP FUNCTION IF EXISTS `mycat_seq_nextval`; DELIMITER ;; CREATE FUNCTION `mycat_seq_nextval`(seq_name VARCHAR(50)) RETURNS varchar(64) CHARSET utf8 DETERMINISTIC BEGIN UPDATE MYCAT_SEQUENCE SET current_value = current_value + increment WHERE name = seq_name; RETURN mycat_seq_currval(seq_name); END ;; DELIMITER ; DROP FUNCTION IF EXISTS `mycat_seq_setval`; DELIMITER ;; CREATE FUNCTION `mycat_seq_setval`(seq_name VARCHAR(50), value INTEGER) RETURNS varchar(64) CHARSET utf8 DETERMINISTIC BEGIN UPDATE MYCAT_SEQUENCE SET current_value = value WHERE name = seq_name; RETURN mycat_seq_currval(seq_name); END ;; DELIMITER ; #插入一条数据,这条数据是我们这个表所需要的。 #必须是大写的字符,不然就会报错 #插入需要自增张的表的策略 #其中cn_chunk代表哪张表实现自增 insert into MYCAT_SEQUENCE (name,current_value,increment) values ('cn_chunk',0,1);
六、关闭默认事务
import org.springframework.context.annotation.Configuration; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; /** * 关闭jpamor默认事务 * @author lixin.shao */ @EnableJpaRepositories(basePackages = "com.hs.lock.bluetooth.chunk", enableDefaultTransactions = false) @Configuration public class JpaTransactionConfig { }