Java?通過手寫分布式雪花SnowFlake生成ID方法詳解
SnowFlake算法
SnowFlake算法生成id的結(jié)果是一個64bit大小的整數(shù),它的結(jié)構(gòu)如下圖:
分為四段:
第一段: 1位為未使用,永遠(yuǎn)固定為0。
(因?yàn)槎M(jìn)制中最高位是符號位,1表示負(fù)數(shù),0表示正數(shù)。生成的id一般都是用正整數(shù),所以最高位固定為0 )
第二段: 41位為毫秒級時間(41位的長度可以使用69年)
第三段: 10位為workerId(10位的長度最多支持部署1024個節(jié)點(diǎn))
(這里的10位又分為兩部分,第一部分5位表示數(shù)據(jù)中心ID(0-31)第二部分5位表示機(jī)器ID(0-31))
第四段: 12位為毫秒內(nèi)的計(jì)數(shù)(12位的計(jì)數(shù)順序號支持每個節(jié)點(diǎn)每毫秒產(chǎn)生4096個ID序號)
代碼實(shí)現(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位(默認(rèn)0-31) private long datacenterId = 0; private long datacenterIdShift = 5; //機(jī)房機(jī)器ID 5位(默認(rèn)0-31) private long workerId = 0; private long workerIdShift = 5; //隨機(jī)數(shù) 12位(默認(rèn)0~4095) private AtomicLong random = new AtomicLong(); private long randomShift = 12; //隨機(jī)數(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(); //如果當(dāng)前時間和上一次時間不在同一毫秒內(nèi),直接返回 if (now > lastTime) { lastTime = now; random.set(0); return getId(); } //將最后的隨機(jī)數(shù),進(jìn)行+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的方法利用位運(yùn)算實(shí)現(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為的機(jī)器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)點(diǎn)
所有生成的id按時間趨勢遞增
整個分布式系統(tǒng)內(nèi)不會產(chǎn)生重復(fù)id(因?yàn)橛衐atacenterId和workerId來做區(qū)分)
SnowFlake不足
由于SnowFlake強(qiáng)依賴時間戳,所以時間的變動會造成SnowFlake的算法產(chǎn)生錯誤。
到此這篇關(guān)于Java 通過手寫分布式雪花SnowFlake生成ID方法詳解的文章就介紹到這了,更多相關(guān)Java SnowFlake內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring?Security如何實(shí)現(xiàn)升級密碼加密方式詳解
這篇文章主要為大家介紹了Spring?Security實(shí)現(xiàn)升級密碼加密方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01Spring @Profile注解實(shí)現(xiàn)多環(huán)境配置
這篇文章主要介紹了Spring @Profile注解實(shí)現(xiàn)多環(huán)境配置,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04Java實(shí)現(xiàn)計(jì)算圖中兩個頂點(diǎn)的所有路徑
這篇文章主要為大家詳細(xì)介紹了如何利用Java語言實(shí)現(xiàn)計(jì)算圖中兩個頂點(diǎn)的所有路徑功能,文中通過示例詳細(xì)講解了實(shí)現(xiàn)的方法,需要的可以參考一下2022-10-10Java的常見熱門ORM框架優(yōu)缺點(diǎn)區(qū)別
Java?ORM框架是一種用于將Java對象映射到關(guān)系型數(shù)據(jù)庫中的工具,使得開發(fā)人員能夠通過對象操作數(shù)據(jù)庫而不必直接使用SQL查詢,Java開發(fā)變得更加高效和易于維護(hù),選擇適合你的ORM框架是根據(jù)你的需求決定的,比如你的應(yīng)用場景,數(shù)據(jù)結(jié)構(gòu)和技術(shù)水平等2024-02-02java日期時間格式化@JsonFormat與@DateTimeFormat的使用
本文主要介紹了java日期時間格式化@JsonFormat與@DateTimeFormat的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08