1

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

4

2 回答 2

2

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

于 2017-07-03T10:00:50.883 回答
2

请参阅UUID文档,您可以通过链接找到rfc4122解释如何生成 UUID。最后 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 是固定大小并包含时间字段,因此值可能会翻转

但这需要运行很长时间才能找到您。因此,我不会对您当前的解决方案感到担心。

于 2017-07-03T10:11:13.750 回答