Serial number generation method and device
By integrating the sequence number generation component and global sequence management services in microservice applications, and using timestamps and identifications to generate sequence numbers, the high concurrency and clock callback problems in distributed systems are solved, and efficient and unique sequence number generation is achieved.
Patent Information
- Application Number
- CN202411336763.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-24
- Publication Date
- 2025-01-14
AI Technical Summary
In distributed systems, traditional serial number generation methods are difficult to meet the high concurrency and performance requirements, especially in financial scenarios, clock callback problems also lead to the problem of serial number duplication.
Adopting a distributed architecture, integrating the sequence number generation component into microservice applications, and adding global sequence management services, generating globally unique and ordered sequence numbers through timestamps, instance identification, clock identification and local sequence numbers, using global The sequence management service uniformly manages serial numbers to solve the clock callback problem.
It realizes efficient local generation of globally unique and ordered serial numbers, greatly improving the serial number generation performance, meeting the needs of distributed business systems in financial scenarios, and avoiding the duplication of serial numbers caused by clock callback.
Smart Images

Figure CN119311770A_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of computer technology, and in particular to a method and device for generating a serial number. Background Art
[0002] In distributed systems, many business scenarios require the generation and use of globally unique serial numbers (also known as globally unique identifiers), such as account serial numbers, serial numbers, and transaction order numbers in the core business systems of the banking industry. The rules for generating such serial numbers require sequence uniqueness and increment. Among them, the increment rule for account serial numbers is to add 1 each time, but the concurrency requirement is relatively low, and it is generally solved by using the method of accumulation using centralized data. For serial numbers, the rule concurrency is particularly high, and the serial number needs to be generated for each transaction. Traditional database solutions may not meet performance requirements. Summary of the invention
[0003] The present disclosure provides a method and device for generating a serial number, which can solve one of the technical problems in the related art at least to a certain extent. The technical solution of the present disclosure is as follows:
[0004] According to a first aspect of an embodiment of the present disclosure, a method for generating a serial number is provided, which is applied to a serial number generation component within a microservice application, and includes: receiving a serial number application request sent by any instance within the microservice application; sending a registration request to a global sequence management service to obtain an instance identifier assigned to the instance by the global sequence management service; generating a clock identifier corresponding to the instance based on a first preset rule, and initializing a local sequence number corresponding to the instance; wherein the clock identifier is used to identify a time window of the instance, and the local sequence number is used to identify a generation order of serial numbers within the corresponding time window; generating a target serial number based on a current timestamp, the instance identifier, the clock identifier, and the initialized local sequence number, and sending the target serial number to the instance.
[0005] According to a second aspect of an embodiment of the present disclosure, a serial number generating device is provided, which is applied to a serial number generating component within a microservice application, and includes: a receiving module, which is used to receive a serial number application request sent by any instance within the microservice application; a registration module, which is used to send a registration request to a global sequence management service to obtain an instance identifier assigned to the instance by the global sequence management service; a first processing module, which is used to generate a clock identifier corresponding to the instance based on a first preset rule, and initialize a local sequence number corresponding to the instance; wherein the clock identifier is used to identify a time window of the instance, and the local sequence number is used to identify a generation order of serial numbers within the corresponding time window; a second processing module, which is used to generate a target serial number based on a current timestamp, the instance identifier, the clock identifier and the initialized local sequence number, and send the target serial number to the instance.
[0006] According to a third aspect of an embodiment of the present disclosure, an electronic device is provided, comprising: a processor; and a memory for storing instructions executable by the processor; wherein the processor is configured to execute the instructions to implement the serial number generation method as described in the embodiment of the first aspect of the present disclosure.
[0007] According to a fourth aspect of an embodiment of the present disclosure, a computer-readable storage medium is provided. When instructions in the computer-readable storage medium are executed by a processor of an electronic device, the electronic device is enabled to execute the serial number generation method as described in the embodiment of the first aspect of the present disclosure.
[0008] According to a fifth aspect of an embodiment of the present disclosure, a computer program product is provided, including: a computer program, which, when executed by a processor, implements the serial number generation method as described in the embodiment of the first aspect of the present disclosure.
[0009] The technical solution provided by the embodiments of the present disclosure brings at least the following beneficial effects:
[0010] In the technical solution, a serial number application request sent by any instance in a microservice application is received; a registration request is sent to a global sequence management service to obtain an instance identifier assigned to the instance by the global sequence management service; a clock identifier corresponding to the instance is generated based on a first preset rule, and a local sequence number corresponding to the instance is initialized; wherein the clock identifier is used to identify the time window of the instance, and the local sequence number is used to identify the generation order of the serial number within the corresponding time window; a target serial number is generated based on the current timestamp, the instance identifier, the clock identifier and the initialized local sequence number, and the target serial number is sent to the instance. Thus, in the process of generating serial numbers, a centralized serial number generation method is not adopted, but a distributed architecture is used. The idea is to integrate the serial number generation component into the microservice application, and add a global sequence management service to uniformly manage the sequence, so as to directly generate the serial number locally, which greatly improves the performance of serial number generation and can meet the needs of distributed business systems in financial scenarios; wherein, the serial number is composed of a timestamp, an instance identifier, a clock identifier, and a sequence number. Therefore, by adding a clock identifier to the serial number, the clock identifier can be replaced after the clock is detected to avoid duplication of the generated sequence and solve the clock dialback problem caused by clock calibration and other reasons in large distributed systems; wherein, by adding a global sequence management service to allocate, manage, and recycle instance identifiers, the global uniqueness of each instance identifier in the distributed system is effectively guaranteed.
[0011] It is to be understood that the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the present disclosure. BRIEF DESCRIPTION OF THE DRAWINGS
[0012] The drawings herein are incorporated into and constitute a part of the specification, illustrate embodiments consistent with the present disclosure, and together with the description are used to explain the principles of the present disclosure, and do not constitute improper limitations on the present disclosure.
[0013] Figure 1 is a flowchart of a method for generating a serial number according to the first embodiment of the present disclosure;
[0014] Figure 2 is a flowchart of a method for generating a serial number according to a second embodiment of the present disclosure;
[0015] Figure 3 is a schematic diagram of a serial number shown in the third embodiment of the present disclosure;
[0016] Figure 4 is a flowchart of a method for generating a serial number according to a fourth embodiment of the present disclosure;
[0017] Figure 5 is a flowchart of a method for generating a serial number according to a fifth embodiment of the present disclosure;
[0018] Figure 6 It is a schematic diagram of the principle of serial number generation provided by the sixth embodiment of the present disclosure;
[0019] Figure 7 is a structural schematic diagram of a serial number generating device shown in the seventh embodiment of the present disclosure;
[0020] Figure 8 It is a schematic structural diagram of an electronic device 800 shown in an exemplary embodiment of the present disclosure. DETAILED DESCRIPTION
[0021] In order to enable ordinary persons in the art to better understand the technical solutions of the present disclosure, the technical solutions in the embodiments of the present disclosure will be clearly and completely described below in conjunction with the accompanying drawings.
[0022] It should be noted that the terms "first", "second", etc. in the specification and claims of the present disclosure and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchanged where appropriate, so that the embodiments of the present disclosure described herein can be implemented in an order other than those illustrated or described herein. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the present disclosure. Instead, they are merely examples of devices and methods consistent with some aspects of the present disclosure as detailed in the appended claims.
[0023] It should be noted that in the technical solution of the present disclosure, the collection, storage, use, processing, transmission, provision and disclosure of user personal information are all carried out with the user's consent, and are in compliance with relevant laws and regulations and do not violate public order and good morals.
[0024] In the related technologies, global serial number generation methods include database sequence, database auto-increment, UUID (Universally Unique Identifier), snowflake algorithm, Redis (Remote Dictionary Server) accumulation, etc. However, each method has its applicable scenarios and limitations:
[0025] 1. Database sequence method:
[0026] This method can ensure that the sequence numbers increase in order, but the performance cannot meet the requirements of large systems.
[0027] 2. Database self-increment prevention:
[0028] In a distributed architecture, the sequence number is not unique after the database is sharded, and the performance is not high, which is not suitable for a distributed cloud microservice unitized architecture.
[0029] 3. UUID method:
[0030] UUID is a standardized method for identifying information. It is a 128-bit number consisting of a set of 32 hexadecimal digits, usually represented by a 32-bit hexadecimal string. UUID can guarantee global uniqueness, but its generation algorithm has poor performance and the generated sequence is a 32-bit string, which is difficult to sort.
[0031] 4. Snowflake algorithm:
[0032] The Snowflake Algorithm is an algorithm for generating unique IDs (identifiers) in distributed systems. It was originally developed by Twitter. Its design goal is to generate globally unique and increasing IDs in distributed systems to solve the performance bottlenecks and single point failure problems that traditional database auto-increment IDs may cause in distributed environments. However, the original Snowflake Algorithm has a clock rollback problem and a worker ID calculation problem in a cloud-native environment.
[0033] 5. Redis accumulation method:
[0034] In Redis, you can use the INCR (INCrement) command to generate a unique serial number. Each time the INCR command is called, it automatically increments the value of the specified key by 1 and returns the incremented value as a unique serial number. Although this method is simple and efficient, there may be problems with sequence data loss and retrieval in a distributed environment.
[0035] To this end, the present disclosure proposes a method and device for generating serial numbers, which integrate serial number generation components into microservice applications using the idea of distributed architecture, and add a global sequence management service to uniformly manage sequences, so as to generate globally unique and ordered serial numbers locally with higher performance.
[0036] The following describes the method and device for generating a serial number according to the embodiments of the present disclosure with reference to the accompanying drawings.
[0037] Figure 1 It is a flowchart of the method for generating a serial number shown in the first embodiment of the present disclosure.
[0038] It should be noted that the serial number generation method of the embodiment of the present disclosure is applied to the serial number generation component in the microservice application program.
[0039] In the present disclosure, a serial number generation component is integrated into each microservice application, and the component obtains necessary information for generating a serial number locally by communicating with a global sequence management service.
[0040] like Figure 1 As shown, the serial number generation method includes the following steps:
[0041] Step 101: Receive a serial number application request sent by any instance in the microservice application.
[0042] In the microservice architecture, an instance refers to a specific running unit of a microservice application. Each microservice application can deploy one or more instances, which are distributed on different physical or virtual machines and provide services to the outside world through load balancing and other technologies. Each instance contains all or part of the code, configuration, dependencies, etc. of the microservice application and can run and process requests independently.
[0043] In order to improve the performance of sequence number generation, the present disclosure integrates a sequence number generation component into a microservice application, so that any instance in the microservice application can send a sequence number application request to the sequence number generation component in the microservice application when a sequence number is needed. Correspondingly, the sequence number generation component can receive a sequence number application request sent by any instance in the microservice application and perform corresponding processing.
[0044] As a possible implementation method, any instance in the microservice application can send a serial number application request to the serial number generation component through an internal call. Optionally, the serial number application request can include business information, such as business type, generation data, etc., to help the serial number generation component generate a serial number that better meets business requirements.
[0045] Step 102: Send a registration request to the global sequence management service to obtain an instance identifier allocated to the instance by the global sequence management service.
[0046] The global sequence management service can be understood as a centralized service or a coordination service in a distributed system, which is used to allocate, manage and recycle instance identifiers.
[0047] The instance identifier is the instance ID, which is used to uniquely identify the corresponding instance.
[0048] In order to ensure the global uniqueness of each instance identifier in a distributed system, a global sequence management service is added in the present disclosure to uniformly manage the sequence, so that the serial number generation component can send a registration request to the global sequence management service after receiving the serial number application request of the instance to obtain the instance identifier allocated to the instance by the global sequence management service.
[0049] As a possible implementation, the sequence number generation component may send a registration request to the global sequence management service through the target communication mechanism. Optionally, the registration request may include basic information of the instance, such as IP address, port number, service name, etc., so that the global sequence management service can identify and manage the instance.
[0050] In the present disclosure, the global sequence management service can assign a globally unique instance identifier to each instance after the instance is successfully registered. The instance identifier is used to uniquely identify the corresponding instance globally; the global sequence management service has high availability and scalability, and can ensure the generation of continuous and unique instance identifiers in a distributed environment.
[0051] Step 103: Generate a clock identifier corresponding to the instance based on the first preset rule, and initialize a local sequence number corresponding to the instance.
[0052] The clock identifier is used to identify the time window of the instance, and the local sequence number is used to identify the generation order of the sequence number in the corresponding time window.
[0053] In order to facilitate further distinguishing different time windows within the instance, avoid generating duplicate serial numbers, and solve the clock dial-back problem, as a possible implementation method, the serial number generation component can generate a clock identifier corresponding to the instance based on a first preset rule, wherein the first preset rule can be based on a hash value of the instance identifier, based on a timestamp, based on a random number, and so on.
[0054] In order to generate a unique sequence number in the same time window, as a possible implementation, the sequence number generation component can also initialize the local sequence number corresponding to the instance. As an example, the sequence number generation component can initialize the local sequence number corresponding to the instance to 0 as the starting point for generating sequence numbers in the corresponding time window.
[0055] Step 104: Generate a target sequence number according to the current timestamp, the instance identifier, the clock identifier and the initialized local sequence number, and send the target sequence number to the instance.
[0056] To ensure that the generated serial number is globally unique, as a possible implementation method, the serial number generation component can obtain the current timestamp, and combine it with the instance identifier, the clock identifier and the initialized local sequence number to generate a target serial number, and send the target serial number to the instance.
[0057] In the present disclosure, the serial number is composed of a timestamp, an instance identifier, a clock identifier, and a sequence number. Therefore, by adding a clock identifier to the serial number, the clock identifier can be changed after the clock is detected to avoid duplication of the generated sequence, thereby solving the clock callback problem caused by clock calibration and other reasons in large distributed systems.
[0058] The serial number generation method of the embodiment of the present disclosure receives a serial number application request sent by any instance in a microservice application; sends a registration request to a global sequence management service to obtain an instance identifier assigned to the instance by the global sequence management service; generates a clock identifier corresponding to the instance based on a first preset rule, and initializes a local sequence number corresponding to the instance; wherein the clock identifier is used to identify a time window of the instance, and the local sequence number is used to identify a generation order of serial numbers within the corresponding time window; generates a target serial number according to a current timestamp, an instance identifier, a clock identifier and an initialized local sequence number, and sends the target serial number to the instance. Thus, in the process of generating serial numbers, a centralized serial number generation method is not adopted, but the idea of a distributed architecture is used to integrate the serial number generation component into the microservice application, and a global sequence management service is added to uniformly manage the sequence, so as to realize direct generation of serial numbers locally, which greatly improves the performance of serial number generation and can meet the requirements of distributed business systems in financial scenarios.
[0059] In order to clearly explain how the target sequence number is generated according to the current timestamp, the instance identifier, the clock identifier and the initialized local sequence number in the above embodiment, the present disclosure proposes another sequence number generation method.
[0060] Figure 2 It is a flowchart of the method for generating a serial number shown in the second embodiment of the present disclosure.
[0061] Similarly, the serial number generation method of the embodiment of the present disclosure is also applied to the serial number generation component within the microservice application.
[0062] like Figure 2 As shown, the serial number generation method includes the following steps:
[0063] Step 201: Receive a serial number application request sent by any instance in the microservice application.
[0064] Step 202: Send a registration request to the global sequence management service to obtain an instance identifier allocated to the instance by the global sequence management service.
[0065] Step 203: Generate a clock identifier corresponding to the instance based on the first preset rule, and initialize a local sequence number corresponding to the instance.
[0066] Step 204, obtain the current timestamp, and determine the clock identifier corresponding to the currently generated serial number according to the clock identifier.
[0067] In the disclosed embodiment, if the system clock is dialed back (i.e., the system time is suddenly reduced), the sequence number generation component needs to be able to detect this situation and modify the clock identifier to prevent duplicate sequence numbers from being generated in the same time window. Optionally, the detection of clock dialing back can be achieved by comparing the current timestamp with the previous timestamp. If the current timestamp is less than the previous timestamp, it is considered that the clock dialing back has occurred.
[0068] As a possible implementation method, in response to not detecting a clock rollback, the clock identifier corresponding to the last generated serial number is determined as the clock identifier corresponding to the currently generated serial number; in response to detecting a clock rollback, the clock identifier corresponding to the last generated serial number is processed according to a second preset rule to generate a clock identifier corresponding to the currently generated serial number; wherein the clock identifier corresponding to the first generated serial number is the clock identifier (generated in step 203).
[0069] The second preset rule may be any pre-set rule, such as adding 1 to the clock identifier corresponding to the last generated serial number.
[0070] In summary, in the present disclosure, by adding a clock identifier to the sequence number and modifying the clock identifier in rotation after the clock rollback is detected, it is possible to avoid duplication of the generated sequence and solve the clock rollback problem.
[0071] Step 205, determining the local sequence number corresponding to the target sequence number generated this time according to the initialized local sequence number or the local sequence number corresponding to the target sequence number generated last time.
[0072] As a possible implementation method, in response to the fact that this is the first time the target sequence number is generated, the local sequence number corresponding to the target sequence number generated this time is determined based on the initialized local sequence number; in response to the fact that this is not the first time the target sequence number is generated, the local sequence number corresponding to the target sequence number generated this time is determined based on the local sequence number corresponding to the last time the target sequence number was generated.
[0073] Optionally, when determining the local sequence number corresponding to the target sequence number generated this time based on the initialized local sequence number, the initialized local sequence number can be determined as the local sequence number corresponding to the target sequence number generated this time; or, the initialized local sequence number can be processed according to a third preset rule to generate the local sequence number corresponding to the target sequence number generated this time.
[0074] The third preset rule may be any preset rule, such as adding 1 to the initialized local sequence number.
[0075] Similarly, when determining the local sequence number corresponding to the target sequence number generated this time based on the local sequence number corresponding to the target sequence number generated last time, the local sequence number corresponding to the target sequence number generated last time can be processed according to the fourth preset rule to generate the local sequence number corresponding to the target sequence number generated this time.
[0076] The fourth preset rule may be any pre-set rule, such as adding 1 to the local sequence number corresponding to the last generated target sequence number.
[0077] Step 206: Combine the current timestamp, the instance identifier, and the clock identifier and the local sequence number corresponding to the currently generated sequence number to obtain a first value of a set type of target bit number.
[0078] The target number of bits may be any pre-set number of bits. It should be noted that in order to ensure that there is enough space to store all necessary information of the serial number and to allow for sufficient growth and change, the target number of bits should be as large as possible, such as 64 bits.
[0079] The setting type may be any pre-set type, such as a bit type.
[0080] As an example, Figure 3 As shown, the current timestamp, the instance identifier, and the clock identifier and the local sequence number corresponding to the sequence number generated this time can be combined into a first value of the bit type with a length of 64 bits.
[0081] Step 207, generating a target sequence number according to a first value of a setting type of a target number of digits.
[0082] In order to make the processing of long data more convenient, as a possible implementation method, the first value of the set type of the target bit number is converted to obtain a second value of the target type; the second value is determined as the target sequence number generated this time.
[0083] As an example, the first value of the bit type with a length of 64 bits formed in step 206 may be converted into a second value of the long type.
[0084] Step 208: Send the target sequence number to the instance.
[0085] It should be noted that the execution process of steps 201 to 203 and step 208 can be implemented in any way in the embodiments of the present disclosure, and the embodiments of the present disclosure do not limit this and will not be described in detail.
[0086] The serial number generation method of the embodiment of the present disclosure obtains the current timestamp, and determines the clock identifier corresponding to the serial number generated this time according to the clock identifier; determines the local sequence number corresponding to the target serial number generated this time according to the initialized local sequence number or the local sequence number corresponding to the target serial number generated last time; determines the local sequence number corresponding to the target serial number generated this time according to the initialized local sequence number or the local sequence number corresponding to the target serial number generated last time; combines the current timestamp, the instance identifier, and the clock identifier and the local sequence number corresponding to the serial number generated this time to obtain a first numerical value of the setting type of the number of target bits; generates the target serial number according to the first numerical value of the setting type of the number of target bits, thereby, the serial number is positively correlated with the generation time, the generated sequence can be avoided from being repeated, and the clock dial-back problem can be solved.
[0087] It should be noted that in the present disclosure, the sequence number generation component can also periodically send a renewal request to the global sequence management service. Figure 4 This process is described below.
[0088] Figure 4 It is a flowchart of the serial number generation method shown in the fourth embodiment of the present disclosure.
[0089] Similarly, the serial number generation method of the embodiment of the present disclosure is also applied to the serial number generation component within the microservice application.
[0090] like Figure 4 As shown, the serial number generation method includes the following steps:
[0091] Step 401: Receive a serial number application request sent by any instance in the microservice application.
[0092] Step 402: Send a registration request to the global sequence management service to obtain an instance identifier allocated to the instance by the global sequence management service.
[0093] Step 403: Generate a clock identifier corresponding to the instance based on the first preset rule, and initialize a local sequence number corresponding to the instance.
[0094] Step 404: Generate a target sequence number according to the current timestamp, the instance identifier, the clock identifier and the initialized local sequence number, and send the target sequence number to the instance.
[0095] Step 405, periodically sending a renewal request to the global sequence management service, so that when the global sequence management service receives the renewal request, it updates the expiration time of the instance identifier based on the renewal request, or, after the instance identifier expires, if no renewal request is received within a set period of time, reclaims the instance identifier.
[0096] The set duration may be any set duration, such as the duration of the time interval for the serial number generation component to send a renewal request.
[0097] In order to facilitate the global sequence management service to promptly learn the activity of each instance, as a possible implementation method, the serial number generation component can send a renewal request to the global sequence management service at a set interval (i.e., every set time interval, such as every few minutes), so that when the global sequence management service receives the renewal request, it can update the expiration time of the instance identifier based on the renewal request, or, after the instance identifier expires, if no renewal request is received within the set time, the instance identifier will be reclaimed.
[0098] It should be noted that the execution process of steps 401 to 404 can be implemented in any manner in the embodiments of the present disclosure, and the embodiments of the present disclosure do not limit this and will not be described in detail.
[0099] The serial number generation method of the embodiment of the present disclosure sends a renewal request to the global sequence management service at a regular interval so that when the global sequence management service receives the renewal request, it updates the expiration time of the instance identifier based on the renewal request, or recycles the instance identifier if no renewal request is received within the specified time. This can prevent the global sequence management service from erroneously reclaiming the instance identifier when the instance is still active. At the same time, the global sequence management service can also rely on the timeout mechanism to recycle the instance identifier that has not been renewed.
[0100] It should be noted that in the present disclosure, the serial number generation component can also send a deregistration request to the global sequence management service when the instance ends or terminates abnormally. Figure 5 This process is described below.
[0101] Figure 5 It is a flowchart of the serial number generation method shown in the fifth embodiment of the present disclosure.
[0102] Similarly, the serial number generation method of the embodiment of the present disclosure is also applied to the serial number generation component within the microservice application.
[0103] like Figure 5 As shown, the serial number generation method includes the following steps:
[0104] Step 501: Receive a serial number application request sent by any instance in the microservice application.
[0105] Step 502: Send a registration request to the global sequence management service to obtain an instance identifier allocated to the instance by the global sequence management service.
[0106] Step 503: Generate a clock identifier corresponding to the instance based on the first preset rule, and initialize a local sequence number corresponding to the instance.
[0107] Step 504: Generate a target sequence number according to the current timestamp, the instance identifier, the clock identifier and the initialized local sequence number, and send the target sequence number to the instance.
[0108] Step 505: In response to the instance ending or abnormal termination, a deregistration request is sent to the global sequence management service, so that the global sequence management service reclaims the instance identifier based on the deregistration request.
[0109] In order to facilitate the global sequence management service to recycle each instance identifier in a timely manner, as a possible implementation method, the serial number generation component can send a deregistration request to the global sequence management service when the instance ends or terminates abnormally, so that the global sequence management service can recycle its instance identifier based on the deregistration request.
[0110] It should be noted that if the instance fails to send a deregistration request normally (for example, due to a crash or network problem), the global sequence management service can rely on a timeout mechanism to reclaim the unrenewed instance ID. That is, the global sequence management service can reclaim the instance ID after the instance ID expires if no renewal request is received within the set time period.
[0111] It should be noted that the execution process of steps 501 to 504 can be implemented in any way in the embodiments of the present disclosure, and the embodiments of the present disclosure do not limit this and will not be described in detail.
[0112] The serial number generation method of the embodiment of the present invention sends a deregistration request to the global sequence management service in response to the end or abnormal termination of the instance, so that the global sequence management service reclaims the instance identifier based on the deregistration request. As a result, the global sequence management service can promptly reclaim each instance identifier so as to reallocate it to other instances.
[0113] Based on any embodiment of the present disclosure, Figure 6 As shown, the serial number generation method of the embodiment of the present disclosure can also be implemented based on the following steps:
[0114] Step 1: Serial Number Application
[0115] like Figure 6 As shown, a serial number generation component is integrated into the microservice application, which obtains the necessary information for generating serial numbers locally by communicating with the global sequence management service. Figure 6In the microservice application, the global sequence management service is responsible for allocating, managing and recycling instance IDs; the serial number generation component in the microservice application is responsible for receiving serial number application requests sent by instances in the microservice application, sending registration requests to the global sequence management service to obtain instance IDs, and generating clock IDs and initializing local sequence numbers according to rules to generate serial numbers based on the current timestamp, instance ID, clock ID and initialized local sequence numbers; instances in the microservice application are responsible for sending sequence number application requests to the serial number generation component to apply for serial numbers of specific sequences.
[0116] In the disclosed embodiment, any instance in the microservice application can send a serial number application request to the serial number in the microservice application when it needs to use the globally unique sequence. Correspondingly, the serial number generation component can receive the serial number application request sent by any instance in the microservice application and perform corresponding processing.
[0117] Step 2: Instance registration
[0118] In the disclosed embodiment, after receiving the serial number application request of any instance, the serial number generation component sends a registration request to the global sequence management service. After the registration is successful, the instance will obtain a set of instance IDs. After that, the serial number generation component can generate a clock ID corresponding to the instance according to the first preset rule, and register the local sequence number corresponding to the instance as 0.
[0119] Step 3: Generate a serial number
[0120] In the disclosed embodiment, the sequence number generation component can combine the current timestamp, instance ID, clock ID, and local sequence number into a bit type value of 64 bits in length, wherein the local sequence number increases by 1 each time it is generated, and the composition rule is as follows: Figure 3 Then the bit type value is converted to a long type value, and the long type value is the generated serial number.
[0121] In the process of generating the serial number, once the clock dial-back is detected, the clock ID is modified in rotation (clock ID+1) to avoid duplication of the generated sequence and solve the clock dial-back problem.
[0122] Step 4: Return the serial number
[0123] In the embodiment of the present disclosure, the sequence number generation component may return the generated sequence number to the instance of the sending sequence number generation component.
[0124] Step 5: Renewal and Cancellation
[0125] In the disclosed embodiment, the serial number generation component may also periodically send a renewal request to the global sequence management service to renew the contract and prevent the instance ID from being recycled; if there is no renewal, the global sequence management service will recycle the instance ID after a certain period of time.
[0126] In the disclosed embodiment, the sequence number generation component may also send a deregistration request to the global sequence management service when the instance ends or terminates abnormally to deregister, so as to facilitate the reallocation of the global sequence management service to other instances.
[0127] In summary, the present disclosure provides a unique serial number generation method under a distributed microservice unitized architecture, which can generate a globally unique and ordered 18-digit number or 10-18-digit string serial number, which can not only ensure the performance of serial number generation under the distributed unitized architecture, but also solve the clock dial-back problem.
[0128] Corresponding to the serial number generating method provided in the above-mentioned embodiment, the present disclosure also provides a serial number generating device. Since the serial number generating device provided in the embodiment of the present disclosure corresponds to the serial number generating method provided in the above-mentioned embodiment, the implementation method of the serial number generating method is also applicable to the serial number generating device provided in the embodiment of the present disclosure, and will not be described in detail in the embodiment of the present disclosure.
[0129] Figure 7 It is a structural diagram of a serial number generating device shown in the seventh embodiment of the present disclosure.
[0130] like Figure 7 As shown, the serial number generating device 700 includes: a receiving module 710, a registration module 720, a first processing module 730 and a second processing module 740.
[0131] Among them, the receiving module 710 is used to receive a serial number application request sent by any instance in the microservice application; the registration module 720 is used to send a registration request to the global sequence management service to obtain the instance identifier assigned to the instance by the global sequence management service; the first processing module 730 is used to generate a clock identifier corresponding to the instance based on the first preset rule, and initialize the local sequence number corresponding to the instance; wherein the clock identifier is used to identify the time window of the instance, and the local sequence number is used to identify the generation order of the serial number within the corresponding time window; the second processing module 740 is used to generate a target serial number based on the current timestamp, instance identifier, clock identifier and initialized local sequence number, and send the target serial number to the instance.
[0132] As a possible implementation of the embodiment of the present disclosure, the second processing module 740 includes: a first processing unit, used to obtain a current timestamp, and determine a clock identifier corresponding to the currently generated serial number based on the clock identifier; a determination unit, used to determine a local sequence number corresponding to the currently generated target serial number based on the initialized local sequence number or the local sequence number corresponding to the last generated target serial number; a combining unit, used to combine the current timestamp, the instance identifier, and the clock identifier and the local sequence number corresponding to the currently generated serial number to obtain a first numerical value of a set type for the target number of bits; and a generating unit, used to generate the target serial number based on the first numerical value of the set type for the target number of bits.
[0133] As a possible implementation method of the embodiment of the present disclosure, the generating unit is used to: perform type conversion on a first value of a set type of the target bit number to obtain a second value of the target type; and determine the second value as the target serial number generated this time.
[0134] As a possible implementation method of the embodiment of the present disclosure, the determination unit is used to: in response to not detecting the clock dial back, determine the clock identifier as the clock identifier corresponding to the serial number generated this time; in response to detecting the clock dial back, process the clock identifier according to a second preset rule to generate a clock identifier corresponding to the serial number generated this time.
[0135] As a possible implementation method of the embodiment of the present disclosure, the determination unit is used to: in response to the target sequence number being generated for the first time, determine the local sequence number corresponding to the target sequence number generated this time according to the initialized local sequence number; in response to the target sequence number being generated this time not being generated for the first time, determine the local sequence number corresponding to the target sequence number generated this time according to the local sequence number corresponding to the target sequence number generated last time.
[0136] As a possible implementation method of the embodiment of the present disclosure, the determination unit is used to: determine the initialized local sequence number as the local sequence number corresponding to the target sequence number generated this time; or, process the initialized local sequence number according to a third preset rule to generate a local sequence number corresponding to the target sequence number generated this time.
[0137] As a possible implementation manner of the embodiment of the present disclosure, the determination unit is used to: process the local sequence number corresponding to the target sequence number generated last time according to a fourth preset rule to generate a local sequence number corresponding to the target sequence number generated this time.
[0138] As a possible implementation method of the embodiment of the present disclosure, the above-mentioned device also includes: a renewal module, which is used to periodically send a renewal request to the global sequence management service, so that when the global sequence management service receives the renewal request, it updates the expiration time of the instance identifier based on the renewal request, or recycles the instance identifier if the renewal request is not received within a specified time.
[0139] As a possible implementation of the embodiment of the present disclosure, the above-mentioned device also includes: a deregistration module, which is used to send a deregistration request to the global sequence management service in response to the end or abnormal termination of the instance, so that the global sequence management service reclaims the instance identifier based on the deregistration request.
[0140] The serial number generating device of the embodiment of the present disclosure receives a serial number application request sent by any instance in the microservice application; sends a registration request to the global sequence management service to obtain the instance identifier assigned to the instance by the global sequence management service; generates a clock identifier corresponding to the instance based on a first preset rule, and initializes the local sequence number corresponding to the instance; wherein the clock identifier is used to identify the time window of the instance, and the local sequence number is used to identify the generation order of the serial number in the corresponding time window; generates a target serial number according to the current timestamp, the instance identifier, the clock identifier and the initialized local sequence number, and sends the target serial number to the instance. Therefore, in the process of generating serial numbers, a centralized serial number generation method is not adopted, but the serial number generation component is integrated into the microservice application by using the idea of distributed architecture, and a global sequence management service is added to uniformly manage the sequence, so as to realize the direct generation of serial numbers locally, which greatly improves the performance of serial number generation and can meet the requirements of distributed business systems in financial scenarios.
[0141] In an exemplary embodiment, an electronic device is also provided.
[0142] Among them, electronic equipment includes:
[0143] processor;
[0144] a memory for storing processor-executable instructions;
[0145] The processor is configured to execute instructions to implement the serial number generation method proposed in any of the aforementioned embodiments.
[0146] As an example, Figure 8 is a schematic diagram of the structure of an electronic device 800 shown in an exemplary embodiment of the present disclosure, such as Figure 8 As shown, the electronic device 800 may further include:
[0147] The memory 810 and the processor 820, a bus 830 connecting different components (including the memory 810 and the processor 820), the memory 810 stores a computer program, and when the processor 820 executes the program, the serial number generation method described in the embodiment of the present disclosure is implemented.
[0148] Bus 830 represents one or more of several types of bus structures, including a memory bus or memory controller, a peripheral bus, an accelerated graphics port, a processor or a local bus using any of a variety of bus architectures. For example, these architectures include but are not limited to Industry Standard Architecture (ISA) bus, Micro Channel Architecture (MAC) bus, Enhanced ISA bus, Video Electronics Standards Association (VESA) local bus and Peripheral Component Interconnect (PCI) bus.
[0149] The electronic device 800 typically includes a variety of electronic device readable media. These media can be any available media that can be accessed by the electronic device 800, including volatile and non-volatile media, removable and non-removable media.
[0150] The memory 810 may also include computer system readable media in the form of volatile memory, such as random access memory (RAM) 840 and / or cache memory 850. The server 800 may further include other removable / non-removable, volatile / non-volatile computer system storage media. By way of example only, the storage system 870 may be used to read and write non-removable, non-volatile magnetic media ( Figure 8 not shown, often referred to as a "hard drive packager"). Although Figure 8 Not shown, a disk driver package for reading and writing a removable non-volatile disk (e.g., a "floppy disk"), and an optical disk driver package for reading and writing a removable non-volatile optical disk (e.g., a CD-ROM, a DVD-ROM, or other optical media) may be provided. In these cases, each driver package can be connected to the bus 830 via one or more data medium interfaces. The memory 810 may include at least one program product having a set (e.g., at least one) of program modules that are configured to perform the functions of the various embodiments of the present disclosure.
[0151] A program / utility 880 having a set (at least one) of program modules 870 may be stored, for example, in the memory 810, such program modules 870 including but not limited to an operating system, one or more application programs, other program modules, and program data, each of which or some combination may include an implementation of a network environment. The program modules 870 generally perform the functions and / or methods of the embodiments described in the present disclosure.
[0152] The electronic device 800 may also communicate with one or more external devices 890 (e.g., keyboard, pointing device, display 891, etc.), may also communicate with one or more devices that enable a user to interact with the electronic device 800, and / or communicate with any device that enables the electronic device 800 to communicate with one or more other computing devices (e.g., network card, modem, etc.). Such communication may be performed through an input / output (I / O) interface 892. In addition, the electronic device 800 may also communicate with one or more networks (e.g., local area network (LAN), wide area network (WAN) and / or public network, such as the Internet) through a network adapter 893. As shown, the network adapter 893 communicates with other modules of the electronic device 800 through a bus 830. It should be understood that, although not shown in the figure, other hardware and / or software modules may be used in conjunction with the electronic device 800, including but not limited to: microcode, device driver package, redundant processing unit, external disk driver package array, RAID system, tape driver package, and data backup storage system, etc.
[0153] The processor 820 executes various functional applications and data processing by running the programs stored in the memory 810 .
[0154] It should be noted that the implementation process and technical principles of the electronic device of this embodiment refer to the aforementioned explanation of the serial number generation method of the embodiment of the present disclosure, and will not be repeated here.
[0155] In an exemplary embodiment, a computer-readable storage medium including instructions is also proposed, such as a memory including instructions, and the above instructions can be executed by a processor of an electronic device to complete the serial number generation method proposed in any of the above embodiments. Optionally, the computer-readable storage medium can be a ROM, a random access memory (RAM), a CD-ROM, a magnetic tape, a floppy disk, an optical data storage device, etc.
[0156] In an exemplary embodiment, a computer program product is also proposed, including a computer program / instruction, characterized in that when the computer program / instruction is executed by a processor, the serial number generation method proposed in any of the above embodiments is implemented.
[0157] Those skilled in the art will readily appreciate other embodiments of the present disclosure after considering the specification and practicing the invention disclosed herein. The present disclosure is intended to cover any variations, uses or adaptations of the present disclosure that follow the general principles of the present disclosure and include common knowledge or customary techniques in the art that are not disclosed in the present disclosure. The description and examples are to be considered exemplary only, and the true scope and spirit of the present disclosure are indicated by the following claims.
[0158] It should be understood that the present disclosure is not limited to the exact structures that have been described above and shown in the drawings, and that various modifications and changes may be made without departing from the scope thereof. The scope of the present disclosure is limited only by the appended claims.
Claims
1. A method for generating a sequence number, characterized in that: The serial number generation component used in microservice applications includes: Receive a serial number application request sent by any instance in the microservice application; Sending a registration request to a global sequence management service to obtain an instance identifier allocated to the instance by the global sequence management service; Generate a clock identifier corresponding to the instance based on a first preset rule, and initialize a local sequence number corresponding to the instance; wherein the clock identifier is used to identify a time window of the instance, and the local sequence number is used to identify a generation order of sequence numbers within the corresponding time window; A target sequence number is generated according to the current timestamp, the instance identifier, the clock identifier and the initialized local sequence number, and the target sequence number is sent to the instance.
2. The method according to claim 1, characterized in that The generating a target sequence number according to the current timestamp, the instance identifier, the clock identifier and the initialized local sequence number includes: Obtain the current timestamp, and determine the clock identifier corresponding to the currently generated serial number based on the clock identifier; Determine the local sequence number corresponding to the target sequence number generated this time according to the initialized local sequence number or the local sequence number corresponding to the target sequence number generated last time; Combine the current timestamp, the instance identifier, and the clock identifier and the local sequence number corresponding to the currently generated sequence number to obtain a first value of a set type of target number of bits; The target sequence number is generated according to a first value of a setting type of the target number of bits.
3. The method according to claim 2, characterized in that The generating the target sequence number according to the first value of the setting type of the target number of bits includes: Performing type conversion on a first value of a setting type of the target number of digits to obtain a second value of a target type; The second value is determined as the target sequence number generated this time.
4. The method according to claim 2, characterized in that: The step of determining the clock identifier corresponding to the currently generated sequence number according to the clock identifier includes: In response to not detecting the clock dialback, determining the clock identifier corresponding to the last generated target sequence number as the clock identifier corresponding to the currently generated sequence number; In response to detecting the clock dial back, processing the clock identifier corresponding to the last generated target sequence number according to a second preset rule to generate a clock identifier corresponding to the sequence number generated this time; The clock identifier corresponding to the first generated target sequence number is the clock identifier.
5. The method according to claim 2, characterized in that: The determining, according to the initialized local sequence number or the local sequence number corresponding to the last generated target sequence number, the local sequence number corresponding to the currently generated target sequence number includes: In response to the target sequence number being generated for the first time, determining the local sequence number corresponding to the target sequence number generated for the current time according to the initialized local sequence number; In response to the fact that the target sequence number is not generated for the first time, the local sequence number corresponding to the target sequence number generated this time is determined according to the local sequence number corresponding to the target sequence number generated last time.
6. The method according to claim 5, characterized in that The step of determining the local sequence number corresponding to the target sequence number generated this time according to the initialized local sequence number includes: Determine the initialized local sequence number as the local sequence number corresponding to the target sequence number generated this time; or, The initialized local sequence number is processed according to the third preset rule to generate a local sequence number corresponding to the target sequence number generated this time.
7. The method according to claim 5, characterized in that The determining, based on the local sequence number corresponding to the target sequence number generated last time, the local sequence number corresponding to the target sequence number generated this time comprises: The local sequence number corresponding to the target sequence number generated last time is processed according to the fourth preset rule to generate the local sequence number corresponding to the target sequence number generated this time.
8. The method according to claim 1, characterized in that: The method further comprises: Periodically send a renewal request to the global sequence management service so that the global sequence management service updates the expiration time of the instance identifier based on the renewal request when receiving the renewal request, or recycles the instance identifier if the renewal request is not received within a set time after the instance identifier expires.
9. The method according to claim 1, characterized in that: The method further comprises: In response to the instance ending or abnormal termination, a deregistration request is sent to the global sequence management service, so that the global sequence management service reclaims the instance identifier based on the deregistration request.
10. A serial number generating device, characterized in that: The serial number generation component used in microservice applications includes: A receiving module, used to receive a serial number application request sent by any instance in the microservice application; A registration module, used for sending a registration request to a global sequence management service to obtain an instance identifier allocated by the global sequence management service to the instance; A first processing module, configured to generate a clock identifier corresponding to the instance based on a first preset rule, and initialize a local sequence number corresponding to the instance; wherein the clock identifier is used to identify a time window of the instance, and the local sequence number is used to identify a generation order of sequence numbers within the corresponding time window; The second processing module is used to generate a target sequence number according to the current timestamp, the instance identifier, the clock identifier and the initialized local sequence number, and send the target sequence number to the instance.
Citation Information
Cited By
Service ID generation method and system
CN121603213A