Snowflake ID generation method, device, equipment and computer-readable storage medium
By using the number of temporary self-increasing nodes in the snowflake ID generation algorithm for the residual calculation, the target machine ID is generated, which solves the problem of poor compatibility of snowflake ID in containerized deployment scenarios, and realizes the stable generation of snowflake ID under different deployment modes .
Patent Information
- Application Number
- CN202011197568.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-10-30
- Publication Date
- 2024-11-26
- Estimated Expiration
- 2040-10-30
AI Technical Summary
The existing snowflake ID generation algorithm has poor compatibility in containerized deployment scenarios and cannot adapt to machine ID representation problems caused by IP address changes after pod restart.
By obtaining the number of the temporary self-increase node, performing the remaining calculation to generate the target machine ID, and storing it to the new temporary self-increase node, ensuring that the IP address in the snowflake ID generation request can correctly generate a unique snowflake ID.
The compatibility and reliability of the snowflake ID generation algorithm is improved, so that it can adapt to different application deployment modes, especially in containerized deployment scenarios to stabilize the generation of unique snowflake IDs.
Smart Images

Figure CN112328261B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of distributed deployment, and in particular to a method, apparatus, device and computer-readable storage medium for generating a snowflake ID. Background Art
[0002] In order to better deploy applications of business systems, enterprise production environments usually use multiple application deployment modes, such as cloud host deployment, microservice deployment and containerized deployment. Therefore, the same application can be deployed on cloud hosts, microservices and containers at the same time. For applications deployed on service nodes such as cloud hosts, microservices and pods, there is a need to generate Snowflake IDs. For containerized deployment scenarios, the IP address of the pod will change after the pod is restarted. With the existing Snowflake ID generation algorithm, when the IP address of the pod changes, the machine ID of the pod will continue to increase, but the number of digits used to represent the machine ID in the Snowflake ID is unified, and there will be a problem that the machine ID cannot be represented in the Snowflake ID, making the existing Snowflake ID generation algorithm incompatible with containerized deployment scenarios and has poor compatibility. Summary of the invention
[0003] The embodiments of the present application provide a snowflake ID generation method, apparatus, device and computer-readable storage medium, aiming to improve the compatibility of the snowflake ID generation algorithm.
[0004] In a first aspect, an embodiment of the present application provides a method for generating a snowflake ID, comprising:
[0005] When a snowflake ID generation request sent by a service node is detected, the first node number of each temporary self-incrementing node and the second node number of the next temporary self-incrementing node are obtained;
[0006] Taking the modulus of a preset number according to each of the first node numbers to obtain a plurality of first remainders, and taking the modulus of the preset number according to the second node number to obtain a second remainder;
[0007] When the second remainder is different from the first remainders, generating a new temporary self-increment node, and storing the second remainder as the target machine ID in the new temporary self-increment node;
[0008] The target machine ID is sent to the service node according to the IP address in the Snowflake ID generation request, so that the service node can generate a Snowflake ID according to the target machine ID.
[0009] In a second aspect, an embodiment of the present application further provides a snowflake ID generating device, the snowflake ID generating device comprising:
[0010] A number acquisition module, used to acquire the first node number of each temporary self-incrementing node and the second node number of the next temporary self-incrementing node when a snowflake ID generation request sent by a service node is detected;
[0011] a remainder module, configured to perform a remainder operation on a preset number according to each of the first node numbers to obtain a plurality of first remainders, and perform a remainder operation on the preset number according to the second node number to obtain a second remainder;
[0012] A node self-increment module, used for generating a new temporary self-increment node when the second remainder is different from a plurality of the first remainders, and storing the second remainder as a target machine ID in the new temporary self-increment node;
[0013] The sending module is used to send the target machine ID to the service node according to the IP address in the Snowflake ID generation request, so that the service node can generate a Snowflake ID according to the target machine ID.
[0014] In a third aspect, an embodiment of the present application also provides a snowflake ID generating device, which includes a processor, a memory, and a computer program stored in the memory and executable by the processor, wherein when the computer program is executed by the processor, the steps of the snowflake ID generating method as described above are implemented.
[0015] In a fourth aspect, an embodiment of the present application further provides a computer-readable storage medium, on which a computer program is stored, wherein when the computer program is executed by a processor, the steps of the snowflake ID generation method as described above are implemented.
[0016] The embodiments of the present application provide a snowflake ID generation method, apparatus, device and computer-readable storage medium, which obtains multiple first remainders by taking the modulus of a preset number based on each first node number, and obtains a second remainder by taking the modulus of the preset number based on the second node number. Then, when the second remainder is different from the multiple first remainders, a new temporary self-incrementing node is generated, and the second remainder is stored in the new temporary self-incrementing node as the target machine ID. Finally, the target machine ID is sent to the service node according to the IP address in the snowflake ID generation request, and the service node generates a snowflake ID based on the target machine ID. This can ensure that the generated snowflake ID is unique, so that the snowflake ID generation algorithm can be compatible with different application deployment modes, greatly improving the reliability and compatibility of the snowflake ID generation algorithm. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0018] Figure 1 It is a flowchart of a method for generating a snowflake ID provided in an embodiment of the present application;
[0019] Figure 2 yes Figure 1 A schematic diagram of the sub-step flow of the snowflake ID generation method in FIG.
[0020] Figure 3 It is a flowchart of another snowflake ID generation method provided in an embodiment of the present application;
[0021] Figure 4 is a schematic block diagram of a snowflake ID generating device provided in an embodiment of the present application;
[0022] Figure 5 yes Figure 4 A schematic block diagram of the submodules of the snowflake ID generating device in;
[0023] Figure 6 is a schematic block diagram of another snowflake ID generating device provided in an embodiment of the present application;
[0024] Figure 7 It is a schematic block diagram of the structure of a snowflake ID generation device provided in an embodiment of the present application.
[0025] The realization of the purpose, functional features and advantages of this application will be further explained in conjunction with the embodiments and with reference to the accompanying drawings. DETAILED DESCRIPTION
[0026] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0027] The flowcharts shown in the accompanying drawings are only examples and do not necessarily include all the contents and operations / steps, nor must they be executed in the order described. For example, some operations / steps may also be decomposed, combined or partially merged, so the actual execution order may change according to actual conditions.
[0028] The embodiment of the present application provides a snowflake ID generation method, apparatus, device and computer-readable storage medium. The snowflake ID generation method can be applied to a snowflake ID generation device, which can be Zookeeper or Redis.
[0029] In conjunction with the accompanying drawings, some embodiments of the present application are described in detail below. In the absence of conflict, the following embodiments and features in the embodiments can be combined with each other.
[0030] See also Figure 1 , Figure 1 It is a flowchart of a snowflake ID generation method provided in an embodiment of the present application.
[0031] like Figure 1 As shown, the snowflake ID generation method includes steps S101 to S104.
[0032] Step S101: When a snowflake ID generation request sent by a service node is detected, the first node number of each temporary self-incrementing node and the second node number of the next temporary self-incrementing node are obtained.
[0033] Among them, the node number of the temporary self-increment node starts from 0 and increases by 1 each time. The temporary self-increment node stores the IP address and machine ID of service nodes such as the cloud host or pod (container group, pod is the smallest unit of k8s, the container is contained in the pod, and a pod has a pause container and several business containers). The storage form can be IP address-machine ID, for example, 192.168.0.1-0, 192.168.0.2-1. When the service node such as the cloud host or pod is restarted or stopped, the corresponding temporary self-increment node is deleted, and the machine ID in the temporary self-increment node is released for subsequent allocation. This can solve the problem that the corresponding machine ID is occupied and cannot be released after the pod is restarted or stopped, resulting in a reduction in the machine ID.
[0034] In one embodiment, a deletion instruction of a temporary self-incrementing node sent by a service node is obtained; the corresponding temporary self-incrementing node is deleted according to the deletion instruction, and the machine ID stored in the deleted temporary self-incrementing node is written into the candidate machine ID set as the candidate machine ID. For example, the IP address-machine ID stored in temporary self-incrementing node 0, temporary self-incrementing node 1, temporary self-incrementing node 2, temporary self-incrementing node 3, temporary self-incrementing node 4, temporary self-incrementing node 5, temporary self-incrementing node 6, temporary self-incrementing node 7, temporary self-incrementing node 8 and temporary self-incrementing node 9 are 192.168.0.1-0, 192.168.0.2-1, 192.168.0.3-2, 192.168.0.4-3, 192.168.0.5-4, 192.168.0.6-5, 192.168.0.7-6, 192.168.0.8-7-8, 192.168.0.9-9, 192.168.0.10-11, 192.168.0.11-12, 192.168.0.12-13, 192.168.0.13-14, 192.168.0.14-15, 192.168.0.15-16 .0.8-7, 192.168.0.9-8, 192.168.0.10-9. Since the cloud hosts or pods with IP addresses of 192.168.0.4, 192.168.0.7, and 192.168.0.9 are restarted or stopped, it is necessary to delete temporary auto-increment node 4, temporary auto-increment node 7, and temporary auto-increment node 9. Therefore, delete temporary auto-increment node 4, temporary auto-increment node 7, and temporary auto-increment node 10, and release machine IDs 4, 7, and 9, so that when machine IDs are allocated subsequently, machine IDs 4, 7, and 9 can be used as part of the machine ID to be allocated.
[0035] When a service node such as a cloud host or pod needs to generate a Snowflake ID, the IP address of the service node such as the cloud host or pod is obtained, and a Snowflake ID generation request is generated based on the IP address, and the Snowflake ID generation request is sent to the machine ID generation device.
[0036] When the machine ID generation device receives a Snowflake ID generation request sent by a service node such as a cloud host or pod, it determines whether there is a temporary self-increment node corresponding to the cloud host or pod in the machine ID generation device based on the IP address in the Snowflake ID generation request. If there is no temporary self-increment node corresponding to the cloud host or pod in the machine ID generation device, the first node number of each temporary self-increment node and the second node number of the next temporary self-increment node are obtained.
[0037] If there is a temporary self-increment node corresponding to the cloud host or pod in the machine ID generation device, the machine ID stored in the corresponding temporary self-increment node is sent to the cloud host or pod, and the cloud host or pod generates a snowflake ID based on the machine ID. The snowflake ID consists of a positive digit (1 bit) + timestamp (41 bits) + machine ID (5 bits) + computer room ID (5 bits) + self-increment (12 bits), a total of 64 bits of a Long type.
[0038] In one embodiment, if Figure 2 As shown, step S101 may include sub-steps S1011 to S1012.
[0039] Sub-step S1011, obtaining the first node number of each temporary self-incrementing node, and comparing the first node number of each temporary self-incrementing node to determine the largest first node number.
[0040] For example, the machine ID generating device includes 5 temporary self-incrementing nodes, and the first node numbers of the 5 temporary self-incrementing nodes are 100, 105, 150, 151 and 199 respectively. Through comparison, it is found that the largest first node number is 199.
[0041] Sub-step S1012: increment the largest first node number by 1 to obtain the second node number of the next temporary self-incrementing node.
[0042] For example, the machine ID generation device includes 5 temporary self-incrementing nodes, and the first node numbers of these 5 temporary self-incrementing nodes are 100, 105, 150, 151 and 199 respectively. Through comparison, it is found that the largest first node number is 199, and the second node number of the next temporary self-incrementing node is 200.
[0043] Step S102: modulo a preset number according to each of the first node numbers to obtain a plurality of first remainders, and modulo the preset number according to the second node number to obtain a second remainder.
[0044] Among them, the preset number can be set based on actual conditions, and the embodiment of the present application does not make any specific limitation on this. For example, the preset number is 1023.
[0045] For example, the preset number is 1023, the machine ID generating device includes 3 temporary self-incrementing nodes, and the first node numbers of these three temporary self-incrementing nodes are 1021, 1022 and 1023 respectively, the second node number of the next temporary self-incrementing node is 1024, 1021 is modulo 1023, the remainder is 1021, 1022 is modulo 1023, the remainder is 1022, 1023 is modulo 1023, the remainder is 0, 1024 is modulo 1023, the remainder is 1, therefore, the first remainder includes 1021, 1022 and 0, and the second remainder is 1.
[0046] For another example, the preset number is 1023, the machine ID generating device includes 3 temporary self-incrementing nodes, and the first node numbers of these three temporary self-incrementing nodes are 1025, 1026 and 1027 respectively, the second node number of the next temporary self-incrementing node is 1028, 1025 is modulo 1023, the remainder is 2, 1026 is modulo 1023, the remainder is 3, 1027 is modulo 1023, the remainder is 4, 1028 is modulo 1023, the remainder is 5, therefore, the first remainder includes 2, 3 and 4, and the second remainder is 5.
[0047] For another example, the preset number is 1023, the machine ID generating device includes 3 temporary self-incrementing nodes, and the first node numbers of these three temporary self-incrementing nodes are 2, 3 and 4 respectively, the second node number of the next temporary self-incrementing node is 1025, 2 is modulo 1023, the remainder is 2, 3 is modulo 1023, the remainder is 3, 4 is modulo 1023, the remainder is 4, and 1025 is modulo 1023, the remainder is 2. Therefore, the first remainder includes 2, 3 and 4, and the second remainder is 2.
[0048] Step S103: When the second remainder is different from the first remainders, a new temporary self-incrementing node is generated, and the second remainder is stored in the new temporary self-incrementing node as the target machine ID.
[0049] After obtaining the second remainder and multiple first remainders, determine whether the second remainder is different from the multiple first remainders. If it is determined that the second remainder is different from the multiple first remainders, generate a new temporary self-increment node, extract the IP address in the Snowflake ID generation request, and store the second remainder as the target machine ID to the new temporary self-increment node according to the IP address, and the node number of the new temporary self-increment node is the second node number.
[0050] For example, the preset number is 1023, the first node numbers of the three temporary self-incrementing nodes are 1021, 1022 and 1023 respectively, and the second node number is 1024. The three first remainders include 1021, 1022 and 0, and the second remainder is 1. By comparison, it is found that the second remainder is different from multiple first remainders, so a new temporary self-incrementing node is generated, and the node number of the new temporary self-incrementing node is 1024. The IP address in the Snowflake ID generation request is 192.168.0.25, then the second remainder 1 is stored as the target machine ID to the new temporary self-incrementing node, and the record of 192.168.0.25-1 can be obtained.
[0051] In one embodiment, when at least one of the multiple first remainders is the same as the second remainder, the second node number is updated; the preset number is modulo the updated second node number to obtain the updated second remainder; when the updated second remainder is different from the multiple first remainders, a new temporary self-incrementing node is generated, wherein the node number of the new temporary self-incrementing node is the updated second node number; when at least one of the multiple first remainders is the same as the updated second remainder, the second node number and the second remainder are continuously updated until the updated second remainder is different from the multiple first remainders. The difference between the second node number before the update and the second node number after the update is 1.
[0052] For example, the preset number is 1023, the first node numbers of the three temporary self-incrementing nodes are 2, 3 and 4 respectively, and the second node number is 1025. Therefore, the multiple first remainders include 2, 3 and 4, and the second remainder is 2. By comparison, it is found that the second remainder 2 is the same as 2 of the three first remainders, so the second node number is updated, and the updated second node number is 1026, so that the second remainder is updated to 3. By comparison, it is found that the updated second remainder 3 is the same as 3 of the three first remainders, so the second node number is updated again, and the updated second node number is 1027, so that the second remainder is updated to 4.
[0053] Through comparison, it is found that the updated second remainder 4 is the same as 4 among the three first remainders, so the second node number is updated again, and the updated second node number is 1028, so that the second remainder is updated to 5. Through comparison, it is found that the updated second remainder 5 is different from the three first remainders 2, 3 and 4, so a new temporary self-increment node is generated, and the node number of the new temporary self-increment node is 1028. The IP address in the snowflake ID generation request is 192.168.0.25, so the second remainder 5 is stored as the target machine ID in the new temporary self-increment node, and the record of 192.168.0.25-5 can be obtained.
[0054] Step S104: according to the IP address in the snowflake ID generation request, send the second remainder as the target machine ID to the service node, so that the service node generates a snowflake ID according to the target machine ID.
[0055] After the second remainder is stored as the target machine ID in the new temporary self-increment node, the target machine ID in the new temporary self-increment node is sent to the service node (cloud host or pod) according to the IP address in the snowflake ID generation request. The service node receives the target machine ID sent by the machine ID generation device, and generates a snowflake ID according to the target machine ID and the preset snowflake ID generation algorithm. Among them, the snowflake ID composition structure: positive digit (occupies 1 bit) + timestamp (occupies 41 bits) + machine ID (occupies 5 bits) + computer room ID (occupies 5 bits) + self-increment (occupies 12 bits), a total of 64 bits of a Long type.
[0056] In one embodiment, the first node number of each temporary self-incrementing node is obtained, and the second node number of the next temporary self-incrementing node is determined based on the first node number of each temporary self-incrementing node; the preset number is modulo the second node number to obtain a target remainder; the target remainder is compared with the machine ID stored in each temporary self-incrementing node, if the target remainder is different from the machine ID stored in each temporary self-incrementing node, a new temporary self-incrementing node is generated, and the target remainder is stored as the target machine ID in the new temporary self-incrementing node; according to the IP address in the snowflake ID generation request, the target machine ID is sent to the service node, so that the service node can generate a snowflake ID based on the target machine ID.
[0057] The snowflake ID generation method provided in the above embodiment obtains multiple first remainders by taking the modulus of the preset number according to each first node number, and obtains the second remainder by taking the modulus of the preset number according to the second node number. Then, when the second remainder is different from the multiple first remainders, a new temporary self-incrementing node is generated, and the second remainder is stored in the new temporary self-incrementing node as the target machine ID. Finally, the target machine ID is sent to the service node according to the IP address in the snowflake ID generation request, and the service node generates a snowflake ID according to the target machine ID. This can ensure that the generated snowflake ID is unique, so that the snowflake ID generation algorithm can be compatible with different application deployment modes, greatly improving the reliability and compatibility of the snowflake ID generation algorithm.
[0058] See also Figure 3 , Figure 3 It is a flowchart of another snowflake ID generation method provided in an embodiment of the present application.
[0059] like Figure 3 As shown, the snowflake ID generation method includes steps S201 to S204.
[0060] Step S201: When a snowflake ID generation request sent by a service node is detected, the first node number of each temporary self-incrementing node and the second node number of the next temporary self-incrementing node are obtained.
[0061] When a service node such as a cloud host or pod needs to generate a snowflake ID, the IP address of the service node such as the cloud host or pod is obtained, and a snowflake ID generation request is generated based on the IP address, and the snowflake ID generation request is sent to the machine ID generation device. When the machine ID generation device receives the snowflake ID generation request sent by the service node such as the cloud host or pod, it determines whether there is a temporary self-increment node corresponding to the cloud host or pod in the machine ID generation device based on the IP address in the snowflake ID generation request. If there is no temporary self-increment node corresponding to the cloud host or pod in the machine ID generation device, the first node number of each temporary self-increment node and the second node number of the next temporary self-increment node are obtained.
[0062] Step S202: when the second node number is less than or equal to the preset number, obtain a set of machine IDs to be selected, and select a target machine ID from the set of machine IDs to be selected.
[0063] After obtaining the second node number of the next temporary self-incrementing node, determine whether the second node number is greater than the preset number. When the second node number is less than or equal to the preset number, obtain the set of machine IDs to be selected, and select the target machine ID from the set of machine IDs to be selected. The selected target machine ID is different from the machine ID stored in each temporary self-incrementing node, and the preset number can be 1023.
[0064] In one embodiment, the method of obtaining the machine ID set to be selected may be: obtaining the machine ID stored in each temporary self-incrementing node, and determining the machine ID set to be selected according to the machine ID stored in each temporary self-incrementing node and a preset machine ID library. The machine ID stored in the preset machine ID library may be in the range of 0-1023.
[0065] For example, the machine ID generating device includes 1018 temporary self-incrementing nodes, and the largest first node among these 1018 temporary self-incrementing nodes is numbered 1021, then the second node is numbered 1022, the preset number is 1023, the machine IDs stored in the 1018 temporary self-incrementing nodes are between 0 and 1017, and the machine IDs stored in the preset machine ID library are between 0 and 1023. Therefore, 1018, 1019, 1020, 1021, 1022 and 1023 can be determined as the machine ID set to be selected.
[0066] In one embodiment, a deletion instruction of a temporary self-increment node sent by a service node is obtained, and the corresponding temporary self-increment node is deleted according to the deletion instruction; the machine ID stored in the deleted temporary self-increment node is written into the candidate machine ID set as the candidate machine ID to update the candidate machine ID set. For example, the IP address-machine IDs stored in temporary auto-increment node 0, temporary auto-increment node 1, temporary auto-increment node 2, temporary auto-increment node 3, temporary auto-increment node 4 and temporary auto-increment node 5 are 192.168.0.1-0, 192.168.0.2-1, 192.168.0.3-2, 192.168.0.4-3 and 192.168.0.5-4 respectively. Since the cloud host or pod with IP addresses 192.168.0.2 and 192.168.0.4 is restarted or stopped, temporary auto-increment node 2 and temporary auto-increment node 4 need to be deleted. Therefore, temporary auto-increment node 2 and temporary auto-increment node 4 are deleted, and machine IDs 1 and 3 are written into the candidate machine ID set.
[0067] In one embodiment, when the second node number is greater than a preset number, the preset number is modulo each first node number to obtain multiple first remainders, and the preset number is modulo the second node number to obtain a second remainder; when the second remainder is different from the multiple first remainders, a new temporary self-incrementing node is generated, and the second remainder is stored as the target machine ID in the new temporary self-incrementing node; based on the IP address in the snowflake ID generation request, the target machine ID is sent to the service node so that the service node can generate a snowflake ID based on the target machine ID.
[0068] Step S203: Generate a new temporary self-incrementing node, and store the selected target machine ID in the new temporary self-incrementing node.
[0069] After selecting the target machine ID from the set of machine IDs to be selected, a new temporary self-incrementing node is generated, and the selected target machine ID is stored in the new temporary self-incrementing node. For example, the machine ID generation device includes 1018 temporary self-incrementing nodes, and the largest first node number among the 1018 temporary self-incrementing nodes is 1021, then the second node number is 1022, the target machine ID is 1020 in the set of machine IDs to be selected {1018, 1019, 1020, 1021, 1022 and 1023}, and the IP address in the snowflake ID generation request is 192.168.0.98, then the storage record of the new temporary self-incrementing node is 192.168.0.98-1020.
[0070] Step S204: Send the target machine ID to the service node according to the IP address in the Snowflake ID generation request, so that the service node can generate a Snowflake ID according to the target machine ID.
[0071] After storing the target machine ID in the new temporary self-increment node, the target machine ID in the new temporary self-increment node is sent to the service node (cloud host or pod) according to the IP address in the Snowflake ID generation request. The service node receives the target machine ID sent by the machine ID generation device, and generates a Snowflake ID based on the target machine ID and the preset Snowflake ID generation algorithm.
[0072] The snowflake ID generation method provided in the above embodiment obtains the first node number of each temporary self-increment node and the second node number of the next temporary self-increment node, and then obtains the candidate machine ID set when the second node number is less than or equal to the preset number, and selects the target machine ID from the candidate machine ID set, and then generates a new temporary self-increment node, and stores the selected target machine ID in the new temporary self-increment node, and finally sends the target machine ID to the service node according to the IP address in the snowflake ID generation request. The service node generates a snowflake ID according to the target machine ID, which can ensure that the generated snowflake ID is unique, so that the snowflake ID generation algorithm can be compatible with different application deployment modes, which greatly improves the reliability and compatibility of the snowflake ID generation algorithm.
[0073] See also Figure 4 , Figure 4 It is a schematic block diagram of a snowflake ID generating device provided in an embodiment of the present application.
[0074] like Figure 4 As shown, the snowflake ID generating device 300 includes: a number acquisition module 310, a remainder module 320, a node self-increment module 330 and a sending module 340, wherein:
[0075] The number acquisition module 310 is used to acquire the first node number of each temporary self-incrementing node and the second node number of the next temporary self-incrementing node when a snowflake ID generation request sent by the service node is detected;
[0076] The remainder module 320 is used to perform a remainder operation on a preset number according to each of the first node numbers to obtain a plurality of first remainders, and perform a remainder operation on the preset number according to the second node number to obtain a second remainder;
[0077] The node self-increment module 330 is used to generate a new temporary self-increment node when the second remainder is different from the first remainders, and store the second remainder as the target machine ID in the new temporary self-increment node;
[0078] The sending module 340 is used to send the target machine ID to the service node according to the IP address in the Snowflake ID generation request, so that the service node can generate a Snowflake ID according to the target machine ID.
[0079] In one embodiment, if Figure 5 As shown, the number acquisition module 310 includes:
[0080] The number comparison submodule 311 is used to obtain the first node number of each temporary self-incrementing node, and compare the first node number of each temporary self-incrementing node to determine the largest first node number;
[0081] The number determination submodule 312 is used to increment the largest first node number by 1 to obtain the second node number of the next temporary self-incrementing node.
[0082] In one embodiment, the snowflake ID generating device 300 further includes:
[0083] A number updating module, configured to update the second node number when at least one of the plurality of first remainders is the same as the second remainder;
[0084] The remainder module 320 is further configured to perform a remainder operation on the preset number according to the updated second node number to obtain an updated second remainder;
[0085] The node self-increment module 330 is further used to generate a new temporary self-increment node when the updated second remainder is different from the multiple first remainders, wherein the node number of the new temporary self-increment node is the updated second node number.
[0086] See also Figure 6 , Figure 6 It is a schematic block diagram of another snowflake ID generating device provided in an embodiment of the present application.
[0087] like Figure 6 As shown, the snowflake ID generating device 400 includes: a number acquisition module 410, an ID selection module 420, a node self-increment module 430 and a sending module 440, wherein:
[0088] The number acquisition module 410 is used to acquire the first node number of each temporary self-incrementing node and the second node number of the next temporary self-incrementing node when a snowflake ID generation request sent by the service node is detected;
[0089] The ID selection module 420 is configured to obtain a candidate machine ID set and select a target machine ID from the candidate machine ID set when the second node number is less than or equal to the preset number;
[0090] The node self-increment module 430 is used to generate a new temporary self-increment node and store the selected target machine ID in the new temporary self-increment node;
[0091] The sending module 440 is used to send the target machine ID to the service node according to the IP address in the Snowflake ID generation request, so that the service node can generate a Snowflake ID according to the target machine ID.
[0092] In one embodiment, the selected target machine ID is different from the machine ID stored in each temporary self-increment node.
[0093] In one embodiment, the snowflake ID generating device 400 further includes:
[0094] A deletion module, used to obtain a deletion instruction of a temporary self-increment node sent by a service node, and delete the corresponding temporary self-increment node according to the deletion instruction;
[0095] The ID updating module is used to write the machine ID stored in the deleted temporary self-increment node into the candidate machine ID set as the candidate machine ID, so as to update the candidate machine ID set.
[0096] In one embodiment, the serial number acquisition module 410 is further used to:
[0097] Get the machine ID stored in each temporary auto-increment node;
[0098] The set of machine IDs to be selected is determined based on the machine IDs stored in each temporary self-increment node and the preset machine ID library.
[0099] It should be noted that technical personnel in the relevant field can clearly understand that, for the convenience and brevity of description, the specific working process of the above-described device and each module and unit can refer to the corresponding process in the aforementioned snowflake ID generation method embodiment, and will not be repeated here.
[0100] The apparatus provided in the above embodiment may be implemented in the form of a computer program. The computer program may be Figure 7 The Snowflake ID is shown running on the generated device.
[0101] See also Figure 7 , Figure 7 1 is a schematic block diagram of a snowflake ID generation device provided in an embodiment of the present application. The snowflake ID generation device may be Zookeeper or Redis.
[0102] like Figure 7 As shown, the snowflake ID generating device includes a processor, a memory and a network interface connected via a system bus, wherein the memory may include a storage medium and an internal memory.
[0103] The storage medium can store an operating system and a computer program. The computer program includes program instructions, and when the program instructions are executed, the processor can execute any snowflake ID generation method.
[0104] The processor is used to provide computing and control capabilities to support the operation of the entire snowflake ID generation device.
[0105] The internal memory provides an environment for the operation of the computer program in the storage medium. When the computer program is executed by the processor, the processor can execute any snowflake ID generation method.
[0106] The network interface is used for network communication, such as sending assigned tasks, etc. Those skilled in the art will appreciate that Figure 7 The structure shown in the figure is merely a block diagram of a partial structure related to the scheme of the present application, and does not constitute a limitation on the snowflake ID generating device to which the scheme of the present application is applied. The specific snowflake ID generating device may include more or fewer components than shown in the figure, or combine certain components, or have a different arrangement of components.
[0107] It should be understood that the processor may be a central processing unit (CPU), and the processor may also be other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. Among them, the general-purpose processor may be a microprocessor or the processor may also be any conventional processor, etc.
[0108] Those skilled in the art will understand that Figure 7 The structure shown in the figure is merely a block diagram of a partial structure related to the scheme of the present application, and does not constitute a limitation on the snowflake ID generating device to which the scheme of the present application is applied. The specific snowflake ID generating device may include more or fewer components than shown in the figure, or combine certain components, or have a different arrangement of components.
[0109] It should be understood that the processor may be a central processing unit (CPU), and the processor may also be other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. Among them, the general-purpose processor may be a microprocessor or the processor may also be any conventional processor, etc.
[0110] In one embodiment, the processor is used to run a computer program stored in the memory to implement the following steps:
[0111] When a snowflake ID generation request sent by a service node is detected, the first node number of each temporary self-incrementing node and the second node number of the next temporary self-incrementing node are obtained;
[0112] Taking the modulus of a preset number according to each of the first node numbers to obtain a plurality of first remainders, and taking the modulus of the preset number according to the second node number to obtain a second remainder;
[0113] When the second remainder is different from the first remainders, generating a new temporary self-increment node, and storing the second remainder as the target machine ID in the new temporary self-increment node;
[0114] The target machine ID is sent to the service node according to the IP address in the Snowflake ID generation request, so that the service node can generate a Snowflake ID according to the target machine ID.
[0115] In one embodiment, the processor is further configured to implement the following steps:
[0116] When at least one of the plurality of first remainders is the same as the second remainder, updating the second node number;
[0117] Taking the modulus of the preset number according to the updated second node number to obtain an updated second remainder;
[0118] When the updated second remainder is different from the plurality of the first remainders, a new temporary self-incrementing node is generated, wherein the node number of the new temporary self-incrementing node is the updated second node number.
[0119] In one embodiment, when obtaining the second node number of the next temporary self-incrementing node, the processor is used to implement:
[0120] Obtaining the first node number of each temporary self-incrementing node, and comparing the first node number of each temporary self-incrementing node to determine the largest first node number;
[0121] The largest first node number is incremented by 1 to obtain the second node number of the next temporary self-incrementing node.
[0122] In another embodiment, the processor is used to run a computer program stored in the memory to implement the following steps:
[0123] When a snowflake ID generation request sent by a service node is detected, the first node number of each temporary self-incrementing node and the second node number of the next temporary self-incrementing node are obtained;
[0124] When the second node number is less than or equal to the preset number, obtaining a set of machine IDs to be selected, and selecting a target machine ID from the set of machine IDs to be selected;
[0125] Generate a new temporary self-increment node, and store the selected target machine ID in the new temporary self-increment node;
[0126] The target machine ID is sent to the service node according to the IP address in the Snowflake ID generation request, so that the service node can generate a Snowflake ID according to the target machine ID.
[0127] In one embodiment, the selected target machine ID is different from the machine ID stored in each temporary self-increment node.
[0128] In one embodiment, the processor is further configured to:
[0129] Obtaining a deletion instruction of a temporary self-increment node sent by a service node, and deleting the corresponding temporary self-increment node according to the deletion instruction;
[0130] The machine ID stored in the deleted temporary self-increment node is written into the candidate machine ID set as the candidate machine ID to update the candidate machine ID set.
[0131] In one embodiment, when obtaining the set of machine IDs to be selected, the processor is used to implement:
[0132] Get the machine ID stored in each temporary auto-increment node;
[0133] The set of machine IDs to be selected is determined based on the machine IDs stored in each temporary self-increment node and the preset machine ID library.
[0134] It should be noted that, those skilled in the art can clearly understand that, for the sake of convenience and brevity of description, the specific working process of the snowflake ID generation device described above can refer to the corresponding process in the aforementioned snowflake ID generation method embodiment, and will not be repeated here.
[0135] Through the description of the above implementation methods, it can be known that those skilled in the art can clearly understand that the present application can be implemented by means of software plus a necessary general hardware platform. Based on this understanding, the technical solution of the present application can be essentially or partly contributed to the prior art in the form of a software product, which can be stored in a storage medium such as ROM / RAM, a disk, an optical disk, etc., including several instructions for enabling a snowflake ID generation device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment of the present application or some parts of the embodiments.
[0136] An embodiment of the present application also provides a computer-readable storage medium, on which a computer program is stored. The computer program includes program instructions. The method implemented when the program instructions are executed can refer to the various embodiments of the snowflake ID generation method of the present application.
[0137] The computer-readable storage medium may be volatile or non-volatile. The computer-readable storage medium may be an internal storage unit of the snowflake ID generating device described in the aforementioned embodiment, such as a hard disk or memory of the snowflake ID generating device. The computer-readable storage medium may also be an external storage device of the snowflake ID generating device, such as a plug-in hard disk, a smart memory card (Smart Media Card, SMC), a secure digital (Secure Digital, SD) card, a flash card (Flash Card), etc., equipped on the snowflake ID generating device.
[0138] Furthermore, the computer-readable storage medium may mainly include a program storage area and a data storage area, wherein the program storage area may store an operating system, an application required for at least one function, etc.; the data storage area may store data created according to the use of the blockchain node, etc.
[0139] The blockchain referred to in this application is a new application model of computer technologies such as distributed data storage, peer-to-peer transmission, consensus mechanism, encryption algorithm, etc. Blockchain is essentially a decentralized database, a string of data blocks generated by cryptographic methods. Each data block contains a batch of network transaction information, which is used to verify the validity of its information (anti-counterfeiting) and generate the next block. Blockchain can include the underlying blockchain platform, platform product service layer, and application service layer.
[0140] It should be understood that the terms used in this application specification are only for the purpose of describing specific embodiments and are not intended to limit the application. As used in this application specification and the appended claims, unless the context clearly indicates otherwise, the singular forms "a", "an" and "the" are intended to include plural forms.
[0141] It should also be understood that the term "and / or" used in the specification of this application and the appended claims refers to any combination of one or more of the associated listed items and all possible combinations, including these combinations. It should be noted that, in this article, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or system including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or system. In the absence of further restrictions, an element defined by the sentence "including a..." does not exclude the presence of other identical elements in the process, method, article or system including the element.
[0142] The serial numbers of the embodiments of the present application are for description only and do not represent the advantages and disadvantages of the embodiments. The above description is only a specific implementation mode of the present application, but the protection scope of the present application is not limited thereto. Any technician familiar with the technical field can easily think of various equivalent modifications or replacements within the technical scope disclosed in the present application, and these modifications or replacements should be included in the protection scope of the present application. Therefore, the protection scope of the present application shall be based on the protection scope of the claims.
Claims
1. A method for generating a snowflake ID, characterized in that: include: When a snowflake ID generation request sent by a service node is detected, the first node number of each temporary self-incrementing node and the second node number of the next temporary self-incrementing node are obtained; Taking the modulus of a preset number according to each of the first node numbers to obtain a plurality of first remainders, and taking the modulus of the preset number according to the second node number to obtain a second remainder; When the second remainder is different from the first remainders, generating a new temporary self-increment node, and storing the second remainder as the target machine ID in the new temporary self-increment node; According to the IP address in the snowflake ID generation request, the target machine ID is sent to the service node, so that the service node generates a snowflake ID according to the target machine ID; After obtaining the first node number of each temporary self-incrementing node and the second node number of the next temporary self-incrementing node, the method further includes: When the second node number is less than or equal to the preset number, obtaining a set of machine IDs to be selected, and selecting a target machine ID from the set of machine IDs to be selected; Generate a new temporary self-increment node, and store the selected target machine ID in the new temporary self-increment node, wherein the selected target machine ID is different from the machine ID stored in each temporary self-increment node; Among them, obtaining the second node number of the next temporary self-incrementing node includes: obtaining the first node number of each temporary self-incrementing node, and comparing the first node number of each temporary self-incrementing node to determine the largest first node number; increasing the largest first node number by 1 to obtain the second node number of the next temporary self-incrementing node.
2. The snowflake ID generation method according to claim 1, characterized in that: The method further comprises: When at least one of the plurality of first remainders is the same as the second remainder, updating the second node number; Taking the modulus of the preset number according to the updated second node number to obtain an updated second remainder; When the updated second remainder is different from the plurality of the first remainders, a new temporary self-incrementing node is generated, wherein the node number of the new temporary self-incrementing node is the updated second node number.
3. The snowflake ID generation method according to claim 1, characterized in that: The method further comprises: Obtaining a deletion instruction of a temporary self-increment node sent by a service node, and deleting the corresponding temporary self-increment node according to the deletion instruction; The machine ID stored in the deleted temporary self-increment node is written into the candidate machine ID set as the candidate machine ID to update the candidate machine ID set.
4. The snowflake ID generation method according to claim 1, characterized in that: The step of obtaining a set of IDs of machines to be selected includes: Get the machine ID stored in each temporary auto-increment node; The set of machine IDs to be selected is determined based on the machine IDs stored in each temporary self-increment node and the preset machine ID library.
5. A snowflake ID generating device, characterized in that: The snowflake ID generating device comprises: A number acquisition module, used to acquire the first node number of each temporary self-incrementing node and the second node number of the next temporary self-incrementing node when a snowflake ID generation request sent by a service node is detected; a remainder module, configured to perform a remainder operation on a preset number according to each of the first node numbers to obtain a plurality of first remainders, and perform a remainder operation on the preset number according to the second node number to obtain a second remainder; A node self-increment module, used for generating a new temporary self-increment node when the second remainder is different from a plurality of the first remainders, and storing the second remainder as a target machine ID in the new temporary self-increment node; A sending module, for sending the target machine ID to the service node according to the IP address in the snowflake ID generation request, so that the service node generates a snowflake ID according to the target machine ID; An ID selection module, configured to obtain a set of machine IDs to be selected and select a target machine ID from the set of machine IDs to be selected when the second node number is less than or equal to the preset number; The node self-increment module is used to generate a new temporary self-increment node, and store the selected target machine ID in the new temporary self-increment node, wherein the selected target machine ID is different from the machine ID stored in each temporary self-increment node; The number acquisition module includes: A number comparison submodule, used for obtaining the first node number of each temporary self-incrementing node, and comparing the first node number of each temporary self-incrementing node to determine the largest first node number; The number determination submodule is used to increment the largest first node number by 1 to obtain the second node number of the next temporary self-incrementing node.
6. A snowflake ID generating device, characterized in that: The snowflake ID generating device includes a processor, a memory, and a computer program stored in the memory and executable by the processor, wherein when the computer program is executed by the processor, the steps of the snowflake ID generating method according to any one of claims 1 to 4 are implemented.
7. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, wherein when the computer program is executed by a processor, the steps of the snowflake ID generation method according to any one of claims 1 to 4 are implemented.
