如何生成唯一ID [英] How to generate unique id
问题描述
如何在同时运行的两个JVM之间生成唯一的ID?我认为 UUID uuid = UUID.randomUUID();
或 UID uid = new UID();
是不够的.
How can we generate unique id's between two JVMs running at the same time?
I think UUID uuid = UUID.randomUUID();
or UID uid = new UID();
is not enough.
推荐答案
请参见 UUID 文档,您可以通过该链接找到 rfc4122解释如何生成UUID.最后48位与您的问题有关,空间唯一节点标识符
确认此值将是 Universally Unique IDentifier .
See UUID docs, you can follow the link to find the rfc4122 explaining how the UUID will be generated. The last 48 bits are concerning your question, the spacial unique node identifier
confirming that this value will be a 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
我能看到的唯一风险来自该部分
The only risk I can see come from that part
UUID是一个在空间和时间上都是唯一的标识符,关于所有UUID的空间.由于UUID是固定的大小并包含一个时间字段,值可能会滚动
A UUID is an identifier that is unique across both space and time, with respect to the space of all UUIDs. Since a UUID is a fixed size and contains a time field, it is possible for values to rollover
但这需要运行很长时间才能出现.因此,对于您当前的解决方案,我不会太担心.
But this would need to run for a long time before it come to you. So, I would not be worried to much with you current solution.
这篇关于如何生成唯一ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!