[英]How to generate unique id
我们如何在同时运行的两个 JVM 之间生成唯一 ID? 我认为UUID uuid = UUID.randomUUID();
或UID uid = new UID();
是不足够的。
您可以连接到 UUID 服务器 ID,因此 UUID 在所有服务器中都是唯一的
请参阅UUID文档,您可以按照链接找到解释 UUID 将如何生成的rfc4122 。 最后 48 位与您的问题有关, spacial unique node identifier
确认该值将是Universally Unique IDentifier 。
4.1.2. Layout and Byte Order
To minimize confusion about bit assignments within octets, the UUID
record definition is defined only in terms of fields that are
integral numbers of octets. The fields are presented with the most
significant one first.
Field Data Type Octet Note
#
time_low unsigned 32 0-3 The low field of the
bit integer timestamp
time_mid unsigned 16 4-5 The middle field of the
bit integer timestamp
time_hi_and_version unsigned 16 6-7 The high field of the
bit integer timestamp multiplexed
with the version number
clock_seq_hi_and_rese unsigned 8 8 The high field of the
rved bit integer clock sequence
multiplexed with the
variant
clock_seq_low unsigned 8 9 The low field of the
bit integer clock sequence
node unsigned 48 10-15 The spatially unique
bit integer node identifier
我能看到的唯一风险来自那部分
UUID 是一个在空间和时间上都是唯一的标识符,相对于所有 UUID 的空间而言。 由于 UUID 是固定大小并包含时间字段,因此值可能会翻转
但这需要运行很长时间才能找到您。 所以,我不会太担心你目前的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.