如何生成唯一ID

时间:2017-07-03 09:56:53

标签: java uuid guid uid timeuuid

我们如何在两个同时运行的JVM之间生成唯一ID? 我认为UUID uuid = UUID.randomUUID();UID uid = new UID();还不够。

2 个答案:

答案 0 :(得分:2)

您可以连接到UUID服务器ID,以便UUID在所有服务器中都是唯一的

答案 1 :(得分:2)

请参阅UUID文档,您可以点击该链接查找解释如何生成UUID的rfc4122。最后48位与您的问题有关,spacial unique node 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是固定的         大小并包含一个时间字段,值可能是         翻滚

但这需要在它来之前运行很长时间。所以,我不会对你当前的解决方案感到担心。