1,导入工具类
package com.sport.finetrackserver.common.utils;
import cn.hutool.core.convert.Convert;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;
public class SnowFlakeUtil {
private final long twepoch = 1611043703161L;
private final long workerIdBits = 5L;
private final long dataCenterIdBits = 5L;
private final long maxWorkerId = -1L ^ (-1L << workerIdBits);
private final long maxDataCenterId = -1L ^ (-1L << dataCenterIdBits);
private final long sequenceBits = 12L;
private final long workerIdShift = sequenceBits;
private final long dataCenterIdShift = sequenceBits + workerIdBits;
private final long timestampLeftShift = sequenceBits + workerIdBits + dataCenterIdBits;
private final long sequenceMask = -1L ^ (-1L << sequenceBits);
private long workerId;
private long dataCenterId;
private long standbyDatacenterId;
private long sequence = 0L;
private long lastTimestamp = -1L;
private boolean isTimestampBack = false;
private static final long MAX_BACKWARD_MS = 3;
SnowFlakeUtil(long workerId, long datacenterId, long standbyDatacenterId) {
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));
}
if (standbyDatacenterId > maxDataCenterId || standbyDatacenterId < 0) {
throw new IllegalArgumentException(String.format("standby datacenter Id can't be greater than %d or less than 0", maxDataCenterId));
}
if (datacenterId == standbyDatacenterId) {
throw new IllegalArgumentException("datacenter Id can't equal to standby datacenter Id.");
}
this.workerId = workerId;
this.dataCenterId = datacenterId;
this.standbyDatacenterId = standbyDatacenterId;
}
synchronized long nextId() {
long timestamp = timeGen();
if (timestamp < lastTimestamp) {
long offset = lastTimestamp - timestamp;
if (offset <= MAX_BACKWARD_MS) {
try {
LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(offset));
timestamp = timeGen();
if (timestamp < lastTimestamp) {
isTimestampBack = true;
} else {
isTimestampBack = false;
}
} catch (Exception e) {
}
}
}
if (lastTimestamp == timestamp) {
sequence = (sequence + 1) & sequenceMask;
if (sequence == 0) {
timestamp = tilNextMillis(lastTimestamp);
}
}
else {
sequence = 0L;
}
lastTimestamp = timestamp;
long datacenterIdToUse = isTimestampBack ? standbyDatacenterId : dataCenterId;
return ((timestamp - twepoch) << timestampLeftShift)
| (datacenterIdToUse << dataCenterIdShift)
| (workerId << workerIdShift)
| sequence;
}
private long tilNextMillis(long lastTimestamp) {
long timestamp = timeGen();
while (timestamp <= lastTimestamp) {
timestamp = timeGen();
}
return timestamp;
}
protected long timeGen() {
return System.currentTimeMillis();
}
private volatile static SnowFlakeUtil snowflakeIdWorker;
public static SnowFlakeUtil getInstance() {
if (snowflakeIdWorker == null) {
synchronized (SnowFlakeUtil.class) {
if (snowflakeIdWorker == null) {
snowflakeIdWorker = new SnowFlakeUtil(1, 0,11);
}
}
}
return snowflakeIdWorker;
}
public static String getNextId() {
return Convert.toStr(getInstance().nextId());
}
}
2,使用:
在需要的地方直接使用即可:
