sequence基于雪花算法的增强版ID生成器
- 解决了时间回拨的问题
- 无需手动指定workId, 微服务环境自适应
- 可配置化
- 依赖引入
<dependency><groupId>io.github.mocreates</groupId><artifactId>uid-generator</artifactId><version>2.0-RELEASE</version></dependency>
- 配置序列器
Sequence
@Beanpublic Sequence sequence() {SequenceConfig sequenceConfig = new SimpleSequenceConfig();return new Sequence(sequenceConfig);}
- 使用序列器生成ID
@Autowiredprivate Sequence sequence;public long generateId() {return sequence.nextId();}
配置解析目前提供两个配置类io.github.mocreates.config.DefaultSequenceConfig
io.github.mocreates.config.SimpleSequenceConfig
详情字段名释义默认值twepoch可以被设置为最接近项目启用前的某个时间点(unix 时间戳)1665817757000LworkerIdBits机器位所占的bit位数19LdatacenterIdBits数据标识位所占的bit位数0LsequenceBits毫秒内自增位数3LworkerId机器位datacenterId数据位0LinetAddress网络相关信息生产推荐使用方式
- 依赖引入
<dependency><groupId>io.github.mocreates</groupId><artifactId>uid-generator</artifactId><version>2.0-RELEASE</version></dependency>
- 创建表
CREATE TABLE `worker_node` (`id` bigint(20) NOT NULL AUTO_INCREMENT,`node_info` varchar(512) NOT NULL,`gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,`gmt_modify` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='DB WorkerID Assigner for UID Generator';
- 配置 (利用主键自增来分配workerId, 解决分布式环境下手动指定workerId的痛点)
@Beanpublic Sequence sequence(WorkerNodeMapper workerNodeMapper) throws UnknownHostException {WorkerNode workerNode = new WorkerNode();InetAddress localHost = InetAddress.getLocalHost();workerNode.setNodeInfo(localHost.toString());workerNodeMapper.insertSelective(workerNode);DefaultSequenceConfig defaultSequenceConfig = new DefaultSequenceConfig();defaultSequenceConfig.setWorkerId(workerNode.getId());return new Sequence(defaultSequenceConfig);}
- 使用序列器生成ID
@Autowiredprivate Sequence sequence;public long generateId() {return sequence.nextId();}
JMH 性能测试测试机硬件情况MacBook Pro (13-inch, M1, 2020)8C 16GSequence 配置参数
private static final DefaultSequenceConfig SEQUENCE_CONFIG = new DefaultSequenceConfig();static {SEQUENCE_CONFIG.setSequenceBits(22);SEQUENCE_CONFIG.setWorkerIdBits(0);SEQUENCE_CONFIG.setDatacenterIdBits(0);SEQUENCE_CONFIG.setTwepoch(System.currentTimeMillis());SEQUENCE_CONFIG.setWorkerId(0L);SEQUENCE_CONFIG.setDatacenterId(0L);}private static final Sequence SEQUENCE = new Sequence(SEQUENCE_CONFIG);
JMH参数@BenchmarkMode(Mode.Throughput)@Threads(10)@Warmup(iterations = 3, time = 10, timeUnit = TimeUnit.SECONDS)@Measurement(iterations = 10, time = 10, timeUnit = TimeUnit.SECONDS)@State(value = https://www.huyubaike.com/biancheng/Scope.Benchmark)@Fork(1)@OutputTimeUnit(TimeUnit.SECONDS)
测试结果BenchmarkModeCntScoreErrorUnitsSingleNodeSequenceTest.nextIdTestthrpt1027825573.565 ± 962298.054ops/sTip如果对qps性能要求较高,可以适当调整sequenceBits仓库地址https://github.com/mocreates/sequence
经验总结扩展阅读
- 基于QT和C++实现的翻金币游戏
- 【算法训练营day4】LeetCode24. 两两交换链表中的结点
- Mysql单表访问方法,索引合并,多表连接原理,基于规则的优化,子查询优化
- Linux Block模块之deadline调度算法代码解析
- 【翻译】Raft 共识算法:集群成员变更
- 基于tauri打造的HTTP API客户端工具-CyberAPI
- 基于纯前端类Excel表格控件实现在线损益表应用
- 知识图谱实体对齐2:基于GNN嵌入的方法
- 15 基于SqlSugar的开发框架循序渐进介绍-- 整合代码生成工具进行前端界面的生成
- 【算法训练营day1】LeetCode704. 二分查找 LeetCode27. 移除元素