本文內(nèi)容腦圖如下:
文章共 760字鹰椒,閱讀大約需要 2分鐘 !
概 述
在前一篇文章 《Spring Boot工程集成全局唯一ID生成器 UidGenerator》 中給大家推薦了一款由百度開(kāi)發(fā)的基于 Snowflake算法實(shí)現(xiàn)的全局唯一ID生成器 UidGenerator疚沐,而本文則給大家再度推薦一款優(yōu)秀的全局唯一ID生成器摸恍,名叫 Vesta吱型。
Vesta 是艷鵬大佬的開(kāi)源作品,基于Java開(kāi)發(fā)园骆,其體驗(yàn)地址 在此舔痪。Vesta 是一款通用的 ID產(chǎn)生器,互聯(lián)網(wǎng)俗稱(chēng)統(tǒng)一發(fā)號(hào)器锌唾,其具有幾大很具有優(yōu)勢(shì)的特性:
- 全局唯一
- 粗略有序
- 可反解
- 可制造
- 分布式
而且支持三種發(fā)布模式:
- 嵌入式發(fā)布模式
- 中心服務(wù)器發(fā)布模式
- REST 發(fā)布模式
根據(jù)業(yè)務(wù)的性能需求锄码,它可以產(chǎn)生 最大峰值型 和 最小粒度型 兩種類(lèi)型的 ID,它的實(shí)現(xiàn)架構(gòu)使其具有高性能晌涕,高可用和可伸縮等互聯(lián)網(wǎng)產(chǎn)品需要的質(zhì)量屬性滋捶,是一款通用的高性能的發(fā)號(hào)器產(chǎn)品。
本文就在 Spring Boot項(xiàng)目中將 Vesta耍起來(lái)渐排!
注: 本文首發(fā)于 My Personal Blog:CodeSheep·程序羊炬太,歡迎光臨 小站
基礎(chǔ)工程搭建
Spring Boot基礎(chǔ)工程的搭建我不再贅述,創(chuàng)建好工程后 pom
中需要加入如下依賴(lài):
<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>
對(duì)應(yīng)的 Jar包去編譯一下 Vesta源碼即可獲得驯耻,源碼在此
Vesta 配置導(dǎo)入
- 在項(xiàng)目
resources
目錄中加入 Vesta的配置文件
引入vesta-rest.properties
亲族,配置如下:
vesta.machine=1021 # 機(jī)器ID
vesta.genMethod=0 # 生成方式炒考,0表示使用嵌入發(fā)布模式
vesta.type=1 # ID類(lèi)型,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>
好斋枢,接下來(lái)我們創(chuàng)建一個(gè) Config配置類(lèi)來(lái)將 vesta-rest-main.xml
配置文件加載進(jìn)項(xiàng)目
- 創(chuàng)建 UidConfig配置類(lèi)
@Configuration
@ImportResource( locations = { "classpath:ext/vesta/vesta-rest-main.xml" } )
public class UidConfig {
}
編寫(xiě) Vesta Service
這里面包含的是和 ID生成器相關(guān)的幾個(gè)重要工具接口,主要有:
-
genId
生成全局唯一 ID號(hào) -
explainId
反解全局唯一 ID號(hào)知给,得到可以解釋 ID號(hào)含義的 JSON數(shù)據(jù) -
makeId
手工制造 ID
來(lái)看代碼吧
@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;
}
}
編寫(xiě)測(cè)試 Controller
我們針對(duì)上述 UidService
中提供的三個(gè)工具接口來(lái)各自編寫(xiě)一個(gè)測(cè)試接口:
@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 );
}
}
實(shí)驗(yàn)驗(yàn)證
- 實(shí)驗(yàn)一
首先我們用瀏覽器調(diào)用接口 genid
瓤帚,來(lái)返回生成的全局唯一 ID流水號(hào),一切都是那么的簡(jiǎn)單優(yōu)雅:
- 實(shí)驗(yàn)二
由于 Vesta生成的全局唯一流水號(hào)具有 可反解 的優(yōu)良特性涩赢,因此我們可以先生成一個(gè)流水號(hào)戈次,然后調(diào)用 expid
接口來(lái)反解出流水號(hào)所代表的意義:
后 記
由于能力有限,若有錯(cuò)誤或者不當(dāng)之處筒扒,還請(qǐng)大家批評(píng)指正怯邪,一起學(xué)習(xí)交流!
- My Personal Blog:CodeSheep 程序羊
- 我的半年技術(shù)博客之路