Python 官方文档:入门教程 => 点击学习
目录基于雪花算法的增强版ID生成器快速开始配置解析目前提供两个配置类详情生产推荐使用方式JMH 性能测试测试机硬件情况Sequence 配置参数JMH参数测试结果Tip基于雪花算法的
1.依赖引入
<dependency>
<groupId>io.GitHub.mocreates</groupId>
<artifactId>uid-generator</artifactId>
<version>2.0-RELEASE</version>
</dependency>
2.配置序列器 Sequence
@Bean
public Sequence sequence() {
SequenceConfig sequenceConfig = new SimpleSequenceConfig();
return new Sequence(sequenceConfig);
}
3.使用序列器生成ID
@Autowired
private Sequence sequence;
public long generateId() {
return sequence.nextId();
}
io.github.mocreates.config.DefaultSequenceConfig
io.github.mocreates.config.SimpleSequenceConfig
前者需要显式地指定 workerId、datacenterId,可以结合数据库来使用,后者是利用网卡信息进行自适应
字段名 | 释义 | 默认值 |
---|---|---|
twepoch | 可以被设置为最接近项目启用前的某个时间点(unix 时间戳) | 1665817757000L |
workerIdBits | 机器位所占的bit位数 | 19L |
datacenterIdBits | 数据标识位所占的bit位数 | 0L |
sequenceBits | 毫秒内自增位数 | 3L |
workerId | 机器位 | |
datacenterId | 数据位 | 0L |
inetAddress | 网络相关信息 |
1.依赖引入
<dependency>
<groupId>io.github.mocreates</groupId>
<artifactId>uid-generator</artifactId>
<version>2.0-RELEASE</version>
</dependency>
2.创建表
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';
3.配置 (利用主键自增来分配workerId, 解决分布式环境下手动指定workerId的痛点)
@Bean
public 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);
}
4.使用序列器生成ID
@Autowired
private Sequence sequence;
public long generateId() {
return sequence.nextId();
}
MacBook Pro (13-inch, M1, 2020) 8C 16G
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);
@BenchmarkMode(Mode.Throughput)
@Threads(10)
@Warmup(iterations = 3, time = 10, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 10, time = 10, timeUnit = TimeUnit.SECONDS)
@State(value = Scope.Benchmark)
@Fork(1)
@OutputTimeUnit(TimeUnit.SECONDS)
Benchmark | Mode | Cnt | Score | Error | Units |
---|---|---|---|---|---|
SingleNodeSequenceTest.nextIdTest | thrpt | 10 | 27825573.565 ± 962298.054 | ops/s |
如果对qps性能要求较高,可以适当调整sequenceBits
仓库地址
https://github.com/mocreates/sequence
到此这篇关于基于雪花算法实现增强版ID生成器详解的文章就介绍到这了,更多相关雪花算法实现ID生成器内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: 基于雪花算法实现增强版ID生成器详解
本文链接: https://lsjlt.com/news/169527.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0