雪花算法工具类
下面是雪花算法的工具类代码 。
public class SnowflakeIdWorkerUtils {
private static volatile SnowflakeIdWorkerUtils instance; // 单例实例
private long datacenterId; // 数据中心ID
private long workerId; // 工作机器ID
private long sequence = 0L; // 序列号
private long twepoch = 1288834974657L; // 初始时间戳
private long datacenterIdBits = 5L; // 数据中心ID所占的位数
private long workerIdBits = 5L; // 工作机器ID所占的位数
private long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); // 数据中心ID最大值
private long maxWorkerId = -1L ^ (-1L << workerIdBits); // 工作机器ID最大值
private long sequenceBits = 12L; // 序列号在ID中占的位数
private long workerIdShift = sequenceBits; // 工作ID偏移量
private long datacenterIdShift = sequenceBits + workerIdBits; // 数据中心ID偏移量
private long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; // 时间戳的偏移量
private long sequenceMask = -1L ^ (-1L << sequenceBits); // 生成序列的掩码
private long lastTimestamp = -1L; // 上一次生成ID的时间截
// 双重检查锁实现单例
public static SnowflakeIdWorkerUtils getInstance(long workerId, long datacenterId) {
if (instance == null) {
synchronized (SnowflakeIdWorkerUtils.class) {
if (instance == null) {
instance = new SnowflakeIdWorkerUtils(workerId, datacenterId);
}
}
}
return instance;
}
public SnowflakeIdWorkerUtils(long workerId, long datacenterId) {
if (workerId > maxWorkerId || workerId < 0) {
throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId));
}
if (datacenterId > maxDatacenterId || datacenterId < 0) {
throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId));
}
this.workerId = workerId;
this.datacenterId = datacenterId;
}
public synchronized long nextId() {
long timestamp = timeGen();
if (timestamp < lastTimestamp)
// 简单的时间回拨处理逻辑:如果时间回拨不超过5毫秒,等待后重试
if (lastTimestamp - timestamp < 5) {
try {
Thread.sleep(lastTimestamp - timestamp);
timestamp = timeGen();
if (timestamp < lastTimestamp) {
throw new RuntimeException("Clock moved backwards. Refusing to generate id");
}
} catch (InterruptedException e) {
throw new RuntimeException("Sleep interrupted", e);
}
} else {
throw new RuntimeException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
}
if (lastTimestamp == timestamp) {
sequence = (sequence + 1) & sequenceMask;
if (sequence == 0) {
timestamp = tilNextMillis(lastTimestamp);
}
} else {
sequence = 0L;
}
lastTimestamp = timestamp;
return ((timestamp - twepoch) << timestampLeftShift) | (datacenterId << datacenterIdShift) | (workerId << workerIdShift) | sequence;
}
protected long tilNextMillis(long lastTimestamp) {
long timestamp = timeGen();
while (timestamp <= lastTimestamp) {
timestamp = timeGen();
}
return timestamp;
}
protected long timeGen() {
return System.currentTimeMillis();
}
}
代码使用
要使用这个SnowflakeIdWorkerUtils类生成全局唯一的标识符ID,首先需要确保系统能够为不同的实例或者部署提供不同的workerId和datacenterId参数。这里提供一个简单的使用示例:
public class IdGenerationTest {
public static void main(String[] args) {
// 实例化SnowflakeIdWorkerUtils,例如使用数据中心ID为1,工作机器ID为1
SnowflakeIdWorkerUtils idWorker = SnowflakeIdWorkerUtils.getInstance(1, 1);
// 生成一个ID
long id = idWorker.nextId();
System.out.println("Generated ID: " + id);
}
}
此示例展示了如何创建SnowflakeIdWorkerUtils的实例并生成一个全局唯一的ID。这个简单的演示假设你已经有了一个基于雪花算法(Snowflake Algorithm)的ID生成器实现。
代码的考虑点
全局唯一性:通过结合数据中心ID和工作机器ID,加上时间戳和序列号,确保即使在分布式系统中也能生成全局唯一的ID。
高性能:使用简单的数学运算,生成ID的过程非常快,适合高并发的场景。
时间有序:生成的ID是基于时间戳的,这意味着生成的ID大体上是时间有序的。
单例模式:通过双重检查锁的单例模式,保证了在多线程环境下安全创建实例,同时减少资源消耗。
时间回拨处理:对系统时间回拨有基本的处理,虽然是简单的重试机制,但提供了处理此类问题的思路。