mirror of
https://github.com/anjoy8/Blog.Core.git
synced 2024-09-20 23:48:27 +08:00
✨⚡️雪花Id优化
1.统一使用 Yitter.IdGenerator 生成雪花Id、 2.使用 SnowflakeId.AutoRegister 自动注册WorkerId 静态使用:IdGeneratorUtility.NextId()
This commit is contained in:
parent
0d5fb831de
commit
2d7350203c
|
@ -894,6 +894,12 @@
|
|||
通过此处的key格式为 xx:xx:x
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:Blog.Core.Controllers.ValuesController.GetSnowflakeId">
|
||||
<summary>
|
||||
获取雪花Id
|
||||
</summary>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="T:Blog.Core.Controllers.WeChatCompanyController">
|
||||
<summary>
|
||||
WeChatCompanyController
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取雪花Id
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
[AllowAnonymous]
|
||||
public long GetSnowflakeId()
|
||||
{
|
||||
return IdGeneratorUtility.NextId();
|
||||
}
|
||||
}
|
||||
|
||||
public class ClaimDto
|
||||
|
|
|
@ -31,12 +31,16 @@
|
|||
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.Elasticsearch" Version="9.0.3" />
|
||||
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
|
||||
<PackageReference Include="StackExchange.Redis" Version="2.7.4" />
|
||||
<PackageReference Include="SnowflakeId.AutoRegister" Version="1.0.1" />
|
||||
<PackageReference Include="SnowflakeId.AutoRegister.SqlServer" Version="1.0.0" />
|
||||
<PackageReference Include="SnowflakeId.AutoRegister.StackExchangeRedis" Version="1.0.0" />
|
||||
<PackageReference Include="StackExchange.Redis" Version="2.8.0" />
|
||||
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.2.0" />
|
||||
<PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.1-dev-00771" />
|
||||
|
||||
<PackageReference Include="Serilog" Version="3.1.1" />
|
||||
<PackageReference Include="Serilog.AspNetCore" Version="8.0.0" />
|
||||
<PackageReference Include="Yitter.IdGenerator" Version="1.0.14" />
|
||||
|
||||
</ItemGroup>
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<SubLibraryBusinessTable>
|
|||
{
|
||||
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<SubLibraryBusinessTable>
|
|||
{
|
||||
new()
|
||||
{
|
||||
Id = SnowFlakeSingle.Instance.NextId(),
|
||||
Id = IdGeneratorUtility.NextId(),
|
||||
Name = "赵六业务数据1",
|
||||
Amount = 50,
|
||||
},
|
||||
new()
|
||||
{
|
||||
Id = SnowFlakeSingle.Instance.NextId(),
|
||||
Id = IdGeneratorUtility.NextId(),
|
||||
Name = "赵六业务数据2",
|
||||
Amount = 60,
|
||||
},
|
||||
|
|
69
Blog.Core.Common/Utlilty/IdGeneratorUtility.cs
Normal file
69
Blog.Core.Common/Utlilty/IdGeneratorUtility.cs
Normal file
|
@ -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<IAutoRegister> 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<RedisOptions>();
|
||||
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<IHostApplicationLifetime>(false).ApplicationStopping.Register(UnRegister);
|
||||
return builder.Build();
|
||||
});
|
||||
|
||||
private static readonly Lazy<IIdGenerator> _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");
|
||||
}
|
||||
}
|
|
@ -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())
|
||||
{
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user