UUID 工具类(UUID utility class)

UUID

UUID 是通用唯一识别码(Universally Unique Identifier)的缩写。UUID 让分布式系统中的所有元素,都能有唯一的辨识信息,而不需要通过中央控制端来做辨识信息的指定。

UUID 的组成

  • 1~8位采用系统时间,在系统时间上精确到毫秒级保证时间上的惟一性;
  • 9~16位采用底层的IP地址,在服务器集群中的惟一性;
  • 17~24位采用当前对象的HashCode值,在一个内部对象上的惟一性;
  • 25~32位采用调用方法的一个随机数,在一个对象内的毫秒级的惟一性。

UUID 重复机率

UUID 在一般情况下很难生成一致的编码,不是说绝对的,但是就现在一般的使用情况下没有重复的出现,故大可不必考虑重复问题。

UUID 用途

利用不重复得特点我们可以作为数据库的主键区分各个实例。Java 中有 UUID 这个工具类,在 java.util 包中,专门用来生成 UUID。

UUID 工具类

class UUIDUtils {
    public static String getUUID(){
        return UUID.randomUUID().toString().replaceAll("-", "");
    }
}
————————

UUID

UUID is the abbreviation of < strong > universal unique identifier. UUID enables all elements in the distributed system to have < strong > unique < / strong > identification information without specifying the identification information through the central control terminal.

UUID 的组成

  • 1 ~ 8 bits adopt system time, which is accurate to milliseconds to ensure the uniqueness of time;
  • 9 ~ 16 bits adopt the underlying IP address, which is unique in the server cluster;
  • 17 ~ 24 bits adopt the hashcode value of the current object to ensure uniqueness on an internal object;
  • 25 ~ 32 bits adopt a random number calling the method, which is unique in milliseconds within an object.

UUID repetition probability

In general, it is difficult for UUIDs to generate consistent codes, which is not absolute. However, in general use, < strong > there is no repetition < / strong >, so there is no need to consider repetition.

UUID 用途

Using the non duplication feature, we can use it as the primary key of the < strong > database < / strong > to distinguish various instances. UUID is a tool class in Java. It is available in < strong > java Util package < / strong > is specially used to generate UUIDs.

UUID 工具类

class UUIDUtils {
    public static String getUUID(){
        return UUID.randomUUID().toString().replaceAll("-", "");
    }
}