一、.NET 版本
using System; namespace Snowflake { class Program { private static readonly IdWorker Worker = new IdWorker(1, 1); // 大併發的情況下,減少new的次數可以有效避免重複的可能 static void Main(string[] args) { // var worker = new IdWorker(1, 1); long id = Worker.NextId(); Console.WriteLine($"生成的ID爲:{id},他的長度是:{id.ToString().Length}"); Console.ReadKey(); } } public class IdWorker { //基準時間 public const long Twepoch = 1288834974657L; //機器標識位數 const int WorkerIdBits = 5; //數據標誌位數 const int DatacenterIdBits = 5; //序列號識位數 const int SequenceBits = 12; //機器ID最大值 const long MaxWorkerId = -1L ^ (-1L << WorkerIdBits); //數據標誌ID最大值 const long MaxDatacenterId = -1L ^ (-1L << DatacenterIdBits); //序列號ID最大值 private const long SequenceMask = -1L ^ (-1L << SequenceBits); //機器ID偏左移12位 private const int WorkerIdShift = SequenceBits; //數據ID偏左移17位 private const int DatacenterIdShift = SequenceBits + WorkerIdBits; //時間毫秒左移22位 public const int TimestampLeftShift = SequenceBits + WorkerIdBits + DatacenterIdBits; private long _sequence = 0L; private long _lastTimestamp = -1L; public long WorkerId { get; protected set; } public long DatacenterId { get; protected set; } public long Sequence { get { return _sequence; } internal set { _sequence = value; } } public IdWorker(long workerId, long datacenterId, long sequence = 0L) { // 如果超出範圍就拋出異常 if (workerId > MaxWorkerId || workerId < 0) { throw new ArgumentException(string.Format("worker Id 必須大於0,且不能大於MaxWorkerId: {0}", MaxWorkerId)); } if (datacenterId > MaxDatacenterId || datacenterId < 0) { throw new ArgumentException(string.Format("region Id 必須大於0,且不能大於MaxWorkerId: {0}", MaxDatacenterId)); } //先檢驗再賦值 WorkerId = workerId; DatacenterId = datacenterId; _sequence = sequence; } readonly object _lock = new Object(); public virtual long NextId() { lock (_lock) { var timestamp = TimeGen(); if (timestamp < _lastTimestamp) { throw new Exception(string.Format("時間戳必須大於上一次生成ID的時間戳. 拒絕爲{0}毫秒生成id", _lastTimestamp - timestamp)); } // 如果上次生成時間和當前時間相同,在同一毫秒內 if (_lastTimestamp == timestamp) { // sequence自增,和sequenceMask相與一下,去掉高位 _sequence = (_sequence + 1) & SequenceMask; // 判斷是否溢出,也就是每毫秒內超過1024,當爲1024時,與sequenceMask相與,sequence就等於0 if (_sequence == 0) { // 等待到下一毫秒 timestamp = TilNextMillis(_lastTimestamp); } } else { // 如果和上次生成時間不同,重置sequence,就是下一毫秒開始,sequence計數重新從0開始累加, // 爲了保證尾數隨機性更大一些,最後一位可以設置一個隨機數 _sequence = 0;//new Random().Next(10); } _lastTimestamp = timestamp; return ((timestamp - Twepoch) << TimestampLeftShift) | (DatacenterId << DatacenterIdShift) | (WorkerId << WorkerIdShift) | _sequence; } } // 防止產生的時間比之前的時間還要小(由於NTP回撥等問題),保持增量的趨勢. protected virtual long TilNextMillis(long lastTimestamp) { var timestamp = TimeGen(); while (timestamp <= lastTimestamp) { timestamp = TimeGen(); } return timestamp; } // 獲取當前的時間戳 protected virtual long TimeGen() { return TimeExtensions.CurrentTimeMillis(); } } public static class TimeExtensions { public static Func<long> currentTimeFunc = InternalCurrentTimeMillis; public static long CurrentTimeMillis() { return currentTimeFunc(); } public static IDisposable StubCurrentTime(Func<long> func) { currentTimeFunc = func; return new DisposableAction(() => { currentTimeFunc = InternalCurrentTimeMillis; }); } public static IDisposable StubCurrentTime(long millis) { currentTimeFunc = () => millis; return new DisposableAction(() => { currentTimeFunc = InternalCurrentTimeMillis; }); } private static readonly DateTime Jan1st1970 = new DateTime (1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); private static long InternalCurrentTimeMillis() { return (long)(DateTime.UtcNow - Jan1st1970).TotalMilliseconds; } } public class DisposableAction : IDisposable { readonly Action _action; public DisposableAction(Action action) { if (action == null) throw new ArgumentNullException("action"); _action = action; } public void Dispose() { _action(); } } }
源碼:https://github.com/dunitian/snowflake-net
博客:https://www.cnblogs.com/dunitian/p/6130543.html