Spring Cloud Alibaba 工程集成全局唯一ID生成器 Vesta

官网简介:

 

        Vesta是一款通用的ID产生器,互联网俗称统一发号器,它支持三种发布模式:嵌入发布模式、中心服务器发布模式、REST发布模式,根据业务的性能需求,它可以产生最大峰值型和最小粒度型两种类型的ID,它的实现架构使其具有高性能,高可用和可伸缩等互联网产品需要的质量属性,是一款通用的高性能的发号器产品。

特性:

  • 全局唯一
  • 粗略有序
  • 可反解
  • 可制造

本章项目实战环境:

Spring Cloud Alibaba 模块化开发,vesta配置在公用模块sm-common,基于业务场景自行定义;

  1. 大部分模块场景下需要用到,则配置在公共模块引入;
  2. 部分模块用到可在模块内引入即可;

Vesta的jar包以及配置文件下载:https://download.csdn.net/download/u011673769/33155559

项目实战

项目架构

sm-commodity:商品模块,只涉及商品相关业务需求;

sm-common:公用模块,主要存公用配置信息,以及工具类等;

sm-gateway:网关模块,所有模块的统一入口;

在sm-common根目录下创建lib文件夹,并导入vesta相关jar包,如下图 

在sm-common的pom.xml中引入vesta的依赖:

<!-- 引入本地jar包-->
<dependency>
	<groupId>vesta.intf</groupId>
	<artifactId>vesta-intf</artifactId>
	<version>1.0.0</version>
	<scope>system</scope>
	<systemPath>${project.basedir}/lib/vesta-intf.jar</systemPath>
</dependency>

<dependency>
	<groupId>vesta.service</groupId>
	<artifactId>vesta-service</artifactId>
	<version>1.0.0</version>
	<scope>system</scope>
	<systemPath>${project.basedir}/lib/vesta-service.jar</systemPath>
</dependency>

在sm-common的resource下导入vesta的配置文件,如下

vesta-rest.properties

# 机器ID
vesta.machine=1021 
## 生成方式,0表示 嵌入发布模式 1表示 中心服务器发布模式 2表示 REST发 布模式
vesta.genMethod=0   
# # ID类型,1表示最小粒度型  0表示最大峰值
vesta.type=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:/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>

创建类 UidService.java

package net.sm.util;

import com.robert.vesta.service.bean.Id;
import com.robert.vesta.service.intf.IdService;
import org.checkerframework.common.reflection.qual.ForName;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>Title: UidService</p>
 * <p>Description: 统一发号器,它具有全局唯一、粗略有序、可反解和可制造等特性</p>
 * 三种发布模式:嵌入发布模式、中心服务器发布模式、REST发布模式
 * @author GaoYanJia
 */
@Service("MyUidService")
public class UidService {

    private final IdService idService;
    public UidService() {
        //重要,这个路径必须指定你存放 vesta-rest-main.xml 的路径
        ApplicationContext ac = new ClassPathXmlApplicationContext(
                "vesta/vesta-rest-main.xml");
        idService = (IdService) ac.getBean("idService");
    }

    public long genId() {
        return idService.genId();
    }

    public String genIdString() {
        return idService.genId()+"";
    }

    public Id explainId(long id) {
        return idService.expId(id);
    }

    public Date transTime(long time) {
        return idService.transTime(time);
    }

    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;
    }

}

API使用说明:

  • genId 生成全局唯一 ID号
  • explainId 反解全局唯一 ID号,得到可以解释 ID号含义的 JSON数据
  • makeId 手工制造 ID
  • transTime对产生的日期反解

新建一个测试的Controller,并引入UidService

package net.sm.controller;

import com.alibaba.fastjson.JSONObject;
import net.sm.util.UidService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @Author GGYYJJ
 * @Date 2021/9/29 17:48
 * @Version 1.0
 * @Title
 * @Desc
 */
@RestController
@RequestMapping("/api/commodity/v1/")
public class CommodityController {

    @Autowired
    private UidService myUidService;

    /**
        测试 Vesta API功能
    **/    
    @PostMapping("/test")
    public JSONObject test(){
        long uid = myUidService.genId();
        System.out.println("获取唯一id"+uid);
        Id config = myUidService.explainId(uid);
        System.out.println("解码,获取uid内配置信息:"+config);
        Date date = myUidService.transTime(config.getTime());
        System.out.println("解码后,将date转为时间:"+date);
    }
}

控制台输入如下,配置成功;

 

以下是配置过程中遇到的问题:

  1. UidService.java 必须用@Service("MyUidService")取别名的方式注入到spring中;
  2. pom.xml 本地的方式引入jar包,必须加  <scope>system</scope>  否则在打包项目的时候,本地的vesta的jar不会打包进入项目内;项目能成功编译通过,但是发布时候会报错,报错  com.robert.vesta.service.intf.IdService  找不到;

至此,Spring Cloud Alibaba 引入Vesta结束,有帮助的朋友点个赞,关注下。后续还有更多项目实战文章;小编初期写文章,谢谢支持。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

♂老码♂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值