YitIdHelperExtension.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // 大名科技(天津)有限公司版权所有 电话:18020030720 QQ:515096995
  2. //
  3. // 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证
  4. namespace Admin.NET.Core;
  5. /// <summary>
  6. /// YitIdHelper 自动获取WorkId拓展(支持分布式部署)
  7. /// </summary>
  8. public static class YitIdHelperExtension
  9. {
  10. private const string MainLockName = "IdGen:WorkerId:Lock";
  11. private const string MainValueKey = "IdGen:WorkerId:Value";
  12. private static readonly List<string> _workIds = new();
  13. private static SnowIdOptions _options;
  14. public static void AddYitIdHelper(this IServiceCollection services, SnowIdOptions options)
  15. {
  16. _options = options;
  17. // 排除开发环境和Windows服务器
  18. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || App.WebHostEnvironment.IsDevelopment())
  19. {
  20. YitIdHelper.SetIdGenerator(_options);
  21. return;
  22. }
  23. var maxLength = Math.Pow(2, _options.WorkerIdBitLength.ParseToDouble());
  24. for (int i = 0; i < maxLength; i++)
  25. {
  26. _workIds.Add(i.ToString());
  27. }
  28. Random ran = new();
  29. Thread.Sleep(ran.Next(10, 1000));
  30. SetWorkId();
  31. }
  32. private static void SetWorkId()
  33. {
  34. var lockName = $"{_options.WorkerPrefix}{MainLockName}";
  35. var valueKey = $"{_options.WorkerPrefix}{MainValueKey}";
  36. var minWorkId = 0;
  37. var maxWorkId = Math.Pow(2, _options.WorkerIdBitLength.ParseToDouble());
  38. var cache = App.GetService<ICache>();
  39. var redisLock = cache.AcquireLock(lockName, 10000, 15000, true);
  40. var keys = cache == Cache.Default
  41. ? cache.Keys.Where(u => u.StartsWith($"{_options.WorkerPrefix}{valueKey}:*"))
  42. : ((FullRedis)cache).Search($"{_options.WorkerPrefix}{valueKey}:*", int.MaxValue);
  43. var tempWorkIds = _workIds;
  44. foreach (var key in keys)
  45. {
  46. var tempWorkId = key[key.LastIndexOf(":", StringComparison.Ordinal)..];
  47. tempWorkIds.Remove(tempWorkId);
  48. }
  49. try
  50. {
  51. string workIdKey = "";
  52. foreach (var item in tempWorkIds)
  53. {
  54. var workIdStr = item;
  55. workIdKey = $"{valueKey}:{workIdStr}";
  56. var exist = cache.Get<bool>(workIdKey);
  57. if (exist)
  58. {
  59. workIdKey = "";
  60. continue;
  61. }
  62. Console.WriteLine($"###########当前应用WorkId:【{workIdStr}】###########");
  63. long workId = workIdStr.ParseToLong();
  64. if (workId < minWorkId || workId > maxWorkId)
  65. continue;
  66. // 设置雪花Id算法机器码
  67. YitIdHelper.SetIdGenerator(new IdGeneratorOptions
  68. {
  69. WorkerId = (ushort)workId,
  70. WorkerIdBitLength = _options.WorkerIdBitLength,
  71. SeqBitLength = _options.SeqBitLength
  72. });
  73. cache.Set(workIdKey, true, TimeSpan.FromSeconds(15));
  74. break;
  75. }
  76. if (string.IsNullOrWhiteSpace(workIdKey)) throw Oops.Oh("未设置有效的机器码,启动失败");
  77. // 开一个任务设置当前workId过期时间
  78. Task.Run(() =>
  79. {
  80. while (true)
  81. {
  82. cache.SetExpire(workIdKey, TimeSpan.FromSeconds(15));
  83. // Task.Delay(5000);
  84. Thread.Sleep(10000);
  85. }
  86. });
  87. }
  88. catch (Exception ex)
  89. {
  90. throw Oops.Oh($"{ex.Message};{ex.StackTrace};{ex.StackTrace}");
  91. }
  92. finally
  93. {
  94. redisLock?.Dispose();
  95. }
  96. }
  97. }