VassasCo.Utility.SnowflakeIdHelper 1.2.0

dotnet add package VassasCo.Utility.SnowflakeIdHelper --version 1.2.0
                    
NuGet\Install-Package VassasCo.Utility.SnowflakeIdHelper -Version 1.2.0
                    
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="VassasCo.Utility.SnowflakeIdHelper" Version="1.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="VassasCo.Utility.SnowflakeIdHelper" Version="1.2.0" />
                    
Directory.Packages.props
<PackageReference Include="VassasCo.Utility.SnowflakeIdHelper" />
                    
Project file
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 VassasCo.Utility.SnowflakeIdHelper --version 1.2.0
                    
#r "nuget: VassasCo.Utility.SnowflakeIdHelper, 1.2.0"
                    
#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 VassasCo.Utility.SnowflakeIdHelper@1.2.0
                    
#: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=VassasCo.Utility.SnowflakeIdHelper&version=1.2.0
                    
Install as a Cake Addin
#tool nuget:?package=VassasCo.Utility.SnowflakeIdHelper&version=1.2.0
                    
Install as a Cake Tool

SnowflakeIdHelper

分布式雪花 ID 生成器(Twitter Snowflake 算法的 C# 实现)。

文件说明

文件 内容
SnowflakeIdHelper.cs ID 生成器、建造者、ID 反解、单例管理

核心特点

  • 杜绝时钟回拨:使用 Stopwatch 硬件单调计数器代替系统时钟
  • 线程安全:lock + Interlocked 保证并发正确
  • 全局单例SnowflakeIdHelper.Next() 开箱即用
  • 集群支持:WorkerId + DataCenterId 共 10bit,最多 1024 节点

ID 结构

[1bit保留] [41bit时间戳] [5bit DataCenter] [5bit Worker] [12bit 序列号]
  • 每毫秒 4096 个 ID
  • 每节点每天可生成约 3500 亿个

快速开始

// 开箱即用
long id = SnowflakeIdHelper.Next();

// 自定义机器码
SnowflakeIdHelper.SetDefault(b => b.SetWorkerId(5).SetDataCenterId(2));
long id = SnowflakeIdHelper.Next();

// 创建独立实例
var gen = SnowflakeIdHelper.Build()
    .SetWorkerId(10)
    .SetDataCenterId(1)
    .SetEpoch(new DateTime(2024, 1, 1, 0, 0, 0, DateTimeKind.Utc))
    .Create();
long id = gen.NextId();

ID 反解

var info = SnowflakeIdHelper.Parse(id);
Console.WriteLine($"时间: {info.Timestamp}");
Console.WriteLine($"DC: {info.DataCenterId}, Worker: {info.WorkerId}");
Console.WriteLine($"序号: {info.Sequence}");

集群 WorkerId 分配方案

方案 适用规模 实现
配置文件 5-10 台 手动配置唯一的 WorkerId+DataCenterId
数据库自增 50 台内 INSERT + LAST_INSERT_ID() % 1024
Redis INCR 100+ 台 redis.Incr("snowflake:counter") % 1024
ZooKeeper 弹性扩缩 临时顺序节点序号 % 1024

特殊情况

// 指定时间生成(使用系统时钟,受回拨影响)
long id = SnowflakeIdHelper.Next(DateTime.UtcNow);
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • net6.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on VassasCo.Utility.SnowflakeIdHelper:

Package Downloads
VassasCo.Utility

C# 桌面开发工具库 (WinForm / WPF / Avalonia): - ConfigHelper:零代码实体类⇋JSON/XML配置双向映射(带注释、热重载、原子保存、列表展开) - LogHelper:异步高性能日志系统(建造者模式、异步队列、自动清理、14种日志类型) - CrashDumpHelper:崩溃捕获+FirstChance异常追踪+MiniDump生成 - SnowflakeIdHelper:分布式雪花ID生成器(单调时钟杜绝回拨、集群WorkerId分配、ID反解) - ScheduleHelper:全能定时任务调度器(CRON/固定速率/固定延迟、重试退避、超时取消、日历过滤、线程池、优雅关闭) - RetryHelper:智能重试器(指数退避+抖动+断路器三态+降级+超时,同步/异步) - EventBus:进程内事件总线(类型发布/订阅、特性自动注册、优先级、条件过滤、粘性事件、多通道),支持 .NET Standard 2.0 / .NET 6 / .NET 8 / .NET 9 / .NET 10 - ExcelMapper:原生对象→Excel映射(嵌套类子表头合并、数组独立Sheet、Dictionary自适应、全可配置样式)

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.0 225 6/22/2026
1.1.0 267 6/18/2026