SnowFlake.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. namespace Business.Core.Utilities
  2. {
  3. /// <summary>
  4. /// 雪花算法生成id
  5. /// </summary>
  6. public class SnowFlake
  7. {
  8. /// <summary>
  9. /// 机器id所占的位数
  10. /// </summary>
  11. private const int workerIdBits = 5;
  12. /// <summary>
  13. /// 数据表述id所占位数
  14. /// </summary>
  15. private const int datacenterIdBits = 5;
  16. /// <summary>
  17. /// 支持的最大机器id,结果是31
  18. /// </summary>
  19. private const long maxWorkerId = -1L ^ (-1L << workerIdBits);
  20. /// <summary>
  21. /// 支持的最大数据表示id,结果是31
  22. /// </summary>
  23. private const long maxDatacenterId = -1L ^ (-1L << datacenterIdBits);
  24. /// <summary>
  25. /// 序列在id中所占的位数
  26. /// </summary>
  27. private const int sequenceBits = 12;
  28. /// <summary>
  29. /// 数据标识id向左移17位
  30. /// </summary>
  31. private const int datacenterIdShift = sequenceBits + workerIdBits;
  32. /// <summary>
  33. /// 机器ID向左移12位
  34. /// </summary>
  35. private const int workerIdShift = sequenceBits;
  36. private const int timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
  37. /// <summary>
  38. /// 生成序列的掩码
  39. /// </summary>
  40. private const long sequenceMask = -1L ^ (-1L << sequenceBits);
  41. /// <summary>
  42. /// 数据中心ID(0-31)
  43. /// </summary>
  44. public long DatacenterId { get; private set; }
  45. /// <summary>
  46. /// 工作机器ID(0-31)
  47. /// </summary>
  48. public long WorkerId { get; private set; }
  49. /// <summary>
  50. /// 毫秒内序列(0-4095)
  51. /// </summary>
  52. public long Sequence { get; private set; }
  53. /// <summary>
  54. /// 上次生成ID的时间戳
  55. /// </summary>
  56. public long LastTimestamp { get; private set; }
  57. /// <summary>
  58. /// 开始时间戳。首次使用前设置,否则无效,默认2010-1-1
  59. /// </summary>
  60. public DateTime StartTimestamp { get; set; } = new DateTime(2010,1,1);
  61. static object syncRoot = new object();
  62. static readonly Lazy<SnowFlake> snowflake = new(() => new SnowFlake(0L,0L));
  63. /// <summary>
  64. /// 默认金泰实例,WorkerId = 0,DatacenterId = 0
  65. /// </summary>
  66. public static SnowFlake Instance { get; } = snowflake.Value;
  67. /// <summary>
  68. /// 构造函数
  69. /// </summary>
  70. /// <param name="workerId">工作机器ID</param>
  71. /// <param name="datacenterId">数据中心ID</param>
  72. /// <exception cref="ArgumentOutOfRangeException"></exception>
  73. public SnowFlake(long workerId,long datacenterId)
  74. {
  75. if (workerId > maxWorkerId || workerId < 0)
  76. {
  77. throw new ArgumentOutOfRangeException(nameof(workerId),$"不能大于{maxWorkerId}或小于0");
  78. }
  79. if (datacenterId > maxDatacenterId || datacenterId < 0)
  80. {
  81. throw new ArgumentOutOfRangeException(nameof(datacenterId), $"不能大于{maxDatacenterId}或小于0");
  82. }
  83. WorkerId = workerId;
  84. DatacenterId = datacenterId;
  85. Sequence = 0L;
  86. LastTimestamp = -1L;
  87. }
  88. /// <summary>
  89. /// 获取下一个ID
  90. /// </summary>
  91. /// <returns></returns>
  92. public long NextId()
  93. {
  94. lock (syncRoot)
  95. {
  96. //获取当前时间戳
  97. long timestamp = GetCurrentTimestamp();
  98. if (timestamp > LastTimestamp)//时间戳改变,毫秒内序列重置
  99. {
  100. Sequence = 0L;
  101. }
  102. else if (timestamp == LastTimestamp)//如果是同一时间生成的,则进行毫秒内序列
  103. {
  104. Sequence = (Sequence + 1) & sequenceMask;
  105. if (Sequence == 0)//毫秒内序列溢出
  106. {
  107. timestamp = GetNextTimestamp(LastTimestamp);//阻塞到下一个毫秒,获取新的时间戳
  108. }
  109. }
  110. else//当前时间小于上一次ID生成的时间戳,证明系统时钟被回拨,此时需要做回拨处理
  111. {
  112. Sequence = (Sequence + 1) & sequenceMask;
  113. if (Sequence > 0)
  114. {
  115. timestamp = LastTimestamp;//停留再最后一次时间戳上,等待系统时间追上后即完全度过了时钟回拨问题
  116. }
  117. else {
  118. timestamp = LastTimestamp + 1;//直接进位到下一个毫秒
  119. }
  120. }
  121. LastTimestamp = timestamp;//上次生成ID的时间戳
  122. //移动并通过或运算拼到一起组成64位的ID
  123. var id = (timestamp << timestampLeftShift)
  124. | (DatacenterId << datacenterIdShift)
  125. | (WorkerId << workerIdShift)
  126. | Sequence;
  127. return id;
  128. }
  129. }
  130. /// <summary>
  131. /// 阻塞到下一个毫秒,直到获得新的时间戳
  132. /// </summary>
  133. /// <param name="lastTimestamp"></param>
  134. /// <returns></returns>
  135. private long GetNextTimestamp(long lastTimestamp)
  136. {
  137. //获取当前时间戳
  138. long timestamp = GetCurrentTimestamp();
  139. while (timestamp <= lastTimestamp)
  140. {
  141. timestamp = GetCurrentTimestamp();
  142. }
  143. return timestamp;
  144. }
  145. /// <summary>
  146. /// 获取当前时间戳
  147. /// </summary>
  148. /// <returns></returns>
  149. private long GetCurrentTimestamp()
  150. {
  151. return (long)(DateTime.Now - StartTimestamp).TotalMilliseconds;
  152. }
  153. }
  154. }