A method, system and related device for generating distributed IDs
By employing multiple generation modes using Redis and database models in a distributed system, asynchronous detection and heartbeat monitoring, and dynamic switching of generation methods, the low fault tolerance problem caused by a single fixed generation method is solved, achieving stability and high availability of distributed ID generation.
Patent Information
- Application Number
- CN202611593851.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-10-15
- Publication Date
- 2026-11-24
AI Technical Summary
Existing distributed ID generators rely on a single, fixed generation method, resulting in low fault tolerance. They are prone to stalling under network fluctuations, hardware failures, or high concurrency pressure, affecting the stability and availability of distributed systems.
A distributed ID generation method with multiple generation modes is adopted, using Redis and database modes as the first and second generation modes. By asynchronously detecting the current number segment usage percentage and heartbeat monitoring, the generation mode is dynamically switched to ensure fault tolerance, including switching to database mode to generate distributed IDs when Redis is abnormal.
It improves the fault tolerance and stability of distributed ID generation, ensures the continuity and high availability of the distributed system under abnormal conditions, and avoids business interruption caused by single-mode failure.
Smart Images

Figure CN123164091A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of Internet technology, and in particular to a method, system and related apparatus for generating distributed IDs. Background Technology
[0002] With the development of internet technology, most of its system architectures adopt a distributed architecture. In a distributed architecture system, the data of each service, cluster, and other node must have a globally unique distributed ID to ensure data consistency within the distributed architecture system.
[0003] Existing distributed ID generators often use a single, fixed generation method to ensure that the distributed IDs are ordered and strictly incremental (for all generated IDs, the value of the later generated ID must be greater than the value of the earlier generated ID). Their existing ID generation architecture is weak and has low fault tolerance. Summary of the Invention
[0004] In view of the above problems, this application provides a method, system, and related apparatus for generating distributed IDs to improve the fault tolerance of distributed ID generation. The specific solution is as follows:
[0005] The first aspect of this application provides a method for generating a distributed ID, the method comprising:
[0006] In response to an ID generation request sent by the user, determine the method for generating the distributed ID;
[0007] If the distributed ID is generated in a strictly incremental manner, then the first generation mode is used to generate the distributed ID in the current number segment of the first generation mode. During the distributed ID generation process of the first generation mode, the percentage of the current distributed ID value in the current number segment of the first generation mode is asynchronously detected. If the percentage exceeds the threshold, the current number segment of the first generation mode is updated, and the current number segment of the second generation mode is updated synchronously.
[0008] The operation of the first generation mode is monitored. If an abnormality is detected in the operation of the first generation mode, the second generation mode is switched and the distributed ID is generated in the current number segment of the second generation mode.
[0009] In one possible implementation, the first generation mode is a remote dictionary service Redis, and the second generation mode is a database mode.
[0010] In one possible implementation, the process of updating the current number segment of the first generation mode, or updating the current number segment of the second generation mode, includes:
[0011] Obtain the maximum value of the current number segment, calculate the sum of the maximum value of the current number segment and the preset number segment step size, and use the sum as the updated maximum value of the current number segment;
[0012] Get the minimum value of the current number segment after the update, and obtain the updated current number segment.
[0013] In one possible implementation, monitoring the operation of the first generation mode includes:
[0014] A heartbeat request is sent to the server on which the first generation mode depends at preset intervals, and the response result of the heartbeat request is used to determine whether the first generation mode is malfunctioning.
[0015] In one possible implementation, the switching invocation of the second generation mode includes:
[0016] The application programming interface (API) of the dynamic naming and configuration service Nacos is invoked to modify the generation mode parameter of the distributed ID in the Nacos configuration, so that the generation mode of the distributed ID is switched from the first generation mode to the second generation mode.
[0017] One possible implementation also includes:
[0018] If the first generation mode resumes normal operation, then switch to calling the first generation mode and update the current number segment of the first generation mode and the current number segment of the second generation mode;
[0019] In the current number segment after the first generation mode is updated, the distributed ID continues to be generated.
[0020] One possible implementation also includes:
[0021] If the distributed ID is generated in a non-strictly increasing manner, then the third generation mode is invoked to generate the distributed ID, so that the generated distributed IDs exhibit an increasing trend.
[0022] A second aspect of this application provides a distributed ID generation system, the distributed ID generation system comprising:
[0023] The receiving unit is used to respond to the ID generation request sent by the user terminal and determine the generation method of the distributed ID;
[0024] The first generation unit is configured to generate the distributed ID in the current number segment of the first generation mode if the generation method of the distributed ID is a strictly incremental method.
[0025] The first generation unit is also used to asynchronously detect the percentage of the value of the current distributed ID in the current number segment of the first generation mode during the distributed ID generation process of the first generation mode. If the percentage exceeds the threshold, the current number segment of the first generation mode is updated, and the current number segment of the second generation mode is updated synchronously.
[0026] The detection unit is used to monitor the operation of the first generation mode. If the first generation mode is detected to be operating abnormally, the second generation unit is triggered.
[0027] The second generation unit is used to invoke the second generation mode and continue to generate the distributed ID in the current number segment of the second generation mode.
[0028] A third aspect of this application provides an electronic device, comprising at least one processor and a memory connected to the processor, wherein:
[0029] The memory is used to store computer programs;
[0030] The processor is used to execute the computer program so that the electronic device can implement the distributed ID generation method of the first aspect or any implementation thereof.
[0031] The fourth aspect of this application provides a computer program product including computer-readable instructions that, when executed on an electronic device, cause the electronic device to implement the distributed ID generation method of the first aspect or any implementation thereof.
[0032] By employing the above technical solutions, this application provides a method, system, and related apparatus for generating distributed IDs. When generating distributed IDs using a first generation mode, this method updates the current number segment of the first generation mode and simultaneously updates the current number segment of the second generation mode. If the first generation mode malfunctions, it can switch to the second generation mode to continue generating distributed IDs within the current number segment. This method utilizes multiple generation modes for distributed IDs, weakening the strong dependence on a single, fixed generation method. When one generation mode malfunctions, it can switch to another generation mode to continue generating distributed IDs, effectively improving the fault tolerance and stability of distributed ID generation. Attached Figure Description
[0033] The above and other features, advantages, and aspects of the embodiments of this disclosure will become more apparent from the accompanying drawings and the following detailed description. Throughout the drawings, the same or similar reference numerals denote the same or similar elements. It should be understood that the drawings are schematic, and the originals and elements are not necessarily drawn to scale.
[0034] Figure 1A flowchart illustrating a distributed ID generation method provided in an embodiment of this application;
[0035] Figure 2 A flowchart illustrating another method for generating a distributed ID provided in an embodiment of this application;
[0036] Figure 3 This is a schematic diagram of the structure of a distributed ID generation system provided in an embodiment of this application;
[0037] Figure 4 This application provides a hardware structure block diagram of an electronic device. Detailed Implementation
[0038] The embodiments of this application are described below with reference to the accompanying drawings. The terminology used in the implementation section of this application is for explaining specific embodiments only and is not intended to limit the scope of this application.
[0039] The embodiments of this application will now be described with reference to the accompanying drawings. Those skilled in the art will recognize that, with technological advancements and the emergence of new scenarios, the technical solutions provided in the embodiments of this application are equally applicable to similar technical problems.
[0040] The terms "first," "second," etc., used in the specification and accompanying drawings of this application are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such terms can be used interchangeably where appropriate; this is merely a way of distinguishing objects with the same attributes in the embodiments of this application. Furthermore, the terms "comprising" and "having," and any variations thereof, are intended to cover non-exclusive inclusion, so that a process, method, system, product, or apparatus that comprises a series of units is not necessarily limited to those units, but may include other units not explicitly listed or inherent to those processes, methods, products, or apparatuses.
[0041] With the rapid development of internet technology, distributed system architecture has become a core foundation supporting large-scale applications. In distributed systems, various service nodes, data clusters, and microservice components need to generate globally unique identifiers to ensure data consistency, avoid conflicts, and achieve efficient data management.
[0042] Existing distributed ID generators generally rely on a single, fixed mechanism to maintain either a strictly increasing ID value (subsequently generated IDs must be strictly greater than previously generated IDs) or a trend-increasing ID value (the overall value shows an increasing trend, but there are local cases where subsequently generated IDs are less than previously generated IDs) to meet business requirements for data ordering. However, this architecture is overly simplistic, lacking necessary redundancy mechanisms and fault tolerance. When core services are interrupted due to network fluctuations, hardware failures, or high concurrency pressure, the distributed ID generation process will immediately halt, causing the entire distributed system to be unable to process new data normally, leading to serious consequences such as service unavailability, data loss, or business interruption. Furthermore, a single mechanism is prone to performance bottlenecks when dealing with sudden traffic surges, making it difficult to guarantee high availability and continuity.
[0043] To address the aforementioned problems, embodiments of this application provide a method for generating distributed IDs. The method for generating distributed IDs according to embodiments of this application will be described in detail below with reference to the accompanying drawings.
[0044] Reference Figure 1 , Figure 1 A flowchart illustrating a distributed ID generation method provided in this application embodiment is shown below. Figure 1 As shown in the figure, the distributed ID generation method provided in this application embodiment may include steps S10 to S12, which are described in detail below.
[0045] S10. In response to the ID generation request sent by the user, determine the generation method of the distributed ID.
[0046] Distributed ID can refer to a globally unique identifier that is jointly generated and used by multiple services or nodes in a distributed system environment to ensure the uniqueness, traceability and consistency of data in a complex distributed system.
[0047] Users can choose between a strictly incremental or non-strictly incremental method for generating distributed IDs, based on their actual business needs. This embodiment provides an API (Application Programming Interface) to the user, allowing the user to select the generation method by calling the API.
[0048] Specifically, when an ID generation request is received from the user, the appropriate ID generation method can be determined based on preset rules or parameters carried in the ID generation request. For example, the business type or explicitly specified parameters in the ID generation request can be used to determine whether a strictly incremental distributed ID or another type of distributed ID is required. Optionally, a configuration table can be maintained internally, and the corresponding generation method can be determined by querying the configuration table based on the specific identifier in the ID generation request; alternatively, the required ID generation method can be indicated by flags in the ID generation request.
[0049] S11. If the distributed ID is generated in a strictly incremental manner, then the first generation mode is used to generate the distributed ID in the current number segment of the first generation mode.
[0050] The first generation mode can refer to the main mechanism or algorithm used to generate strictly incremental distributed IDs. This first generation mode can have high read and write performance, facilitate the rapid generation of distributed IDs, and is suitable for high-concurrency scenarios in distributed systems, achieving high availability of distributed IDs. Specifically, in this embodiment, the first generation mode can be Redis (Remote Dictionary Server). Redis is a high-performance key-value storage system, commonly used in caching, message queues, and real-time data processing scenarios. It supports atomic operations, enabling efficient atomic increment of values. Furthermore, Redis can store ID data in memory, providing extremely low latency and extremely high throughput. It also ensures data recovery after service restarts through persistence mechanisms, making Redis distributed ID generation highly adaptable, especially suitable for scenarios requiring strict increment and extremely high concurrency. Therefore, due to Redis's memory operation characteristics and atomic increment instructions, this embodiment preferentially uses Redis as the first generation mode to generate distributed IDs.
[0051] The current number segment in the first generation mode can refer to a pre-allocated numerical range for the ID generation mode. Within this range, the ID generation mode can generate distributed IDs sequentially or in other ways, and when the IDs within this number segment are used to a certain extent, it can trigger an update of the current number segment. The first generation mode can generate distributed IDs within the maximum value limit of the current number segment.
[0052] Specifically, during the distributed ID generation process in the first generation mode, this embodiment can asynchronously detect the percentage of the current distributed ID value within the current number segment in the first generation mode. If the percentage exceeds a threshold, the current number segment in the first generation mode is updated, and the current number segment in the second generation mode is updated synchronously. Asynchronous detection can refer to a non-blocking detection mechanism that monitors other operational states in parallel without interrupting or waiting for the main process. In this embodiment, to ensure a smooth transition of the current number segment, while the first generation mode continuously generates distributed IDs, an independent, non-blocking process or thread is set up to monitor the usage of the current number segment and update it accordingly.
[0053] After each distributed ID is generated, the value of the current distributed ID can be compared with the maximum value of the current number segment to determine the percentage of the current distributed ID value in the current number segment. For example, if the maximum value of the current number segment is 1000 and the value of the currently generated distributed ID is 300, then its percentage is 30% (30% = 300 ÷ 1000 × 100%).
[0054] In this embodiment, the percentage threshold can refer to a preset limit or proportion. If the proportion of distributed IDs generated in the current number segment is not lower than the threshold, the current number segment will be updated. Specifically, in this embodiment, the specific value of the threshold can be 30%, and the threshold can be updated in real time.
[0055] The second generation mode can refer to a backup or auxiliary distributed ID generation mechanism as the first generation mode. When the first generation mode encounters problems or fails to operate normally, it can be switched to the second generation mode to continue providing distributed ID generation services, thereby improving the fault tolerance of the system's distributed ID generation.
[0056] Specifically, in this embodiment, the second generation mode can be a database mode. A database mode refers to a mechanism for generating distributed IDs using a relational database (such as MySQL or PostgreSQL). In a relational database, distributed IDs can be generated through auto-incrementing primary keys, sequences, or dedicated sequence tables. The advantage of a database mode lies in its data persistence; it can provide highly reliable ID generation services and ensure the continuity and uniqueness of distributed IDs even after a system crash.
[0057] Of course, in another alternative embodiment, the database mode can be used as the first generation mode, while Redis can be used as the second generation mode. If the first generation mode fails, the system can switch to the second generation mode to continue generating distributed IDs.
[0058] Furthermore, if the distributed ID generation method is strictly incremental, the first generation mode is used; however, if the distributed ID generation method is not strictly incremental, the third generation mode is invoked to generate the distributed ID, ensuring that the generated distributed IDs exhibit a trend of increasing increments. This allows the system to flexibly select the distributed ID generation mode according to actual business needs. Specifically, the third generation mode in this embodiment can be CosId, which is a high-performance distributed ID generation tool with built-in distributed ID generation algorithms, such as the Snowflake algorithm. This embodiment can flexibly select different generation strategies according to the business requirements of the distributed ID, effectively improving the system's compatibility and scalability.
[0059] In this embodiment, to ensure that the second generation mode can immediately take over the generation of distributed IDs when needed, the current number segment of the first generation mode is updated simultaneously with the current number segment of the second generation mode. Synchronous update can refer to a coordinated update operation where, when multiple related data or states need to be updated, the update operations are executed simultaneously or in a manner that ensures consistency, to avoid data mismatches or inconsistencies.
[0060] The update process for the current number segment in both the first and second generation modes can be as follows: obtain the maximum value of the current number segment, calculate the sum of the maximum value of the current number segment and the preset number segment step size, and use the sum as the maximum value of the updated current number segment; obtain the minimum value of the updated current number segment, and obtain the updated current number segment.
[0061] The maximum value of the current number segment can refer to the maximum value of the IDs that have been allocated or reserved in the number segment currently in use. This maximum value can be read directly from the persistent storage system, cache system or memory variable that stores number segment information, or it can be inferred by querying the maximum distributed ID value that has been generated so far.
[0062] The preset number segment step size can refer to a pre-set value that represents the increase in the maximum value of the number segment each time the number segment is updated. This preset number segment step size can be a fixed value defined in the system configuration, or it can be dynamically adjusted according to factors such as system load and ID generation rate. By calculating the sum of the current maximum value of the number segment and the preset number segment step size, the upper limit of the new number segment can be determined, thereby expanding the range of available distributed IDs.
[0063] The minimum value of the current number segment can refer to the smallest number of IDs that have been allocated or reserved in the currently used number segment. For the first generation mode that is running, when updating the current number segment, only the maximum value of the current number segment can be updated, and the minimum value of the current number segment before the update can be directly reused to obtain the current number segment after the first generation mode is updated. For example, if the current number segment before the update is 1-1000, and when the percentage of distributed IDs in the current number segment is 35%, exceeding the threshold, then the maximum value of the current number segment is obtained as 1000. With a preset number segment step size of 500, the maximum value of the updated current number segment is 1500 (1500 = 1000 + 500). By reusing the minimum value of the current number segment before the update, the updated current number segment is 1-1500. For the second generation mode that is not yet running, when updating the current number segment, the minimum value of the current number segment can be updated simultaneously with the maximum value of the current number segment. Optionally, the minimum value of the current number segment after the update can be the value of the maximum value of the current number segment before the update plus 1; or the minimum value of the current number segment after the update can be the value of the maximum value of the current number segment before the update plus the offset and then plus 1. For example, if the current number segment before the update is 1-1000, and the percentage of distributed IDs in the current number segment is 35%, exceeding the threshold, then the maximum value of the current number segment is obtained as 1000, the preset number segment step size is 500, the maximum value of the current number segment after the update is 1500 (1500=1000+500), and the minimum value of the current number segment before the update is 1001 (1001=1000+1), then the updated current number segment is 1001-1500.
[0064] The updated current number segment can refer to a new range of values obtained after the above calculations, which can be used to generate distributed IDs. This range provides new space for distributed ID generation, ensuring the continuity of the distributed ID generation service. Automatic updates to the current number segment effectively solve the problem of how to smoothly and accurately expand the ID generation range when the number segment is about to run out, guaranteeing the continuous availability of the distributed ID service.
[0065] S12. Monitor the operation of the first generation mode. If an abnormality is detected in the operation of the first generation mode, switch to the second generation mode and continue to generate distributed IDs in the current number segment of the second generation mode.
[0066] The operational anomaly of the first generation mode can refer to a state that deviates from the expected behavior during operation, such as service downtime, response timeout, or resource exhaustion. This embodiment can configure a dedicated monitoring component to continuously monitor the operational status of the first generation mode during system operation.
[0067] Specifically, this embodiment includes a heartbeat monitoring module, which can send heartbeat requests to the server on which the first generation mode depends every preset time interval, and determine whether the first generation mode is malfunctioning based on the response results of the heartbeat requests.
[0068] The server on which the first generation mode depends can refer to: the hardware or software entity that carries or supports the normal operation of the first generation mode.
[0069] A heartbeat request can refer to a mechanism used to detect whether a remote service or server is alive and responsive. It is typically a lightweight request used to verify connection availability and basic service responsiveness. The response to a heartbeat request can refer to the information returned by the server after receiving the request. This response can take various forms, such as a success response code, specific response content, connection timeout, connection rejection, or error messages. This embodiment can determine whether the first generation mode is in an abnormal operating state based on the heartbeat request response. The criteria for this determination can include: not receiving a response within a preset time, receiving an error response, connection rejection, or response content not meeting expectations.
[0070] The preset duration for sending heartbeat requests by the heartbeat monitoring module can be set according to the requirements of real-time performance and resource consumption in the actual application scenario. It can be a fixed value or dynamically adjusted according to system load, network conditions, etc. Specifically, in this embodiment, the preset duration for sending heartbeat requests can be 1 second or 500 milliseconds.
[0071] This embodiment can proactively send heartbeat requests to the server running the first generation mode through the heartbeat monitoring module to detect the server's activity, network connectivity, and the basic responsiveness of the first generation mode. This ensures continuous monitoring of the first generation mode's operating status and rapid fault detection, providing a reliable basis for timely switching to the second generation mode, thereby guaranteeing the continuity and stability of the distributed ID generation service.
[0072] When this embodiment determines that the first generation mode is malfunctioning, it can switch to the second generation mode to continue generating distributed IDs. Specifically, this embodiment can call the Nacos application programming interface (API) to modify the distributed ID generation mode parameter in the Nacos configuration, thereby switching the distributed ID generation mode from the first generation mode to the second generation mode.
[0073] Nacos (Dynamic Naming and Configuration Service) can refer to a service discovery, configuration management, and service governance platform applied in microservice architectures. Its API provides a standardized programming interface, allowing external systems to send requests via protocols or invoke it using clients. This embodiment utilizes Nacos' configuration management capabilities to dynamically update preset configuration items. These items can indicate the generation mode used to generate distributed IDs, thereby modifying the distributed ID generation mode parameter in the Nacos configuration. Optionally, Nacos configuration modifications can be made automatically by calling its API or manually through the Nacos management console. In this embodiment, Nacos acts as an automatic switch for generation mode switching. When Nacos is on (the Nacos generation mode parameter is modified to true), it switches to Redis for distributed ID generation; when Nacos is off (the Nacos generation mode parameter is modified to false), it switches to database mode for distributed ID generation.
[0074] When Nacos configuration parameters are modified, Nacos can push the latest configuration information. In this embodiment, a Nacos configuration monitoring module can be set up, and its internal distributed ID generation mode can be dynamically adjusted according to the new configuration value received, so that the distributed ID generation mode can smoothly transition from the first generation mode to the second generation mode.
[0075] This embodiment, by introducing Nacos, effectively solves the problem of how to dynamically and smoothly switch distributed ID generation modes when the first generation mode malfunctions. Based on the dynamic switching mechanism of the configuration center, the system can quickly respond to anomalies in the first generation mode and switch to the second generation mode without restarting the service. This effectively reduces the risk of business interruption caused by failures in the first generation mode, thereby ensuring the continuity and high availability of the distributed ID generation service.
[0076] Furthermore, if the first generation mode malfunctions, the system switches to the second generation mode to continue generating distributed IDs. If the first generation mode resumes normal operation during the generation of distributed IDs in the second generation mode, the system can switch back to the first generation mode and update the current number segment of both the first and second generation modes. Distributed IDs can then continue to be generated within the updated current number segment of the first generation mode.
[0077] In this embodiment, the status of the first generation mode can be continuously monitored to determine whether it has returned to normal. For example, this can be done by periodically sending heartbeat requests, service availability detection, or resource utilization monitoring. When the abnormal indicators of the first generation mode return to the normal range and remain so for a period of time, it can be determined that the first generation mode has returned to normal operation. After the first generation mode returns to normal operation, this embodiment can automatically call Nacos to switch the generation mode. Of course, this embodiment can also be manually intervened by the user. After confirming that the first generation mode has returned to normal, the user can manually modify the Nacos configuration to switch the generation mode.
[0078] After switching generation modes, in order to avoid duplicate distributed IDs, this embodiment synchronously updates the current number segment of the first generation mode and the current number segment of the second generation mode to the next current number segment (the minimum value is the maximum value of the current number segment before the update plus 1, and the maximum value is the sum of the maximum value of the current number segment before the update and the preset number segment step size). The first generation mode skips the unused IDs in the current number segment before the update and starts directly from the updated current number segment to continue generating distributed IDs, effectively ensuring the strict incremental characteristics of distributed IDs.
[0079] Therefore, an overall embodiment is provided to illustrate the distributed ID generation process in this embodiment. The first generation mode is Redis, and the second generation mode is a database mode, as follows: Figure 2 As shown:
[0080] S20. The user selects the distributed ID generation method (including strict increment method and non-strict increment method), receives the ID generation request sent by the user, and determines whether the distributed ID generation method is strict increment method. If it is strict increment method, then execute step S21; if it is not strict increment method, then execute step S22.
[0081] S21. Determine whether the current generation mode of the distributed ID is Redis. If it is Redis, proceed to step S23; otherwise, proceed to step S25. Here, after the business project starts, the current generation mode of the distributed ID is determined by listening to the Nacos configuration.
[0082] S22. Trigger the CosId encapsulation module to generate a distributed ID using CosId;
[0083] S23. Use Redis to generate distributed IDs. Specifically, based on the current distributed ID, use the INCR auto-increment method (Redis's atomic auto-increment command, which increments by 1 each time it is called and returns the latest value) to generate a new distributed ID.
[0084] S24. Process the value of NextId (output the next new distributed ID for system calls).
[0085] S25. Generate a distributed ID using a database model. Specifically, under the current number segment in the database model, update the table fields by locking and querying, then return the new distributed ID and execute step S24.
[0086] S26. Each time Redis generates a distributed ID, asynchronously check the value of the current distributed ID and calculate the percentage it occupies in the current ID segment;
[0087] S27. Determine whether the percentage exceeds the threshold. If the percentage exceeds the threshold, proceed to step S28; if the percentage does not exceed the threshold, proceed to step S29.
[0088] S28. When using Redis to generate distributed IDs, update the current ID segment in Redis (update the maximum value of the current ID segment), and synchronously update the current ID segment in the database mode (update to the next ID segment).
[0089] S29. No action taken;
[0090] S30. Monitor the availability of Redis through the heartbeat monitoring module. The heartbeat monitoring module can send heartbeat requests to the server that Redis depends on at preset intervals. The availability of Redis is determined by the response results of the obtained heartbeat requests. When Redis is unavailable, change the configuration of Nacos to trigger step S25.
[0091] This application provides a method for generating distributed IDs. When generating distributed IDs using a first generation mode, the method updates the current number segment of the first generation mode and simultaneously updates the current number segment of a second generation mode. If the first generation mode malfunctions, the method can switch to the second generation mode to continue generating distributed IDs within the current number segment. This method employs multiple generation modes for distributed IDs, weakening the strong dependence on a single, fixed generation method. When one generation mode malfunctions, it can switch to another generation mode to continue generating distributed IDs, effectively improving the fault tolerance and stability of distributed ID generation.
[0092] The above describes a method for generating a distributed ID provided by an embodiment of this application. The following will describe a system that applies the above-described method for generating a distributed ID.
[0093] Please see Figure 3 , Figure 3 This is a schematic diagram of the structure of a distributed ID generation system provided in an embodiment of this application. Figure 3As shown, the distributed ID generation system may include:
[0094] The receiving unit 100 is used to respond to the ID generation request sent by the user terminal and determine the generation method of the distributed ID;
[0095] The first generation unit 110 is used to generate distributed IDs in the current number segment of the first generation mode if the generation method of the distributed ID is a strict increment method.
[0096] The first generation unit 110 is also used to asynchronously detect the percentage of the current distributed ID value in the current number segment of the first generation mode during the distributed ID generation process of the first generation mode. If the percentage exceeds the threshold, the current number segment of the first generation mode is updated, and the current number segment of the second generation mode is updated synchronously.
[0097] The detection unit 120 is used to monitor the operation of the first generation mode. If the first generation mode is detected to be operating abnormally, the second generation unit 130 is triggered.
[0098] The second generation unit 130 is used to call the second generation mode and continue to generate distributed IDs in the current number segment of the second generation mode.
[0099] In one possible implementation, the first generation mode can be specifically configured as a remote dictionary service Redis, and the second generation mode can be specifically configured as a database mode.
[0100] In one possible implementation, the process of updating the current number segment of the first generation mode or the current number segment of the second generation mode in the first generation unit 110 can be specifically configured as follows:
[0101] Get the maximum value of the current number segment, calculate the sum of the maximum value of the current number segment and the preset number segment step size, and use the sum as the updated maximum value of the current number segment; get the minimum value of the updated current number segment, and obtain the updated current number segment.
[0102] In one possible implementation, the detection unit 120 monitors the operation of the first generation mode, which can be specifically configured as follows:
[0103] Every preset time interval, a heartbeat request is sent to the server on which the first generation mode depends, and the response result of the heartbeat request is used to determine whether the first generation mode is running abnormally.
[0104] In one possible implementation, the detection unit 120 switches to the second generation mode, which can be specifically configured as follows:
[0105] Call the Nacos application programming interface (API) to modify the distributed ID generation mode parameter in the Nacos configuration, switching the distributed ID generation mode from the first generation mode to the second generation mode.
[0106] In one possible implementation, the detection unit 120 can also be specifically configured as follows:
[0107] If the first generation mode resumes normal operation, the first generation mode is switched and invoked, and the current number segment of the first generation mode and the current number segment of the second generation mode are updated; distributed IDs continue to be generated in the current number segment updated by the first generation mode.
[0108] In one possible implementation, the distributed ID generation system may also include a third generation unit;
[0109] The third generation unit is used to generate distributed IDs if the generation method of the distributed ID is not strictly incremental. In this case, the third generation mode is called to generate distributed IDs, so that the generated distributed IDs show an increasing trend.
[0110] This application also provides an electronic device in its embodiments. (See reference...) Figure 4 The diagram illustrates a structural schematic suitable for implementing the electronic device in the embodiments of this application. The electronic device in the embodiments of this application may include, but is not limited to, fixed terminals such as mobile phones, laptops, PDAs (personal digital assistants), PADs (tablet computers), desktop computers, etc. Figure 4 The electronic device shown is merely an example and should not impose any limitation on the functionality and scope of use of the embodiments of this application.
[0111] like Figure 4 As shown, the electronic device may include a processing unit (e.g., a central processing unit, a graphics processing unit, etc.) 401, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 402 or a program loaded from a storage device 408 into a random access memory (RAM) 403. When the electronic device is powered on, the RAM 403 also stores various programs and data required for the operation of the electronic device. The processing unit 401, ROM 402, and RAM 403 are interconnected via a bus 404. An input / output interface (I / O interface) 405 is also connected to the bus 404.
[0112] Typically, the following devices can be connected to I / O interface 405: input devices 406 including, for example, touchscreens, touchpads, keyboards, mice, cameras, microphones, accelerometers, gyroscopes, etc.; output devices 407 including, for example, liquid crystal displays (LCDs), speakers, vibrators, etc.; storage devices 408 including, for example, memory cards, hard drives, etc.; and communication devices 409. Communication device 409 allows electronic devices to communicate wirelessly or wiredly with other devices to exchange data. Although Figure 4 Electronic devices with various devices are shown, but it should be understood that it is not required to implement or have all of the devices shown. More or fewer devices may be implemented or have instead.
[0113] This application also provides a computer program product including computer-readable instructions, which, when executed on an electronic device, cause the electronic device to implement any of the distributed ID generation methods provided in this application.
[0114] This application also provides a computer-readable storage medium that carries one or more computer programs. When the one or more computer programs are executed by an electronic device, the electronic device can implement any of the distributed ID generation methods provided in this application.
[0115] It should also be noted that the system embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate, and the components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs. In addition, in the system embodiment drawings provided in this application, the connection relationship between modules indicates that they have a communication connection, which can be implemented as one or more communication buses or signal lines.
[0116] Through the above description of the embodiments, those skilled in the art can clearly understand that this application can be implemented by means of software plus necessary general-purpose hardware, or it can be implemented by special-purpose hardware including application-specific integrated circuits, special-purpose CPUs, special-purpose memory, special-purpose components, etc. Generally, any function performed by a computer program can be easily implemented by corresponding hardware, and the specific hardware structure used to implement the same function can also be diverse, such as analog circuits, digital circuits, or special-purpose circuits. However, for this application, software program implementation is more often the preferred implementation method. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product is stored in a readable storage medium, such as a computer floppy disk, USB flash drive, mobile hard disk, ROM, RAM, magnetic disk, or optical disk, etc., and includes several instructions to cause a computer device (which may be a personal computer, training equipment, or network device, etc.) to execute the methods described in the various embodiments of this application.
[0117] In the above embodiments, implementation can be achieved, in whole or in part, through software, hardware, firmware, or any combination thereof. When implemented in software, it can be implemented, in whole or in part, as a computer program product.
[0118] The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, all or part of the processes or functions described in the embodiments of this application are generated. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions may be stored in a computer-readable storage medium or transmitted from one computer-readable storage medium to another. For example, the computer instructions may be transmitted from one website, computer, training device, or data center to another website, computer, training device, or data center via wired (e.g., coaxial cable, fiber optic, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) means. The computer-readable storage medium may be any available medium that a computer can store or a data storage device such as a training device or data center that integrates one or more available media. The available media may be magnetic media (e.g., floppy disks, hard disks, magnetic tapes), optical media (e.g., DVDs), or semiconductor media (e.g., solid-state drives (SSDs)).
[0119] The various embodiments in this specification are described in a related manner. Similar or identical parts between embodiments can be referred to mutually. Each embodiment focuses on describing the differences from other embodiments. In particular, the apparatus embodiments are basically similar to the method embodiments, so the description is relatively simple; relevant parts can be referred to the descriptions of the method embodiments.
[0120] It is understood that before using the technical solutions disclosed in the various embodiments of this disclosure, users should be informed of the types, scope of use, and usage scenarios of the personal information involved in this disclosure in an appropriate manner in accordance with relevant laws and regulations, and user authorization should be obtained.
[0121] The above are merely embodiments of this application and are not intended to limit the scope of this application. Various modifications and variations can be made to this application by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of this application should be included within the protection scope of this application.
Claims
1. A method for generating a distributed ID, characterized in that, The method for generating the distributed ID includes: In response to an ID generation request sent by the user, determine the method for generating the distributed ID; If the distributed ID is generated in a strictly incremental manner, then the first generation mode is used to generate the distributed ID in the current number segment of the first generation mode. During the distributed ID generation process of the first generation mode, the percentage of the current distributed ID value in the current number segment of the first generation mode is asynchronously detected. If the percentage exceeds the threshold, the current number segment of the first generation mode is updated, and the current number segment of the second generation mode is updated synchronously. The operation of the first generation mode is monitored. If an abnormality is detected in the operation of the first generation mode, the second generation mode is switched and the distributed ID is generated in the current number segment of the second generation mode.
2. The method for generating a distributed ID according to claim 1, characterized in that, The first generation mode is a remote dictionary service Redis, and the second generation mode is a database mode.
3. The method for generating a distributed ID according to claim 1, characterized in that, The process of updating the current number segment of the first generation mode or the current number segment of the second generation mode includes: Obtain the maximum value of the current number segment, calculate the sum of the maximum value of the current number segment and the preset number segment step size, and use the sum as the updated maximum value of the current number segment; Get the minimum value of the current number segment after the update, and obtain the updated current number segment.
4. The method for generating a distributed ID according to claim 1, characterized in that, The monitoring of the operation of the first generation mode includes: A heartbeat request is sent to the server on which the first generation mode depends at preset intervals, and the response result of the heartbeat request is used to determine whether the first generation mode is malfunctioning.
5. The method for generating a distributed ID according to claim 1, characterized in that, The switching and invocation of the second generation mode includes: The application programming interface (API) of the dynamic naming and configuration service Nacos is invoked to modify the generation mode parameter of the distributed ID in the Nacos configuration, so that the generation mode of the distributed ID is switched from the first generation mode to the second generation mode.
6. The method for generating a distributed ID according to claim 1, characterized in that, Also includes: If the first generation mode resumes normal operation, then switch to calling the first generation mode and update the current number segment of the first generation mode and the current number segment of the second generation mode; In the current number segment after the first generation mode is updated, the distributed ID continues to be generated.
7. The method for generating a distributed ID according to claim 1, characterized in that, Also includes: If the distributed ID is generated in a non-strictly increasing manner, then the third generation mode is invoked to generate the distributed ID, so that the generated distributed IDs exhibit an increasing trend.
8. A distributed ID generation system, characterized in that, The distributed ID generation system includes: The receiving unit is used to respond to the ID generation request sent by the user terminal and determine the generation method of the distributed ID; The first generation unit is configured to generate the distributed ID in the current number segment of the first generation mode if the generation method of the distributed ID is a strictly incremental method. The first generation unit is also used to asynchronously detect the percentage of the value of the current distributed ID in the current number segment of the first generation mode during the distributed ID generation process of the first generation mode. If the percentage exceeds the threshold, the current number segment of the first generation mode is updated, and the current number segment of the second generation mode is updated synchronously. The detection unit is used to monitor the operation of the first generation mode. If the first generation mode is detected to be operating abnormally, the second generation unit is triggered. The second generation unit is used to invoke the second generation mode and continue to generate the distributed ID in the current number segment of the second generation mode.
9. An electronic device, characterized in that, It includes at least one processor and a memory connected to the processor, wherein: The memory is used to store computer programs; The processor is used to execute the computer program to enable the electronic device to implement the distributed ID generation method as described in any one of claims 1 to 7.
10. A computer program product, characterized in that, It includes computer-readable instructions that, when executed on an electronic device, cause the electronic device to implement the distributed ID generation method as described in any one of claims 1 to 7.
