Java?通過手寫分布式雪花SnowFlake生成ID方法詳解
SnowFlake算法
SnowFlake算法生成id的結(jié)果是一個64bit大小的整數(shù),它的結(jié)構(gòu)如下圖:
分為四段:
第一段: 1位為未使用,永遠固定為0。
(因為二進制中最高位是符號位,1表示負數(shù),0表示正數(shù)。生成的id一般都是用正整數(shù),所以最高位固定為0 )
第二段: 41位為毫秒級時間(41位的長度可以使用69年)
第三段: 10位為workerId(10位的長度最多支持部署1024個節(jié)點)
(這里的10位又分為兩部分,第一部分5位表示數(shù)據(jù)中心ID(0-31)第二部分5位表示機器ID(0-31))
第四段: 12位為毫秒內(nèi)的計數(shù)(12位的計數(shù)順序號支持每個節(jié)點每毫秒產(chǎn)生4096個ID序號)
代碼實現(xiàn):
import java.util.HashSet; import java.util.concurrent.atomic.AtomicLong; public class SnowFlake { //時間 41位 private static long lastTime = System.currentTimeMillis(); //數(shù)據(jù)中心ID 5位(默認0-31) private long datacenterId = 0; private long datacenterIdShift = 5; //機房機器ID 5位(默認0-31) private long workerId = 0; private long workerIdShift = 5; //隨機數(shù) 12位(默認0~4095) private AtomicLong random = new AtomicLong(); private long randomShift = 12; //隨機數(shù)的最大值 private long maxRandom = (long) Math.pow(2, randomShift); public SnowFlake() { } public SnowFlake(long workerIdShift, long datacenterIdShift){ if (workerIdShift < 0 || datacenterIdShift < 0 || workerIdShift + datacenterIdShift > 22) { throw new IllegalArgumentException("參數(shù)不匹配"); } this.workerIdShift = workerIdShift; this.datacenterIdShift = datacenterIdShift; this.randomShift = 22 - datacenterIdShift - workerIdShift; this.maxRandom = (long) Math.pow(2, randomShift); } //獲取雪花的ID private long getId() { return lastTime << (workerIdShift + datacenterIdShift + randomShift) | workerId << (datacenterIdShift + randomShift) | datacenterId << randomShift | random.get(); } //生成一個新的ID public synchronized long nextId() { long now = System.currentTimeMillis(); //如果當前時間和上一次時間不在同一毫秒內(nèi),直接返回 if (now > lastTime) { lastTime = now; random.set(0); return getId(); } //將最后的隨機數(shù),進行+1操作 if (random.incrementAndGet() < maxRandom) { return getId(); } //自選等待下一毫秒 while (now <= lastTime) { now = System.currentTimeMillis(); } lastTime = now; random.set(0); return getId(); } //測試 public static void main(String[] args) { SnowFlake snowFlake = new SnowFlake(); HashSet<Long> set = new HashSet<>(); for (int i = 0; i < 10000; i++) { set.add(snowFlake.nextId()); } System.out.println(set.size()); } }
代碼中獲取id的方法利用位運算實現(xiàn)
1 | 41 | 5 | 5 | 12
0|0001100 10100010 10111110 10001001 01011100 00|00000|0 0000|0000 00000000 //41位的時間
0|000000?0 00000000 00000000 00000000 00000000 00|10001|0 0000|0000 00000000 //5位的數(shù)據(jù)中心ID
0|0000000 00000000 00000000 00000000 00000000 00|00000|1 1001|0000 00000000 //5為的機器ID
or 0|0000000 00000000 00000000 00000000 00000000 00|00000|0 0000|?0000 00000000? //12位的sequence
------------------------------------------------------------------------------------------
0|0001100 10100010 10111110 10001001 01011100 00|10001|1 1001|?0000 00000000? //結(jié)果:910499571847892992
SnowFlake優(yōu)點
所有生成的id按時間趨勢遞增
整個分布式系統(tǒng)內(nèi)不會產(chǎn)生重復(fù)id(因為有datacenterId和workerId來做區(qū)分)
SnowFlake不足
由于SnowFlake強依賴時間戳,所以時間的變動會造成SnowFlake的算法產(chǎn)生錯誤。
到此這篇關(guān)于Java 通過手寫分布式雪花SnowFlake生成ID方法詳解的文章就介紹到這了,更多相關(guān)Java SnowFlake內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring?Security如何實現(xiàn)升級密碼加密方式詳解
這篇文章主要為大家介紹了Spring?Security實現(xiàn)升級密碼加密方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01Spring @Profile注解實現(xiàn)多環(huán)境配置
這篇文章主要介紹了Spring @Profile注解實現(xiàn)多環(huán)境配置,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04java日期時間格式化@JsonFormat與@DateTimeFormat的使用
本文主要介紹了java日期時間格式化@JsonFormat與@DateTimeFormat的使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08