YitIdHelperExtension.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using Microsoft.Extensions.Hosting;
  2. namespace Admin.NET.Core;
  3. /// <summary>
  4. /// YitIdHelper 自动获取WorkId拓展
  5. /// </summary>
  6. public static class YitIdHelperExtension
  7. {
  8. private const int MinWorkId = 0;
  9. private const int MaxWorkId = 63;
  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. int milliseconds = ran.Next(10, 1000);
  30. Thread.Sleep(milliseconds);
  31. SetWorkId();
  32. }
  33. private static void SetWorkId()
  34. {
  35. var lockName = $"{_options.CachePrefix}{MainLockName}";
  36. var valueKey = $"{_options.CachePrefix}{MainValueKey}"; ;
  37. var client = App.GetService<ICache>();
  38. var redisLock = client.AcquireLock(lockName, 10000, 15000, true);
  39. var keys = client.Keys.Where(o => o.Contains($"{_options.CachePrefix}{valueKey}:*"));
  40. var tempWorkIds = _workIds;
  41. foreach (var key in keys)
  42. {
  43. var tempWorkId = key[key.LastIndexOf(":", StringComparison.Ordinal)..];
  44. tempWorkIds.Remove(tempWorkId);
  45. }
  46. try
  47. {
  48. string workIdKey = "";
  49. foreach (var item in tempWorkIds)
  50. {
  51. string workIdStr = "";
  52. workIdStr = item;
  53. workIdKey = $"{valueKey}:{workIdStr}";
  54. var exist = client.Get<bool>(workIdKey);
  55. if (exist)
  56. {
  57. workIdKey = "";
  58. continue;
  59. }
  60. Console.WriteLine($"###########当前应用WorkId:【{workIdStr}】###########");
  61. long workId = workIdStr.ParseToLong();
  62. if (workId is < MinWorkId or > MaxWorkId)
  63. continue;
  64. // 设置雪花Id算法机器码
  65. YitIdHelper.SetIdGenerator(new IdGeneratorOptions
  66. {
  67. WorkerId = (ushort)workId,
  68. WorkerIdBitLength = _options.WorkerIdBitLength,
  69. SeqBitLength = _options.SeqBitLength
  70. });
  71. client.Set(workIdKey, true, TimeSpan.FromSeconds(15));
  72. break;
  73. }
  74. if (string.IsNullOrWhiteSpace(workIdKey)) throw Oops.Oh("未设置有效的机器码,启动失败");
  75. // 开一个任务设置当前workId过期时间
  76. Task.Run(() =>
  77. {
  78. while (true)
  79. {
  80. client.SetExpire(workIdKey, TimeSpan.FromSeconds(15));
  81. //Task.Delay(5000);
  82. Thread.Sleep(10000);
  83. }
  84. });
  85. }
  86. catch (Exception e)
  87. {
  88. throw Oops.Oh($"{e.Message};{e.StackTrace};{e.StackTrace}");
  89. }
  90. finally
  91. {
  92. redisLock?.Dispose();
  93. }
  94. }
  95. }