參考的版本是:tddl-sequence-3.2.jar
,java
@Bean(name = "errorCenterIdSequence", initMethod = "init") public GroupSequence errorCenterIdSequence(SequenceDao sequenceDao) { GroupSequence sequence = new GroupSequence(); sequence.setSequenceDao(sequenceDao); sequence.setName("seq_decision_exec_error_center_id"); groupSequenceMap.putIfAbsent("seq_decision_exec_error_center_id", sequence); return sequence; } @Bean(initMethod = "init") public GroupSequenceDao idSequenceDao() { GroupSequenceDao idSequenceDao = new GroupSequenceDao(); idSequenceDao.setAppName(appname); idSequenceDao.setDbGroupKeys(Lists.newArrayList(seqGroup)); /* * 默認是sequence name value gmt_modified * idSequenceDao.setDscount(2); * idSequenceDao.setInnerStep(1000); * idSequenceDao.setRetryTimes(2); * idSequenceDao.setTableName("sequence"); * idSequenceDao.setNameColumnName("name"); * idSequenceDao.setValueColumnName("value"); * idSequenceDao.setGmtModifiedColumnName("gmt_modified"); */ idSequenceDao.setAdjust(true); return idSequenceDao; }
private boolean check(int index, long value) { return (value % outStep) == (index * innerStep); // 這裏不相等,就意味着outStep有調整過 } // 具體調整 newValue = (newValue - newValue % outStep) + outStep + index * innerStep; // newValue - newValue % outStep 就是把數據縮減到最近一個能夠整除outStep的值,而後再加上一個outStep。
數據庫表:sql
CREATE TABLE `sequence` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `gmt_created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '建立時間', `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改時間', `name` varchar(64) DEFAULT NULL, `value` bigint(20) unsigned DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `uk_sequence_name` (`name`) ) ENGINE=InnoDB AUTO_INCREMENT=100016 DEFAULT CHARSET=utf8 COMMENT='sequence生成器';