詳解Java中雪花算法的實現(xiàn)
前言
本文主要介紹了Java雪花算法的實現(xiàn)
一、雪花算法
雪花算法是一種分布式的id生成算法。原理是將long分成若干個區(qū)段分別管理。默認包括時間戳、數(shù)據(jù)中心ID/機房ID,woker id(機器編號),以及sequence四個部分,用戶可以自由擴展第五個部分type。 同時用戶也可以動態(tài)調(diào)整這5個部分的占比關系。
二、使用步驟
1.引入庫
1.1 編譯并發(fā)布到本地
gradle clean build publishToMavenLocal-x test
1.2 gradle (gradle.org)
implementation'io.github.kylin-hunter:k-commons:1.0.7'
1.3 maven (maven.apache.org)
<dependency> <groupId>io.github.kylin-hunter</groupId> <artifactId>io.github.kylin-hunter:k-commons</artifactId> <version>1.0.7</version> </dependency>
2.示例
2.1 主要Api
2.1.1 構造器
/** * @param type 業(yè)務類型 * @param workerId 機器編號 * @param datacenterId 機房編號 * @title UidGenerator 構造器 * @description * @author BiJi'an * @date 2022-12-11 16:19 */ public UidGenerator(long type,long workerId,long datacenterId); /** * @param sequenceBits 序列號 占的bit位數(shù) * @param typeBits 業(yè)務類型 占的bit位數(shù) * @param workerIdBits 機器編號 占的bit位數(shù) * @param datacenterIdBits 機房編號 占的bit位數(shù) * @param type 業(yè)務類型 * @param workerId 機器編號 * @param datacenterId 機房編號 * @title UidGenerator 構造器 * @description * @author BiJi'an * @date 2022-12-11 16:20 */ public UidGenerator(int sequenceBits,int typeBits,int workerIdBits,int datacenterIdBits, long type,long workerId, long datacenterId);
2.1.2 生成和反解uid
/** * @return long * @title 獲取下一個uid * @description * @author BiJi'an * @date 2022-12-11 00:39 */ public synchronized long nextId(); /** * @param uid uid * @return io.github.kylinhunter.commons.uid.UidInfo * @title 通過uid 反解出uid的信息 * @description * @author BiJi'an * @date 2022-12-11 16:30 */ public UidInfo parse(long uid);
2.2代碼示例
// 業(yè)務代碼 3,默認業(yè)務代碼支持范圍 0-15,可以通過構造器2調(diào)整支持范圍 // 機器編碼 4,默認機器編碼支持范圍0-15,可以通過構造器2調(diào)整支持范圍 // 機房編碼 1,默認機房支持范圍 0-4,可以通過構造器2調(diào)整支持范圍 // 各個編碼范圍,均可以調(diào)整 UidGenerator worker = new UidGenerator(3, 4, 1); for (int i = 0; i < 10; i++) { long nextId = worker.nextId(); System.out.println(nextId + "=>" + worker.parse(nextId)); }
2.3 結果輸出
2022-12-11 22:54:26.177 [Test worker] INFO - i.g.k.commons.uid.UidGenerator[107]: timestampBits 41,datacenterIdBits 2, workerIdBits 4,typeBits 4, sequenceBits 12
161155503589961728=>UidInfo[sequence=0, type=3, workerId=4, datacenterId=1, timestamp=1670921666180/2022-12-13 16:54:26]
161155503644487680=>UidInfo[sequence=0, type=3, workerId=4, datacenterId=1, timestamp=1670921666193/2022-12-13 16:54:26]
161155503648681984=>UidInfo[sequence=0, type=3, workerId=4, datacenterId=1, timestamp=1670921666194/2022-12-13 16:54:26]
161155503648681985=>UidInfo[sequence=1, type=3, workerId=4, datacenterId=1, timestamp=1670921666194/2022-12-13 16:54:26]
161155503648681986=>UidInfo[sequence=2, type=3, workerId=4, datacenterId=1, timestamp=1670921666194/2022-12-13 16:54:26]
161155503648681987=>UidInfo[sequence=3, type=3, workerId=4, datacenterId=1, timestamp=1670921666194/2022-12-13 16:54:26]
161155503648681988=>UidInfo[sequence=4, type=3, workerId=4, datacenterId=1, timestamp=1670921666194/2022-12-13 16:54:26]
161155503648681989=>UidInfo[sequence=5, type=3, workerId=4, datacenterId=1, timestamp=1670921666194/2022-12-13 16:54:26]
161155503648681990=>UidInfo[sequence=6, type=3, workerId=4, datacenterId=1, timestamp=1670921666194/2022-12-13 16:54:26]
161155503648681991=>UidInfo[sequence=7, type=3, workerId=4, datacenterId=1, timestamp=1670921666194/2022-12-13 16:54:26]
到此這篇關于詳解Java中雪花算法的實現(xiàn)的文章就介紹到這了,更多相關Java雪花算法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring Boot 2和Redis例子實現(xiàn)過程解析
這篇文章主要介紹了Spring Boot2發(fā)布與調(diào)用REST服務過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-11-11SpringBoot處理form-data表單接收對象數(shù)組的方法
form-data則是一種更加靈活的編碼方式,它可以處理二進制數(shù)據(jù)(如圖片、文件等)以及文本數(shù)據(jù),這篇文章主要介紹了SpringBoot處理form-data表單接收對象數(shù)組,需要的朋友可以參考下2023-11-11