本文内容脑图以下:java
文章共 760字,阅读大约须要 2分钟 !git
在前一篇文章 《Spring Boot工程集成全局惟一ID生成器 UidGenerator》 中给你们推荐了一款由百度开发的基于 Snowflake算法实现的全局惟一ID生成器 UidGenerator,而本文则给你们再度推荐一款优秀的全局惟一ID生成器,名叫 Vesta。github
Vesta 是艳鹏大佬的开源做品,基于Java开发,其体验地址 在此。Vesta 是一款通用的 ID产生器,互联网俗称统一发号器,其具备几大很具备优点的特性:算法
并且支持三种发布模式:spring
根据业务的性能需求,它能够产生 最大峰值型 和 最小粒度型 两种类型的 ID,它的实现架构使其具备高性能,高可用和可伸缩等互联网产品须要的质量属性,是一款通用的高性能的发号器产品。浏览器
本文就在 Spring Boot项目中将 Vesta耍起来!bash
注: 本文首发于 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源码便可得到,源码在此app
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
配置文件加载进项目
@Configuration
@ImportResource( locations = { "classpath:ext/vesta/vesta-rest-main.xml" } )
public class UidConfig {
}
复制代码
这里面包含的是和 ID生成器相关的几个重要工具接口,主要有:
genId
生成全局惟一 ID号explainId
反解全局惟一 ID号,获得能够解释 ID号含义的 JSON数据makeId
手工制造 ID来看代码吧
@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;
}
}
复制代码
咱们针对上述 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
接口来反解出流水号所表明的意义:
因为能力有限,如有错误或者不当之处,还请你们批评指正,一块儿学习交流!
可 长按 或 扫描 下面的 当心心 来订阅做者公众号 CodeSheep,获取更多 务实、能看懂、可复现的 原创文 ↓↓↓