diff --git a/fund-common/src/main/java/com/fundplatform/common/util/SnowflakeIdGenerator.java b/fund-common/src/main/java/com/fundplatform/common/util/SnowflakeIdGenerator.java new file mode 100644 index 0000000..9fc8e05 --- /dev/null +++ b/fund-common/src/main/java/com/fundplatform/common/util/SnowflakeIdGenerator.java @@ -0,0 +1,54 @@ +package com.fundplatform.common.util; + +import cn.hutool.core.lang.Snowflake; +import cn.hutool.core.util.IdUtil; + +/** + * 雪花算法ID生成器 + * 生成字符串类型的分布式唯一ID + * + *

雪花ID特点:

+ * + * + * @author fundplatform team + */ +public class SnowflakeIdGenerator { + + /** + * 雪花算法实例 + * workerId: 工作机器ID (0-31) + * datacenterId: 数据中心ID (0-31) + * 生产环境应根据实际部署情况配置 + */ + private static final Snowflake SNOWFLAKE = IdUtil.getSnowflake(1, 1); + + /** + * 私有构造函数,防止实例化 + */ + private SnowflakeIdGenerator() { + } + + /** + * 生成下一个字符串类型的雪花ID + * + * @return 19位数字字符串 + */ + public static String nextId() { + return String.valueOf(SNOWFLAKE.nextId()); + } + + /** + * 生成下一个Long类型的雪花ID + * 供MyBatis Plus IdentifierGenerator使用 + * + * @return Long类型ID + */ + public static Long nextIdAsLong() { + return SNOWFLAKE.nextId(); + } +}