IdGen ( P+ g: I+ c8 Z- J* p- ~7 ~5 n' T
IdGen 是 .NET 平台下开源的类似 Twitter Snowflake 的 ID 生成器。在某些情况下,你需要一个低延迟、分布式、按时间排序、紧凑且高度可用的 Id 生成系统。实际上,IdGen 会生成 63 位 Id。一个 Id 由 3 部分组成:
7 G$ y- i% Z$ X! N Timestamp Generator-id Sequence. P6 D8 d/ `, i4 u, o0 U4 K
默认 Default 生成的 Id 的 IdStructure 结构如下:0 O8 B5 y* @( w$ b( ]. {8 r. j# B4 X
3 V% F% j" {0 _
其中的 generator-id 部分,是需要手动配置的,不同的线程,不同的主机,不同的地区等,它在系统中应该是唯一的。7 Z# A1 j4 w. E" K6 M3 R- y6 t3 y
快速使用 4 w5 G, \, I) Q9 @. c# ]$ [
使用 Nuget 安装 IdGen.: n1 B6 x6 L& e- a
- q0 Q( o' A# S& ], G 修改代码如下:) ?& g1 r$ `" M% C. b
using IdGen;
using System.Linq;
class Program
{
static void Main(string[] args)
{
var generator = new IdGenerator(0);
var id = generator.CreateId();
// Example id: 862817670527975424
}
} 非常简单,你已经创建了你的第一个 Id!想要创建 100 个 ID?代码改成下面的即可。
9 _( t; |& T6 l( y0 ?. \, ~ var id = generator.Take(100);项目地址
) f8 E! G, n o1 \ https://github.com/RobThree/IdGen