Snowflake算法生成Id

网上大部分C#写的都有点乱糟糟,我简化了一下。这个算法总体思想是生成一个64位整数,前42位放从前某特定时间点到当前时间的毫秒数,中间10位放生成id的机器的唯一编码,后12位放顺序号(如果同一毫秒内生成多次,此顺序号可避免产生重复id)

using System;

namespace xxx
{
    /// <summary>
    /// Id 生成类
    /// </summary>
    class Snowflake
    {
        private const string LOCK_OBJ = "76003AEB-E3F9-460A-BD31-D9AE9E7684C0";

        private const int MACHINE_BIT_SIZE = 10; // 机器编号长度10位
        private const int SEQUENCE_BIT_SIZE = 12; // 序号长度12位

        private static Snowflake _snowflake;

        private long _machineNumber; // 机器序号
        private long _timestamp; // 时间戳
        private long _sequence; // 序号

        private Snowflake() { }

        /// <summary>
        /// 设置机器序号
        /// </summary>
        public int MachineNumber
        {
            set { _machineNumber = value; }
        }

        /// <summary>
        /// 得到一个实例
        /// </summary>
        /// <returns></returns>
        public static Snowflake GetInstance()
        {
            if (_snowflake == null)
            {
                lock (LOCK_OBJ)
                {
                    if (_snowflake == null)
                    {
                        _snowflake = new Snowflake();
                    }
                }
            }
            return _snowflake;
        }

        /// <summary>
        /// 产生一个id,由时间戳、机器编码、顺序号组成
        /// </summary>
        /// <returns></returns>
        public long GenerateId(Func<DateTime> GetTime)
        {
            lock (LOCK_OBJ)
            {
                if (_machineNumber > (-1L ^ -1L << MACHINE_BIT_SIZE))
                {
                    throw new ArgumentException("机器编号超出最大值");
                }

                long timestamp = GetTimestamp(GetTime());
                if (timestamp < _timestamp)
                {
                    throw new ArgumentException("时间戳错误");
                }

                if (timestamp == _timestamp)
                {
                    _sequence++;
                    if (_sequence > (-1L ^ -1L << SEQUENCE_BIT_SIZE))
                    {
                        while (true) // 序号已用完,等下一毫秒
                        {
                            timestamp = GetTimestamp(GetTime());
                            if (timestamp > _timestamp)
                            {
                                _sequence = 0;
                                break;
                            }
                        }
                    }
                }
                else
                {
                    _sequence = 0;
                }

                long id = timestamp << (MACHINE_BIT_SIZE + SEQUENCE_BIT_SIZE) 
                    | _machineNumber << SEQUENCE_BIT_SIZE 
                    | _sequence;

                _timestamp = timestamp;

                return id;
            }
        }

        // 当前时间戳
        private long GetTimestamp(DateTime now)
        {
            return (long)(now - new DateTime(2024, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
        }
    }
}

调用:

using System;

namespace xxx
{
    public class IdGenerator
    {
        /// <summary>
        /// 生成Id
        /// </summary>
        /// <returns></returns>
        public static long GenerateId()
        {
            Snowflake sf = Snowflake.GetInstance();
            sf.MachineNumber = YourGetMachineNumberFunction();
            long id = sf.GenerateId(GetTime);
            return id;
        }

        private static DateTime GetTime()
        {
            return YourGetNowTimeFunction();
        }
    }
}
posted @ 2024-01-15 20:12  会长  阅读(45)  评论(0编辑  收藏  举报