|
|
|
@ -2,15 +2,10 @@ package com.mashibing.common.util;
|
|
|
|
|
|
|
|
|
|
import com.mashibing.common.enums.ExceptionEnums;
|
|
|
|
|
import com.mashibing.common.exception.ApiException;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
|
|
import java.time.Instant;
|
|
|
|
|
import java.time.LocalDate;
|
|
|
|
|
import java.time.ZoneId;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 雪花算法生成全局唯一ID
|
|
|
|
@ -24,7 +19,6 @@ import java.util.Date;
|
|
|
|
|
* @description
|
|
|
|
|
*/
|
|
|
|
|
@Component
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class SnowFlakeUtil {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -98,16 +92,64 @@ public class SnowFlakeUtil {
|
|
|
|
|
/**
|
|
|
|
|
* 时间戳需要位移的位数
|
|
|
|
|
*/
|
|
|
|
|
private long timestampShift = sequenceBits + serviceIdBits + maxMachineId;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private long timestampShift = sequenceBits + serviceIdBits + machineIdBits;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 序列的最大值
|
|
|
|
|
*/
|
|
|
|
|
private long maxSequenceId = -1 ^ (-1 << sequenceBits);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 记录最近一次获取id的时间
|
|
|
|
|
*/
|
|
|
|
|
private long lastTimestamp = -1;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取系统时间毫秒值
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private long timeGen(){
|
|
|
|
|
return System.currentTimeMillis();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public synchronized long nextId(){
|
|
|
|
|
//1、 拿到当前系统时间的毫秒值
|
|
|
|
|
long timestamp = timeGen();
|
|
|
|
|
// 避免时间回拨造成出现重复的id
|
|
|
|
|
if(timestamp < lastTimestamp){
|
|
|
|
|
// 说明出现了时间回拨
|
|
|
|
|
System.out.println("当前服务出现时间回拨!!!");
|
|
|
|
|
throw new ApiException(ExceptionEnums.SNOWFLAKE_TIME_BACK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//2、 判断当前生成id的时间和上一次生成的时间
|
|
|
|
|
if(timestamp == lastTimestamp){
|
|
|
|
|
// 同一毫秒值生成id
|
|
|
|
|
sequence = (sequence + 1) & maxSequenceId;
|
|
|
|
|
// 0000 10100000 :sequence
|
|
|
|
|
// 1111 11111111 :maxSequenceId
|
|
|
|
|
if(sequence == 0){
|
|
|
|
|
// 进到这个if,说明已经超出了sequence序列的最大取值范围
|
|
|
|
|
// 需要等到下一个毫秒再做回来生成具体的值
|
|
|
|
|
timestamp = timeGen();
|
|
|
|
|
while(timestamp <= lastTimestamp){
|
|
|
|
|
// 时间还没动。
|
|
|
|
|
timestamp = timeGen();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
// 另一个时间点生成id
|
|
|
|
|
sequence = 0;
|
|
|
|
|
}
|
|
|
|
|
//3、重新给lastTimestamp复制
|
|
|
|
|
lastTimestamp = timestamp;
|
|
|
|
|
|
|
|
|
|
//4、计算id,将几位值拼接起来。 41bit位的时间,5位的机器,5位的服务 ,12位的序列
|
|
|
|
|
return ((timestamp - timeStart) << timestampShift) |
|
|
|
|
|
(machineId << machineIdShift) |
|
|
|
|
|
(serviceId << serviceIdShift) |
|
|
|
|
|
sequence &
|
|
|
|
|
Long.MAX_VALUE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|