程序员的资源宝库

网站首页 > gitee 正文

Hutool里的生成唯一Id唯的工具类

sanyeah 2024-04-12 17:42:06 gitee 6 ℃ 0 评论

目录

一、介绍

二、使用

2.1 UUID

2.2 ObjectId

2.3 Snowflake

三、测试类

3.1 测试类

3.2 测试结果


一、介绍

在分布式环境中,唯一ID生成应用十分广泛,生成方法也多种多样,Hutool针对一些常用生成策略做了简单封装。

唯一ID生成器的工具类,涵盖了:

  • UUID
  • ObjectId(MongoDB)
  • Snowflake(Twitter)

二、使用

2.1 UUID

UUID全称通用唯一识别码(universally unique identifier),JDK通过java.util.UUID提供了 Leach-Salz 变体的封装。在Hutool中,生成一个UUID字符串方法如下:

//生成的UUID是带-的字符串,类似于:a5c8a5e8-df2b-4706-bea4-08d0939410e3
String uuid = IdUtil.randomUUID();
//生成的是不带-的字符串,类似于:b17f24ff026d40949c85a24f4f375d42
String simpleUUID = IdUtil.simpleUUID();

说明Hutool重写java.util.UUID的逻辑,对应类为cn.hutool.core.lang.UUID,使生成不带-的UUID字符串不再需要做字符替换,性能提升一倍左右。

2.2 ObjectId

ObjectId是MongoDB数据库的一种唯一ID生成策略,是UUID version1的变种,详细介绍可见:服务化框架-分布式Unique ID的生成方法一览。

Hutool针对此封装了cn.hutool.core.lang.ObjectId,快捷创建方法为:

//生成类似:5b9e306a4df4f8c54a39fb0c
String id = ObjectId.next();
//方法2:从Hutool-4.1.14开始提供
String id2 = IdUtil.objectId();

2.3 Snowflake

分布式系统中,有一些需要使用全局唯一ID的场景,有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。Twitter的Snowflake 算法就是这种生成器。

使用方法如下:

//参数1为终端ID
//参数2为数据中心ID
Snowflake snowflake = IdUtil.createSnowflake(1, 1);
// 有两种返回值类型
long id = snowflake.nextId();
String nextIdStr = snowflake.nextIdStr();

三、测试类

3.1 测试类

public class Test {

    public static void main(String[] args) {

        String simpleUUID = IdUtil.simpleUUID();
        System.out.println("simpleUUID ---------> " + simpleUUID);
        String fastSimpleUUID = IdUtil.fastSimpleUUID();
        System.out.println("fastSimpleUUID -----> " + fastSimpleUUID);
        String fastUUID = IdUtil.fastUUID();
        System.out.println("fastUUID -----------> " + fastUUID);
        String objectId = IdUtil.objectId();
        System.out.println("objectId -----------> " + objectId);
        String randomUUID = IdUtil.randomUUID();
        System.out.println("randomUUID ---------> " + randomUUID);
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        String nextIdStr = snowflake.nextIdStr();
        long nextId = snowflake.nextId();
        System.out.println("nextIdStr ----------> " + nextIdStr);
        System.out.println("nextId -------------> " + nextId);
        Snowflake snowflake1 = IdUtil.getSnowflake(2, 2);
        String nextIdStr1 = snowflake1.nextIdStr();
        long nextId1 = snowflake1.nextId();
        System.out.println("nextIdStr1 ---------> " + nextIdStr1);
        System.out.println("nextId1 ------------> " + nextId1);
    }
}

3.2 测试结果

simpleUUID ----------> 0b28ddf56e23443eb5f1918ba7a44e5c
fastSimpleUUID -----> 097ed26444ec4c0893799f41514b952c
fastUUID --------------> 9e6dfcb4-7701-4f3b-be0e-3e36af56d888
objectId ----------------> 5e8c61c590608c7d18cac881
randomUUID ---------> 2d62f0af-cbdc-4e3d-aa56-f3d4e2740166
nextIdStr --------------> 1247484152861822976
nextId ------------------> 1247484152861822977
nextIdStr1 ------------> 1247484152920678400
nextId1 ----------------> 1247484152920678401

如图所示:


【参考资料】唯一ID工具——IdUtil:https://www.bookstack.cn/read/hutool/bfd2d43bcada297e.md

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表