SnowflakeId.AutoRegister.StackExchangeRedis
1.0.1
See the version list below for details.
dotnet add package SnowflakeId.AutoRegister.StackExchangeRedis --version 1.0.1
NuGet\Install-Package SnowflakeId.AutoRegister.StackExchangeRedis -Version 1.0.1
<PackageReference Include="SnowflakeId.AutoRegister.StackExchangeRedis" Version="1.0.1" />
paket add SnowflakeId.AutoRegister.StackExchangeRedis --version 1.0.1
#r "nuget: SnowflakeId.AutoRegister.StackExchangeRedis, 1.0.1"
// Install SnowflakeId.AutoRegister.StackExchangeRedis as a Cake Addin #addin nuget:?package=SnowflakeId.AutoRegister.StackExchangeRedis&version=1.0.1 // Install SnowflakeId.AutoRegister.StackExchangeRedis as a Cake Tool #tool nuget:?package=SnowflakeId.AutoRegister.StackExchangeRedis&version=1.0.1
Snowflake Id(雪花Id) 自动注册
SnowflakeId.AutoRegister
是一个 C# 库,帮助你为 Snowflake ID 自动注册 WorkerId。
它不生成 Snowflake ID,仅为 WorkerId 的分配和注册提供支持。
支持多种存储机制(SQL Server、Redis 等),可轻松集成到任何使用 Snowflake ID 的库中。
入门指南
SnowflakeId AutoRegister 是一个库,提供了一种简单的方法在 SnowflakeId 中自动注册 WorkerId。
它本身不生成 Snowflake Id,只帮助你自动注册 WorkerId。
核心特点
- 多存储机制支持:Redis、SQL Server、MySQL 等
- 轻量级设计:无依赖,运行时动态加载驱动
- 灵活配置:通过链式 API 自定义注册逻辑
- 高兼容性:支持 .NET Standard 2.0,可在多种平台运行
- 简化开发流程:减少手动维护 WorkerId 的复杂性
注意
- 为了兼容多种驱动以及多种版本,不包含任何驱动,避免过多依赖,运行时动态加载驱动
安装
安装核心包
使用 NuGet 包管理工具快速安装:
Install-Package SnowflakeId.AutoRegister
可选存储支持
Redis 存储支持:
Install-Package SnowflakeId.AutoRegister.Redis
SQL Server 存储支持:
Install-Package SnowflakeId.AutoRegister.SqlServer
注意:需自行安装 SQL Server 驱动:
Microsoft.Data.SqlClient
、System.Data.SqlClient
一般情况业务上都有安装对应驱动,如果没有安装,请自行安装Install-Package Microsoft.Data.SqlClient
或
Install-Package System.Data.SqlClient
MySQL 存储支持:
Install-Package SnowflakeId.AutoRegister.MySql
注意:需自行安装 MySQL 驱动
MySql.Data
、MySqlConnector
一般情况业务上都有安装对应驱动,如果没有安装,请自行安装Install-Package MySql.Data
或
-
Install-Package MySqlConnector
快速开始
以下是使用 SnowflakeId.AutoRegister 的基本示例
初始化 AutoRegister
实例
使用 AutoRegisterBuilder
构建一个单例实例:
static readonly IAutoRegister AutoRegister = new AutoRegisterBuilder()
// 注册选项
// 使用以下行设置标识符。
// 推荐设置以区分单台机器上的多个应用程序
.SetExtraIdentifier(Environment.CurrentDirectory)
// 区分同路径exe,多个进程
// .SetExtraIdentifier(Environment.CurrentDirectory + Process.GetCurrentProcess().Id)S
// 使用以下行设置 WorkerId 范围。
.SetWorkerIdScope(1, 31)
// 使用以下行设置注册选项。
// .SetRegisterOption(option => {})
// 使用以下行使用默认存储。
// 仅适用于开发使用、本地测试等。
//.UseDefaultStore()
// 使用以下行使用 Redis 存储。
.UseRedisStore("localhost:6379,allowAdmin=true")
// 使用以下行使用 SQL Server 存储。
//.UseSqlServerStore("Server=localhost;Database=SnowflakeTest;User Id=sa;Password=123456;")
// Use the following line to use the MySQL store.
.UseMySqlStore("Server=localhost;Port=3306;Database=snowflaketest;Uid=test;Pwd=123456;SslMode=None;")
.Build();
注册 WorkerId
通过 AutoRegister
实例获取 WorkerId
配置:
// 注册 WorkerId。
SnowflakeIdConfig config = AutoRegister.Register();
Console.WriteLine($"WorkerId: {config.WorkerId}");
程序退出时注销 WorkerId
在程序退出时,主动注销 WorkerId,确保资源释放:
//主动注销WorkId,程序退出时调用
//如果程序异常退出,下次启动时会自动尝试获取上次的WorkerId,如果获取失败会重新注册
AutoRegister.UnRegister();
//可以使用AppDomain.CurrentDomain.ProcessExit事件
AppDomain.CurrentDomain.ProcessExit += (_, _) =>
{
builder.UnRegister();
Console.WriteLine("Unregistered.");
};
//.Net Core及以上版本可以使用ApplicationStopping事件
applicationLifetime.ApplicationStopping.Register(() =>
{
builder.UnRegister();
Console.WriteLine("Unregistered.");
});
集成其他 Snowflake ID 库
Yitter.IdGenerator
以下是集成 Yitter.IdGenerator 的示例:
var config = AutoRegister.Register();
var options = new IdGeneratorOptions
{
WorkerId = (ushort)config.WorkerId,
};
IIdGenerator idGenInstance = new DefaultIdGenerator(options);
long id = idGenInstance.NewLong();
Console.WriteLine($"Id: {id}");
对于其他 Snowflake ID 生成库,可以参考上述示例进行集成。
常见问题 (FAQ)
Q: 如果程序崩溃了,WorkerId 会被释放吗?
A: 不会。程序异常退出时,下次启动会尝试分配上一次的 WorkerId。如果失败,则重新注册新的 WorkerId。
Q: 如何避免多进程重复分配 WorkerId?
A: 在 SetExtraIdentifier 中添加进程相关的标识符,例如当前进程 ID。
Q: 默认存储机制适合生产环境吗?
A: 默认存储机制仅适合开发和本地测试。在生产环境中,建议使用 Redis、SQL Server 或 MySQL 存储。
贡献指南
欢迎提交拉取请求!在贡献代码前,请遵循以下步骤:
- Fork 本仓库并创建新分支。
- 确保代码通过所有测试,并保持与主分支同步。
- 如果有重大更改,请先打开一个 Issue 讨论你想要更改的内容。 请确保适当更新测试。
- 提交 PR 并描述所做的更改。
构建源码
克隆仓库:
git clone https://github.com/LemonNoCry/SnowflakeId.AutoRegister.git
导航到项目目录:
cd SnowflakeId.AutoRegister
恢复包:
dotnet restore
构建项目:
dotnet build
许可证
This project is licensed under the MIT License.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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 was computed. 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. |
.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. |
-
.NETStandard 2.0
- SnowflakeId.AutoRegister (>= 1.0.2)
- StackExchange.Redis (>= 2.8.22)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on SnowflakeId.AutoRegister.StackExchangeRedis:
Package | Downloads |
---|---|
IMEX.CORE
Package Description |
|
WWB.NET
NET基础框架 |
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on SnowflakeId.AutoRegister.StackExchangeRedis:
Repository | Stars |
---|---|
anjoy8/Blog.Core
💖 ASP.NET Core 8.0 全家桶教程,前后端分离后端接口,vue教程姊妹篇,官方文档:
|