项目地址
https://github.com/kylin-hunter/k-commons
文章目录
前言
雪花算法的实现
一、雪花算法
雪花算法是一种分布式的id生成算法。原理是将long分成若干个区段分别管理。默认包括时间戳、数据中心ID/机房ID,woker id(机器编号),以及sequence四个部分,用户可以自由扩展第五个部分type。 同时用户也可以动态调整这5个部分的占比关系。
二、使用步骤
1 引入库
1.1 编译并发布到本地
gradle clean build publishToMavenLocal-x test
1.2 gradle (gradle.org)
implementation'io.github.kylin-hunter:k-commons:1.0.7'
1.3 maven (maven.apache.org)
<dependency>
<groupId>io.github.kylin-hunter</groupId>
<artifactId>io.github.kylin-hunter:k-commons</artifactId>
<version>1.0.7</version>
</dependency>
2 示例
2.1 主要Api:
2.1.1 构造器
/**
* @param type 业务类型
* @param workerId 机器编号
* @param datacenterId 机房编号
* @title UidGenerator 构造器
* @description
* @author BiJi'an
* @date 2022-12-11 16:19
*/
public UidGenerator(long type,long workerId,long datacenterId);
/**
* @param sequenceBits 序列号 占的bit位数
* @param typeBits 业务类型 占的bit位数
* @param workerIdBits 机器编号 占的bit位数
* @param datacenterIdBits 机房编号 占的bit位数
* @param type 业务类型
* @param workerId 机器编号
* @param datacenterId 机房编号
* @title UidGenerator 构造器
* @description
* @author BiJi'an
* @date 2022-12-11 16:20
*/
public UidGenerator(int sequenceBits,int typeBits,int workerIdBits,int datacenterIdBits, long type,long workerId, long datacenterId);
2.1.2 生成和反解uid
/**
* @return long
* @title 获取下一个uid
* @description
* @author BiJi'an
* @date 2022-12-11 00:39
*/
public synchronized long nextId();
/**
* @param uid uid
* @return io.github.kylinhunter.commons.uid.UidInfo
* @title 通过uid 反解出uid的信息
* @description
* @author BiJi'an
* @date 2022-12-11 16:30
*/
public UidInfo parse(long uid);
2.2代码示例:
// 业务代码 3,默认业务代码支持范围 0-15,可以通过构造器2调整支持范围
// 机器编码 4,默认机器编码支持范围0-15,可以通过构造器2调整支持范围
// 机房编码 1,默认机房支持范围 0-4,可以通过构造器2调整支持范围
// 各个编码范围,均可以调整
UidGenerator worker = new UidGenerator(3, 4, 1);
for (int i = 0; i < 10; i++) {
long nextId = worker.nextId();
System.out.println(nextId + "=>" + worker.parse(nextId));
}
2.3 结果输出:
2022-12-11 22:54:26.177 [Test worker] INFO - i.g.k.commons.uid.UidGenerator[107]: timestampBits 41,datacenterIdBits 2, workerIdBits 4,typeBits 4, sequenceBits 12
161155503589961728=>UidInfo[sequence=0, type=3, workerId=4, datacenterId=1, timestamp=1670921666180/2022-12-13 16:54:26]
161155503644487680=>UidInfo[sequence=0, type=3, workerId=4, datacenterId=1, timestamp=1670921666193/2022-12-13 16:54:26]
161155503648681984=>UidInfo[sequence=0, type=3, workerId=4, datacenterId=1, timestamp=1670921666194/2022-12-13 16:54:26]
161155503648681985=>UidInfo[sequence=1, type=3, workerId=4, datacenterId=1, timestamp=1670921666194/2022-12-13 16:54:26]
161155503648681986=>UidInfo[sequence=2, type=3, workerId=4, datacenterId=1, timestamp=1670921666194/2022-12-13 16:54:26]
161155503648681987=>UidInfo[sequence=3, type=3, workerId=4, datacenterId=1, timestamp=1670921666194/2022-12-13 16:54:26]
161155503648681988=>UidInfo[sequence=4, type=3, workerId=4, datacenterId=1, timestamp=1670921666194/2022-12-13 16:54:26]
161155503648681989=>UidInfo[sequence=5, type=3, workerId=4, datacenterId=1, timestamp=1670921666194/2022-12-13 16:54:26]
161155503648681990=>UidInfo[sequence=6, type=3, workerId=4, datacenterId=1, timestamp=1670921666194/2022-12-13 16:54:26]
161155503648681991=>UidInfo[sequence=7, type=3, workerId=4, datacenterId=1, timestamp=1670921666194/2022-12-13 16:54:26]