Generating a unique ID

Program:

A, UUID

UUID (Universally Unique Identifier) is a 32 hexadecimal digits, hyphens divided into five sections, the form of 36 characters 8-4-4-4-12. 32 characters and four hyphen '-', we generally use the hyphen will be deleted uuid.toString().replaceAll("-","").

Currently there are five generated UUID way versions, different versions of each algorithm, the scope of application is different.

  • Time-based UUID - Version 1 : This is usually through the current time, a random number, and address local Mac calculated, you can org.apache.logging.log4j.core.utilpackage UuidUtil.getTimeBasedUuid()to use tools or other package. The use of a MAC address, it is possible to ensure uniqueness, but also exposed the MAC address, privacy is not good enough.
  • DCE security UUID - Version 2 DCE (Distributed Computing Environment) UUID and UUID security algorithm based on the same time, but the time stamp will change the position of the front 4 POSIX UID or GID. This version of the UUID less frequently used in practice.
  • Name-based UUID (MD5) - version 3 name-based UUID worth to name and name space by calculating the MD5 hash. This version of the UUID ensures: uniqueness of the same name space generated UUID of different names; the uniqueness of different namespace UUID of; the same namespace the same name repeatedly generating the same UUID.
  • Random UUID - version 4 UUID generated according to a random number or pseudo-random number. The probability of this UUID generated duplicate can be calculated, but the potential for duplication is negligible, so this version is also frequently used version. JDK is used in this version.
  • Name-based UUID (SHA1) - version 5 and UUID algorithm name is similar, but the hash value is calculated using the SHA1 (Secure Hash Algorithm 1) algorithm.

We in Java JDK that comes with version 4 UUID way to generate a random number generated UUID and version of name-based UUID 3, are interested can go to look at its source code.

public static void main (String [ ] args ) { // Get a version 4 UUID according to a random array of bytes. UUID the UUID = the UUID . RandomUUID ( ) ; the System .out . The println (UUID . ToString ( ) . The replaceAll ( "-" , "" ) ) ; // get a version 3 (based on the name) of the UUID specified byte array in accordance with . byte [ ] The nbyte = { 10 , 20 is , 30 } ; the UUID uuidFromBytes = the UUID .nameUUIDFromBytes(nbyte); System.out.println(uuidFromBytes.toString().replaceAll("-","")); }

Benefits: generate simple, each system can be generated locally.
Disadvantages: too long and disorder, can cause the following problems:
1, many business scenarios do not apply, such as the issuance of coupons, coupons pool of 12 possible will be able to meet the needs of distributed, and too long not only easy to store, but also cause can not be used to show the coupon code
2, is not conducive to index. When mysql stored uuid length is too long is not conducive to a primary key, and as the primary cause of the disorder key insertion position data frequently changes affect the performance of
the case where so many scenes or for the primary key, can not be used uuid

Second, the database auto-increment primary keys

Can create a table, insert the primary key is obtained from increasing, as a globally unique id corresponding to the data
shortcomings are obvious, it is a highly concurrent scenarios, limited by the performance of a single mysql. And poor usability, DB problems can cause id can not be generated.
Of course, can be enhanced by the availability of a master-slave mode, while increasing the increment table using different ways to increase the step size of the concurrent performance, suppose we want to deploy such machines 3, the step size should be set to 3, the initial value of each order of 1, 2.3; but also brought new problems, such as not conducive to development, want to improve the performance would stack machine, but steps have been determined not to change the master-slave delay will lead to the only unique id.

三、snowflake

This revenue is distributed twitter id generation algorithm, such the 64-bit program are divided into a plurality of segments, denoted separate machines, time, etc., for example, in the 64-bit Snowflake respectively below (image from the network) shown in FIG. :


 
image.png

The first section above, is one bit: 0, the second part is meaningless is above 41 bit: indicates that the timestamp; the third section above a 5 bit: indicates that the room id, the fourth part is above 10001 5 bit: indicates that the machine id, 1 1001 the above is the fifth part 12 bit: indicates the number is on a machine within the millisecond simultaneously generated in a room id number, 000,000,000,000
advantages: flexibility, 5 can be used for the service identifier
disadvantages: comparison dependent clock

 

More implementation: https://cloud.tencent.com/developer/article/1490531

Guess you like

Origin www.cnblogs.com/seven717/p/11920671.html