Class SnowflakeGenerator
java.lang.Object
cloud.opencode.base.id.snowflake.SnowflakeGenerator
- All Implemented Interfaces:
IdGenerator<Long>
Snowflake ID Generator
雪花ID生成器
Generates 64-bit unique IDs with the following structure:
生成具有以下结构的64位唯一ID:
- 1 bit - sign (always 0) | 符号位(始终为0)
- 41 bits - timestamp (69 years) | 时间戳(约69年)
- 5 bits - datacenter ID (0-31) | 数据中心ID(0-31)
- 5 bits - worker ID (0-31) | 工作节点ID(0-31)
- 12 bits - sequence (0-4095/ms) | 序列号(每毫秒0-4095)
Features | 主要功能:
- Time-ordered IDs - 时间有序ID
- High throughput (4096/ms) - 高吞吐量(每毫秒4096个)
- Clock backward handling - 时钟回拨处理
- Configurable bit allocation - 可配置位分配
Usage Examples | 使用示例:
// Default generator
SnowflakeGenerator gen = SnowflakeGenerator.create();
long id = gen.generate();
// Custom worker/datacenter
SnowflakeGenerator gen2 = SnowflakeGenerator.create(1, 1);
long id2 = gen2.generate();
// Using builder
SnowflakeGenerator gen3 = SnowflakeGenerator.builder()
.workerId(1)
.datacenterId(1)
.clockBackwardStrategy(Wait.ofSeconds(5))
.build();
Security | 安全性:
- Thread-safe: Yes (ReentrantLock) - 线程安全: 是(可重入锁)
- Since:
- JDK 25, opencode-base-id V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic SnowflakeBuilderbuilder()Creates a builder for customized generator 创建用于自定义生成器的构建器static SnowflakeGeneratorcreate()Creates a default generator 创建默认生成器static SnowflakeGeneratorcreate(long workerId, long datacenterId) Creates a generator with worker and datacenter IDs 使用工作节点ID和数据中心ID创建生成器generate()Generates the next ID 生成下一个IDGenerates an ID and returns as string 生成ID并返回字符串Gets the configuration 获取配置longGets the datacenter ID 获取数据中心IDlonggetEpoch()Gets the epoch timestamp 获取起始时间戳getType()Gets the generator type name 获取生成器类型名称longGets the worker ID 获取工作节点IDparse(long id) Parses a snowflake ID 解析雪花IDMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface IdGenerator
generateBatch
-
Method Details
-
create
Creates a default generator 创建默认生成器- Returns:
- generator | 生成器
-
create
Creates a generator with worker and datacenter IDs 使用工作节点ID和数据中心ID创建生成器- Parameters:
workerId- the worker node ID (0-31) | 工作节点ID(0-31)datacenterId- the datacenter ID (0-31) | 数据中心ID(0-31)- Returns:
- generator | 生成器
-
builder
Creates a builder for customized generator 创建用于自定义生成器的构建器- Returns:
- builder | 构建器
-
generate
Description copied from interface:IdGeneratorGenerates the next ID 生成下一个ID- Specified by:
generatein interfaceIdGenerator<Long>- Returns:
- generated ID | 生成的ID
-
generateStr
Generates an ID and returns as string 生成ID并返回字符串- Returns:
- ID string | ID字符串
-
getWorkerId
public long getWorkerId()Gets the worker ID 获取工作节点ID- Returns:
- worker ID | 工作节点ID
-
getDatacenterId
public long getDatacenterId()Gets the datacenter ID 获取数据中心ID- Returns:
- datacenter ID | 数据中心ID
-
getEpoch
public long getEpoch()Gets the epoch timestamp 获取起始时间戳- Returns:
- epoch timestamp | 起始时间戳
-
getConfig
-
parse
Parses a snowflake ID 解析雪花ID- Parameters:
id- the ID to parse | 要解析的ID- Returns:
- parsed result | 解析结果
-
getType
Description copied from interface:IdGeneratorGets the generator type name 获取生成器类型名称- Specified by:
getTypein interfaceIdGenerator<Long>- Returns:
- type name | 类型名称
-