Class RandomAssigner
java.lang.Object
cloud.opencode.base.id.snowflake.RandomAssigner
- All Implemented Interfaces:
WorkerIdAssigner
Random Worker ID Assigner
随机工作节点ID分配器
Randomly assigns worker ID and datacenter ID. Simple and suitable for testing or low-collision scenarios.
随机分配工作节点ID和数据中心ID。 简单,适用于测试或低碰撞场景。
Note | 注意:
- IDs are generated once at creation time - ID在创建时生成一次
- Collision is possible in large clusters - 在大型集群中可能发生碰撞
Usage Examples | 使用示例:
WorkerIdAssigner assigner = RandomAssigner.create();
long workerId = assigner.assignWorkerId();
long datacenterId = assigner.assignDatacenterId();
Features | 主要功能:
- Assigns worker ID using secure random generation - 使用安全随机生成分配工作ID
- Suitable for environments without stable network identity - 适用于无稳定网络标识的环境
Security | 安全性:
- Thread-safe: Yes (immutable) - 线程安全: 是(不可变)
- Null-safe: No - 空值安全: 否
- Since:
- JDK 25, opencode-base-id V1.1.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionlongAssigns a datacenter ID 分配数据中心IDlongAssigns a worker ID 分配工作节点IDstatic RandomAssignercreate()Creates a random assigner 创建随机分配器Gets a description of the assignment strategy 获取分配策略描述static RandomAssignerof(long workerId, long datacenterId) Creates an assigner with fixed IDs 创建使用固定ID的分配器
-
Method Details
-
create
-
of
Creates an assigner with fixed IDs 创建使用固定ID的分配器- Parameters:
workerId- the worker ID | 工作节点IDdatacenterId- the datacenter ID | 数据中心ID- Returns:
- assigner | 分配器
-
assignWorkerId
public long assignWorkerId()Description copied from interface:WorkerIdAssignerAssigns a worker ID 分配工作节点ID- Specified by:
assignWorkerIdin interfaceWorkerIdAssigner- Returns:
- worker ID within valid range | 有效范围内的工作节点ID
-
assignDatacenterId
public long assignDatacenterId()Description copied from interface:WorkerIdAssignerAssigns a datacenter ID 分配数据中心ID- Specified by:
assignDatacenterIdin interfaceWorkerIdAssigner- Returns:
- datacenter ID within valid range | 有效范围内的数据中心ID
-
getStrategyName
Description copied from interface:WorkerIdAssignerGets a description of the assignment strategy 获取分配策略描述- Specified by:
getStrategyNamein interfaceWorkerIdAssigner- Returns:
- strategy description | 策略描述
-