Snowflake ID 是一種分布式系統中生成唯一 ID 的算法,由 Twitter 開源。它可以在不依賴數據庫或其他存儲設備的情況下生成全局唯一的 ID。Snowflake ID 通常是一個 64 位的整數,由以下部分組成:
以下是一個簡單的 C# 實現:
using System;
using System.Threading;
public class SnowflakeIdWorker
{
private const long Twepoch = 1288834974657L;
private const int WorkerIdBits = 10;
private const int SequenceBits = 12;
private const long MaxWorkerId = -1L ^ (-1L<< WorkerIdBits);
private const long SequenceMask = -1L ^ (-1L << SequenceBits);
private static readonly object SyncRoot = new object();
private readonly long _workerId;
private long _sequence;
private long _lastTimestamp;
public SnowflakeIdWorker(long workerId)
{
if (workerId > MaxWorkerId || workerId < 0)
throw new ArgumentException($"Worker Id can't be greater than {MaxWorkerId} or less than 0");
_workerId = workerId;
}
public long NextId()
{
lock (SyncRoot)
{
var timestamp = TimeGen();
if (timestamp < _lastTimestamp)
throw new Exception($"Invalid system clock! Refusing to generate id for {_lastTimestamp - timestamp} milliseconds");
if (_lastTimestamp == timestamp)
{
_sequence = (_sequence + 1) & SequenceMask;
if (_sequence == 0) timestamp = TilNextMillis(_lastTimestamp);
}
else
{
_sequence = 0;
}
_lastTimestamp = timestamp;
var id = ((timestamp - Twepoch) << (WorkerIdBits + SequenceBits)) | (_workerId << SequenceBits) | _sequence;
return id;
}
}
protected virtual long TilNextMillis(long lastTimestamp)
{
var timestamp = TimeGen();
while (timestamp <= lastTimestamp) timestamp = TimeGen();
return timestamp;
}
protected virtual long TimeGen()
{
return DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
}
}
要使用此類生成 Snowflake ID,請創建一個 SnowflakeIdWorker
實例并調用 NextId()
方法。例如:
var worker = new SnowflakeIdWorker(1); // Use worker ID 1
var id = worker.NextId();
Console.WriteLine("Generated Snowflake ID: " + id);
注意:這個實現不是線程安全的,如果需要在多線程環境中使用,請確保對 NextId()
方法進行適當的同步。在上面的代碼中,我們使用了 lock
關鍵字來實現同步。