Tenon.DistributedId.Snowflake 0.0.1-alpha-202502241449

This is a prerelease version of Tenon.DistributedId.Snowflake.
dotnet add package Tenon.DistributedId.Snowflake --version 0.0.1-alpha-202502241449
                    
NuGet\Install-Package Tenon.DistributedId.Snowflake -Version 0.0.1-alpha-202502241449
                    
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="Tenon.DistributedId.Snowflake" Version="0.0.1-alpha-202502241449" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Tenon.DistributedId.Snowflake" Version="0.0.1-alpha-202502241449" />
                    
Directory.Packages.props
<PackageReference Include="Tenon.DistributedId.Snowflake" />
                    
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 Tenon.DistributedId.Snowflake --version 0.0.1-alpha-202502241449
                    
#r "nuget: Tenon.DistributedId.Snowflake, 0.0.1-alpha-202502241449"
                    
#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 Tenon.DistributedId.Snowflake@0.0.1-alpha-202502241449
                    
#: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=Tenon.DistributedId.Snowflake&version=0.0.1-alpha-202502241449&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Tenon.DistributedId.Snowflake&version=0.0.1-alpha-202502241449&prerelease
                    
Install as a Cake Tool

Tenon.DistributedId.Snowflake

NuGet version License: MIT

基于 Yitter.IdGenerator 的分布式 ID 生成器实现,为 .NET 应用程序提供高性能、可靠的分布式唯一 ID 生成服务。

✨ 功能特性

  • 🚀 基于 Yitter.IdGenerator 的高性能实现
  • 🔧 支持 Redis 工作节点管理
  • 💉 集成 .NET 依赖注入框架
  • 🎯 自动工作节点注册和注销
  • 🔄 支持工作节点自动刷新
  • 📊 完整的日志监控支持
  • 🛡️ 完善的异常处理机制

📦 安装方式

通过 NuGet 包管理器安装:

dotnet add package Tenon.DistributedId.Snowflake

🚀 快速入门

1. 配置 appsettings.json

{
  "SnowflakeId": {
    "ServiceName": "OrderService",
    "WorkerNode": {
      "Prefix": "distributedId:workerIds:",
      "ExpireTimeInSeconds": 60,
      "RefreshTimeInSeconds": 30,
      "Redis": {
        "ConnectionString": "localhost:6379,defaultDatabase=0"
      }
    }
  }
}

2. 注册服务

// 添加分布式 ID 生成服务
services.AddDistributedId(options =>
{
    // 使用 Snowflake 算法
    options.UseSnowflake(configuration.GetSection("DistributedId"));
    // 使用 StackExchange.Redis 作为工作节点提供者
    options.UseWorkerNode<StackExchangeProvider>(
        configuration.GetSection("DistributedId:WorkerNode"));
});

// 或者使用委托配置
services.AddDistributedId(options => 
{
    options.UseSnowflake(snowflakeOptions => 
    {
        snowflakeOptions.ServiceName = "OrderService";
        snowflakeOptions.WorkerNode = new WorkerNodeOptions 
        {
            Prefix = "distributedId:workerIds:",
            ExpireTimeInSeconds = 60,
            RefreshTimeInSeconds = 30,
            Redis = new RedisOptions 
            {
                ConnectionString = "localhost:6379"
            }
        };
    });
});

3. 使用 ID 生成器

public class OrderService
{
    private readonly IDGenerator _idGenerator;

    public OrderService(IDGenerator idGenerator)
    {
        _idGenerator = idGenerator;
    }

    public long CreateOrderId()
    {
        return _idGenerator.GetNextId();
    }
}

📖 工作节点管理

工作节点配置

public class WorkerNodeOptions
{
    // Redis 键前缀
    public string Prefix { get; set; } = "distributedId:workerIds:";
    
    // 工作节点过期时间(秒)
    public int ExpireTimeInSeconds { get; set; } = 60;
    
    // 工作节点刷新时间(秒)
    public int RefreshTimeInSeconds { get; set; }
    
    // Redis 配置选项
    public RedisOptions Redis { get; set; }
}

工作节点生命周期

// 服务启动时自动注册工作节点
public override async Task StartAsync(CancellationToken cancellationToken)
{
    await _workerNode.RegisterAsync();
    await base.StartAsync(cancellationToken);
}

// 服务停止时自动注销工作节点
public override async Task StopAsync(CancellationToken cancellationToken)
{
    await _workerNode.UnRegisterAsync();
    await base.StopAsync(cancellationToken);
}

⚙️ 配置选项说明

基础配置

配置项 说明 默认值
ServiceName 服务名称(必填) -
WorkerNode.Prefix Redis 键前缀 distributedId:workerIds:
WorkerNode.ExpireTimeInSeconds 工作节点过期时间 60
WorkerNode.RefreshTimeInSeconds 工作节点刷新时间 0

ID 生成器配置

基于 Yitter.IdGenerator 的配置:

  • WorkerIdBitLength: 6 位
  • SeqBitLength: 6 位
  • 最大支持的工作节点数:2^6 = 64 个

🔨 项目依赖

  • Tenon.DistributedId.Abstractions
  • Tenon.Infra.Redis
  • Microsoft.Extensions.DependencyInjection
  • Microsoft.Extensions.Hosting
  • Microsoft.Extensions.Options
  • Yitter.IdGenerator

📝 使用注意事项

1. Redis 配置

  • 确保 Redis 连接可用
  • 合理设置过期时间
  • 配置适当的刷新间隔

2. 工作节点管理

  • 服务名称必须唯一
  • 监控节点注册状态
  • 关注节点过期情况

3. 性能优化

  • 合理设置 WorkerIdBitLength
  • 适当配置 SeqBitLength
  • 避免频繁重启服务

🌰 应用场景示例

1. 订单 ID 生成

public class OrderIdGenerator
{
    private readonly IDGenerator _idGenerator;
    
    public string GenerateOrderId()
    {
        return _idGenerator.GetNextId().ToString("D18");
    }
}

2. 分布式主键生成

public class EntityIdGenerator
{
    private readonly IDGenerator _idGenerator;
    
    public void SetEntityId<T>(T entity) where T : IEntity
    {
        if (entity.Id <= 0)
        {
            entity.Id = _idGenerator.GetNextId();
        }
    }
}

🔍 异常处理

项目定义了两种主要异常类型:

  1. IDGeneratorException

    • ID 生成器异常基类
    • 处理 ID 生成相关的异常
  2. IdGeneratorWorkerNodeException

    • 工作节点异常
    • 处理节点注册、注销等操作异常

🤝 参与贡献

欢迎参与项目贡献!请阅读我们的贡献指南了解如何参与项目开发。

📄 开源协议

本项目采用 MIT 开源协议 - 详情请查看 LICENSE 文件。

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.0.1-alpha-202502241449 96 2/24/2025
0.0.1-alpha-202502101554 92 2/10/2025
0.0.1-alpha-202502101448 89 2/10/2025
0.0.1-alpha-202502101434 85 2/10/2025
0.0.1-alpha-202501130258 83 1/13/2025
0.0.1-alpha-202412311524 103 12/31/2024
0.0.1-alpha-202412061617 94 12/6/2024
0.0.1-alpha-202412051527 85 12/5/2024
0.0.1-alpha-202412051431 86 12/5/2024
0.0.1-alpha-202412041445 81 12/4/2024
0.0.1-alpha-202412021409 81 12/2/2024
0.0.1-alpha-202411301019 83 11/30/2024
0.0.1-alpha-202411170525 86 11/17/2024
0.0.1-alpha-202411161308 84 11/16/2024
0.0.1-alpha-202411131604 93 11/13/2024
0.0.1-alpha-202411111439 93 11/11/2024
0.0.1-alpha-202411051434 78 11/5/2024
0.0.1-alpha-202410281339 83 10/28/2024
0.0.1-alpha-202410131500 97 10/13/2024
0.0.1-alpha-202407261457 96 7/26/2024
0.0.1-alpha-202407261325 84 7/26/2024
0.0.1-alpha-202406271301 86 6/27/2024
0.0.1-alpha-202406251508 87 6/25/2024
0.0.1-alpha-202406251310 90 6/25/2024
0.0.1-alpha-202406141611 89 6/14/2024
0.0.1-alpha-202406141550 81 6/14/2024
0.0.1-alpha-202406121515 89 6/12/2024
0.0.1-alpha-202406061553 92 6/6/2024
0.0.1-alpha-202406041519 86 6/4/2024
0.0.1-alpha-202406011613 86 6/1/2024
0.0.1-alpha-202406011238 87 6/1/2024
0.0.1-alpha-202405311458 77 5/31/2024
0.0.1-alpha-202405291213 97 5/29/2024
0.0.1-alpha-202405190457 89 5/19/2024
0.0.1-alpha-202405161229 78 5/16/2024
0.0.1-alpha-202405141510 88 5/14/2024
0.0.1-alpha-202405101323 85 5/10/2024
0.0.1-alpha-202405081356 97 5/8/2024
0.0.1-alpha-202405021337 55 5/2/2024
0.0.1-alpha-202405021336 62 5/2/2024
0.0.1-alpha-202405020452 73 5/2/2024
0.0.1-alpha-202405011443 75 5/1/2024
0.0.1-alpha-202404291541 84 4/29/2024
0.0.1-alpha-202404281218 84 4/28/2024