前言 分布式场景下,一张表可能分散到多个数据结点上。因此需要一些分布式ID的解决方案。 分布式ID需要有几个特点: 全局唯一(必要) :在多个库的主键放在一起也不会重复 有序(必要) :避免频繁触发索引重建 信息安全:ID连续,可以根据订单编号计算一天的单量,造成信息泄露 包含时间戳:能够快速根据ID得知生成时间 下面几种方案按推荐顺序排序,越推荐使用越靠前。 一、雪花算法snowflake 64 位的 long 类型的唯一 id 1)1位不用 带符号整数第1位是符号位,正数是0,ID一般为正数,此位不用。 2)41位毫秒级时间戳 41位存储当前时间截 – 开始时间截得到的差值,可以表示 2 41 2^{41} 241个毫秒的值,转化成单位年则是: 2 41 1000 ∗ 60 ∗ 60 ∗ 24 ∗ 365 = 69 年 \frac{2^{41}}{1000∗60∗60∗24∗365}=69年 1000∗60∗60∗24∗365241=69年 注:开始时间截由程序指定,一般是id生成器开始使用的时间,设置好后避免更改。依赖服务器时间,服务器时钟回拨时可能会生成重复 id。...
Is this you? As a journalist, you can create a free Muck Rack account to customize your profile, list your contact preferences, and upload a portfolio of your best work.
Claim your profile
Get in touch with long
Contact long, search articles and posts on X, monitor coverage, and track replies from one place.
Learn more about Muck Rack