🏆 作者简介,愚公搬代码 🏆《头衔》:华为云特约编辑,华为云云享专家,华为开发者专家,华为产品云测专家,CSDN博客专家,阿里云专家博主,腾讯云优秀博主,掘金优秀博主,51CTO博客专家等。 🏆《近期荣誉》:2022年CSDN博客之星TOP2,2022年华为云十佳博主等。 🏆《博客内容》:.NET、Java、Python、Go、Node、前端、IOS、Android、鸿蒙、Linux、物联网、网络安全、大数据、人工智能、U3D游戏、小程序等相关领域知识。 🏆🎉欢迎 👍点赞✍评论⭐收藏
分布式唯一ID(Distributed Unique ID,简称DUID)是指在分布式系统中,为了避免ID冲突而使用的一种ID生成方式。
在分布式系统中,多个节点同时生成ID,如果采用单机自增序列的方式,容易造成ID重复的问题。为了解决这个问题,产生了DUID的概念。
常见的DUID生成方式有:
本文主要介绍DeveloperSharp的使用,DeveloperSharp是一个研发中大型项目必备的系统平台,也是一个低代码平台。
它主要包括了如下一些功能:
DeveloperSharp
1、WEB项目
using DeveloperSharp.Framework.CoreUtility; //从NuGet引用DeveloperSharp包
--------------------------
//首先在Startup.cs或Program.cs文件中进行工具预载
Services.AddTransient<IUtility, Utility>();
--------------------------
//IU是在相关文件中,通过依赖注入方式获取的IUtility类型对象
var Id = IU.GenerateId("Order");//产生分布式唯一Id
2.控制台项目
using DeveloperSharp.Framework.CoreUtility;//从NuGet引用DeveloperSharp包
------------------------
IUtility IU = new Utility();
var Id = IU.GenerateId("Order");//产生分布式唯一Id
以下是一个基于C#的雪花算法ID生成器示例代码:
public class SnowflakeIdGenerator
{
private static readonly long twepoch = 1288834974657L;
private static readonly long workerIdBits = 5L;
private static readonly long datacenterIdBits = 5L;
private static readonly long maxWorkerId = -1L ^ (-1L << (int)workerIdBits);
private static readonly long maxDatacenterId = -1L ^ (-1L << (int)datacenterIdBits);
private static readonly long sequenceBits = 12L;
private static readonly long workerIdShift = sequenceBits;
private static readonly long datacenterIdShift = sequenceBits + workerIdBits;
private static readonly long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
private static readonly long sequenceMask = -1L ^ (-1L << (int)sequenceBits);
private static readonly Random random = new Random();
private readonly long workerId;
private readonly long datacenterId;
private long lastTimestamp = -1L;
private long sequence;
public SnowflakeIdGenerator(long workerId, long datacenterId)
{
if (workerId > maxWorkerId || workerId < 0)
{
throw new ArgumentException($"worker Id can't be greater than {maxWorkerId} or less than 0");
}
if (datacenterId > maxDatacenterId || datacenterId < 0)
{
throw new ArgumentException($"datacenter Id can't be greater than {maxDatacenterId} or less than 0");
}
this.workerId = workerId;
this.datacenterId = datacenterId;
}
public long NextId()
{
lock (this)
{
long timestamp = GetTimestamp();
if (timestamp < lastTimestamp)
{
throw new Exception($"clock moved backwards. Refusing to generate id for {lastTimestamp - timestamp} milliseconds");
}
if (lastTimestamp == timestamp)
{
sequence = (sequence + 1) & sequenceMask;
if (sequence == 0)
{
timestamp = TilNextMillis(lastTimestamp);
}
}
else
{
sequence = random.Next(1, 10);
}
lastTimestamp = timestamp;
return ((timestamp - twepoch) << (int)timestampLeftShift) |
(datacenterId << (int)datacenterIdShift) |
(workerId << (int)workerIdShift) |
sequence;
}
}
private long TilNextMillis(long lastTimestamp)
{
long timestamp = GetTimestamp();
while (timestamp <= lastTimestamp)
{
timestamp = GetTimestamp();
}
return timestamp;
}
private static long GetTimestamp()
{
return (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
}
}
使用方法:
static void Main(string[] args)
{
SnowflakeIdGenerator generator = new SnowflakeIdGenerator(1, 1);
long id = generator.NextId();
Console.WriteLine(id);
}
这样就可以生成一个雪花算法生成的唯一ID了。