|
|
@@ -1,4 +1,8 @@
|
|
|
-namespace Business.Core.Utilities
|
|
|
+using Amazon.Runtime.Internal.Util;
|
|
|
+using System.Net;
|
|
|
+using System.Runtime.InteropServices;
|
|
|
+
|
|
|
+namespace Business.Core.Utilities
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// 雪花算法生成id
|
|
|
@@ -73,35 +77,113 @@
|
|
|
public DateTime StartTimestamp { get; set; } = new DateTime(2010,1,1);
|
|
|
|
|
|
static object syncRoot = new object();
|
|
|
- static readonly Lazy<SnowFlake> snowflake = new(() => new SnowFlake(0L,0L));
|
|
|
+ static readonly Lazy<SnowFlake> snowflake = new(() => new SnowFlake());
|
|
|
|
|
|
/// <summary>
|
|
|
- /// 默认金泰实例,WorkerId = 0,DatacenterId = 0
|
|
|
+ /// 默认静态实例,WorkerId = 0,DatacenterId = 0
|
|
|
/// </summary>
|
|
|
public static SnowFlake Instance { get; } = snowflake.Value;
|
|
|
|
|
|
/// <summary>
|
|
|
/// 构造函数
|
|
|
/// </summary>
|
|
|
- /// <param name="workerId">工作机器ID</param>
|
|
|
- /// <param name="datacenterId">数据中心ID</param>
|
|
|
/// <exception cref="ArgumentOutOfRangeException"></exception>
|
|
|
- public SnowFlake(long workerId,long datacenterId)
|
|
|
+ public SnowFlake()
|
|
|
{
|
|
|
- if (workerId > maxWorkerId || workerId < 0)
|
|
|
+ //生成workerid和datacenterid
|
|
|
+ SetWorkIdAndDatacenterId();
|
|
|
+ if (WorkerId > maxWorkerId || WorkerId < 0)
|
|
|
{
|
|
|
- throw new ArgumentOutOfRangeException(nameof(workerId),$"不能大于{maxWorkerId}或小于0");
|
|
|
+ throw new ArgumentOutOfRangeException(nameof(WorkerId),$"不能大于{maxWorkerId}或小于0");
|
|
|
}
|
|
|
- if (datacenterId > maxDatacenterId || datacenterId < 0)
|
|
|
+ if (DatacenterId > maxDatacenterId || DatacenterId < 0)
|
|
|
{
|
|
|
- throw new ArgumentOutOfRangeException(nameof(datacenterId), $"不能大于{maxDatacenterId}或小于0");
|
|
|
+ throw new ArgumentOutOfRangeException(nameof(DatacenterId), $"不能大于{maxDatacenterId}或小于0");
|
|
|
}
|
|
|
- WorkerId = workerId;
|
|
|
- DatacenterId = datacenterId;
|
|
|
Sequence = 0L;
|
|
|
LastTimestamp = -1L;
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 生成WorkerId和DatacenterId
|
|
|
+ /// </summary>
|
|
|
+ /// <returns></returns>
|
|
|
+ private void SetWorkIdAndDatacenterId()
|
|
|
+ {
|
|
|
+ string ipAdress = "";
|
|
|
+ string hostName = "";
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
|
|
+ {
|
|
|
+ hostName = Environment.GetEnvironmentVariable("MY_NODE_NAME");
|
|
|
+ ipAdress = Environment.GetEnvironmentVariable("MY_HOST_IP");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ hostName = Dns.GetHostName();
|
|
|
+ IPHostEntry ipEntity = Dns.GetHostEntry(hostName);
|
|
|
+ for (int i = 0; i < ipEntity.AddressList.Length; i++)
|
|
|
+ {
|
|
|
+ if (ipEntity.AddressList[i].AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
|
|
|
+ {
|
|
|
+ ipAdress = ipEntity.AddressList[i].ToString();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (string.IsNullOrEmpty(ipAdress) || string.IsNullOrEmpty(hostName))
|
|
|
+ {
|
|
|
+ WorkerId = 0;
|
|
|
+ DatacenterId = 0;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ WorkerId = StringToLong(ipAdress);
|
|
|
+ DatacenterId = StringToLong(hostName);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ WorkerId = 0;
|
|
|
+ DatacenterId = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 生成workerid和datacenterid
|
|
|
+ /// </summary>
|
|
|
+ /// <param name=""></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ private long StringToLong(string strInput)
|
|
|
+ {
|
|
|
+ int[] arrays = ToCodePoints(strInput);
|
|
|
+ int sum = 0;
|
|
|
+ for (int i = 0; i < arrays.Length; i++)
|
|
|
+ {
|
|
|
+ sum += arrays[i];
|
|
|
+ }
|
|
|
+ return (long)(sum % 32);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 将字符串转换成数组
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="strInput"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ private int[] ToCodePoints(string strInput)
|
|
|
+ {
|
|
|
+ var codePoints = new List<int>(strInput.Length);
|
|
|
+ for (int i = 0; i < strInput.Length; i++)
|
|
|
+ {
|
|
|
+ codePoints.Add(char.ConvertToUtf32(strInput, i));
|
|
|
+ if (char.IsHighSurrogate(strInput[i]))
|
|
|
+ {
|
|
|
+ i += 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return codePoints.ToArray();
|
|
|
+ }
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 获取下一个ID
|
|
|
/// </summary>
|