Distributed id generation method based on optimized snowflake algorithm and application
Patent Information
- Application Number
- CN202011640737.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-12-31
- Publication Date
- 2026-10-27
- Estimated Expiration
- 2040-12-31
AI Technical Summary
[0005]然而,现有的雪花算法至少存在如下缺陷:一方面,雪花算法强依赖于时钟,如果时钟回拨,会生成重复的ID
[0036]本发明由于采用以上技术方案,与现有技术相比,作为举例,具有以下的优点和积极效果:本发明的分布式ID主要由时间戳、机器标识位和序列号位计算得到,一方面,将雪花算法中的时间戳位扩展到63bit、机器标识位(Workid)扩展到32bit、序列号位扩展到32bit之后,使得生成的ID的可用年限、服务数量和并发量上的限制显著降低;同时,机器标识位的数量提升后,基本无需考虑机器标识位复用的问题,不仅可以降低系统逻辑复杂度,还使得在遇到时钟回拨的情况时,可以通过更新切换机器标识位持续生成ID,提高了ID生成效率。另一方面,设置了独立的机器标识位注册服务模块,在需要机器标识位时,触发机器标识位注册服务模块连接一次数据库生成机器标识位即可,降低了对数据库的依赖。
Smart Images

Figure CN114691642B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of distributed systems technology, and in particular to a distributed ID generation method and its application based on an optimized snowflake algorithm. Background Technology
[0002] In distributed systems, it is often necessary to use identifiers to identify data. These identifiers must be globally unique within the same business scenario. For example, in a business system, each order is assigned a unique order number, which can be understood as the ID corresponding to the order; another example is the contract number in a contract system, or the sequence number such as the loan number or bank card number in a core, collateral, or collection system. These unique identifiers are distributed IDs.
[0003] There are many distributed ID generation algorithms, such as UUID, database primary key auto-increment, and Snowflake algorithm. UUID's advantage lies in its local generation without network I / O, resulting in high efficiency. However, its completely unordered nature leads to low database storage and query efficiency. Database primary key auto-increment is simple and convenient, with ordered incrementing, facilitating sorting and primary key index lookup. However, it relies on the database and can lead to duplicates in sharded databases. The Snowflake algorithm boasts high performance, low latency, guaranteed uniqueness in distributed environments, trend-incrementing characteristics, and independence from the database or other middleware, making it the most widely used algorithm for generating identifiers.
[0004] The Snowflake algorithm was proposed by Twitter. See [link / reference]. Figure 1 The diagram illustrates the bit distribution of a distributed ID (identifier) generated using the Snowflake algorithm in existing technologies. An ID typically consists of 64 binary bits: 1 bit for the sign, 41 bits for the timestamp (accurate to milliseconds), 10 bits for the worker machine ID, and 12 bits for the sequence number. The timestamp is generated based on the worker machine's local clock. Multiple identifiers are generated at the same timestamp, so these identifiers must be distinguished by different sequence numbers, which increase in number over time. In the 10-bit worker machine ID, Twitter uses the first 5 bits as the data center identifier and the last 5 bits as the machine identifier. The smallest counting unit for the timestamp generated by the existing Snowflake algorithm is milliseconds, therefore, it can represent a time range of 2^64 seconds. 41 / (3600*24*365*1000)≈69.7 years; can be deployed up to 2 10 =Out of 1024 working machines, within one millisecond, the same working machine can accommodate 2 12= 4096 distinct identifiers. The sum of the number of bits in the binary number representing the worker machine ID and the number of bits in the binary number representing the sequence number determines the system's concurrency capability; that is, the existing Snowflake algorithm can support 1024 * 4096 = 4,194,304 concurrent processes.
[0005] However, existing snowflake algorithms have at least the following drawbacks: Firstly, snowflake algorithms are heavily reliant on the clock. If the clock rolls back, duplicate IDs will be generated. Since the timestamp is generated based on the local clock of the working machine, when the working machine experiences clock rollback—that is, the working machine's clock rewinds from the current moment to a previous moment, such as an hour ago—the timestamp of the generated ID will also roll back. If the ID is still generated according to the rolled-back timestamp, it is possible that the generated ID is the same as the ID generated at that moment before the clock rollback, resulting in duplicate IDs.
[0006] On the other hand, the Snowflake algorithm has a fixed bit allocation and a limited lifespan (69.7 years), which may lead to the exhaustion of IDs after a certain period, making expansion impossible. At the same time, the machine identifier bit length is insufficient, making horizontal scaling of the server inconvenient.
[0007] In conclusion, whether the snowflake algorithm can be optimized and improved to provide a distributed ID generation scheme that can prevent duplicate IDs during clock rollback, has smaller limitations in terms of age and quantity, and has a lower dependence on databases is a technical problem that urgently needs to be solved. Summary of the Invention
[0008] The purpose of this invention is to overcome the shortcomings of existing technologies and provide a distributed ID generation method and application based on an optimized snowflake algorithm. The distributed ID generation method provided by this invention optimizes the snowflake algorithm by extending the timestamp bits to 63 bits, the machine identifier (Workid) bits to 32 bits, and the sequence number bits to 32 bits. This significantly reduces the limitations on the usable years, service quantity, and concurrency of the generated IDs. Furthermore, the extension of the machine identifier bits eliminates the need for reuse, allowing for continuous ID generation by updating the machine identifier bits during clock rollback. Simultaneously, a separate machine identifier registration service module is set up, which can generate machine identifier bits using a database. The machine identifier bit registration service module is only invoked when needed, reducing the dependence on the database.
[0009] To achieve the above objectives, the present invention provides the following technical solution:
[0010] A distributed ID generation method based on an optimized snowflake algorithm includes the following steps:
[0011] Receive ID generation request and obtain the current timestamp;
[0012] The machine identifier registration service module is triggered to generate the machine identifier, which can use the data persistence unit to generate the machine identifier.
[0013] Based on the aforementioned current timestamp and machine identifier, a distributed ID is generated using the optimized snowflake algorithm. The distributed ID generated by the optimized snowflake algorithm includes a 63-bit timestamp, a 32-bit machine identifier, and a 32-bit sequence number. The sequence number is generated by an auto-incrementing arbitrary value.
[0014] Furthermore, the distributed ID is a 128-bit digital identifier with a time sequence, and the first bit is the sign bit.
[0015] Furthermore, the steps to trigger the call to the machine identifier registration service module to generate the machine identifier include:
[0016] Check if the machine identifier is empty. If it is empty, call the machine identifier registration service module to generate a machine identifier.
[0017] Determine if the current timestamp is equal to the timestamp of the last ID generation; if they are equal, increment the sequence number bit by 1; if they are not equal, continue to determine if the current timestamp is greater than the timestamp of the last ID acquisition; if it is greater than the timestamp of the last ID acquisition, set the sequence number bit to 0; otherwise, determine if a clock rollback has occurred, set the sequence number bit to 0, and call the machine identifier bit registration service module again to generate a new machine identifier bit.
[0018] Furthermore, the machine identifier registration service module includes a data persistence unit, which is a database; the database stores a data table for generating machine identifiers, the data table uses the machine identifier as the primary key, and the primary key is set to an auto-incrementing type;
[0019] Alternatively, the machine identifier registration service module is connected to a data persistence unit, which is a database. The database stores a data table for generating machine identifiers, with the machine identifier as the primary key and the primary key set to an auto-incrementing type.
[0020] Furthermore, the steps for generating an ID using the optimized snowflake algorithm based on the aforementioned current timestamp and machine identifier include:
[0021] Perform bitwise operations on the aforementioned current timestamp, machine identifier, and sequence number; wherein, shift the aforementioned current timestamp to the left by one bit, the first bit being the total length of the machine identifier and sequence number bits, and then shift the aforementioned machine identifier to the left by two bits, the second bit being the length of the sequence number bits;
[0022] Perform an OR operation on the parts obtained after bitwise operations to get a unique ID.
[0023] The present invention also provides a distributed ID generation system based on an optimized snowflake algorithm, including a distributed ID microservice cluster unit and a distributed ID application layer unit with communication connection;
[0024] The distributed ID application layer unit is user-oriented and is used to collect users' ID generation requests and return the generated distributed IDs to users.
[0025] The distributed ID microservice cluster unit includes an ID generation service module and a machine identifier registration service module with communication connection. The machine identifier registration service module can generate machine identifiers using the data persistence unit.
[0026] The ID generation service module is configured to: receive an ID generation request, obtain the current timestamp based on the ID generation request and trigger the call to the machine identifier registration service module to generate a machine identifier; and generate a distributed ID using the optimized snowflake algorithm based on the aforementioned current timestamp and machine identifier. The distributed ID generated by the optimized snowflake algorithm includes a 63-bit timestamp, a 32-bit machine identifier, and a 32-bit sequence number, wherein the sequence number is generated by an auto-incrementing arbitrary value.
[0027] Furthermore, the distributed ID application layer unit includes a microservice module that connects to the ID generation service module. The microservice module sends ID generation requests to the aforementioned ID generation service module and obtains the distributed ID generated by the ID generation service module.
[0028] Furthermore, the distributed ID application layer unit integrates an ID generation module, which is integrated with the microservice module to form an integrated ID generation microservice module. The integrated ID generation microservice module is connected to the machine identifier registration service module of the distributed ID microservice cluster unit, and obtains ID generation requests and generates distributed IDs through the integrated ID generation microservice module.
[0029] Furthermore, the system also includes the aforementioned data persistence unit, which includes a database and an application database application system;
[0030] The database is connected to the machine identifier registration service module. The database stores a data table for generating machine identifiers. The data table uses the machine identifier as the primary key and sets the primary key to an auto-incrementing type.
[0031] The database application system is connected to the aforementioned integrated ID generation microservice module and is used to enter distributed IDs as primary keys into the database.
[0032] Furthermore, when the machine identifier registration service module is triggered to generate a machine identifier, the ID generation service module is configured to: monitor the generation process of the distributed ID, determine whether an interruption has occurred during the generation process, trigger the ID generation service to restart when an interruption occurs, and call the machine identifier registration service module to generate a new machine identifier when the ID generation service restarts.
[0033] Furthermore, when the machine identifier registration service module is triggered to generate the machine identifier, the ID generation service module is configured as follows:
[0034] Check if the machine identifier is empty. If it is empty, call the machine identifier registration service module to generate a machine identifier.
[0035] Determine if the current timestamp is equal to the timestamp of the last ID generation; if they are equal, increment the sequence number bit by 1; if they are not equal, continue to determine if the current timestamp is greater than the timestamp of the last ID acquisition; if it is greater than the timestamp of the last ID acquisition, set the sequence number bit to 0; otherwise, determine if a clock rollback has occurred, set the sequence number bit to 0, and call the machine identifier bit registration service module again to generate a new machine identifier bit.
[0036] Compared with existing technologies, this invention, by adopting the above technical solutions, has the following advantages and positive effects, as an example: The distributed ID of this invention is mainly calculated from timestamps, machine identifier bits, and sequence number bits. On the one hand, by extending the timestamp bit to 63 bits, the machine identifier bit (Workid) to 32 bits, and the sequence number bit to 32 bits in the Snowflake algorithm, the limitations on the usable years, service quantity, and concurrency of the generated ID are significantly reduced. Simultaneously, with the increased number of machine identifier bits, the issue of machine identifier bit reuse is largely eliminated, which not only reduces system logic complexity but also allows for continuous ID generation by updating and switching machine identifier bits in the event of clock rollback, improving ID generation efficiency. On the other hand, an independent machine identifier bit registration service module is set up. When a machine identifier bit is needed, the machine identifier bit registration service module is triggered to connect to the database once to generate the machine identifier bit, reducing dependence on the database. Attached Figure Description
[0037] Figure 1 A schematic diagram of the bit distribution of the snowflake algorithm provided by Twitter.
[0038] Figure 2 This is a schematic diagram of the bit distribution of a distributed ID provided in an embodiment of the present invention.
[0039] Figure 3 This is a flowchart of a distributed ID generation method according to an embodiment of the present invention.
[0040] Figure 4 The flowchart illustrates the judgment and processing of clock rollback in the distributed ID generation method provided in this embodiment of the invention.
[0041] Figure 5 This is a module structure diagram of the distributed ID generation system provided in an embodiment of the present invention.
[0042] Figure 6 This is an example diagram of information transmission in a distributed ID generation system provided in an embodiment of the present invention. Detailed Implementation
[0043] The following detailed description, in conjunction with the accompanying drawings and specific embodiments, provides a further detailed explanation of the distributed ID generation method and its application based on the optimized snowflake algorithm disclosed in this invention. It should be noted that the technical features or combinations of technical features described in the following embodiments should not be considered isolated; they can be combined to achieve better technical effects. In the accompanying drawings of the following embodiments, the same reference numerals in each drawing represent the same features or components, which can be applied to different embodiments. Therefore, once an item is defined in one drawing, it does not need to be further discussed in subsequent drawings.
[0044] It should be noted that the structures, proportions, sizes, etc., illustrated in the accompanying drawings are merely for illustrative purposes and to aid those skilled in the art in understanding and reading the invention. They are not intended to limit the conditions under which the invention can be implemented. Any modifications to the structure, changes in proportions, or adjustments to size, provided they do not affect the effectiveness or purpose of the invention, should fall within the scope of the technical content disclosed in the invention. The scope of the preferred embodiments of the present invention includes other implementations, wherein functions may be performed not in the order stated or discussed, including substantially simultaneously or in reverse order, depending on the functions involved. This should be understood by those skilled in the art to which the embodiments of the present invention pertain.
[0045] Techniques, methods, and apparatus known to those skilled in the art may not be discussed in detail, but where appropriate, such techniques, methods, and apparatus should be considered part of the specification. In all examples shown and discussed herein, any specific values should be interpreted as merely exemplary and not as limitations. Therefore, other examples of exemplary embodiments may have different values.
[0046] Example
[0047] This invention optimizes and improves Twitter's open-source Snowflake algorithm by extending the timestamp to 63 bits (accurate to milliseconds), the Workid to 32 bits, and the sequence number to 32 bits, ultimately generating a 128-bit integer as a distributed globally unique ID (hereinafter referred to as the distributed ID). See also Figure 2 As shown, the distributed ID is a 128-bit digital identifier with time sequence. The first bit (first position) is the sign bit, followed by a 63-bit timestamp bit, a 32-bit machine identifier bit, and a 32-bit sequence number bit.
[0048] The first bit (also called the highest bit) is the sign bit (also called the invalidation bit). It is 0 for positive numbers and 1 for negative numbers. Generally, the generated ID is a positive number, so it is 0.
[0049] The timestamp (also called the time bit) is 63 bits, and the smallest counting unit of the timestamp can be milliseconds (millisecond-level time). It can represent a time range of 2^66. 63 The value is 3600*24*365*1000, meaning its usage period is essentially unlimited. Generally, implementations don't store the current timestamp, but rather the difference between timestamps (current time - fixed start time), allowing generated IDs to start from a smaller value. The timestamp bits can be sorted by time, which helps improve query speed.
[0050] Machine identifier bits are suitable for identifying each node in a distributed environment with multiple nodes. The length of the 32-bit machine identifier bits can be designed and divided according to the number of nodes and deployment conditions. For example, 10 bits can be divided from the 32 bits to represent process bits, etc.
[0051] The sequence number is a series of auto-incrementing numbers, which can be generated by incrementing any value. A 32-bit counting sequence number supports generating 2 per millisecond per node. 32 Each ID serial number.
[0052] See Figure 3 As shown, this invention provides a distributed ID generation method based on an optimized snowflake algorithm, comprising the following steps:
[0053] S100 receives the ID generation request and obtains the current timestamp.
[0054] Preferably, the timestamp represents the difference between the current time and the base time. As an example, and not a limitation, the base time can be the initial startup time of the working machine (or working equipment).
[0055] S200, triggering the call to the machine identifier registration service module to generate machine identifier bits, the machine identifier registration service module can generate machine identifier bits using the data persistence unit.
[0056] In this embodiment, a machine identifier registration service is provided through a machine identifier registration service module. The function of the machine identifier registration service module is to generate machine identifiers using a data persistence unit.
[0057] The data persistence unit is a structure with data storage capabilities, and can be implemented using databases (DB), remote dictionary servers (REDIS), or distributed application coordination services (ZOOKEEPER). In this embodiment, a database is preferred as the data persistence unit.
[0058] In specific implementation, the machine identifier registration service module may include a database (the database is part of the machine identifier registration service module), the database stores a data table for generating machine identifiers, the data table uses the machine identifier as the primary key, and the primary key is set to an auto-increment type.
[0059] Alternatively, in another embodiment, the machine identifier registration service module is connected to a database (the machine identifier registration service module and the database are set separately), and the database stores a data table for generating machine identifiers, the data table using the machine identifier as the primary key and setting the primary key to an auto-incrementing type.
[0060] The database, as a typical example, is preferably an Oracle database (a relational database), which generates the machine identifier by using an auto-incrementing sequence and inserting it into a table with the machine identifier as the primary key, ensuring that it is not repeated, and the maximum length is 32 bits.
[0061] S300, based on the aforementioned current timestamp and machine identifier, a distributed ID is generated using the optimized snowflake algorithm. The distributed ID generated by the optimized snowflake algorithm includes a 63-bit timestamp, a 32-bit machine identifier, and a 32-bit sequence number. The sequence number is generated by an auto-incrementing arbitrary value.
[0062] The steps for generating an ID using the optimized snowflake algorithm based on the aforementioned current timestamp and machine identifier can be as follows: Perform bitwise operations on the aforementioned current timestamp, machine identifier, and sequence number; wherein, shift the aforementioned current timestamp to the left by one bit, the first bit being the total length of the machine identifier and sequence number bits, then shift the aforementioned machine identifier to the left by two bits, the second bit being the length of the sequence number bits; perform an OR operation on the parts obtained after the bitwise operations, that is, concatenate the parts that have undergone the bitwise operations in order to obtain a unique ID.
[0063] In one embodiment of this example, when the service system for generating distributed IDs restarts, it can trigger the call to the machine identifier registration service module to generate a machine identifier.
[0064] In another embodiment of this example, see Figure 4 As shown, the process also includes a clock rollback determination step. In this case, the specific steps for triggering the call to the machine identifier registration service module to generate the machine identifier bit are as follows: First, determine if the machine identifier bit is empty. If it is empty, call the machine identifier registration service module to generate a machine identifier bit. Second, determine if the current timestamp is equal to the timestamp of the last ID generation. If they are equal, increment the sequence number bit. If they are not equal, continue to determine if the current timestamp is greater than the timestamp of the last ID acquisition. If it is greater than the timestamp of the last ID acquisition, set the sequence number bit to 0. Otherwise, determine that a clock rollback has occurred, set the sequence number bit to 0, and call the machine identifier registration service module again to generate a new machine identifier bit. This method is particularly suitable for application scenarios where other related systems call the service system for generating distributed IDs.
[0065] The distributed ID generation method provided in this embodiment, on the one hand, can ensure normal service without refusing service by re-acquiring the Workid when a clock rollback occurs; on the other hand, by extending the length of the timestamp to 63 bits, the machine identifier to 32 bits, and the sequence number to 32 bits, the complexity of the machine identifier due to the need to consider reuse is reduced, so that the service life of the system and the horizontal expansion of the machine are basically unlimited.
[0066] Another embodiment of the present invention provides a distributed ID generation system based on an optimized snowflake algorithm.
[0067] The system may include a distributed ID microservice cluster unit and a distributed ID application layer unit with communication connections.
[0068] The distributed ID application layer unit is user-oriented and used to collect users' ID generation requests and return the generated distributed IDs to users. As an example, and not a limitation, the application layer can be a user token (marking sequence), a unique primary key in a database table, a call ID for tracing in a distributed system, an idempotent primary key in a microservice, etc.
[0069] The distributed ID microservice cluster unit includes an ID generation service module and a machine identifier registration service module with communication connections.
[0070] The ID generation service module can be connected to any microservice module, and can be connected to and used by any other service that requires distributed IDs.
[0071] The machine identifier registration service module can generate machine identifiers using the data persistence unit.
[0072] See Figure 5 As shown, the system may further include a data persistence unit, which is communicatively connected to the distributed ID microservice cluster unit and the distributed ID application layer unit, respectively.
[0073] In this embodiment, the ID generation service module is configured to: receive an ID generation request, obtain the current timestamp according to the ID generation request and trigger the call to the machine identifier registration service module to generate a machine identifier; and generate a distributed ID using the optimized snowflake algorithm based on the aforementioned current timestamp and machine identifier. The distributed ID generated by the optimized snowflake algorithm includes a 63-bit timestamp, a 32-bit machine identifier, and a 32-bit sequence number, wherein the sequence number is generated by an auto-incrementing arbitrary value.
[0074] In an optional embodiment, the distributed ID application layer unit may include a microservice module that connects to the ID generation service module. The microservice module sends an ID generation request to the aforementioned ID generation service module and obtains the distributed ID generated by the ID generation service module.
[0075] The microservice module is used to provide microservices. A microservice is a small service comprised of a single application, possessing its own process and lightweight processing. The service is designed according to business functions and deployed in a fully automated manner, typically communicating with other services via HTTP APIs. Depending on actual needs, the service can be implemented using different programming languages and database components. In this embodiment, the microservice module can send an ID generation request to the ID generation service module of the distributed ID microservice cluster unit, and then the ID generation service module sends a request to the machine identifier registration service module to register the machine identifier.
[0076] In an optional embodiment, the distributed ID application layer unit may further integrate an ID generation module. This ID generation module is integrated with the microservice module to form an integrated ID generation microservice module. This integrated ID generation microservice module is connected to the machine identifier registration service module of the distributed ID microservice cluster unit, and obtains ID generation requests and generates distributed IDs through this integrated ID generation microservice module.
[0077] In practical implementation, the integrated ID generation microservice module can receive ID generation requests and send a request to the machine identifier registration service module to register the machine identifier, thereby generating a distributed ID. This allows conventional microservice modules to generate distributed IDs, reducing service call processes and improving processing efficiency and performance.
[0078] Using the above scheme, the system can be configured with the following two usage modes: direct call mode and integrated SDK (Software Development Kit) mode.
[0079] In direct call mode, the distributed ID application layer unit can directly obtain the distributed ID by calling the ID generation service module. See [link / reference]. Figure 5 The information transmission method uses a solid line connecting the microservice module - ID generation service module - Wordid registration service module (i.e., machine identifier registration service module). This mode is also suitable for most application scenarios.
[0080] In integrated SDK mode, the distributed ID is generated by the SDK in the microservice module using the integrated ID. See also... Figure 5 The information transmission method between the integrated ID generation microservice module and the Wordid registration service module (i.e., the machine identifier registration service module) connected by dotted lines can bypass the microservice module and the ID generation service module.
[0081] Optionally, a microservice module and an integrated ID generation microservice module can be configured simultaneously to support multiple modes of information processing. The microservice module can also connect to the integrated ID generation microservice module for information interaction.
[0082] See also Figure 5 As shown, the distributed ID application layer unit may further include a user interface module and a log analysis module. The user interface module and log analysis module are connected to the microservice module, and / or the user interface module and log analysis module are connected to the integrated ID generation microservice module.
[0083] The user interface module is user-oriented and can be used to collect user ID generation requests and return the generated distributed ID to the user.
[0084] The log analysis module is used to generate log information.
[0085] In this embodiment, preferably, see [reference needed]. Figure 6 As shown, the microservice module is configured to: obtain the distributed ID generated by the ID generation service module.
[0086] Furthermore, the microservice module can also be configured to: initiate an idempotency request to the integrated ID generation microservice module using the distributed ID as the idempotency credential, as needed by the application; and / or use the primary key of the distributed ID as the user's token (mark sequence); and / or use the distributed ID as the primary key of the tracing log, etc.
[0087] The data persistence unit is a structure with data storage capabilities, and can be implemented using databases (DB), remote dictionary servers (REDIS), or distributed application coordination services (ZOOKEEPER). In this embodiment, a database is preferred as the data persistence unit.
[0088] Specifically, the data persistence unit may include the database and the database application system (or application database module).
[0089] The database is connected to the machine identifier registration service module. The database stores a data table for generating machine identifiers. The data table uses the machine identifier as the primary key and sets the primary key to an auto-incrementing type.
[0090] Optionally, the database application system can be connected to the aforementioned integrated ID generation microservice module for using distributed IDs as primary keys to enter data into the database.
[0091] In one embodiment of this example, when the machine identifier registration service module is triggered to generate a machine identifier, the ID generation service module is configured to: monitor the generation process of the distributed ID, determine whether an interruption has occurred during the generation process, trigger the ID generation service to restart when an interruption occurs, and call the machine identifier registration service module to generate a new machine identifier when the ID generation service restarts.
[0092] In another embodiment of this example, when the machine identifier registration service module is triggered to generate the machine identifier, the ID generation service module is configured as follows:
[0093] Check if the machine identifier is empty. If it is empty, call the machine identifier registration service module to generate a machine identifier.
[0094] Determine if the current timestamp is equal to the timestamp of the last ID generation; if they are equal, increment the sequence number bit by 1; if they are not equal, continue to determine if the current timestamp is greater than the timestamp of the last ID acquisition; if it is greater than the timestamp of the last ID acquisition, set the sequence number bit to 0; otherwise, determine if a clock rollback has occurred, set the sequence number bit to 0, and call the machine identifier bit registration service module again to generate a new machine identifier bit.
[0095] Other technical features are described in the previous embodiments and will not be repeated here.
[0096] In the above description, the disclosure of this invention is not intended to limit itself to these aspects. Rather, within the scope of the objectives of this disclosure, components can be selectively and operationally combined in any number. Furthermore, terms such as “comprising,” “encompassing,” and “having” should be interpreted by default as inclusive or open-ended, rather than exclusive or closed, unless explicitly defined as such. All technical, scientific, or other terms are to be understood by those skilled in the art, unless defined as such. Public terms found in dictionaries should not be interpreted in the context of the relevant technical documents in an overly idealistic or impractical manner, unless explicitly defined as such in this disclosure. Any modifications or alterations made by those skilled in the art based on the foregoing disclosure are within the scope of the claims.
Claims
1. A distributed ID generation system based on an optimized snowflake algorithm, characterized in that: This includes a distributed ID microservice cluster unit and a distributed ID application layer unit for communication connections; The distributed ID application layer unit is user-oriented and is used to collect users' ID generation requests and return the generated distributed IDs to users. The distributed ID microservice cluster unit includes an ID generation service module and a machine identifier registration service module connected by communication. The machine identifier registration service module can generate machine identifiers using a data persistence unit. The data persistence unit is connected by communication with the distributed ID microservice cluster unit and the distributed ID application layer unit, respectively. The ID generation service module is configured to: receive an ID generation request, obtain the current timestamp based on the ID generation request and trigger the call to the machine identifier registration service module to generate a machine identifier; and generate a distributed ID using the optimized snowflake algorithm based on the aforementioned current timestamp and machine identifier, which is a 128-bit numeric identifier with time order, including a first sign bit, a 63-bit timestamp bit, a 32-bit machine identifier bit, and a 32-bit sequence number bit, wherein the sequence number is generated by an auto-incrementing arbitrary value; Specifically, when the machine identifier registration service module is triggered to generate a machine identifier, the ID generation service module is configured to: determine whether the machine identifier is empty; if it is empty, call the machine identifier registration service module to generate a machine identifier; determine whether the current timestamp is equal to the timestamp of the last ID generation; if they are equal, increment the sequence number bit; if they are not equal, continue to determine whether the current timestamp is greater than the timestamp of the last ID acquisition; if it is greater than the timestamp of the last ID acquisition, set the sequence number bit to 0; otherwise, determine that a clock rollback has occurred, set the sequence number bit to 0, and call the machine identifier registration service module again to generate a new machine identifier.
2. The system according to claim 1, characterized in that: The distributed ID application layer unit includes a microservice module that connects to the ID generation service module. The microservice module sends ID generation requests to the aforementioned ID generation service module and obtains the distributed ID generated by the ID generation service module.
3. The system according to claim 1 or 2, characterized in that: The distributed ID application layer unit integrates an ID generation module. The ID generation module is integrated with the microservice module to form an integrated ID generation microservice module. The integrated ID generation microservice module is connected to the machine identifier registration service module of the distributed ID microservice cluster unit. The integrated ID generation microservice module obtains ID generation requests and generates distributed IDs.
4. The system according to claim 3, characterized in that: It also includes the aforementioned data persistence unit, The data persistence unit includes a database and a database application system; The database is connected to the machine identifier registration service module. The database stores a data table for generating machine identifiers. The data table uses the machine identifier as the primary key and sets the primary key to an auto-incrementing type. The database application system is connected to the aforementioned integrated ID generation microservice module and is used to enter distributed IDs as primary keys into the database.
5. The system according to claim 3, characterized in that: When both a microservice module and an integrated ID generation microservice module are set up, the microservice module is configured to use the distributed ID as the credential for determining the idempotency of the microservice to initiate an idempotency request to the integrated ID generation microservice module.
6. The system according to claim 1, characterized in that: When the machine identifier registration service module is triggered to generate a machine identifier, the ID generation service module is configured to: monitor the generation process of the distributed ID, determine whether an interruption has occurred during the generation process, trigger the ID generation service to restart when an interruption occurs, and call the machine identifier registration service module to generate a new machine identifier when the ID generation service restarts.
7. The system according to claim 1, characterized in that: The steps for generating an ID using the optimized snowflake algorithm based on the aforementioned current timestamp and machine identifier include: Perform bitwise operations on the aforementioned current timestamp, machine identifier, and sequence number; wherein, shift the aforementioned current timestamp to the left by one bit, the first bit being the total length of the machine identifier and sequence number bits, and then shift the aforementioned machine identifier to the left by two bits, the second bit being the length of the sequence number bits; Perform an OR operation on the parts obtained after bitwise operations to get a unique ID.