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.
#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 67 2/24/2025
0.0.1-alpha-202502101554 76 2/10/2025
0.0.1-alpha-202502101448 72 2/10/2025
0.0.1-alpha-202502101434 64 2/10/2025
0.0.1-alpha-202501130258 67 1/13/2025
0.0.1-alpha-202412311524 84 12/31/2024
0.0.1-alpha-202412061617 75 12/6/2024
0.0.1-alpha-202412051527 67 12/5/2024
0.0.1-alpha-202412051431 67 12/5/2024
0.0.1-alpha-202412041445 65 12/4/2024
0.0.1-alpha-202412021409 65 12/2/2024
0.0.1-alpha-202411301019 66 11/30/2024
0.0.1-alpha-202411170525 68 11/17/2024
0.0.1-alpha-202411161308 62 11/16/2024
0.0.1-alpha-202411131604 71 11/13/2024
0.0.1-alpha-202411111439 78 11/11/2024
0.0.1-alpha-202411051434 60 11/5/2024
0.0.1-alpha-202410281339 64 10/28/2024
0.0.1-alpha-202410131500 81 10/13/2024
0.0.1-alpha-202407261457 78 7/26/2024
0.0.1-alpha-202407261325 68 7/26/2024
0.0.1-alpha-202406271301 68 6/27/2024
0.0.1-alpha-202406251508 69 6/25/2024
0.0.1-alpha-202406251310 66 6/25/2024
0.0.1-alpha-202406141611 73 6/14/2024
0.0.1-alpha-202406141550 65 6/14/2024
0.0.1-alpha-202406121515 69 6/12/2024
0.0.1-alpha-202406061553 77 6/6/2024
0.0.1-alpha-202406041519 66 6/4/2024
0.0.1-alpha-202406011613 72 6/1/2024
0.0.1-alpha-202406011238 71 6/1/2024
0.0.1-alpha-202405311458 63 5/31/2024
0.0.1-alpha-202405291213 77 5/29/2024
0.0.1-alpha-202405190457 72 5/19/2024
0.0.1-alpha-202405161229 60 5/16/2024
0.0.1-alpha-202405141510 67 5/14/2024
0.0.1-alpha-202405101323 72 5/10/2024
0.0.1-alpha-202405081356 80 5/8/2024
0.0.1-alpha-202405021337 40 5/2/2024
0.0.1-alpha-202405021336 40 5/2/2024
0.0.1-alpha-202405020452 54 5/2/2024
0.0.1-alpha-202405011443 62 5/1/2024
0.0.1-alpha-202404291541 69 4/29/2024
0.0.1-alpha-202404281218 67 4/28/2024