Jelajahi Sumber

😉优化雪花Id算法配置

zuohuaijun 3 tahun lalu
induk
melakukan
5236448fc0

+ 3 - 1
Admin.NET/Admin.NET.Application/Configuration/SnowId.json

@@ -2,6 +2,8 @@
     "$schema": "https://gitee.com/dotnetchina/Furion/raw/net6/schemas/v3/furion-schema.json",
 
     "SnowId": {
-        "WorkerId": 5 // 取值范围0~63,默认1
+        "WorkerId": 1, // 机器码 全局唯一
+        "WorkerIdBitLength": 1, // 机器码位长 默认值6,取值范围 [1, 19]
+        "SeqBitLength": 6 // 序列数位长 默认值6,取值范围 [3, 21](建议不小于4)
     }
 }

+ 11 - 2
Admin.NET/Admin.NET.Application/Service/TestService.cs

@@ -15,7 +15,7 @@ public class TestService : IDynamicApiController, ITransient
     }
 
     /// <summary>
-    /// 测试
+    /// 接口测试
     /// </summary>
     public string GetName()
     {
@@ -23,7 +23,16 @@ public class TestService : IDynamicApiController, ITransient
     }
 
     /// <summary>
-    /// 获取列表
+    /// 增加一条数据
+    /// </summary>
+    /// <returns></returns>
+    public async Task<bool> AddTest()
+    {
+        return await _testRep.InsertAsync(new Test() { Name = "王五" });
+    }
+
+    /// <summary>
+    /// 获取数据列表
     /// </summary>
     /// <returns></returns>
     [HttpGet("/test/list")]

+ 0 - 5
Admin.NET/Admin.NET.Core/Admin.NET.Core.xml

@@ -3784,11 +3784,6 @@
             雪花Id配置选项
             </summary>
         </member>
-        <member name="P:Admin.NET.Core.SnowIdOptions.WorkerId">
-            <summary>
-            机器码
-            </summary>
-        </member>
         <member name="T:Admin.NET.Core.UploadOptions">
             <summary>
             文件上传配置选项

+ 2 - 1
Admin.NET/Admin.NET.Core/GlobalUsings.cs

@@ -47,4 +47,5 @@ global using System.Runtime.InteropServices;
 global using System.Text;
 global using System.Text.RegularExpressions;
 global using System.Web;
-global using UAParser;
+global using UAParser;
+global using Yitter.IdGenerator;

+ 1 - 5
Admin.NET/Admin.NET.Core/Option/SnowIdOptions.cs

@@ -3,10 +3,6 @@
 /// <summary>
 /// 雪花Id配置选项
 /// </summary>
-public sealed class SnowIdOptions : IConfigurableOptions
+public sealed class SnowIdOptions : IdGeneratorOptions, IConfigurableOptions
 {
-    /// <summary>
-    /// 机器码
-    /// </summary>
-    public ushort WorkerId { get; set; }
 }

+ 1 - 1
Admin.NET/Admin.NET.Core/Service/Auth/SysAuthService.cs

@@ -221,7 +221,7 @@ public class SysAuthService : IDynamicApiController, ITransient
     [SuppressMonitor]
     public dynamic GetCaptcha()
     {
-        var codeId = Yitter.IdGenerator.YitIdHelper.NextId();
+        var codeId = YitIdHelper.NextId();
         var captcha = _captcha.Generate(codeId.ToString());
         return new { Id = codeId, Img = captcha.Base64 };
     }

+ 1 - 1
Admin.NET/Admin.NET.Core/Service/File/SysFileService.cs

@@ -187,7 +187,7 @@ public class SysFileService : IDynamicApiController, ITransient
 
         var newFile = new SysFile
         {
-            Id = Yitter.IdGenerator.YitIdHelper.NextId(),
+            Id = YitIdHelper.NextId(),
             // BucketName = _OSSProviderOptions.IsEnable ? _OSSProviderOptions.Provider.ToString() : "Local",
             // 阿里云对bucket名称有要求,1.只能包括小写字母,数字,短横线(-)2.必须以小写字母或者数字开头  3.长度必须在3-63字节之间
             // 无法使用Provider

+ 1 - 1
Admin.NET/Admin.NET.Core/Service/Server/SysServerService.cs

@@ -87,7 +87,7 @@ public class SysServerService : IDynamicApiController, ITransient
     {
         var furionAssembly = typeof(App).Assembly.GetName();
         var sqlSugarAssembly = typeof(ISqlSugarClient).Assembly.GetName();
-        var yitIdAssembly = typeof(Yitter.IdGenerator.YitIdHelper).Assembly.GetName();
+        var yitIdAssembly = typeof(YitIdHelper).Assembly.GetName();
         var redisAssembly = typeof(Redis).Assembly.GetName();
         var jsonAssembly = typeof(NewtonsoftJsonMvcCoreBuilderExtensions).Assembly.GetName();
         var excelAssembly = typeof(IExcelImporter).Assembly.GetName();

+ 1 - 1
Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarSetup.cs

@@ -108,7 +108,7 @@ public static class SqlSugarSetup
                 {
                     var id = entityInfo.EntityColumnInfo.PropertyInfo.GetValue(entityInfo.EntityValue);
                     if (id == null || (long)id == 0)
-                        entityInfo.SetValue(Yitter.IdGenerator.YitIdHelper.NextId());
+                        entityInfo.SetValue(YitIdHelper.NextId());
                 }
                 if (entityInfo.PropertyName == "CreateTime")
                     entityInfo.SetValue(DateTime.Now);

+ 2 - 5
Admin.NET/Admin.NET.Web.Core/Startup.cs

@@ -170,11 +170,8 @@ public class Startup : AppStartup
             });
         }
 
-        // 配置雪花Id算法机器码
-        YitIdHelper.SetIdGenerator(new IdGeneratorOptions
-        {
-            WorkerId = App.GetOptions<SnowIdOptions>().WorkerId
-        });
+        // 雪花Id
+        YitIdHelper.SetIdGenerator(App.GetOptions<SnowIdOptions>());
 
         // 验证码
         services.AddLazyCaptcha();