Snowflake ID有64bits長,由如下三部分組成:html
time—42bits,精確到ms,那就意味着其能夠表示長達(2^42-1)/(1000360024*365)=139.5年,另外使用者能夠本身定義一個開始紀元(epoch),而後用(當前時間-開始紀元)算出time,這表示在time這個部分在140年的時間裏是不會重複的,官方文檔在這裏寫成了41bits,應該是寫錯了。另外,這裏用time還有一個很重要的緣由,就是能夠直接更具time進行排序,對於twitter這種更新頻繁的應用,時間排序就顯得尤其重要了。less
machine id—10bits,該部分其實由datacenterId和workerId兩部分組成,這兩部分是在配置文件中指明的。ui
datacenterId的做用(我的見解)this
1.方便搭建多個生成uid的service,並保證uid不重複,好比在datacenter0將機器0,1,2組成了一個生成uid的service,而datacenter1此時也須要一個生成uid的service,從本中心獲取uid顯然是最快最方便的,那麼它能夠在本身中心搭建,只要保證datacenterId惟一。若是沒有datacenterId,即用10bits,那麼在搭建一個新的service前必須知道目前已經在用的id,不然不能保證生成的id惟一,好比搭建的兩個uid service中都有machine id爲100的機器,若是其server時間相同,那麼產生相同id的狀況不可避免。orm
2.加快server啓動速度。啓動一臺uid server時,會去檢查zk同workerId目錄中其餘機器的狀況,如其在zk上註冊的id和向它請求返回的work_id是否相同,是否處同一個datacenter下,另外還會檢查該server的時間與目前已有機器的平均時間偏差是否在10s範圍內等,這些檢查是會耗費必定時間的。將一個datacenter下的機器數限制在32臺(5bits)之內,在必定程度上也保證了server的啓動速度。server
workerId是實際server機器的代號,最大到32,同一個datacenter下的workerId是不能重複的。它會被註冊到zookeeper上,確保workerId未被其餘機器佔用,並將host:port值存入,註冊成功後就能夠對外提供服務了。htm
sequence id —12bits,該id能夠表示4096個數字,它是在time相同的狀況下,遞增該值直到爲0,即一個循環結束,此時便只能等到下一個ms到來,通常狀況下4096/ms的請求是不太可能出現的,因此足夠使用了。blog
Snowflake ID即是經過這三部分實現了UID的產生,策略也並不複雜。下面咱們來看看它的一些關鍵源碼排序
/**
* 核心代碼就是毫秒級時間41位+機器ID 10位+毫秒內序列12位
* */文檔
public class IdWorker {
private long workerId;
private long datacenterId;
private long sequence = 0L;
private long twepoch = 1288834974657L;
private long workerIdBits = 5L;
private long datacenterIdBits = 5L;
private long maxWorkerId = -1L ^ (-1L << workerIdBits);
private long maxDatacenterId = -1L ^ (-1L << datacenterIdBits);
private long sequenceBits = 12L;
private long workerIdShift = sequenceBits;
private long datacenterIdShift = sequenceBits + workerIdBits;
private long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
private long sequenceMask = -1L ^ (-1L << sequenceBits);
private long lastTimestamp = -1L;
public IdWorker(long workerId, long datacenterId) {
// sanity check for workerId
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) {
System.out.println("clock is moving backwards. Rejecting requests until "+lastTimestamp);
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();
}
}
原文:https://www.cnblogs.com/dayhand/p/3851248.html