Method and apparatus for generating a unique identification

By obtaining machine codes from the distributed business system and registering them with the registry center, and using the improved snowflake algorithm to generate globally unique identifiers, the cumbersome configuration and coupling problems of the traditional snowflake algorithm are solved, achieving global uniqueness and fast data acquisition. It is suitable for distributed clusters and sharded database environments.

CN113449488BActive Publication Date: 2026-03-17BEIJING JINGDONG TUOXIAN TECH CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2020-03-27
Publication Date
2026-03-17

AI Technical Summary

Technical Problem

Traditional snowflake algorithms are cumbersome to configure in distributed business systems, have difficulty guaranteeing global uniqueness, and are inaccurate in data retrieval in sharded database and table environments. Furthermore, the business system is highly coupled with microservices, and network latency affects efficiency.

Method used

By obtaining the machine code and registering it with the registry center, the improved snowflake algorithm is used to generate the first identifier. The database and data table identifiers are then concatenated to generate a unique identifier, supporting global uniqueness in distributed clusters and enabling rapid data retrieval in a sharded database environment.

Benefits of technology

It achieves global uniqueness of unique identifiers in a distributed cluster system, supports horizontal scaling of the cluster, quickly and accurately obtains data, generates data with high efficiency and no network latency, and is suitable for different business scenarios.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113449488B_ABST
    Figure CN113449488B_ABST
Patent Text Reader

Abstract

The application discloses a unique identification generation method and device, and relates to the technical field of computers. A specific implementation of the method comprises the following steps: obtaining a machine code of a local machine, and registering the machine code to a registration center; inputting the machine code registered successfully into an identification generation algorithm, so that the identification generation algorithm generates a first identification according to the machine code; obtaining a database identification and a data table identification, and splicing the database identification and the data table identification with the first identification to obtain a unique identification. The implementation guarantees the global uniqueness of the unique identification generated under a distributed cluster system environment, does not need to separately configure the machine, is convenient for horizontal expansion of the cluster, and can quickly and accurately obtain data under the condition of saving data in a database and a data table.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of computer technology, and in particular to a method and apparatus for generating a unique identifier. Background Technology

[0002] Most distributed business systems require a unique identifier (e.g., a globally unique identifier ID in an algorithm), such as an order ID, doctor ID, or patient ID. The Snowflake algorithm is one of the commonly used methods for generating globally unique identifiers.

[0003] In the process of realizing this invention, the inventors discovered at least the following problems in the prior art:

[0004] 1) In the traditional snowflake algorithm, the machine code needs to be configured differently for each machine in the microservice cluster. If there are thousands of machines in the cluster, the configuration will be a huge project. The configuration is cumbersome and may not be able to guarantee the global uniqueness of the generated unique identifier. At the same time, it is not convenient for the cluster to scale horizontally freely.

[0005] 2) In a sharded database environment, data cannot be accurately retrieved based on the globally unique identifier;

[0006] 3) In a clustered environment, generating globally unique IDs using the Snowflake algorithm typically involves encapsulating the algorithm into a microservice, which the business system then calls via RPC to obtain the global ID. However, this approach results in strong coupling and dependency between the business system and the microservice; if the microservice becomes unavailable, it will affect the entire business system. Furthermore, network latency significantly limits the efficiency of ID generation. Summary of the Invention

[0007] In view of this, embodiments of the present invention provide a method and apparatus for generating unique identifiers, which can guarantee the global uniqueness of unique identifiers generated in a distributed cluster system environment, and do not require separate configuration of machines, which facilitates the horizontal scaling of the cluster. At the same time, when data is stored in sharded databases and tables, data can be obtained quickly and accurately.

[0008] To achieve the above objectives, according to one aspect of the present invention, a method for generating a unique identifier is provided.

[0009] A method for generating a unique identifier includes: obtaining the machine code of the local machine and registering the machine code with a registration center; inputting the successfully registered machine code into an identifier generation algorithm, so that the identifier generation algorithm generates a first identifier based on the machine code; obtaining a database identifier and a data table identifier, and concatenating the database identifier and the data table identifier with the first identifier to obtain a unique identifier.

[0010] Optionally, obtaining the machine code of the local machine includes: obtaining the network address of the local machine and calculating the encoding of the network address; and taking the modulo of the encoding of the network address with the maximum number of machines supported by the identifier generation algorithm to obtain the machine code.

[0011] Optionally, inputting the successfully registered machine code into the identifier generation algorithm includes: initializing the successfully registered machine code into the identifier generation algorithm as a parameter passed to the constructor function.

[0012] Optionally, the identifier generation algorithm is an improved snowflake algorithm.

[0013] Optionally, the identifier generation algorithm consists of the following parts: a 1-bit identifier, a 40-bit timestamp, a 6-bit machine code, and a 7-bit random sequence number.

[0014] Optionally, the identifier generation algorithm consists of the following parts: a 1-bit identifier, a 38-bit timestamp, a 7-bit machine code, and an 8-bit random sequence number.

[0015] Optionally, the method is implemented as a software development kit.

[0016] According to another aspect of the present invention, an apparatus for generating a unique identifier is provided.

[0017] A unique identifier generation device includes: a machine code registration module for acquiring the machine code of the local machine and registering the machine code with a registration center; an algorithm execution module for inputting the successfully registered machine code into an identifier generation algorithm, so that the identifier generation algorithm generates a first identifier based on the machine code; and an identifier concatenation module for acquiring a database identifier and a data table identifier, and concatenating the database identifier and the data table identifier with the first identifier to obtain a unique identifier.

[0018] According to another aspect of the present invention, a unique identifier generation electronic device is provided.

[0019] An electronic device for generating a unique identifier includes: one or more processors; and a storage device for storing one or more programs, which, when executed by the one or more processors, cause the one or more processors to implement the unique identifier generation method provided in the embodiments of the present invention.

[0020] According to another aspect of the present invention, a computer-readable medium is provided.

[0021] A computer-readable medium having a computer program stored thereon, which, when executed by a processor, implements the method for generating a unique identifier provided in embodiments of the present invention.

[0022] One embodiment of the above invention has the following advantages or beneficial effects: By obtaining the machine code of the local machine and registering it with the registration center; inputting the successfully registered machine code into the identifier generation algorithm, the algorithm generates a first identifier based on the machine code; obtaining the database identifier and data table identifier, and concatenating them with the first identifier to obtain a unique identifier, the global uniqueness of the unique identifier generated in the distributed cluster system environment is guaranteed, and no separate configuration of the machine is required, facilitating horizontal scaling of the cluster. Furthermore, in the case of data sharding and table partitioning, data can be retrieved quickly and accurately. Improvements to the Snowflake algorithm can meet the application needs of different business scenarios, offering flexibility and convenience; by encapsulating the unique identifier generation method and device into a software development kit and introducing it into the project, it is non-intrusive to the business, and can generate unique identifiers locally with high efficiency and no network latency.

[0023] The further effects of the aforementioned unconventional alternative methods will be explained below in conjunction with specific implementation methods. Attached Figure Description

[0024] The accompanying drawings are provided to better understand the invention and are not intended to unduly limit the scope of the invention. Wherein:

[0025] Figure 1 This is a schematic diagram of the existing snowflake algorithm's structure;

[0026] Figure 2 This is a schematic diagram illustrating the main steps of a method for generating a unique identifier according to an embodiment of the present invention;

[0027] Figure 3 This is a schematic diagram of the composition structure of an improved snowflake algorithm according to an embodiment of the present invention;

[0028] Figure 4 This is a schematic diagram of the composition structure of an improved snowflake algorithm according to another embodiment of the present invention;

[0029] Figure 5 This is a schematic diagram illustrating the splicing principle of a globally unique identifier according to an embodiment of the present invention;

[0030] Figure 6 This is a schematic diagram of the process for generating a globally unique identifier according to an embodiment of the present invention;

[0031] Figure 7 This is a schematic diagram of the main modules of the device for generating a unique identifier according to an embodiment of the present invention;

[0032] Figure 8 This is an exemplary system architecture diagram in which embodiments of the present invention can be applied;

[0033] Figure 9This is a schematic diagram of the structure of a computer system suitable for implementing terminal devices or servers of the present invention. Detailed Implementation

[0034] The following description, in conjunction with the accompanying drawings, illustrates exemplary embodiments of the present invention, including various details to aid understanding. These details should be considered merely exemplary. Therefore, those skilled in the art will recognize that various changes and modifications can be made to the embodiments described herein without departing from the scope and spirit of the invention. Similarly, for clarity and brevity, descriptions of well-known functions and structures are omitted in the following description.

[0035] In existing technologies, the snowflake algorithm is a commonly used algorithm for generating globally unique identifiers (IDs). Figure 1 This is a schematic diagram of the existing snowflake algorithm's structure. For example... Figure 1 As shown, the existing snowflake algorithm mainly consists of the following parts:

[0036] (1) 1-bit identifier: always 0. Since the long data type (long integer) is signed in Java, the highest bit is the sign bit. Positive numbers are 0 and negative numbers are 1. Since id is generally a positive number, the highest bit is 0.

[0037] (2) 41-bit timestamp: The 41-bit timestamp does not store the current time, but rather the value obtained by storing the difference between the timestamps (current timestamp - start timestamp). The start timestamp here is generally the time when the ID generator starts using it, which is specified by the program.

[0038] (3) 10-digit machine code: 1024 nodes can be deployed. If the machines are deployed in different data centers (IDCs), these 10 digits can be composed of 5-digit data center ID + 5-digit machine ID.

[0039] (4) 12-bit sequence number: the count within milliseconds. The 12-bit counting sequence number supports each node to generate 4096 ID sequence numbers per millisecond (same machine, same timestamp).

[0040] To address the issues existing in the Snowflake algorithm for generating globally unique identifiers, this invention provides a method and apparatus for generating unique identifiers, enabling the generated unique identifiers to meet the needs of different application scenarios. Furthermore, by incorporating the machine code of each machine in the cluster and inputting it into the identifier generation algorithm, the global uniqueness of the unique identifier generated by each machine in the cluster is guaranteed. Additionally, by concatenating the database and table information stored in the unique identifier into the unique identifier itself, accurate location of the database and table in a sharded environment can be achieved. Finally, by encapsulating the unique identifier generation method and apparatus into a software development kit (JAR file) for integration into the project, it is non-intrusive to business logic and can generate unique identifiers locally with high efficiency and no network latency.

[0041] Figure 2 This is a schematic diagram illustrating the main steps of a method for generating a unique identifier according to an embodiment of the present invention. Figure 2 As shown, the method for generating a unique identifier in this embodiment of the invention mainly includes the following steps S201 to S203.

[0042] Step S201: Obtain the machine code of this machine and register the machine code with the registration center;

[0043] Step S202: Input the successfully registered machine code into the identifier generation algorithm so that the identifier generation algorithm generates the first identifier based on the machine code;

[0044] Step S203: Obtain the database identifier and the data table identifier, and concatenate the database identifier and the data table identifier with the first identifier to obtain a unique identifier.

[0045] In embodiments of the present invention, the unique identifier is, for example, a globally unique identifier in the algorithm.

[0046] According to one embodiment of the present invention, the step of obtaining the machine code of the local machine may include:

[0047] Obtain the local network address and calculate the network address encoding;

[0048] The machine code is obtained by taking the modulo of the network address encoding with the maximum number of machines supported by the identifier generation algorithm.

[0049] In an embodiment of the invention, the local IP address (network address) is obtained during program initialization, and its hash code is calculated (other encoding methods can also be selected as needed). Then, the machine code is obtained by taking the modulo of the hash code of the IP address with the maximum number of machines in the cluster supported by the identifier generation algorithm. The machine code is then registered with the registry center. If registration fails, it means the machine code already exists, and the machine code can be incremented by 1 and registered again. A timeout can also be set to control the registration time. In this embodiment, Redis is used as the registry center, and Redis's single-threaded implementation principle ensures that registered machine codes are not duplicated.

[0050] During program initialization, the machine code of the current machine is generated and registered on Redis. The successfully registered machine code is then input into the identifier generation algorithm. In the identifier generation algorithm of this embodiment, the unique identifier is represented by a globally unique identifier in the program algorithm. Specifically, inputting the successfully registered machine code into the identifier generation algorithm can be achieved by initializing the algorithm as a constructor parameter. Afterwards, the process of generating the globally unique identifier by the identifier generation algorithm is decoupled from Redis, ensuring that even if the Redis host crashes, the application can still generate globally unique identifiers normally.

[0051] In embodiments of the present invention, the identifier generation algorithm is, for example, the snowflake algorithm. Furthermore, the present invention improves the snowflake algorithm so that the length of the first identifier generated by the improved snowflake algorithm is controllable and consists entirely of numbers, thus meeting the length requirements of unique identifiers in different applications.

[0052] Figure 3 This is a schematic diagram of the composition structure of an improved snowflake algorithm according to an embodiment of the present invention. In one embodiment of the present invention, the identifier generation algorithm (improved snowflake algorithm) consists of the following parts: a 1-bit identifier, a 40-bit timestamp, a 6-bit machine code, and a 7-bit random sequence number.

[0053] according to Figure 3 The improved snowflake algorithm generates the following first identifiers: the first identifier is up to 16 bits long, suitable for applications with length requirements; it can be generated continuously for 34.8 years; the algorithm supports a maximum of 64 machines in the cluster; and it generates 128 first identifiers per millisecond.

[0054] The calculation method for the service life (hereinafter the same) is as follows:

[0055] (2 40 -1) / (1000*60*60*24*356)=34.8 years.

[0056] Figure 4 This is a schematic diagram of the composition structure of an improved snowflake algorithm according to another embodiment of the present invention. In one embodiment of the present invention, the identifier generation algorithm (improved snowflake algorithm) consists of the following parts: a 1-bit identifier, a 38-bit timestamp, a 7-bit machine code, and an 8-bit random sequence number.

[0057] according to Figure 4 The improved snowflake algorithm generates the following first identifiers: the first identifier is up to 16 bits long; it can be generated continuously for 8.7 years; the maximum number of machines in the cluster supported by the algorithm is 128; and 256 first identifiers are generated every millisecond.

[0058] Will Figure 3 and Figure 4 The improved snowflake algorithm presented in the illustrated embodiments is summarized in Table 1 below.

[0059] Table 1

[0060]

[0061] Figure 3 The improved snowflake algorithm shown in the embodiment generates a shorter first identifier, which is suitable for scenarios where the length of the unique identifier ID is required, and can save some space. Figure 4 The improved snowflake algorithm presented in the illustrated embodiment has high concurrency and is suitable for scenarios with a large number of machines in the cluster and very frequent generation of unique identifiers.

[0062] After generating the first identifier using the identifier generation algorithm, the database identifier and the data table identifier can be concatenated with the first identifier to obtain a unique identifier. This allows for accurate and rapid data retrieval based on the unique identifier even with sharded databases and tables. Furthermore, since the first identifier generated by the improved snowflake algorithm in this embodiment is 16 bits, while the unique identifier generated by the existing snowflake algorithm is a 19-bit long integer, a 19-bit globally unique identifier can be generated by concatenating the first identifier with a 1-bit database identifier and a 2-bit data table identifier, maintaining consistency with the length of existing unique identifiers.

[0063] Figure 5 This is a schematic diagram illustrating the splicing principle of a globally unique identifier according to an embodiment of the present invention. For example... Figure 5 As shown, the first identifier generated by the improved snowflake algorithm in this embodiment of the invention is 16 bits: "XXXX XXXX XXXX XXXX", the concatenated database identifier is "1", and the concatenated data table identifier is "12".

[0064] In practical applications, if a user account provided by an external system is known, and each user account corresponds to a globally unique identifier, when it is necessary to find the user data corresponding to that user account in another system, it is necessary to first obtain its corresponding globally unique identifier and then perform the data search based on the globally unique identifier. In this case, a hash value can be obtained by performing a hash operation on the user account, then the hash value can be modulo 10 to generate a 1-bit database identifier, and the hash value can be modulo 100 to generate a 2-bit data table identifier. This allows for accurate determination of the storage location of the user account, thus quickly obtaining the corresponding globally unique identifier.

[0065] Figure 6 This is a schematic diagram illustrating the process of generating a globally unique identifier according to an embodiment of the present invention. Figure 6 As shown, when a machine in the cluster used to generate a globally unique identifier comes online, the process begins by obtaining the machine's IP address. Then, the machine code is obtained by hashing the IP address and taking the modulo of the maximum number of machines in the cluster supported by the identifier generation algorithm. Next, the machine code is registered with the registry center, using Redis as the registry center implementation and a single thread for registration. If registration is successful, the successfully registered machine code is injected into the identifier generation algorithm (snowflake algorithm) to generate the first identifier. Otherwise, the machine code is incremented by 1 and the machine code is registered with the registry center again until successful. After generating the first identifier, it is concatenated with the database identifier and the data table identifier to generate a globally unique identifier, which is then saved based on the database identifier and the data table identifier.

[0066] Figure 7 This is a schematic diagram of the main modules of the device for generating a unique identifier according to an embodiment of the present invention. Figure 7 As shown, the unique identifier generation device 700 of this embodiment mainly includes a machine code registration module 701, an algorithm execution module 702, and an identifier splicing module 703.

[0067] The machine code registration module 701 is used to obtain the machine code of the local machine and register the machine code with the registration center;

[0068] Algorithm execution module 702 is used to input the successfully registered machine code into the identifier generation algorithm, so that the identifier generation algorithm generates a first identifier based on the machine code;

[0069] The identifier splicing module 703 is used to obtain the database identifier and the data table identifier, and splice the database identifier and the data table identifier with the first identifier to obtain a unique identifier.

[0070] According to one embodiment of the present invention, the machine code registration module 701 can also be used for:

[0071] Obtain the network address of the local machine and calculate the encoding of the network address;

[0072] The machine code is obtained by taking the modulo of the network address encoding with the maximum number of machines supported by the identifier generation algorithm.

[0073] According to another embodiment of the present invention, the algorithm execution module 702 can also be used for:

[0074] The successfully registered machine code is initialized into the identifier generation algorithm as a parameter passed to the constructor.

[0075] According to an embodiment of the present invention, the identifier generation algorithm is an improved snowflake algorithm.

[0076] According to one embodiment of the present invention, the identifier generation algorithm comprises the following parts:

[0077] It consists of a 1-bit identifier, a 40-bit timestamp, a 6-bit machine code, and a 7-bit random serial number.

[0078] According to another embodiment of the present invention, the identifier generation algorithm comprises the following parts:

[0079] It consists of a 1-bit identifier, a 38-bit timestamp, a 7-bit machine code, and an 8-bit random serial number.

[0080] According to yet another embodiment of the present invention, the apparatus is implemented as a software development kit.

[0081] According to the technical solution of this invention, the machine code of the local machine is obtained and registered with the registration center; the successfully registered machine code is input into the identifier generation algorithm, so that the identifier generation algorithm generates a first identifier based on the machine code; the database identifier and the data table identifier are obtained, and the database identifier and the data table identifier are concatenated with the first identifier to obtain a unique identifier. This ensures the global uniqueness of the unique identifier generated in the distributed cluster system environment, and does not require separate configuration of the machine, facilitating the horizontal scaling of the cluster. At the same time, in the case of data being stored in sharded databases and tables, data can be retrieved quickly and accurately. By improving the Snowflake algorithm, it can meet the application needs of different business scenarios, offering flexibility and convenience; by encapsulating the unique identifier generation method and device into a software development kit and introducing it into the project, it is non-intrusive to the business, and can generate unique identifiers locally with high generation efficiency and no network time consumption.

[0082] According to embodiments of the present invention, the main beneficial effects are as follows:

[0083] (1) Redis, as a registry center, can ensure that machines in the cluster receive different machine codes, thereby ensuring that the ID generated by the snowflake algorithm is globally unique;

[0084] (2) It has strong functional robustness. The process of generating globally unique identifiers is decoupled from Redis. Even if the Redis machine crashes, it will not affect the continued generation of globally unique identifiers by various business systems.

[0085] (3) The Snowflake algorithm guarantees that the generated globally unique identifiers are sequentially increasing, and in a cluster environment, it guarantees that the generated globally unique identifiers are trending upwards, and it has strong confidentiality for other user information, etc.

[0086] (4) The generated globally unique identifiers are all composed of numbers, which can be used as primary keys in a data warehouse environment. They are highly readable and have high query efficiency.

[0087] (5) The algorithm is packaged into a software development kit (JAR) package for direct use. It is non-intrusive to business operations, has no network latency, and is highly efficient in generating globally unique identifiers.

[0088] (6) The generated globally unique identifier has a long service life;

[0089] (7) The access configuration is simple, the horizontal expansion of the cluster does not require additional configuration, and the number of supported machines meets the needs of most applications;

[0090] (8) The identifier generation algorithm is an improvement of the snowflake algorithm. These two improved schemes can be used as configuration items for the system to call to meet different business needs;

[0091] (9) In the case of database sharding and table partitioning, the database and table location of the data to be queried can be directly located based on the globally unique identifier.

[0092] Figure 8 An exemplary system architecture 800 is shown, which can be applied to a method or apparatus for generating a unique identifier according to embodiments of the present invention.

[0093] like Figure 8 As shown, system architecture 800 may include terminal devices 801, 802, and 803, a network 804, and a server 805. Network 804 serves as the medium for providing communication links between terminal devices 801, 802, and 803 and server 805. Network 804 may include various connection types, such as wired or wireless communication links or fiber optic cables, etc.

[0094] Users can use terminal devices 801, 802, and 803 to interact with server 805 via network 804 to receive or send messages, etc. Various communication client applications can be installed on terminal devices 801, 802, and 803, such as shopping applications, web browser applications, search applications, instant messaging tools, email clients, social media platform software, etc. (for example only).

[0095] Terminal devices 801, 802, and 803 can be various electronic devices with displays and web browsing capabilities, including but not limited to smartphones, tablets, laptops, and desktop computers.

[0096] Server 805 can be a server providing various services, such as a backend management server supporting shopping websites browsed by users using terminal devices 801, 802, and 803 (for example only). The backend management server can analyze and process data such as received product information query requests, and feed back the processing results (such as target push information and product information—for example only) to the terminal devices.

[0097] It should be noted that the method for generating a unique identifier provided in this embodiment of the invention is generally executed by server 805, and correspondingly, the device for generating a unique identifier is generally located in server 805.

[0098] It should be understood that Figure 8 The number of terminal devices, networks, and servers shown is merely illustrative. Depending on implementation needs, any number of terminal devices, networks, and servers can be included.

[0099] The following is for reference. Figure 9 It shows a schematic diagram of the structure of a computer system 900 suitable for implementing terminal devices or servers of the present invention. Figure 9 The terminal device or server shown is merely an example and should not impose any limitation on the functionality and scope of use of the embodiments of the present invention.

[0100] like Figure 9 As shown, the computer system 900 includes a central processing unit (CPU) 901, which can perform various appropriate actions and processes based on programs stored in read-only memory (ROM) 902 or programs loaded from storage section 908 into random access memory (RAM) 903. The RAM 903 also stores various programs and data required for the operation of the system 900. The CPU 901, ROM 902, and RAM 903 are interconnected via a bus 904. An input / output (I / O) interface 905 is also connected to the bus 904.

[0101] The following components are connected to I / O interface 905: an input section 906 including a keyboard, mouse, etc.; an output section 907 including a cathode ray tube (CRT), liquid crystal display (LCD), etc., and speakers, etc.; a storage section 908 including a hard disk, etc.; and a communication section 909 including a network interface card such as a LAN card, modem, etc. The communication section 909 performs communication processing via a network such as the Internet. A drive 910 is also connected to I / O interface 905 as needed. A removable medium 911, such as a disk, optical disk, magneto-optical disk, semiconductor memory, etc., is installed on drive 910 as needed so that computer programs read from it can be installed into storage section 908 as needed.

[0102] In particular, according to the embodiments disclosed in this invention, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, embodiments disclosed in this invention include a computer program product comprising a computer program carried on a computer-readable medium, the computer program containing program code for performing the methods shown in the flowcharts. In such embodiments, the computer program can be downloaded and installed from a network via communication section 909, and / or installed from removable medium 911. When the computer program is executed by central processing unit (CPU) 901, it performs the functions defined above in the system of this invention.

[0103] It should be noted that the computer-readable medium shown in this invention can be a computer-readable signal medium or a computer-readable storage medium, or any combination thereof. A computer-readable storage medium can be, for example,—but not limited to—an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof. More specific examples of a computer-readable storage medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer disk, a hard disk, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage device, magnetic storage device, or any suitable combination thereof. In this invention, a computer-readable storage medium can be any tangible medium containing or storing a program that can be used by or in conjunction with an instruction execution system, apparatus, or device. In this invention, a computer-readable signal medium can include a data signal propagated in baseband or as part of a carrier wave, carrying computer-readable program code. Such propagated data signals can take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination thereof. Computer-readable signal media can also be any computer-readable medium other than computer-readable storage media, which can send, propagate, or transmit a program for use by or in connection with an instruction execution system, apparatus, or device. The program code contained on the computer-readable medium can be transmitted using any suitable medium, including but not limited to: wireless, wire, optical fiber, RF, etc., or any suitable combination thereof.

[0104] The flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of systems, methods, and computer program products according to various embodiments of the present invention. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions indicated in the blocks may occur in a different order than those indicated in the drawings. For example, two consecutively indicated blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in a block diagram or flowchart, and combinations of blocks in a block diagram or flowchart, may be implemented using a dedicated hardware-based system that performs the specified function or operation, or using a combination of dedicated hardware and computer instructions.

[0105] The units or modules described in the embodiments of the present invention can be implemented in software or hardware. The described units or modules can also be housed in a processor; for example, a processor can be described as including a machine code registration module, an algorithm execution module, and an identifier concatenation module. The names of these units or modules do not necessarily limit the specific unit or module itself; for example, a machine code registration module can also be described as "a module for obtaining the machine code of the local machine and registering the machine code with a registration center."

[0106] In another aspect, the present invention also provides a computer-readable medium, which may be included in the device described in the above embodiments; or it may exist independently and not assembled into the device. The computer-readable medium carries one or more programs, which, when executed by the device, cause the device to: acquire its own machine code and register the machine code with a registration center; input the successfully registered machine code into an identifier generation algorithm, so that the identifier generation algorithm generates a first identifier based on the machine code; acquire a database identifier and a data table identifier, and concatenate the database identifier and the data table identifier with the first identifier to obtain a unique identifier.

[0107] According to the technical solution of this invention, the machine code of the local machine is obtained and registered with the registration center; the successfully registered machine code is input into the identifier generation algorithm, so that the identifier generation algorithm generates a first identifier based on the machine code; the database identifier and the data table identifier are obtained, and the database identifier and the data table identifier are concatenated with the first identifier to obtain a unique identifier. This ensures the global uniqueness of the unique identifier generated in the distributed cluster system environment, and does not require separate configuration of the machine, facilitating the horizontal scaling of the cluster. At the same time, in the case of data being stored in sharded databases and tables, data can be retrieved quickly and accurately. By improving the Snowflake algorithm, it can meet the application needs of different business scenarios, offering flexibility and convenience; by encapsulating the unique identifier generation method and device into a software development kit and introducing it into the project, it is non-intrusive to the business, and can generate unique identifiers locally with high generation efficiency and no network time consumption.

[0108] The specific embodiments described above do not constitute a limitation on the scope of protection of this invention. Those skilled in the art should understand that various modifications, combinations, sub-combinations, and substitutions can occur depending on design requirements and other factors. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of this invention should be included within the scope of protection of this invention.

Claims

1. A method of generating a unique identification, characterized by, The method comprises: obtaining a machine code of the local machine, and registering the machine code to a registration center; inputting the registered machine code to an identity generation algorithm, so that the identity generation algorithm generates a first identity according to the machine code; the identity generation algorithm is an improved snowflake algorithm, and the first identity is controllable in length and all numbers; obtaining a database identity and a data table identity, and splicing the database identity and the data table identity with the first identity to obtain a unique identity.

2. The method of claim 1, wherein, The method for obtaining the machine code of the local machine comprises: obtaining a network address of the local machine, and calculating the encoding of the network address; modulating the encoding of the network address by the maximum number of machines supported by the identity generation algorithm to obtain the machine code.

3. The method of claim 1, wherein, The method for inputting the registered machine code to the identity generation algorithm comprises: initializing the registered machine code to the identity generation algorithm in the form of a constructor parameter.

4. The method of claim 1, wherein, The identity generation algorithm comprises: a 1-bit identifier, a 40-bit timestamp, a 6-bit machine code, and a 7-bit random sequence number.

5. The method of claim 1, wherein, The identity generation algorithm comprises: a 1-bit identifier, a 38-bit timestamp, a 7-bit machine code, and an 8-bit random sequence number.

6. The method of claim 1, wherein, The method is implemented as a software development kit.

7. An apparatus for generating a unique identification, characterized by The method comprises: a machine code registration module for obtaining a machine code of the local machine, and registering the machine code to a registration center; an algorithm execution module for inputting the registered machine code to an identity generation algorithm, so that the identity generation algorithm generates a first identity according to the machine code; the identity generation algorithm is an improved snowflake algorithm, and the first identity is controllable in length and all numbers; an identity splicing module for obtaining a database identity and a data table identity, and splicing the database identity and the data table identity with the first identity to obtain a unique identity.

8. An electronic device generating a unique identification, characterized by The method comprises: one or more processors; a storage device for storing one or more programs, when the one or more programs are executed by the one or more processors, the one or more processors implement the method according to any one of claims 1-6.

9. A computer readable medium having stored thereon a computer program, characterized in that, The program is executed by the processor to implement the method according to any one of claims 1-6.

Citation Information

Patent Citations

  • Unique identifier generation method and device applied to distribution type system

    CN108647193A