diff --git a/aspnet-core/modules/common/LINGYUN.Abp.IdGenerator/LINGYUN/Abp/IdGenerator/Snowflake/SnowflakeIdGenerator.cs b/aspnet-core/modules/common/LINGYUN.Abp.IdGenerator/LINGYUN/Abp/IdGenerator/Snowflake/SnowflakeIdGenerator.cs
index 22f4f9f7b..467063899 100644
--- a/aspnet-core/modules/common/LINGYUN.Abp.IdGenerator/LINGYUN/Abp/IdGenerator/Snowflake/SnowflakeIdGenerator.cs
+++ b/aspnet-core/modules/common/LINGYUN.Abp.IdGenerator/LINGYUN/Abp/IdGenerator/Snowflake/SnowflakeIdGenerator.cs
@@ -32,29 +32,42 @@ namespace LINGYUN.Abp.IdGenerator.Snowflake
{
var idGenerator = new SnowflakeIdGenerator(options)
{
+ WorkerId = options.WorkerId,
+ DatacenterId = options.DatacenterId,
Sequence = options.Sequence,
MaxWorkerId = -1L ^ (-1L << options.WorkerIdBits),
MaxDatacenterId = -1L ^ (-1L << options.DatacenterIdBits)
};
- if (!int.TryParse(Environment.GetEnvironmentVariable("WORKERID", EnvironmentVariableTarget.Machine), out var workerId))
- {
- workerId = RandomHelper.GetRandom((int)idGenerator.MaxWorkerId);
- }
-
- if (!int.TryParse(Environment.GetEnvironmentVariable("DATACENTERID", EnvironmentVariableTarget.Machine), out var datacenterId))
- {
- datacenterId = RandomHelper.GetRandom((int)idGenerator.MaxDatacenterId);
+ if (idGenerator.WorkerId == 0 || (int)Math.Log10(options.WorkerId) + 1 > idGenerator.MaxWorkerId)
+ {
+ if (!int.TryParse(Environment.GetEnvironmentVariable("WORKERID", EnvironmentVariableTarget.Machine), out var workerId))
+ {
+ workerId = RandomHelper.GetRandom((int)idGenerator.MaxWorkerId);
+ }
+
+ if (workerId > idGenerator.MaxWorkerId || workerId < 0)
+ {
+ throw new ArgumentException($"worker Id can't be greater than {idGenerator.MaxWorkerId} or less than 0");
+ }
+
+ idGenerator.WorkerId = workerId;
}
- if (workerId > idGenerator.MaxWorkerId || workerId < 0)
- throw new ArgumentException($"worker Id can't be greater than {idGenerator.MaxWorkerId} or less than 0");
-
- if (datacenterId > idGenerator.MaxDatacenterId || datacenterId < 0)
- throw new ArgumentException($"datacenter Id can't be greater than {idGenerator.MaxDatacenterId} or less than 0");
+ if (idGenerator.DatacenterId == 0 || (int)Math.Log10(options.DatacenterId) + 1 > idGenerator.MaxDatacenterId)
+ {
+ if (!int.TryParse(Environment.GetEnvironmentVariable("DATACENTERID", EnvironmentVariableTarget.Machine), out var datacenterId))
+ {
+ datacenterId = RandomHelper.GetRandom((int)idGenerator.MaxDatacenterId);
+ }
- idGenerator.WorkerId = workerId;
- idGenerator.DatacenterId = datacenterId;
+ if (datacenterId > idGenerator.MaxDatacenterId || datacenterId < 0)
+ {
+ throw new ArgumentException($"datacenter Id can't be greater than {idGenerator.MaxDatacenterId} or less than 0");
+ }
+
+ idGenerator.DatacenterId = datacenterId;
+ }
return idGenerator;
}
@@ -69,6 +82,7 @@ namespace LINGYUN.Abp.IdGenerator.Snowflake
{
var timestamp = TimeGen();
+ // TODO: 时间回退解决方案, 保存一个时间节点, 当服务器时间发生改变, 从保存的节点开始递增
if (timestamp < _lastTimestamp)
throw new Exception(
$"InvalidSystemClock: Clock moved backwards, Refusing to generate id for {_lastTimestamp - timestamp} milliseconds");
diff --git a/aspnet-core/modules/common/LINGYUN.Abp.IdGenerator/LINGYUN/Abp/IdGenerator/Snowflake/SnowflakeIdOptions.cs b/aspnet-core/modules/common/LINGYUN.Abp.IdGenerator/LINGYUN/Abp/IdGenerator/Snowflake/SnowflakeIdOptions.cs
index 06b884c97..289b6ade4 100644
--- a/aspnet-core/modules/common/LINGYUN.Abp.IdGenerator/LINGYUN/Abp/IdGenerator/Snowflake/SnowflakeIdOptions.cs
+++ b/aspnet-core/modules/common/LINGYUN.Abp.IdGenerator/LINGYUN/Abp/IdGenerator/Snowflake/SnowflakeIdOptions.cs
@@ -2,21 +2,27 @@
public class SnowflakeIdOptions
{
+ ///
+ /// 机器Id
+ ///
+ public int WorkerId { get; set; }
///
/// 机器Id长度
///
public int WorkerIdBits { get; set; }
///
- /// 机房Id长度
+ /// 数据中心Id
///
- public int DatacenterIdBits { get; set; }
+ public int DatacenterId { get; set; }
///
- /// 12bit 的序号
+ /// 数据中心Id长度
///
- public long Sequence { get; set; }
+ public int DatacenterIdBits { get; set; }
///
- /// 每秒生成Id的数量
+ /// 12bit 的序号
///
+ public long Sequence { get; set; }
+
public int SequenceBits { get; set; }
public SnowflakeIdOptions()
diff --git a/aspnet-core/modules/logging/LINGYUN.Abp.Serilog.Enrichers.UniqueId/LINGYUN/Abp/Serilog/Enrichers/UniqueId/AbpSerilogEnrichersUniqueIdModule.cs b/aspnet-core/modules/logging/LINGYUN.Abp.Serilog.Enrichers.UniqueId/LINGYUN/Abp/Serilog/Enrichers/UniqueId/AbpSerilogEnrichersUniqueIdModule.cs
index b287d5417..b27755d9b 100644
--- a/aspnet-core/modules/logging/LINGYUN.Abp.Serilog.Enrichers.UniqueId/LINGYUN/Abp/Serilog/Enrichers/UniqueId/AbpSerilogEnrichersUniqueIdModule.cs
+++ b/aspnet-core/modules/logging/LINGYUN.Abp.Serilog.Enrichers.UniqueId/LINGYUN/Abp/Serilog/Enrichers/UniqueId/AbpSerilogEnrichersUniqueIdModule.cs
@@ -1,4 +1,6 @@
using LINGYUN.Abp.IdGenerator;
+using LINGYUN.Abp.IdGenerator.Snowflake;
+using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Modularity;
namespace LINGYUN.Abp.Serilog.Enrichers.UniqueId
@@ -6,5 +8,10 @@ namespace LINGYUN.Abp.Serilog.Enrichers.UniqueId
[DependsOn(typeof(AbpIdGeneratorModule))]
public class AbpSerilogEnrichersUniqueIdModule : AbpModule
{
+ public override void ConfigureServices(ServiceConfigurationContext context)
+ {
+ var options = context.Services.ExecutePreConfiguredActions();
+ UniqueIdEnricher.DistributedIdGenerator = SnowflakeIdGenerator.Create(options.SnowflakeIdOptions);
+ }
}
}
diff --git a/aspnet-core/modules/logging/LINGYUN.Abp.Serilog.Enrichers.UniqueId/LINGYUN/Abp/Serilog/Enrichers/UniqueId/AbpSerilogEnrichersUniqueIdOptions.cs b/aspnet-core/modules/logging/LINGYUN.Abp.Serilog.Enrichers.UniqueId/LINGYUN/Abp/Serilog/Enrichers/UniqueId/AbpSerilogEnrichersUniqueIdOptions.cs
new file mode 100644
index 000000000..1959d1829
--- /dev/null
+++ b/aspnet-core/modules/logging/LINGYUN.Abp.Serilog.Enrichers.UniqueId/LINGYUN/Abp/Serilog/Enrichers/UniqueId/AbpSerilogEnrichersUniqueIdOptions.cs
@@ -0,0 +1,12 @@
+using LINGYUN.Abp.IdGenerator.Snowflake;
+
+namespace LINGYUN.Abp.Serilog.Enrichers.UniqueId;
+
+public class AbpSerilogEnrichersUniqueIdOptions
+{
+ public SnowflakeIdOptions SnowflakeIdOptions { get; set; }
+ public AbpSerilogEnrichersUniqueIdOptions()
+ {
+ SnowflakeIdOptions = new SnowflakeIdOptions();
+ }
+}
diff --git a/aspnet-core/modules/logging/LINGYUN.Abp.Serilog.Enrichers.UniqueId/LINGYUN/Abp/Serilog/Enrichers/UniqueId/UniqueIdEnricher.cs b/aspnet-core/modules/logging/LINGYUN.Abp.Serilog.Enrichers.UniqueId/LINGYUN/Abp/Serilog/Enrichers/UniqueId/UniqueIdEnricher.cs
index 82d66954b..c6b478b9f 100644
--- a/aspnet-core/modules/logging/LINGYUN.Abp.Serilog.Enrichers.UniqueId/LINGYUN/Abp/Serilog/Enrichers/UniqueId/UniqueIdEnricher.cs
+++ b/aspnet-core/modules/logging/LINGYUN.Abp.Serilog.Enrichers.UniqueId/LINGYUN/Abp/Serilog/Enrichers/UniqueId/UniqueIdEnricher.cs
@@ -1,5 +1,4 @@
using LINGYUN.Abp.IdGenerator;
-using LINGYUN.Abp.IdGenerator.Snowflake;
using Serilog.Core;
using Serilog.Events;
@@ -7,15 +6,14 @@ namespace LINGYUN.Abp.Serilog.Enrichers.UniqueId
{
public class UniqueIdEnricher : ILogEventEnricher
{
- private readonly static IDistributedIdGenerator _distributedIdGenerator
- = SnowflakeIdGenerator.Create(new SnowflakeIdOptions());
+ internal static IDistributedIdGenerator DistributedIdGenerator;
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
logEvent.AddOrUpdateProperty(
propertyFactory.CreateProperty(
AbpSerilogUniqueIdConsts.UniqueIdPropertyName,
- _distributedIdGenerator.Create()));
+ DistributedIdGenerator.Create()));
}
}
}
diff --git a/aspnet-core/services/LY.MicroService.BackendAdmin.HttpApi.Host/BackendAdminHttpApiHostModule.Configure.cs b/aspnet-core/services/LY.MicroService.BackendAdmin.HttpApi.Host/BackendAdminHttpApiHostModule.Configure.cs
index 67a08e514..e64af5e9c 100644
--- a/aspnet-core/services/LY.MicroService.BackendAdmin.HttpApi.Host/BackendAdminHttpApiHostModule.Configure.cs
+++ b/aspnet-core/services/LY.MicroService.BackendAdmin.HttpApi.Host/BackendAdminHttpApiHostModule.Configure.cs
@@ -3,6 +3,7 @@ using LINGYUN.Abp.ExceptionHandling;
using LINGYUN.Abp.ExceptionHandling.Emailing;
using LINGYUN.Abp.Localization.CultureMap;
using LINGYUN.Abp.Serilog.Enrichers.Application;
+using LINGYUN.Abp.Serilog.Enrichers.UniqueId;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.DataProtection;
@@ -37,6 +38,14 @@ public partial class BackendAdminHttpApiHostModule
private void PreConfigureApp()
{
AbpSerilogEnrichersConsts.ApplicationName = "Backend-Admin";
+
+ PreConfigure(options =>
+ {
+ // 以开放端口区别
+ options.SnowflakeIdOptions.WorkerId = 30010;
+ options.SnowflakeIdOptions.WorkerIdBits = 5;
+ options.SnowflakeIdOptions.DatacenterId = 1;
+ });
}
private void PreConfigureCAP(IConfiguration configuration)
diff --git a/aspnet-core/services/LY.MicroService.BackendAdmin.HttpApi.Host/Properties/launchSettings.json b/aspnet-core/services/LY.MicroService.BackendAdmin.HttpApi.Host/Properties/launchSettings.json
index ceb66ec05..731195696 100644
--- a/aspnet-core/services/LY.MicroService.BackendAdmin.HttpApi.Host/Properties/launchSettings.json
+++ b/aspnet-core/services/LY.MicroService.BackendAdmin.HttpApi.Host/Properties/launchSettings.json
@@ -14,7 +14,7 @@
"launchBrowser": false,
"applicationUrl": "http://127.0.0.1:30010",
"environmentVariables": {
- "ASPNETCORE_ENVIRONMENT": "Development"
+ "ASPNETCORE_ENVIRONMENT": "Production"
}
}
}
diff --git a/aspnet-core/services/LY.MicroService.LocalizationManagement.HttpApi.Host/LocalizationManagementHttpApiHostModule.Configure.cs b/aspnet-core/services/LY.MicroService.LocalizationManagement.HttpApi.Host/LocalizationManagementHttpApiHostModule.Configure.cs
index 1c83328dc..bf46fa625 100644
--- a/aspnet-core/services/LY.MicroService.LocalizationManagement.HttpApi.Host/LocalizationManagementHttpApiHostModule.Configure.cs
+++ b/aspnet-core/services/LY.MicroService.LocalizationManagement.HttpApi.Host/LocalizationManagementHttpApiHostModule.Configure.cs
@@ -3,6 +3,7 @@ using LINGYUN.Abp.ExceptionHandling;
using LINGYUN.Abp.ExceptionHandling.Emailing;
using LINGYUN.Abp.Localization.CultureMap;
using LINGYUN.Abp.Serilog.Enrichers.Application;
+using LINGYUN.Abp.Serilog.Enrichers.UniqueId;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.DataProtection;
@@ -35,6 +36,14 @@ public partial class LocalizationManagementHttpApiHostModule
private void PreConfigureApp()
{
AbpSerilogEnrichersConsts.ApplicationName = "Localization";
+
+ PreConfigure(options =>
+ {
+ // 以开放端口区别
+ options.SnowflakeIdOptions.WorkerId = 30030;
+ options.SnowflakeIdOptions.WorkerIdBits = 5;
+ options.SnowflakeIdOptions.DatacenterId = 1;
+ });
}
private void PreConfigureCAP(IConfiguration configuration)
diff --git a/aspnet-core/services/LY.MicroService.PlatformManagement.HttpApi.Host/PlatformManagementHttpApiHostModule.Configure.cs b/aspnet-core/services/LY.MicroService.PlatformManagement.HttpApi.Host/PlatformManagementHttpApiHostModule.Configure.cs
index 274e49bc7..1a5636120 100644
--- a/aspnet-core/services/LY.MicroService.PlatformManagement.HttpApi.Host/PlatformManagementHttpApiHostModule.Configure.cs
+++ b/aspnet-core/services/LY.MicroService.PlatformManagement.HttpApi.Host/PlatformManagementHttpApiHostModule.Configure.cs
@@ -3,6 +3,7 @@ using LINGYUN.Abp.ExceptionHandling;
using LINGYUN.Abp.ExceptionHandling.Emailing;
using LINGYUN.Abp.Localization.CultureMap;
using LINGYUN.Abp.Serilog.Enrichers.Application;
+using LINGYUN.Abp.Serilog.Enrichers.UniqueId;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.DataProtection;
@@ -39,6 +40,14 @@ public partial class PlatformManagementHttpApiHostModule
private void PreConfigureApp()
{
AbpSerilogEnrichersConsts.ApplicationName = "Platform";
+
+ PreConfigure(options =>
+ {
+ // 以开放端口区别
+ options.SnowflakeIdOptions.WorkerId = 30025;
+ options.SnowflakeIdOptions.WorkerIdBits = 5;
+ options.SnowflakeIdOptions.DatacenterId = 1;
+ });
}
private void PreConfigureCAP(IConfiguration configuration)
diff --git a/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/RealtimeMessageHttpApiHostModule.Configure.cs b/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/RealtimeMessageHttpApiHostModule.Configure.cs
index 365820866..4442fd402 100644
--- a/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/RealtimeMessageHttpApiHostModule.Configure.cs
+++ b/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/RealtimeMessageHttpApiHostModule.Configure.cs
@@ -7,6 +7,7 @@ using LINGYUN.Abp.Localization.CultureMap;
using LINGYUN.Abp.MessageService.Localization;
using LINGYUN.Abp.MessageService.Permissions;
using LINGYUN.Abp.Serilog.Enrichers.Application;
+using LINGYUN.Abp.Serilog.Enrichers.UniqueId;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.DataProtection;
@@ -41,6 +42,14 @@ public partial class RealtimeMessageHttpApiHostModule
private void PreConfigureApp()
{
AbpSerilogEnrichersConsts.ApplicationName = "MessageService";
+
+ PreConfigure(options =>
+ {
+ // 以开放端口区别
+ options.SnowflakeIdOptions.WorkerId = 30020;
+ options.SnowflakeIdOptions.WorkerIdBits = 5;
+ options.SnowflakeIdOptions.DatacenterId = 1;
+ });
}
private void PreConfigureCAP(IConfiguration configuration)
diff --git a/aspnet-core/services/LY.MicroService.identityServer.HttpApi.Host/IdentityServerHttpApiHostModule.Configure.cs b/aspnet-core/services/LY.MicroService.identityServer.HttpApi.Host/IdentityServerHttpApiHostModule.Configure.cs
index 45f838445..c62646ca1 100644
--- a/aspnet-core/services/LY.MicroService.identityServer.HttpApi.Host/IdentityServerHttpApiHostModule.Configure.cs
+++ b/aspnet-core/services/LY.MicroService.identityServer.HttpApi.Host/IdentityServerHttpApiHostModule.Configure.cs
@@ -3,6 +3,7 @@ using LINGYUN.Abp.ExceptionHandling;
using LINGYUN.Abp.ExceptionHandling.Emailing;
using LINGYUN.Abp.Localization.CultureMap;
using LINGYUN.Abp.Serilog.Enrichers.Application;
+using LINGYUN.Abp.Serilog.Enrichers.UniqueId;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.DataProtection;
@@ -39,6 +40,14 @@ public partial class IdentityServerHttpApiHostModule
private void PreConfigureApp()
{
AbpSerilogEnrichersConsts.ApplicationName = "Identity-Server-Admin";
+
+ PreConfigure(options =>
+ {
+ // 以开放端口区别
+ options.SnowflakeIdOptions.WorkerId = 30015;
+ options.SnowflakeIdOptions.WorkerIdBits = 5;
+ options.SnowflakeIdOptions.DatacenterId = 1;
+ });
}
private void PreConfigureCAP(IConfiguration configuration)
diff --git a/aspnet-core/services/LY.MicroService.identityServer/IdentityServerModule.Configure.cs b/aspnet-core/services/LY.MicroService.identityServer/IdentityServerModule.Configure.cs
index 30580cfb2..ad3c3aad3 100644
--- a/aspnet-core/services/LY.MicroService.identityServer/IdentityServerModule.Configure.cs
+++ b/aspnet-core/services/LY.MicroService.identityServer/IdentityServerModule.Configure.cs
@@ -2,6 +2,7 @@
using LINGYUN.Abp.IdentityServer.IdentityResources;
using LINGYUN.Abp.Localization.CultureMap;
using LINGYUN.Abp.Serilog.Enrichers.Application;
+using LINGYUN.Abp.Serilog.Enrichers.UniqueId;
using LY.MicroService.IdentityServer.IdentityResources;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.DataProtection;
@@ -37,6 +38,14 @@ public partial class IdentityServerModule
private void PreConfigureApp()
{
AbpSerilogEnrichersConsts.ApplicationName = "Identity-Server-STS";
+
+ PreConfigure(options =>
+ {
+ // 以开放端口区别
+ options.SnowflakeIdOptions.WorkerId = 44385;
+ options.SnowflakeIdOptions.WorkerIdBits = 5;
+ options.SnowflakeIdOptions.DatacenterId = 1;
+ });
}
private void PreConfigureCAP(IConfiguration configuration)