|
|
|
@ -32,6 +32,26 @@ public class SnowflakeIdMaker {
|
|
|
|
|
private long datacenterId;
|
|
|
|
|
private long sequence = 0L;
|
|
|
|
|
|
|
|
|
|
private volatile static SnowflakeIdMaker snowflakeIdMaker = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 单列模式实例化对象
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static SnowflakeIdMaker getSingleton(){
|
|
|
|
|
if (snowflakeIdMaker == null){
|
|
|
|
|
synchronized (SnowflakeIdMaker.class){
|
|
|
|
|
if (snowflakeIdMaker == null) {
|
|
|
|
|
snowflakeIdMaker = new SnowflakeIdMaker();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return snowflakeIdMaker;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 禁止私自构建,防止ID重复
|
|
|
|
|
*/
|
|
|
|
@ -42,6 +62,8 @@ public class SnowflakeIdMaker {
|
|
|
|
|
this.sequence = 0L;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 禁止私自构建 防止 ID 生成重复
|
|
|
|
|
* @param workerId 工作区ID
|
|
|
|
@ -104,12 +126,12 @@ public class SnowflakeIdMaker {
|
|
|
|
|
|
|
|
|
|
private long lastTimestamp = -1L;
|
|
|
|
|
|
|
|
|
|
private long getWorkerId(){
|
|
|
|
|
return workerId;
|
|
|
|
|
public long getWorkerId(){
|
|
|
|
|
return this.workerId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private long getDatacenterId(){
|
|
|
|
|
return datacenterId;
|
|
|
|
|
public long getDatacenterId(){
|
|
|
|
|
return this.datacenterId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public synchronized long nextId() {
|
|
|
|
|