Java实战:分布式ID生成方案
import java.util.concurrent.atomic.AtomicLong;
public class DistributedIdWorker {
private final long workerId;
private final static AtomicLong lastTimestamp = new AtomicLong(0L);
public DistributedIdWorker(long workerId) {
if (workerId > maxWorkerId || workerId < 0) {
throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId));
}
this.workerId = workerId;
}
public synchronized long nextId() {
long timestamp = timeGen();
if (timestamp < lastTimestamp.get()) {
throw new RuntimeException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp.get() - timestamp));
}
if (lastTimestamp.compareAndSet(lastTimestamp.get(), timestamp)) {
return ((timestamp - twepoch) << timestampLeftShift) |
(workerId << workerIdShift) |
(sequence % sequenceMask);
} else {
return nextId();
}
}
protected long timeGen() {
return System.currentTimeMillis();
}
// Default workerId is based on the machine's MAC address
public static long getDefaultWorkerId() {
// TODO: Implement a way to get machine's MAC address and convert it to a workerId
return 0L; // Placeholder
}
private final static long twepoch = 1288834974657L;
private final static long workerIdBits = 5L;
private final static long datacenterIdBits = 5L;
// ... other constants definition omitted for brevity
private long workerId;
// ... other member variables and methods omitted for brevity
}
在这个简化的代码示例中,我们假设了一个获取默认workerId的方法getDefaultWorkerId()
,但是实际上需要实现根据机器的MAC地址来生成workerId的逻辑。同时,示例中的nextId
方法也被简化了,去除了对数据中心ID的支持,并且使用了更简单的逻辑来生成ID。这个简化的版本主要用于教学目的,展示了分布式ID生成的核心思想,但在实际应用中需要完善和测试。
评论已关闭