diff --git a/Blog.Core.Api/Blog.Core.xml b/Blog.Core.Api/Blog.Core.xml index 8beea22..0b19984 100644 --- a/Blog.Core.Api/Blog.Core.xml +++ b/Blog.Core.Api/Blog.Core.xml @@ -894,6 +894,12 @@ 通过此处的key格式为 xx:xx:x + + + 获取雪花Id + + + WeChatCompanyController diff --git a/Blog.Core.Api/Controllers/ValuesController.cs b/Blog.Core.Api/Controllers/ValuesController.cs index 9d0c155..e8978aa 100644 --- a/Blog.Core.Api/Controllers/ValuesController.cs +++ b/Blog.Core.Api/Controllers/ValuesController.cs @@ -19,6 +19,7 @@ using RabbitMQ.Client.Events; using System.ComponentModel.DataAnnotations; using System.Linq.Expressions; using System.Text; +using Blog.Core.Common.Utlilty; namespace Blog.Core.Controllers { @@ -472,6 +473,17 @@ namespace Blog.Core.Controllers { return _seqOptions.ToJson(); } + + /// + /// 获取雪花Id + /// + /// + [HttpGet] + [AllowAnonymous] + public long GetSnowflakeId() + { + return IdGeneratorUtility.NextId(); + } } public class ClaimDto diff --git a/Blog.Core.Common/Blog.Core.Common.csproj b/Blog.Core.Common/Blog.Core.Common.csproj index aab4f0e..d608975 100644 --- a/Blog.Core.Common/Blog.Core.Common.csproj +++ b/Blog.Core.Common/Blog.Core.Common.csproj @@ -31,12 +31,16 @@ - + + + + + diff --git a/Blog.Core.Common/DB/Aop/SqlsugarAop.cs b/Blog.Core.Common/DB/Aop/SqlsugarAop.cs index 826984e..cd0481f 100644 --- a/Blog.Core.Common/DB/Aop/SqlsugarAop.cs +++ b/Blog.Core.Common/DB/Aop/SqlsugarAop.cs @@ -5,13 +5,15 @@ using StackExchange.Profiling; using System; using Serilog; using Blog.Core.Common.LogHelper; +using Blog.Core.Common.Utlilty; using Blog.Core.Model; namespace Blog.Core.Common.DB.Aop; public static class SqlSugarAop { - public static void OnLogExecuting(ISqlSugarClient sqlSugarScopeProvider, string user, string table, string operate, string sql, SugarParameter[] p, ConnectionConfig config) + public static void OnLogExecuting(ISqlSugarClient sqlSugarScopeProvider, string user, string table, string operate, string sql, + SugarParameter[] p, ConnectionConfig config) { try { @@ -25,7 +27,8 @@ public static class SqlSugarAop { using (LogContextExtension.Create.SqlAopPushProperty(sqlSugarScopeProvider)) { - Log.Information("------------------ \r\n User:[{User}] Table:[{Table}] Operate:[{Operate}] ConnId:[{ConnId}]【SQL语句】: \r\n {Sql}", + Log.Information( + "------------------ \r\n User:[{User}] Table:[{Table}] Operate:[{Operate}] ConnId:[{ConnId}]【SQL语句】: \r\n {Sql}", user, table, operate, config.ConfigId, UtilMethods.GetNativeSql(sql, p)); } } @@ -42,7 +45,7 @@ public static class SqlSugarAop { if (rootEntity.Id == 0) { - rootEntity.Id = SnowFlakeSingle.Instance.NextId(); + rootEntity.Id = IdGeneratorUtility.NextId(); } } @@ -113,7 +116,8 @@ public static class SqlSugarAop if (App.User?.ID > 0 && dyCreateId != null && dyCreateId.GetValue(entityInfo.EntityValue) == null) dyCreateId.SetValue(entityInfo.EntityValue, App.User.ID); - if (dyCreateTime != null && dyCreateTime.GetValue(entityInfo.EntityValue) != null && (DateTime)dyCreateTime.GetValue(entityInfo.EntityValue) == DateTime.MinValue) + if (dyCreateTime != null && dyCreateTime.GetValue(entityInfo.EntityValue) != null && + (DateTime)dyCreateTime.GetValue(entityInfo.EntityValue) == DateTime.MinValue) dyCreateTime.SetValue(entityInfo.EntityValue, DateTime.Now); break; @@ -155,4 +159,4 @@ public static class SqlSugarAop return key; } -} +} \ No newline at end of file diff --git a/Blog.Core.Common/Seed/SeedData/SubBusinessDataSeedData.cs b/Blog.Core.Common/Seed/SeedData/SubBusinessDataSeedData.cs index be8462e..909e1fc 100644 --- a/Blog.Core.Common/Seed/SeedData/SubBusinessDataSeedData.cs +++ b/Blog.Core.Common/Seed/SeedData/SubBusinessDataSeedData.cs @@ -2,6 +2,7 @@ using SqlSugar; using System.Collections.Generic; using System.Threading.Tasks; +using Blog.Core.Common.Utlilty; namespace Blog.Core.Common.Seed.SeedData; @@ -29,13 +30,13 @@ public class SubBusinessDataSeedData : IEntitySeedData { new() { - Id = SnowFlakeSingle.Instance.NextId(), + Id = IdGeneratorUtility.NextId(), Name = "王五业务数据1", Amount = 100, }, new() { - Id = SnowFlakeSingle.Instance.NextId(), + Id = IdGeneratorUtility.NextId(), Name = "王五业务数据2", Amount = 1000, }, @@ -50,13 +51,13 @@ public class SubBusinessDataSeedData : IEntitySeedData { new() { - Id = SnowFlakeSingle.Instance.NextId(), + Id = IdGeneratorUtility.NextId(), Name = "赵六业务数据1", Amount = 50, }, new() { - Id = SnowFlakeSingle.Instance.NextId(), + Id = IdGeneratorUtility.NextId(), Name = "赵六业务数据2", Amount = 60, }, diff --git a/Blog.Core.Common/Utlilty/IdGeneratorUtility.cs b/Blog.Core.Common/Utlilty/IdGeneratorUtility.cs new file mode 100644 index 0000000..8bf1c70 --- /dev/null +++ b/Blog.Core.Common/Utlilty/IdGeneratorUtility.cs @@ -0,0 +1,69 @@ +using Blog.Core.Common.DB; +using Blog.Core.Common.Option; +using Microsoft.Extensions.Hosting; +using Serilog; +using SnowflakeId.AutoRegister.Builder; +using SnowflakeId.AutoRegister.Interfaces; +using SqlSugar; +using Yitter.IdGenerator; + +namespace Blog.Core.Common.Utlilty; + +public class IdGeneratorUtility +{ + private static readonly Lazy AutoRegister = new(() => + { + var builder = new AutoRegisterBuilder() + // Register Option + // Use the following line to set the identifier. + // Recommended setting to distinguish multiple applications on a single machine + .SetExtraIdentifier(App.Configuration["urls"] ?? string.Empty) + // Use the following line to set the WorkerId scope. + .SetWorkerIdScope(1, 30) + // Use the following line to set the register option. + // .SetRegisterOption(option => {}) + ; + var redisOptions = App.GetOptions(); + if (redisOptions.Enable) + // Use the following line to use the Redis store. + builder.UseRedisStore(redisOptions.ConnectionString); + else if (BaseDBConfig.LogConfig != null && BaseDBConfig.LogConfig.DbType == DbType.SqlServer) + // Use the following line to use the SQL Server store. + builder.UseSqlServerStore(BaseDBConfig.LogConfig.ConnectionString); + else + // Use the following line to use the default store. + // Only suitable for standalone use, local testing, etc. + builder.UseDefaultStore(); + + App.GetService(false).ApplicationStopping.Register(UnRegister); + return builder.Build(); + }); + + private static readonly Lazy _idGenInstance = new(() => + { + var config = AutoRegister.Value.Register(); + + //WorkerId DataCenterId 取值 1-31 + var options = new IdGeneratorOptions + { + WorkerId = (ushort)config.WorkerId, + }; + IIdGenerator idGenInstance = new DefaultIdGenerator(options); + return idGenInstance; + }); + + private static IIdGenerator IdGenInstance => _idGenInstance.Value; + + public static long NextId() + { + return IdGenInstance.NewLong(); + } + + public static void UnRegister() + { + if (!AutoRegister.IsValueCreated) return; + + AutoRegister.Value.UnRegister(); + Log.Information("Snowflake Id Unregistered"); + } +} \ No newline at end of file diff --git a/Blog.Core.Extensions/ServiceExtensions/SqlsugarSetup.cs b/Blog.Core.Extensions/ServiceExtensions/SqlsugarSetup.cs index d6bfbb2..fee96fc 100644 --- a/Blog.Core.Extensions/ServiceExtensions/SqlsugarSetup.cs +++ b/Blog.Core.Extensions/ServiceExtensions/SqlsugarSetup.cs @@ -7,6 +7,7 @@ using Microsoft.Extensions.DependencyInjection; using SqlSugar; using Blog.Core.Common.Caches; using System.Text.RegularExpressions; +using Blog.Core.Common.Utlilty; namespace Blog.Core.Extensions { @@ -21,6 +22,8 @@ namespace Blog.Core.Extensions { if (services == null) throw new ArgumentNullException(nameof(services)); + StaticConfig.CustomSnowFlakeFunc = IdGeneratorUtility.NextId; + // 默认添加主数据库连接 if (!AppSettings.app("MainDB").IsNullOrEmpty()) { diff --git a/Blog.Core.Services/GuestbookServices.cs b/Blog.Core.Services/GuestbookServices.cs index c63dfdb..c8301a2 100644 --- a/Blog.Core.Services/GuestbookServices.cs +++ b/Blog.Core.Services/GuestbookServices.cs @@ -7,6 +7,7 @@ using Blog.Core.Services.BASE; using System; using System.Threading.Tasks; using Blog.Core.Common.DB; +using Blog.Core.Common.Utlilty; using Blog.Core.Repository.UnitOfWorks; using SqlSugar; @@ -227,7 +228,7 @@ namespace Blog.Core.Services await Db.Insertable(new PasswordLib() { - PLID = SnowFlakeSingle.Instance.NextId(), + PLID = IdGeneratorUtility.NextId(), IsDeleted = false, plAccountName = "aaa", plCreateTime = DateTime.Now @@ -257,7 +258,7 @@ namespace Blog.Core.Services await db.Insertable(new PasswordLib() { - PLID = SnowFlakeSingle.Instance.NextId(), + PLID = IdGeneratorUtility.NextId(), IsDeleted = false, plAccountName = "aaa", plCreateTime = DateTime.Now diff --git a/Blog.Core.Services/PasswordLibServices.cs b/Blog.Core.Services/PasswordLibServices.cs index 6f5a468..38ca009 100644 --- a/Blog.Core.Services/PasswordLibServices.cs +++ b/Blog.Core.Services/PasswordLibServices.cs @@ -2,6 +2,7 @@ using System.Threading.Tasks; using Blog.Core.Common; using Blog.Core.Common.DB; +using Blog.Core.Common.Utlilty; using Blog.Core.IRepository.Base; using Blog.Core.IServices; using Blog.Core.Model.Models; @@ -31,7 +32,7 @@ namespace Blog.Core.Services { await _dal.Add(new PasswordLib() { - PLID = SnowFlakeSingle.Instance.NextId(), + PLID = IdGeneratorUtility.NextId(), IsDeleted = false, plAccountName = "aaa", plCreateTime = DateTime.Now @@ -58,7 +59,7 @@ namespace Blog.Core.Services { await db.Insertable(new PasswordLib() { - PLID = SnowFlakeSingle.Instance.NextId(), + PLID = IdGeneratorUtility.NextId(), IsDeleted = false, plAccountName = "aaa", plCreateTime = DateTime.Now @@ -76,7 +77,7 @@ namespace Blog.Core.Services Console.WriteLine(""); await db.Insertable(new PasswordLib() { - PLID = SnowFlakeSingle.Instance.NextId(), + PLID = IdGeneratorUtility.NextId(), IsDeleted = false, plAccountName = "aaa", plCreateTime = DateTime.Now diff --git a/Blog.Core.Tasks/QuartzNet/Jobs/Job_Blogs_Quartz.cs b/Blog.Core.Tasks/QuartzNet/Jobs/Job_Blogs_Quartz.cs index 6ca3178..c589962 100644 --- a/Blog.Core.Tasks/QuartzNet/Jobs/Job_Blogs_Quartz.cs +++ b/Blog.Core.Tasks/QuartzNet/Jobs/Job_Blogs_Quartz.cs @@ -2,6 +2,7 @@ using Quartz; using System; using System.Threading.Tasks; +using Blog.Core.Common.Utlilty; using Blog.Core.Model.Models; using Blog.Core.Repository.UnitOfWorks; using SqlSugar; @@ -49,7 +50,7 @@ namespace Blog.Core.Tasks }).ExecuteReturnSnowflakeIdAsync(); await db.Insertable(new PasswordLib() { - PLID = SnowFlakeSingle.Instance.NextId(), + PLID = IdGeneratorUtility.NextId(), IsDeleted = false, plAccountName = "aaa", plCreateTime = DateTime.Now @@ -59,7 +60,7 @@ namespace Blog.Core.Tasks Console.WriteLine(db.ContextID); await db.Insertable(new PasswordLib() { - PLID = SnowFlakeSingle.Instance.NextId(), + PLID = IdGeneratorUtility.NextId(), IsDeleted = false, plAccountName = "aaa", plCreateTime = DateTime.Now