Rabbit.Common.IdGen
1.3.1
dotnet add package Rabbit.Common.IdGen --version 1.3.1
NuGet\Install-Package Rabbit.Common.IdGen -Version 1.3.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Rabbit.Common.IdGen" Version="1.3.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Rabbit.Common.IdGen" Version="1.3.1" />
<PackageReference Include="Rabbit.Common.IdGen" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Rabbit.Common.IdGen --version 1.3.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Rabbit.Common.IdGen, 1.3.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Rabbit.Common.IdGen@1.3.1
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Rabbit.Common.IdGen&version=1.3.1
#tool nuget:?package=Rabbit.Common.IdGen&version=1.3.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Rabbit.Common.IdGen
Distributed snowflake ID generator based on IdGen library. Supports both DI injection (IdGenerator) and static access (IdGenHelper.CreateId()).
分布式 ID 生成组件,基于雪花算法。
安装
dotnet add package Rabbit.Common.IdGen
依赖
- Rabbit.Common.Lite
- IdGen 3.0.7
功能
- 18 位雪花 ID
- 支持多实例(0-31)
- 单调递增
使用方式
Console 项目
using Microsoft.Extensions.DependencyInjection;
using Rabbit.Common.IdGen;
using IdGen;
// 1. 创建 DI 容器
var services = new ServiceCollection();
// 2. 注册 ID 生成器
services.AddIdGenConfigure(generatorId: 0); // generatorId: 0-31
// 3. 构建服务提供者
var provider = services.BuildServiceProvider();
// 4. 生成 ID(方式一:注入)
var idGenerator = provider.GetRequiredService<IdGenerator>();
var id1 = idGenerator.CreateId();
// 方式二:静态方法(在 AddIdGenConfigure 后可用)
var id2 = IdGenHelper.CreateId();
// 生成多个 ID
var ids = IdGenHelper.CreateIds(10); // 生成 10 个 ID
Web API 项目
using Rabbit.Common.IdGen;
// 单机部署
services.AddIdGenConfigure(generatorId: 0);
// 多实例部署(每个实例使用不同的 ID)
// 实例1: services.AddIdGenConfigure(0)
// 实例2: services.AddIdGenConfigure(1)
使用示例
using Rabbit.Common.IdGen;
public class OrderService
{
private readonly IdGenerator _idGenerator;
public OrderService(IdGenerator idGenerator)
{
_idGenerator = idGenerator;
}
public long CreateOrder()
{
// 方式1: 注入 IdGenerator
var id = _idGenerator.CreateId();
// 方式2: 使用静态方法
var id2 = IdGenHelper.CreateId();
return id;
}
}
直接使用(不依赖 DI)
// 不依赖 DI
IdGenHelper.IdGenerator = new IdGenerator(0);
var id = IdGenHelper.CreateId();
ID 结构
雪花 ID(18位):
- 时间戳(41位)
- 机器 ID(10位,0-1023)
- 序列号(12位)
注意事项
- 多实例部署时每个实例必须使用不同的 generatorId(0-1023)
- 单机部署使用 0 即可
许可证
MIT
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net10.0
- IdGen (>= 3.0.7)
- Rabbit.Common.Lite (>= 1.3.1)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Rabbit.Common.IdGen:
| Package | Downloads |
|---|---|
|
Rabbit.Common.Full
Meta-package referencing all Rabbit.Common utility packages. / Rabbit.Common 全家桶,引用所有子包 |
GitHub repositories
This package is not used by any popular GitHub repositories.