Jordium.Snowflake.NET 1.0.0

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package Jordium.Snowflake.NET --version 1.0.0
                    
NuGet\Install-Package Jordium.Snowflake.NET -Version 1.0.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="Jordium.Snowflake.NET" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Jordium.Snowflake.NET" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Jordium.Snowflake.NET" />
                    
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 Jordium.Snowflake.NET --version 1.0.0
                    
#r "nuget: Jordium.Snowflake.NET, 1.0.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 Jordium.Snowflake.NET@1.0.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=Jordium.Snowflake.NET&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Jordium.Snowflake.NET&version=1.0.0
                    
Install as a Cake Tool

Jordium.Snowflake.NET

NuGet .NET License

�����ֲܷ�ʽ ID ������������ Twitter Snowflake �㷨�� .NET ʵ�֡�֧������ʵ�ַ�ʽ�������ڲ�ͬ��Ӧ�ó�����

����

  • ? ȫ��Ψһ��֧�ֶ��������ġ����������
  • ? ���Ƶ�����ID ��ʱ���������֧�����ݿ������Ż�
  • ? ���������������ɴ� 200 �� - 2000 �� ID/��
  • ? ����ʵ����Ư���㷨����ͳ�㷨�������㷨
  • ? ʱ��ز��������Զ�����ϵͳʱ��ز�����
  • ? ���������֧���Զ���λ������

��װ

NuGet CLI

Install-Package Jordium.Snowflake.NET

.NET CLI

dotnet add package Jordium.Snowflake.NET

PackageReference

<PackageReference Include="Jordium.Snowflake.NET" Version="1.0.0" />

���ٿ�ʼ

�����÷�

using Jordium.Framework.Snowflake;

// ���� ID ������
var options = new IDGeneratorOptions
{
    WorkerId = 1,        // ���� ID (0-31)
    DataCenterId = 1,    // �������� ID (0-31)
    Method = 1           // �㷨�汾 (1=Ư��, 2=��ͳ, 3=����)
};

var generator = new DefaultIDGenerator(options);

// ���� ID
long id = generator.NewLong();
Console.WriteLine($"Generated ID: {id}");

�߼�����

var options = new IDGeneratorOptions
{
    WorkerId = 1,
    DataCenterId = 1,
    Method = 1,
    
    // �Զ���λ������
    WorkerIdBitLength = 5,      // Worker ID λ�� (Ĭ�� 5)
    DataCenterIdBitLength = 5,  // �������� ID λ�� (Ĭ�� 5)
    SeqBitLength = 12,          // ���к�λ�� (Ĭ�� 12)
    
    // ��׼ʱ�䣨���ڼ���ʱ�����
    BaseTime = new DateTime(2020, 1, 1, 0, 0, 0, DateTimeKind.Utc),
    
    // ���кŷ�Χ
    MinSeqNumber = 0,
    MaxSeqNumber = 4095,  // 2^12 - 1
    
    // Ư���㷨ר�ã����Ư�ƴ���
    TopOverCostCount = 2000
};

var generator = new DefaultIDGenerator(options);

ASP.NET Core ����ע��

// Program.cs �� Startup.cs
public void ConfigureServices(IServiceCollection services)
{
    // ע��Ϊ����
    services.AddSingleton<IIDGenerator>(sp =>
    {
        var options = new IDGeneratorOptions
        {
            WorkerId = 1,
            DataCenterId = 1,
            Method = 1
        };
        return new DefaultIDGenerator(options);
    });
}

// Controller ��ʹ��
public class OrderController : ControllerBase
{
    private readonly IIDGenerator _idGenerator;

    public OrderController(IIDGenerator idGenerator)
    {
        _idGenerator = idGenerator;
    }

    [HttpPost]
    public IActionResult CreateOrder()
    {
        long orderId = _idGenerator.NewLong();
        // ʹ�����ɵ� ID
        return Ok(new { OrderId = orderId });
    }
}

�㷨�Ա�

����ʵ�ַ�ʽ

�㷨�汾 ʵ���� ���ó��� ���� ʱ��ز����� �Ƽ���
Method 1 SnowflakeWorkerV1 ����Ӧ�á��߲������� ???? ? �������к� ?????
Method 2 SnowflakeWorkerV2 ͨ�ó�������׼ʵ�� ???? ?? �׳��쳣 ???
Method 3 SnowflakeWorkerV3 �ֲ�ʽ��Ⱥ��΢���� ????? ?? ����ز����쳣 ????

��ϸ˵��

Method 1 - Ư���㷨���Ƽ���

�ص���

  • ? �Զ�����ʱ��ز���ʹ��Ԥ�����к� 1-4��
  • ? ���к����ʱ�Զ�"Ư��"��δ��ʱ��
  • ? �����Ƶ��¼��ص�����
  • ?? ʹ�� lock ͬ��

������

  • ����Ӧ�ã����߳̾���ͬһ�� WorkerId��
  • ��ʱ��ز���е�ҵ��
  • ��Ҫ��� ID ����״̬��ϵͳ

����ʾ����

var options = new IDGeneratorOptions
{
    WorkerId = 1,
    DataCenterId = 1,
    Method = 1,
    TopOverCostCount = 2000  // ���Ư�ƴ���
};

var generator = new DefaultIDGenerator(options);

// ��ѡ�������¼�
generator.GenIdActionAsync = arg =>
{
    if (arg.ActionType == 1) // ��ʼƯ��
    {
        Console.WriteLine($"��ʼƯ��: WorkerId={arg.WorkerId}");
    }
};

Method 2 - ��ͳ�㷨

�ص���

  • ? ��׼ Snowflake ʵ��
  • ? �߼��򵥣��������
  • ?? ʱ��ز�ʱ�׳��쳣
  • ?? ʹ�� lock ͬ��

������

  • ѧϰ����� Snowflake �㷨
  • ʱ��ز����ٵ��ȶ�����
  • ����Ҫ���ӹ��ܵļ򵥳���

����ʾ����

var options = new IDGeneratorOptions
{
    WorkerId = 1,
    DataCenterId = 1,
    Method = 2
};

var generator = new DefaultIDGenerator(options);

try
{
    long id = generator.NewLong();
}
catch (Exception ex)
{
    // ʱ��ز�ʱ���׳��쳣
    Console.WriteLine($"ʱ��ز�����: {ex.Message}");
}

Method 3 - �����㷨�������ܣ�

�ص���

  • ? ʹ�� CAS �����������������
  • ? ʱ���������״̬�У�����ϵͳ����
  • ? �ڷֲ�ʽ������������������
  • ?? ��ʱ��ز��Զ����ߵȴ�
  • ?? ����ز���> 1000ms���׳��쳣

������

  • ΢����Ⱥ��ÿ��������� WorkerId��
  • Kubernetes ����ÿ�� Pod ���� WorkerId��
  • �߲����ֲ�ʽϵͳ
  • ׷�������ܵij���

����ʾ����

var options = new IDGeneratorOptions
{
    WorkerId = 1,
    DataCenterId = 1,
    Method = 3
};

var generator = new DefaultIDGenerator(options);

// �����㷨�ڶ� WorkerId �������������
// ʾ����΢����Ⱥ
// Service 1: WorkerId = 1
// Service 2: WorkerId = 2
// Service 3: WorkerId = 3
// ...

���ܶԱ�

�� WorkerId ����������8 �̣߳�
�㷨 ������ ˵��
Method 1 600-800 �� ID/�� Lock �ڵ��������±����ȶ�
Method 2 500-700 �� ID/�� ��׼ʵ�֣������е�
Method 3 400-600 �� ID/�� CAS ��ͻ���������½�

�������� WorkerId �߲����������Ƽ� Method 1


�� WorkerId ����������8 �� WorkerId��
�㷨 ������ ˵��
Method 1 800-1200 �� ID/�� Lock �й̶�����
Method 2 700-1100 �� ID/�� ��׼ʵ��
Method 3 1200-1800 �� ID/�� ������������ ?

�������� WorkerId �ֲ�ʽ�������Ƽ� Method 3

���ܲ��Ա���

���Ի���

  • CPU: Intel Core i7-12700K (12 �� 20 �߳�) @ 3.6GHz
  • �ڴ�: 32GB DDR4 3200MHz
  • ����ϵͳ: Windows 11 Pro
  • .NET �汾: .NET 8.0
  • ���Է�ʽ: Release ģʽ���޵�����

�������ܲ��ԣ�Method 1��

�߳��� ������ ƽ���ӳ� ��ע
1 1200-1500 �� ID/�� 0.067-0.083 ��s ���߳��޾���
2 900-1200 �� ID/�� 0.167-0.222 ��s ��Ⱦ���
4 700-900 �� ID/�� 0.444-0.571 ��s �жȾ���
8 600-800 �� ID/�� 1.000-1.333 ��s �߶Ⱦ���

�ֲ�ʽ���ܲ��ԣ�Method 3��

WorkerId ���� ÿ�������� �������� ��������
2 50,000 1000-1300 �� ID/�� +10-15%
4 25,000 1200-1500 �� ID/�� +35-45%
8 12,500 1400-1800 �� ID/�� +50-60%

��ȷ�Բ���

������ �������� �ظ��� ���
�� WorkerId ���� 100 �� 0 ? ͨ��
�� WorkerId ���� 100 �� 0 ? ͨ��
���޲�����32 �̣߳� 320 �� 0 ? ͨ��
����ѹ����5 �룩 1000 ��+ 0 ? ͨ��

�ȶ��Բ���

  • ? ���򼶲������������� 100 �� ID�����ظ�
  • ? ����ѹ��������5 ��������ɣ����ظ�
  • ? ���������IJ�����3 ���������� �� 3 �� Worker�����ظ�
  • ? ʱ��ز�������Method 1 �Զ������Method 2/3 ��ȷ���쳣

����˵��

ע����ʵ�������ܶ�������Ӱ�죺

  • CPU �ͺź���Ƶ
  • �ڴ��ٶ�
  • ����ϵͳ����
  • .NET ����ʱ�汾
  • �Ƿ��� Debug/Release ģʽ
  • ϵͳ�������

��������Ϊ���Ͳ��Ի����µIJο�ֵ��ʵ�ʲ��𻷾������� ��30% �IJ����� �Ƽ���ʵ��Ӳ�������½��л�׼�����Ի��׼ȷ���ݡ�

ID �ṹ

��׼ 64 λѩ�� ID

����������������������������������������������������������������������������������������������������������������������
�� ʱ��� (41 λ)   �� ��������(5λ)�� ����ID (5λ) �� ���к�(12λ)  ��
�������������������������������������ة��������������������������ة��������������������������ة�����������������������������
  Լ 69 ��          32 ��        32 ̨        4096/����

λ������ʾ��

���� Worker λ DC λ Seq λ ����
��׼ 5 5 12 32 DC �� 32 Worker �� 4096/ms
���������� 4 6 12 64 DC �� 16 Worker �� 4096/ms
����� 6 4 12 16 DC �� 64 Worker �� 4096/ms
�Ͳ��� 5 5 10 32 DC �� 32 Worker �� 1024/ms

Լ��������

  • Worker λ + DC λ + Seq λ �� 22
  • Seq λ �� 12

��������

1. ���ѡ�� WorkerId��

�Ƽ�������

  • ����Ӧ�ã��̶�ֵ���� 1��
  • ΢���񣺸��ݷ�������ϣȡģ
  • K8s��ʹ�� Pod ��ţ�StatefulSet��
  • �������ģ������÷����ȡ
// ʾ�����ӻ���������ȡ
var workerId = Environment.GetEnvironmentVariable("WORKER_ID");
var options = new IDGeneratorOptions
{
    WorkerId = ushort.Parse(workerId ?? "1"),
    DataCenterId = 1,
    Method = 1
};

2. ʱ��ز���δ����

�㷨 �����ʽ
Method 1 ʹ��Ԥ�����к� 1-4�����֧�� 4 �λز�
Method 2 ֱ���׳��쳣
Method 3 ��ʱ�䣨< 1�룩���ߵȴ�����ʱ�����쳣

3. ��α�֤�ֲ�ʽΨһ�ԣ�

ȷ��ÿ�������� WorkerId �� DataCenterId ���Ψһ���ɣ�

// ����ʾ�� ?
// Server 1: WorkerId=1, DataCenterId=1
// Server 2: WorkerId=1, DataCenterId=1  �� ������ظ� ID

// ��ȷʾ�� ?
// Server 1: WorkerId=1, DataCenterId=1
// Server 2: WorkerId=2, DataCenterId=1
// Server 3: WorkerId=1, DataCenterId=2

4. ���ܲ���Ԥ����ô�죿

����嵥��

  • ? ʹ�õ���ģʽ��AddSingleton��
  • ? ����Ƶ������ DefaultIDGenerator ʵ��
  • ? �����߲���ѡ�� Method 1
  • ? �ֲ�ʽ����ѡ�� Method 3
  • ? ��� WorkerId �Ƿ��ظ�
  • ? ʹ�� Release ģʽ����
  • ? ȷ��ϵͳʱ���ȶ�

���֤

MIT License

��ϵ����

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

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
1.2.2 458 11/19/2025
1.2.1 407 11/19/2025
1.2.0 363 11/17/2025
1.0.2 256 11/16/2025
1.0.1 191 11/15/2025
1.0.0 196 11/15/2025

Initial release with support for .NET 8.0, .NET 6.0, and .NET Standard 2.0