7 changed files with 70 additions and 3 deletions
@ -0,0 +1,16 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFramework>net10.0</TargetFramework> |
|||
<ImplicitUsings>enable</ImplicitUsings> |
|||
<RootNamespace/> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<ProjectReference Include="..\Lion.AbpPro.Core\Lion.AbpPro.Core.csproj"/> |
|||
</ItemGroup> |
|||
|
|||
<ItemGroup> |
|||
<PackageReference Include="Yitter.IdGenerator"/> |
|||
</ItemGroup> |
|||
</Project> |
|||
@ -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<ILogger<AbpProIdGeneratorModule>>(); |
|||
// 从环境变量或配置文件读取 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); |
|||
} |
|||
} |
|||
@ -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 |
|||
{ |
|||
} |
|||
|
|||
@ -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()); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue