From c06c4d776b1810b45396a957c2c52aa05bc6ccc0 Mon Sep 17 00:00:00 2001
From: wangjunzzz <>
Date: Thu, 8 Jan 2026 17:10:38 +0800
Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E9=9B=AA=E8=8A=B1?=
=?UTF-8?q?=E7=AE=97=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
aspnet-core/Directory.Build.targets | 1 +
aspnet-core/Lion.AbpPro.slnx | 3 ++-
.../Lion.AbpPro.IdGenerator.csproj | 16 +++++++++++++
.../IdGenerator/AbpProIdGeneratorModule.cs | 22 +++++++++++++++++
.../Lion.AbpPro.Core.Tests.csproj | 1 +
.../Lion/AbpPro/Core/AbpProTestBaseModule.cs | 6 +++--
.../AbpPro/IdGenerator/IdGeneratorTests.cs | 24 +++++++++++++++++++
7 files changed, 70 insertions(+), 3 deletions(-)
create mode 100644 aspnet-core/frameworks/src/Lion.AbpPro.IdGenerator/Lion.AbpPro.IdGenerator.csproj
create mode 100644 aspnet-core/frameworks/src/Lion.AbpPro.IdGenerator/Lion/AbpPro/IdGenerator/AbpProIdGeneratorModule.cs
create mode 100644 aspnet-core/frameworks/test/Lion.AbpPro.Core.Tests/Lion/AbpPro/IdGenerator/IdGeneratorTests.cs
diff --git a/aspnet-core/Directory.Build.targets b/aspnet-core/Directory.Build.targets
index fb6f557c..48fdc93a 100644
--- a/aspnet-core/Directory.Build.targets
+++ b/aspnet-core/Directory.Build.targets
@@ -114,5 +114,6 @@
+
\ No newline at end of file
diff --git a/aspnet-core/Lion.AbpPro.slnx b/aspnet-core/Lion.AbpPro.slnx
index aa5a81ee..b85fd600 100644
--- a/aspnet-core/Lion.AbpPro.slnx
+++ b/aspnet-core/Lion.AbpPro.slnx
@@ -24,6 +24,7 @@
+
@@ -150,4 +151,4 @@
-
+
\ No newline at end of file
diff --git a/aspnet-core/frameworks/src/Lion.AbpPro.IdGenerator/Lion.AbpPro.IdGenerator.csproj b/aspnet-core/frameworks/src/Lion.AbpPro.IdGenerator/Lion.AbpPro.IdGenerator.csproj
new file mode 100644
index 00000000..23776c6f
--- /dev/null
+++ b/aspnet-core/frameworks/src/Lion.AbpPro.IdGenerator/Lion.AbpPro.IdGenerator.csproj
@@ -0,0 +1,16 @@
+
+
+
+ net10.0
+ enable
+
+
+
+
+
+
+
+
+
+
+
diff --git a/aspnet-core/frameworks/src/Lion.AbpPro.IdGenerator/Lion/AbpPro/IdGenerator/AbpProIdGeneratorModule.cs b/aspnet-core/frameworks/src/Lion.AbpPro.IdGenerator/Lion/AbpPro/IdGenerator/AbpProIdGeneratorModule.cs
new file mode 100644
index 00000000..7713f494
--- /dev/null
+++ b/aspnet-core/frameworks/src/Lion.AbpPro.IdGenerator/Lion/AbpPro/IdGenerator/AbpProIdGeneratorModule.cs
@@ -0,0 +1,22 @@
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Logging;
+using Volo.Abp.Modularity;
+using Yitter.IdGenerator;
+
+namespace Lion.AbpPro.IdGenerator;
+
+public class AbpProIdGeneratorModule : AbpModule
+{
+ public override void ConfigureServices(ServiceConfigurationContext context)
+ {
+ var logger = context.Services.BuildServiceProvider().GetRequiredService>();
+ // 从环境变量或配置文件读取 WorkerId, 如果没有则随机生成从0-63 随机获取一个
+ var workerId = Environment.GetEnvironmentVariable("WORKER_ID") ?? new Random().Next(0, 64).ToString();
+ logger.LogInformation($"当前雪花算法WorkerId: {workerId}");
+ var options = new IdGeneratorOptions
+ {
+ WorkerId = ushort.Parse(workerId)
+ };
+ YitIdHelper.SetIdGenerator(options);
+ }
+}
\ No newline at end of file
diff --git a/aspnet-core/frameworks/test/Lion.AbpPro.Core.Tests/Lion.AbpPro.Core.Tests.csproj b/aspnet-core/frameworks/test/Lion.AbpPro.Core.Tests/Lion.AbpPro.Core.Tests.csproj
index d4e31bfe..f5416d65 100644
--- a/aspnet-core/frameworks/test/Lion.AbpPro.Core.Tests/Lion.AbpPro.Core.Tests.csproj
+++ b/aspnet-core/frameworks/test/Lion.AbpPro.Core.Tests/Lion.AbpPro.Core.Tests.csproj
@@ -22,6 +22,7 @@
+
diff --git a/aspnet-core/frameworks/test/Lion.AbpPro.Core.Tests/Lion/AbpPro/Core/AbpProTestBaseModule.cs b/aspnet-core/frameworks/test/Lion.AbpPro.Core.Tests/Lion/AbpPro/Core/AbpProTestBaseModule.cs
index b3112014..2bc76c94 100644
--- a/aspnet-core/frameworks/test/Lion.AbpPro.Core.Tests/Lion/AbpPro/Core/AbpProTestBaseModule.cs
+++ b/aspnet-core/frameworks/test/Lion.AbpPro.Core.Tests/Lion/AbpPro/Core/AbpProTestBaseModule.cs
@@ -1,11 +1,13 @@
-using Volo.Abp;
+using Lion.AbpPro.IdGenerator;
+using Volo.Abp;
using Volo.Abp.Autofac;
using Volo.Abp.Modularity;
namespace Lion.AbpPro.Core
{
[DependsOn(typeof(AbpTestBaseModule),
- typeof(AbpAutofacModule))]
+ typeof(AbpAutofacModule),
+ typeof(AbpProIdGeneratorModule))]
public class AbpProTestBaseModule : AbpModule
{
}
diff --git a/aspnet-core/frameworks/test/Lion.AbpPro.Core.Tests/Lion/AbpPro/IdGenerator/IdGeneratorTests.cs b/aspnet-core/frameworks/test/Lion.AbpPro.Core.Tests/Lion/AbpPro/IdGenerator/IdGeneratorTests.cs
new file mode 100644
index 00000000..90712e7f
--- /dev/null
+++ b/aspnet-core/frameworks/test/Lion.AbpPro.Core.Tests/Lion/AbpPro/IdGenerator/IdGeneratorTests.cs
@@ -0,0 +1,24 @@
+using Lion.AbpPro.Core;
+using Xunit.Abstractions;
+using Yitter.IdGenerator;
+
+namespace Lion.AbpPro.IdGenerator;
+
+public class IdGeneratorTests : AbpProTestBase
+{
+ private readonly ITestOutputHelper _testOutputHelper;
+
+ public IdGeneratorTests(ITestOutputHelper testOutputHelper)
+ {
+ _testOutputHelper = testOutputHelper;
+ }
+
+ [Fact]
+ public void NextId()
+ {
+ for (int j = 0; j < 1000; j++)
+ {
+ _testOutputHelper.WriteLine(YitIdHelper.NextId().ToString());
+ }
+ }
+}
\ No newline at end of file