阅读量:0
Snowflake ID 是一种分布式系统中生成唯一 ID 的算法,由 Twitter 开源。它可以在不依赖数据库或其他存储设备的情况下生成全局唯一的 ID。Snowflake ID 通常是一个 64 位的整数,由以下部分组成:
- 前 1 位未使用,任意填充。
- 41 位表示时间戳。
- 10 位表示工作机器 ID,可通过部署多个 Snowflake 服务。
- 12 位序列号,单节点每毫秒内可以生成 4096 个 ID。
以下是一个简单的 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
关键字来实现同步。