网上大部分C#写的都有点乱糟糟,我简化了一下:
using System;
namespace xxx
{
/// <summary>
/// Id 生成类
/// </summary>
class Snowflake
{
private const string LOCK_OBJ = "76003AEB-E3F9-460A-BD31-D9AE9E7684C0";
private const int MACHINE_BIT_SIZE = 10; // 机器编号长度10位
private const int SEQUENCE_BIT_SIZE = 12; // 序号长度12位
private static Snowflake _snowflake;
private long _machineNumber; // 机器序号
private long _timestamp; // 时间戳
private long _sequence; // 序号
private Snowflake() { }
/// <summary>
/// 设置机器序号
/// </summary>
public int MachineMumber
{
set { _machineNumber = value; }
}
/// <summary>
/// 得到一个实例
/// </summary>
/// <returns></returns>
public static Snowflake GetInstance()
{
if (_snowflake == null)
{
lock (LOCK_OBJ)
{
if (_snowflake == null)
{
_snowflake = new Snowflake();
}
}
}
return _snowflake;
}
/// <summary>
/// 产生一个id,由时间戳、机器编码、顺序号组成
/// </summary>
/// <returns></returns>
public long GenerateId(DateTime now)
{
lock (LOCK_OBJ)
{
if (_machineNumber > (-1L ^ -1L << MACHINE_BIT_SIZE))
{
throw new ArgumentException("机器编号超出最大值");
}
long timestamp = GetTimestamp(now);
if (timestamp < _timestamp)
{
throw new ArgumentException("时间戳错误");
}
if (timestamp == _timestamp)
{
_sequence++;
if (_sequence > (-1L ^ -1L << SEQUENCE_BIT_SIZE))
{
throw new ArgumentException("序号超出最大值");
}
}
else
{
_sequence = 0;
}
long id = timestamp << (MACHINE_BIT_SIZE + SEQUENCE_BIT_SIZE)
| _machineNumber << SEQUENCE_BIT_SIZE
| _sequence;
_timestamp = timestamp;
return id;
}
}
// 当前时间戳
private long GetTimestamp(DateTime now)
{
return (long)(now - new DateTime(2024, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
}
}
}
调用:
namespace xxx
{
public class IdGenerator
{
/// <summary>
/// 生成Id
/// </summary>
/// <returns>id</returns>
public static string GenerateId()
{
Snowflake sf = Snowflake.GetInstance();
sf.MachineMumber = yourGetMachineFunction();
long id = sf.GenerateId(yourGetTimestampFunction());
return id.ToString();
}
}
}
标签:1L,long,Id,Snowflake,public,算法,private,snowflake
From: https://www.cnblogs.com/zzy0471/p/17966215