1. 雪花ID使用配置项

2. 使用自定义日志格式
3. 修复数据模型和新建流程的bug
This commit is contained in:
2023-03-14 20:36:51 +08:00
parent 1d73df3235
commit a4ed390e82
12 changed files with 445 additions and 397 deletions

View File

@@ -28,7 +28,7 @@ public class DataBaseManager : IDataBaseManager, ITransient
/// <summary>
/// 初始化客户端.
/// </summary>
private static SqlSugarScope? _sqlSugarClient;
private static SqlSugarScope _sqlSugarClient;
/// <summary>
/// 用户管理器.
@@ -785,7 +785,7 @@ public class DataBaseManager : IDataBaseManager, ITransient
sb.Append(@"select a.relname F_TABLE,a.n_live_tup F_SUM,b.description F_TABLENAME from sys_stat_user_tables a left outer join sys_description b on a.relid = b.objoid where a.schemaname='public' and b.objsubid='0'");
break;
case "postgresql":
sb.Append(@"select cast(relname as varchar) as F_TABLE,reltuples as F_SUM, cast(obj_description(relfilenode,'pg_class') as varchar) as F_TABLENAME from pg_class c inner join pg_namespace n on n.oid = c.relnamespace and nspname='public' inner join pg_tables z on z.tablename=c.relname where relkind = 'r' and relname not like 'pg_%' and relname not like 'sql_%' and schemaname='public' order by relname");
sb.Append(@"select cast(relname as varchar) as F_TABLE, cast(reltuples as varchar) as F_SUM, cast(obj_description(relfilenode,'pg_class') as varchar) as F_TABLENAME from pg_class c inner join pg_namespace n on n.oid = c.relnamespace and nspname='public' inner join pg_tables z on z.tablename=c.relname where relkind = 'r' and relname not like 'pg_%' and relname not like 'sql_%' and schemaname='public' order by relname");
break;
default:
throw new Exception("不支持");

View File

@@ -1,5 +1,7 @@
using System.Runtime.InteropServices;
using JNPF.Common.Cache;
using JNPF.Common.Const;
using JNPF.FriendlyException;
using Yitter.IdGenerator;
namespace JNPF.Common.Security;
@@ -9,59 +11,56 @@ namespace JNPF.Common.Security;
/// </summary>
public class SnowflakeIdHelper
{
// 定义dll路径
public const string RegWorkerId_DLL_NAME = "lib\\regworkerid_lib_v1.3.1\\yitidgengo.dll";
// 定义dll路径
public const string RegWorkerId_DLL_NAME = "lib\\regworkerid_lib_v1.3.1\\yitidgengo.dll";
// 根据文档定义三个接口
// 根据文档定义三个接口
// 注册一个 WorkerId会先注销所有本机已注册的记录
// ip: redis 服务器地址
// port: redis 端口
// password: redis 访问密码,可为空字符串“”
// maxWorkerId: 最大 WorkerId
[DllImport(RegWorkerId_DLL_NAME, EntryPoint = "RegisterOne", CallingConvention = CallingConvention.Cdecl, ExactSpelling = false)]
private static extern ushort RegisterOne(string ip, int port, string password, int maxWorkerId);
// 注册一个 WorkerId会先注销所有本机已注册的记录
// ip: redis 服务器地址
// port: redis 端口
// password: redis 访问密码,可为空字符串“”
// maxWorkerId: 最大 WorkerId
[DllImport(RegWorkerId_DLL_NAME, EntryPoint = "RegisterOne", CallingConvention = CallingConvention.Cdecl, ExactSpelling = false)]
private static extern ushort RegisterOne(string ip, int port, string password, int maxWorkerId);
// 注销本机已注册的 WorkerId
[DllImport(RegWorkerId_DLL_NAME, EntryPoint = "UnRegister", CallingConvention = CallingConvention.Cdecl, ExactSpelling = false)]
private static extern void UnRegister();
// 注销本机已注册的 WorkerId
[DllImport(RegWorkerId_DLL_NAME, EntryPoint = "UnRegister", CallingConvention = CallingConvention.Cdecl, ExactSpelling = false)]
private static extern void UnRegister();
// 检查本地WorkerId是否有效0-有效,其它-无效)
[DllImport(RegWorkerId_DLL_NAME, EntryPoint = "Validate", CallingConvention = CallingConvention.Cdecl, ExactSpelling = false)]
private static extern int Validate(int workerId);
// 检查本地WorkerId是否有效0-有效,其它-无效)
[DllImport(RegWorkerId_DLL_NAME, EntryPoint = "Validate", CallingConvention = CallingConvention.Cdecl, ExactSpelling = false)]
private static extern int Validate(int workerId);
/// <summary>
/// 缓存配置.
/// </summary>
private static CacheOptions _cacheOptions = App.GetConfig<CacheOptions>("Cache", true);
/// <summary>
/// 生成ID.
/// </summary>
/// <returns></returns>
public static string NextId()
/// <summary>
/// 初始化YitId配置
/// </summary>
public static void InitYitIdWorker()
{
//var option = new IdGeneratorOptions
//{
// WorkerId = 1, //必须 全局唯一,必须 程序设定,理论最大值 2^WorkerIdBitLength-1
// //BaseTime = new DateTime(2023, 1, 1, 0, 0, 0, DateTimeKind.Utc), //用生成ID时的系统时间与基础时间的差值(毫秒数)作为生成ID的时间戳
// WorkerIdBitLength = 16, //机器码位长,决定 WorkerId 的最大值,默认值6,取值范围 [1, 16]
// //SeqBitLength = 4, //序列数位长,默认值6,取值范围 [3, 21](建议不小于4),决定每毫秒基础生成的ID个数
// //MinSeqNumber = 5, //最小序列数,默认值5,取值范围 [5, MaxSeqNumber]
// //MaxSeqNumber 最大序列数,设置范围 [MinSeqNumber, 2^SeqBitLength-1]默认值0
//};
var option = App.GetConfig<IdGeneratorOptions>("YitId");
if (option.WorkerId == 0)
{
// 这个if判断在高并发的情况下可能会有问题
if (YitIdHelper.IdGenInstance == null)
{
UnRegister();
// 如果不用自动注册WorkerId的话直接传一个数值就可以了
var workerId = RegisterOne(_cacheOptions.ip, _cacheOptions.port, _cacheOptions.password, 63);
// 创建 IdGeneratorOptions 对象,可在构造函数中输入 WorkerId
var options = new IdGeneratorOptions(workerId);
options.WorkerIdBitLength = 16; // 默认值6限定 WorkerId 最大值为2^6-1即默认最多支持64个节点。
options.SeqBitLength = 6; // 默认值6限制每毫秒生成的ID个数。若生成速度超过5万个/秒,建议加大 SeqBitLength 到 10。
// options.BaseTime = Your_Base_Time; // 如果要兼容老系统的雪花算法此处应设置为老系统的BaseTime。
// ...... 其它参数参考 IdGeneratorOptions 定义。
// 保存参数(务必调用,否则参数设置不生效):
YitIdHelper.SetIdGenerator(options);
// 以上过程只需全局一次且应在生成ID之前完成。
}
return YitIdHelper.NextId().ToString();
CacheOptions _cacheOptions = App.GetConfig<CacheOptions>("Cache", true);
option.WorkerId = RegisterOne(_cacheOptions.ip, _cacheOptions.port, _cacheOptions.password, 63);
}
YitIdHelper.SetIdGenerator(option);
}
/// <summary>
/// 生成ID.
/// </summary>
/// <returns></returns>
public static string NextId()
{
return YitIdHelper.NextId().ToString();
}
}

View File

@@ -17,12 +17,16 @@
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="6.0.0" />
<PackageReference Include="System.Management" Version="6.0.0" />
<PackageReference Include="NPOI" Version="2.5.5" />
<PackageReference Include="Tnb.SqlSugar" Version="2023.3.10.2014" />
<PackageReference Include="UAParser" Version="3.1.47" />
<PackageReference Include="Yitter.IdGenerator" Version="1.0.14" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="2.88.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Tnb.SqlSugar" Version="2023.3.14.2031" />
<!--<ProjectReference Include="..\..\..\Tnb.Core\src\Tnb.SqlSugar\Tnb.SqlSugar.csproj" />-->
</ItemGroup>
<!--<ItemGroup>
<PackageReference Update="Roslynator.Analyzers" Version="4.1.0">
<PrivateAssets>all</PrivateAssets>