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