【问题标题】:How to generate unique id如何生成唯一标识
【发布时间】:2017-12-06 13:11:58
【问题描述】:

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

【问题讨论】:

  • 一般应该没问题,除非您需要每秒生成数十亿个唯一 ID。
  • 是什么让你觉得这还不够?
  • 我认为,在不同的机器上生成相同的uuid是可能的

标签: java uuid guid uid timeuuid


【解决方案1】:

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

【讨论】:

  • UUID 已经包含 48 位机器特定的位,以保证空间唯一性。
  • 2个虚拟机也位于同一物理机上?
  • 不,在不同的机器上
【解决方案2】:

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

但这需要运行很长时间才能找到您。所以,我不会太担心你目前的解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-06
    • 2017-10-20
    • 1970-01-01
    相关资源
    最近更新 更多