Spring Boot 工程整合全域性唯一ID生成器 Vesta

AE-86
本文內容腦圖如下:

本文內容腦圖
文章共 760字,閱讀大約需要 2分鐘 !
概 述
在前一篇文章 ofollow,noindex">《Spring Boot工程整合全域性唯一ID生成器 UidGenerator》 中給大家推薦了一款由百度開發的基於 Snowflake演算法實現的全域性唯一ID生成器 UidGenerator ,而本文則給大家再度推薦一款優秀的全域性唯一ID生成器,名叫 Vesta。
Vesta 是豔鵬大佬的開源作品,基於Java開發,其體驗地址 在此 。Vesta 是一款通用的 ID產生器,網際網路俗稱統一發號器,其具有幾大很具有優勢的特性:
- 全域性唯一
- 粗略有序
- 可反解
- 可製造
- 分散式
而且支援三種釋出模式:
根據業務的效能需求,它可以產生 最大峰值型 和 最小粒度型 兩種型別的 ID,它的實現架構使其具有高效能,高可用和可伸縮等網際網路產品需要的質量屬性,是一款通用的高效能的發號器產品。
本文就在 Spring Boot專案中將 Vesta耍起來!
注:本文首發於 My Personal Blog:CodeSheep·程式羊 ,歡迎光臨 小站
基礎工程搭建
Spring Boot基礎工程的搭建我不再贅述,建立好工程後 pom
中需要加入如下依賴:
<dependency> <groupId>com.robert.vesta</groupId> <artifactId>vesta-service</artifactId> <version>0.0.1</version> </dependency> <dependency> <groupId>com.robert.vesta</groupId> <artifactId>vesta-intf</artifactId> <version>0.0.1</version> </dependency>
對應的 Jar包去編譯一下 Vesta原始碼即可獲得, 原始碼在此
Vesta 配置匯入
- 在專案
resources
目錄中加入 Vesta的配置檔案
引入 vesta-rest.properties
,配置如下:
vesta.machine=1021# 機器ID vesta.genMethod=0# 生成方式,0表示使用嵌入釋出模式 vesta.type=1# ID型別,1表示最小粒度型
引入 vesta-rest-main.xml
,配置如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations" value="classpath:ext/vesta/vesta-rest.properties"/> </bean> <bean id="idService" class="com.robert.vesta.service.factory.IdServiceFactoryBean" init-method="init"> <property name="providerType" value="PROPERTY"/> <property name="type" value="${vesta.type}"/> <property name="genMethod" value="${vesta.genMethod}"/> <property name="machineId" value="${vesta.machine}"/> </bean> </beans>
好,接下來我們建立一個 Config配置類來將 vesta-rest-main.xml
配置檔案載入進專案
- 建立 UidConfig配置類
@Configuration @ImportResource( locations = { "classpath:ext/vesta/vesta-rest-main.xml" } ) public class UidConfig { }
編寫 Vesta Service
這裡麵包含的是和 ID生成器相關的幾個重要工具介面,主要有:
genId explainId makeId
來看程式碼吧
@Service public class UidService { @Resource private IdService idService; public long genId() { return idService.genId(); } public Id explainId( long id ) { return idService.expId(id); } public long makeId( long version, long type, long genMethod, long machine, long time, long seq ) { long madeId = -1; if (time == -1 || seq == -1) throw new IllegalArgumentException( "Both time and seq are required." ); else if (version == -1) { if (type == -1) { if (genMethod == -1) { if (machine == -1) { madeId = idService.makeId(time, seq); } else { madeId = idService.makeId(machine, time, seq); } } else { madeId = idService.makeId(genMethod, machine, time, seq); } } else { madeId = idService.makeId(type, genMethod, machine, time, seq); } } else { madeId = idService.makeId(version, type, genMethod, time, seq, machine); } return madeId; } }
編寫測試 Controller
我們針對上述 UidService
中提供的三個工具介面來各自編寫一個測試介面:
@RestController public class UidController { @Autowired private UidService uidService; @RequestMapping("/genid") public long genId() { return uidService.genId(); } @RequestMapping("/expid") public Id explainId(@RequestParam(value = "id", defaultValue = "0") long id) { return uidService.explainId( id ); } @RequestMapping("/makeid") public long makeId( @RequestParam(value = "version", defaultValue = "-1") long version, @RequestParam(value = "type", defaultValue = "-1") long type, @RequestParam(value = "genMethod", defaultValue = "-1") long genMethod, @RequestParam(value = "machine", defaultValue = "-1") long machine, @RequestParam(value = "time", defaultValue = "-1") long time, @RequestParam(value = "seq", defaultValue = "-1") long seq) { return uidService.makeId( version, type, genMethod, machine, time, seq ); } }
實驗驗證
- 實驗一
首先我們用瀏覽器呼叫介面 genid
,來返回生成的全域性唯一 ID流水號,一切都是那麼的簡單優雅:

生成全域性唯一流水號
- 實驗二
由於 Vesta生成的全域性唯一流水號具有 可反解 的優良特性,因此我們可以先生成一個流水號,然後呼叫 expid
介面來反解出流水號所代表的意義:

全域性唯一流水號的反解效果
後 記
由於能力有限,若有錯誤或者不當之處,還請大家批評指正,一起學習交流!
- My Personal Blog: CodeSheep 程式羊
- 我的半年技術部落格之路