Identification Generation Method, Device, Electronic Device and Storage Medium
By mutating the snowflake algorithm, using the server address to generate target machine codes, and re-dividing the ID structure, the problem of snowflake algorithm generating too many IDs per second and relying on data center IDs is solved, reducing ID waste and improving algorithm flexibility is achieved, and supporting longer-term ID generation.
Patent Information
- Application Number
- CN202111659877.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-30
- Publication Date
- 2025-07-25
- Estimated Expiration
- 2041-12-30
AI Technical Summary
When generating IDs, the existing snowflake algorithm generates too many IDs per second, resulting in waste and reliance on data center ID and data node ID, which lacks flexibility.
By mutating the snowflake algorithm, the target machine code is generated using the server address, and the ID structure is redied into 2-bit reserved code, 10-bit machine code, 32-bit time code and 20-bit sequence code, reducing the number of IDs generated per second, and using the server address to generate the target machine code, getting rid of the dependence on data center ID and data node ID.
Reduces ID waste, enhances algorithm flexibility, supports longer years and self-increase characteristics, and meets enterprise business needs.
Smart Images

Figure CN114356925B_ABST
Abstract
Description
Technical Field
[0001] Embodiments of the present invention relate to the technical field of data processing, and in particular, to an identifier generation method, apparatus, electronic device, and storage medium. Background Art
[0002] Currently, in a database software system, it is necessary to generate a unique string of numbers or characters within the database for different objects as the identity identifier (ID) of a record in the database, that is, the primary key in database terms.
[0003] In the prior art, the Snowflake algorithm can be used to generate corresponding IDs for each object. This algorithm is an open-source distributed algorithm and has the ability to generate globally unique and incrementing IDs in a distributed system. However, in the actual application of the Snowflake algorithm, the number of IDs generated per second in the system is too large, and enterprises cannot effectively utilize all the IDs, resulting in a large amount of ID waste. At the same time, in the process of generating IDs, the Snowflake algorithm must rely on the ID of the data center and the IDs of each data node to construct the corresponding primary key, which leads to certain limitations of the Snowflake algorithm. Summary of the Invention
[0004] The present invention provides an identifier generation method, apparatus, electronic device, and storage medium, which reduce the number of IDs generated per second, avoid a large amount of ID waste, get rid of the dependence on the ID of the data center and the ID of the data node in the traditional primary key generation method, and enhance the flexibility of the algorithm.
[0005] In a first aspect, an embodiment of the present invention provides an identifier generation method, which includes:
[0006] When receiving a service request, determining a target reserved code corresponding to the service request;
[0007] Obtaining a target server address corresponding to the service request, and determining a target machine code corresponding to the service request based on the target server address;
[0008] Determining a target time code corresponding to the service request according to the to-be-processed timestamp information carried in the service request and preset timestamp information;
[0009] Determining a target sequence code corresponding to the service request according to the to-be-processed timestamp information, and constructing a target identifier corresponding to the service request according to the target reserved code, the target machine code, the target time code, and the target sequence code.
[0010] In a second aspect, an embodiment of the present invention further provides an identifier generation apparatus, which includes:
[0011] A target reservation code determination module, configured to determine a target reservation code corresponding to the service request when receiving the service request;
[0012] A target machine code determination module, configured to obtain a target server address corresponding to the service request, and determine a target machine code corresponding to the service request based on the target server address;
[0013] A target time code determination module, configured to determine a target time code corresponding to the service request according to the to-be-processed timestamp information carried in the service request and preset timestamp information;
[0014] A target identifier determination module, configured to determine a target sequence code corresponding to the service request according to the to-be-processed timestamp information, and construct a target identifier corresponding to the service request according to the target reservation code, the target machine code, the target time code, and the target sequence code.
[0015] In a third aspect, an embodiment of the present invention further provides an electronic device, where the electronic device includes:
[0016] One or more processors;
[0017] A storage device, configured to store one or more programs,
[0018] When the one or more programs are executed by the one or more processors, the one or more processors implement the identifier generation method according to any one of the embodiments of the present invention.
[0019] In a fourth aspect, an embodiment of the present invention further provides a storage medium containing computer-executable instructions, where the computer-executable instructions are used to execute the identifier generation method according to any one of the embodiments of the present invention when executed by a computer processor.
[0020] In the technical solution of the embodiment of the present invention, when a service request is received, a target reservation code corresponding to the service request is determined, a target server address corresponding to the service request is obtained, and a target machine code corresponding to the service request is determined based on the target server address. According to the timestamp information to be processed carried in the service request and the preset timestamp information, a target time code corresponding to the service request is determined. According to the timestamp information to be processed, a target sequence code corresponding to the service request is determined. Finally, the above various pieces of information are fused, that is, a target identifier corresponding to the service request is constructed according to the target reservation code, the target machine code, the target time code, and the target sequence code. Through the implementation of a variant of the Snowflake algorithm, the composition of each part of the generated primary key is changed. This not only reduces the number of IDs generated per second and avoids the waste of a large number of IDs, but also uses the server address to generate the target machine code, getting rid of the dependence on the data center ID and the data node ID in the traditional primary key generation method, and enhancing the flexibility of the algorithm. BRIEF DESCRIPTION OF THE DRAWINGS
[0021] In order to more clearly illustrate the technical solutions of the exemplary embodiments of the present invention, the following briefly introduces the drawings required for describing the embodiments. Obviously, the introduced drawings are only the drawings of a part of the embodiments to be described in the present invention, rather than all the drawings. For those of ordinary skill in the art, other drawings can be obtained based on these drawings without creative efforts.
[0022] Figure 1 It is a schematic flowchart of a method for generating an identifier provided in Embodiment 1 of the present invention;
[0023] Figure 2 It is a schematic flowchart of a method for generating an identifier provided in Embodiment 2 of the present invention;
[0024] Figure 3 It is a structural block diagram of an identifier generation device provided in Embodiment 3 of the present invention;
[0025] Figure 4 It is a schematic structural diagram of an electronic device provided in Embodiment 4 of the present invention. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0026] The present invention will be further described in detail below with reference to the drawings and embodiments. It can be understood that the specific embodiments described herein are only used to explain the present invention, rather than limiting the present invention. Additionally, it should be noted that for the sake of description, only the parts related to the present invention are shown in the drawings rather than all the structures.
[0027] Embodiment 1
[0028] Figure 1The flowchart of a method for generating identifiers provided in Embodiment 1 of the present invention. This embodiment is applicable to the situation of optimizing the variant of the Snowflake algorithm and constructing corresponding primary keys using the server address. This method can be executed by an identifier generation device, which can be implemented in the form of software and / or hardware. The hardware can be an electronic device, such as a mobile terminal, a PC, or a server, etc.
[0029] As Figure 1 shown, the method specifically includes the following steps:
[0030] S110. When receiving a service request, determine the target reservation code corresponding to the service request.
[0031] Among them, the service request can be various types of requests associated with each business. For example, the service request can be a request received during the business development and testing phase, or a request received when the business is officially launched and operated. It can be understood that the service request is a variety of requests that need to construct a globally unique ID and store it in a specific database.
[0032] In this embodiment, when receiving a service request, the distributed system can execute the pre-written target program code. Among them, the target program code refers to an algorithm for generating corresponding primary keys for various objects. In actual application, the target program code can be a variant optimization of the Snowflake algorithm. The existing Snowflake algorithm is described below.
[0033] For the Snowflake algorithm, after executing its corresponding program code, the constructed ID is usually 64 bits. In the specific primary key structure, the identification bit occupies 1 bit, which can be understood as the sign bit, 0 for positive numbers and 1 for negative numbers, so the ID is generally a positive number and takes 0; the timestamp occupies 41 bits (millisecond level). At the same time, the 41-bit timestamp is not the timestamp of the current moment stored, but the difference from the preset storage timestamp. The preset timestamp can usually be set as the time when the primary key generator starts to be used; the data machine bit occupies 10 bits and can be deployed on 1024 nodes, where the first 5 bits represent the data center ID and the last 5 bits represent the data node ID; the last sequence bit occupies 12 bits. Since the Snowflake algorithm is a millisecond-level count, within 1 millisecond, each node can generate 4096 ID serial numbers based on the 12-bit counting sequence number. After integrating the above bits, more than four million IDs can be generated per second based on the Snowflake algorithm.
[0034] In this embodiment, based on the existing Snowflake algorithm, it can be variably optimized to obtain the target program code. Correspondingly, during the execution of the target program code, it is also necessary to generate a corresponding target reservation code for the received service request. Among them, the target reservation code corresponds to the identification bit in the traditional ID structure. It should be noted that after the Snowflake algorithm is variably optimized, during the construction of the ID, the generated target reservation code can be two digits.
[0035] S120. Obtain the target server address corresponding to the service request, and determine the target machine code corresponding to the service request based on the target server address.
[0036] In this embodiment, during the process of constructing the ID based on the target program code, it is also necessary to generate the target machine code corresponding to the data machine bit in the traditional ID structure. Among them, the target machine code is still used to represent the machine associated with the service request. However, different from the data machine bit constructed by the existing Snowflake algorithm, the target machine code in this embodiment gets rid of the dependence on the data center ID and the data node ID during the generation process. That is to say, the target machine code is not generated based on the above two IDs, but is generated according to the address of the target server. Therefore, in order to determine the target machine code corresponding to the service request, it is also necessary to obtain the target server address corresponding to the service request.
[0037] Specifically, the target server address can be the Internet Protocol (IP) address corresponding to the target server. As the unified address format provided by the IP protocol, by assigning a logical address to each host of each network on the Internet, the differences in the physical addresses of each server are masked. In this embodiment, when the IP address of the server associated with the service request is obtained, the corresponding target machine code can be generated based on this IP address. It should be noted that after the Snowflake algorithm is variably optimized, during the construction of the ID, the generated target machine code is still 10 digits.
[0038] S130. Determine the target time code corresponding to the service request according to the timestamp information to be processed carried by the service request and the preset timestamp information.
[0039] In this embodiment, during the process of constructing an ID based on the target program code, it is also necessary to generate a target time code corresponding to the timestamp in the traditional ID structure. Among them, the target time code is still used to represent the difference between the timestamp associated with the service request and the preset timestamp. It can be understood that when receiving a service request, first determine the to-be-processed timestamp information carried in the request, that is, determine the moment when the distributed system receives this service request. At the same time, it is also necessary to determine the preset timestamp information in the distributed system. This timestamp information can be either the time information corresponding to the start identifier generator or the time information pre-edited by the staff according to actual needs. The embodiments of the present disclosure do not make specific limitations here. Further, by taking the difference between the above two timestamp information, the time information of this service request can be obtained. Finally, after processing this time information based on the target program code, the target time code in the ID can be generated.
[0040] Since for some enterprises, they do not need more than four million IDs per second, therefore, in order to reduce the number of IDs generated by the algorithm per second and avoid waste of a large number of IDs, the target time code generated based on the target program code is 32 bits.
[0041] S140. Determine a target sequence code corresponding to the service request according to the to-be-processed timestamp information, and construct a target identifier corresponding to the service request according to the target reserved code, the target machine code, the target time code, and the target sequence code.
[0042] In this embodiment, when determining the to-be-processed timestamp information carried in the service request, a target sequence code corresponding to the service request can also be determined. Among them, the target sequence code is still used to represent the counting sequence number of this service request at the current moment. It can be understood that when receiving a service request, determine the number of IDs that have been generated for other service requests at the current moment (within one second). On this basis, add one to the determined number to determine the counting sequence number corresponding to the currently received service request. Further, process this technical sequence number based on the target program code to obtain the target sequence code corresponding to the service request.
[0043] In this embodiment, after determining the target reserved code, the target machine code, the target time code, and the target sequence code, further, splice and integrate the above four codes to construct a target identifier corresponding to the service request. It can be understood that the finally constructed target identifier is a 64-bit long binary number. At the same time, after constructing a corresponding target identifier for the service request, the target identifier can be associated with the request and stored in the corresponding database for subsequent calls.
[0044] In the technical solution of this embodiment, when a service request is received, a target reservation code corresponding to the service request is determined, a target server address corresponding to the service request is obtained, and a target machine code corresponding to the service request is determined based on the target server address. According to the timestamp information to be processed carried in the service request and the preset timestamp information, a target time code corresponding to the service request is determined. According to the timestamp information to be processed, a target sequence code corresponding to the service request is determined. Finally, the above various information is fused, that is, a target identifier corresponding to the service request is constructed according to the target reservation code, the target machine code, the target time code, and the target sequence code. Through the implementation of a variant of the Snowflake algorithm, the composition of each part of the generated primary key is changed, which not only reduces the number of IDs generated per second and avoids the waste of a large number of IDs, but also uses the server address to generate the target machine code, getting rid of the dependence on the data center ID and the data node ID in the traditional primary key generation method, and enhancing the flexibility of the algorithm.
[0045] Embodiment 2
[0046] Figure 2 It is a schematic flowchart of a method for generating an identifier provided by Embodiment 2 of the present invention. On the basis of the foregoing embodiment, the 64-bit ID constructed by the existing Snowflake algorithm is re-divided, and is in the form of 2-bit target reservation bits, 10-bit target machine code, 32-bit target time code, and 20-bit target sequence code. In seconds, it can not only reflect the application service corresponding to the service request, thereby meeting the business needs of enterprises, but also enables the optimized algorithm after variant to support a longer time limit while retaining the original increment and high-efficiency characteristics of the Snowflake algorithm. The specific implementation manner can refer to the technical solution of this embodiment. Among them, the same or corresponding technical terms as those in the above embodiment will not be elaborated here.
[0047] As Figure 2 shown, the method specifically includes the following steps:
[0048] S210. When a service request is received, determine a target process corresponding to the service request in the distributed system, and determine the target reservation code according to the last two digits of the number of the target process.
[0049] In this embodiment, by implementing a variant of the existing Snowflake algorithm, when a service request is received and a corresponding target reservation code is generated for it based on the target program code, the symbol bits that have no meaning in the traditional identifier can be changed into target reservation bits representing specific application services.
[0050] Specifically, when a service request is received, the target process associated with the service request can be determined in the distributed system. Further, the last two digits of the target process number are selected in the system as the target reservation code. Exemplarily, the target reservation code can be 00, 11, 01, or 10. Since different target process numbers correspond to different application services, it can be understood that the target reservation code can also reflect the specific application service.
[0051] It should be noted that in the actual application process, when the application service is started, its process number can be obtained in the distributed system and the remainder of the process number divided by 4 is taken. Further, the obtained value is used as the value of the target reservation bit corresponding to the service request. Those skilled in the art should understand that the specific method for determining the target reservation bit can be selected according to the actual situation, and the embodiments of the present disclosure do not make specific limitations in this regard.
[0052] S220. Obtain the target server address corresponding to the service request, and determine the target machine code corresponding to the service request based on the target server address.
[0053] In this embodiment, since the target machine code is no longer generated depending on the data center ID and the data node ID, but is generated based on the IP address of the target server, there are at least two ways to generate the corresponding target machine code based on the IP address, which will be described separately below.
[0054] The first way is to obtain the target server address and determine the target machine code based on the pre-deployed service component.
[0055] Among them, the pre-deployed service component can be Zookeeper. As an open-source project, this component provides distributed configuration services, synchronization services, and naming registration for large-scale distributed computing, and its architecture realizes high availability through redundant services. Specifically in this embodiment, after the IP address of the target server associated with the service request is obtained based on the cloud-native environment, the ID of the machine can be generated by using the pre-deployed Zookeeper, which will not be elaborated in the embodiments of the present disclosure.
[0056] The second way is to obtain the target server address and convert the target server address into the corresponding binary address field; combine the pre-edited specified field and the last eight bits of the binary address field to obtain the target machine code.
[0057] Exemplarily, after obtaining the IP address "192.168.1.123" of the target server, the last 8 bits of the IP address (i.e., 123) can be selected and converted into the corresponding binary address field "01111011". At the same time, since the target machine code constructed in this embodiment is 10 bits, after obtaining the above binary address field, two bits specified by the user (usually default to 0) also need to be combined with the above field to obtain the ten-bit target machine code "0001111011".
[0058] It should be noted that since this embodiment only relies on the machine IP address to generate the corresponding target machine code, for a single application service, the number of associated machines does not exceed 1024.
[0059] S230. Calculate the difference between the to-be-processed timestamp information and the preset timestamp information to obtain the target timestamp information, convert the target timestamp information into the corresponding binary time field, and determine the binary time field as the target time code.
[0060] In this embodiment, after receiving the service request, determining the to-be-processed timestamp information carried in the request, and the preset timestamp information of the distributed system, calculations can be performed based on the above two pieces of information, and the obtained difference is used as the target timestamp information. Further, in a manner similar to the conversion to obtain the target machine code, the target timestamp information can be converted to obtain a 32-bit binary time field, which is used as the target time code.
[0061] It should be noted that in this embodiment, for each service request, the unit of each corresponding timestamp information is seconds. By modifying the timestamp unit in the identifier generated by the existing Snowflake algorithm from milliseconds to seconds, the optimized variant algorithm has a validity period of 136 years, which is longer than the 69-year validity period of the original algorithm. That is to say, the modified algorithm can support a longer time limit while reducing the number of IDs generated per second.
[0062] S240. Determine the target moment according to the to-be-processed timestamp information, and in the service request list associated with the target moment, determine the to-be-processed sequence code corresponding to the previous service request of the service request; accumulate the to-be-processed sequence code to obtain the target sequence code.
[0063] In this embodiment, when determining the to-be-processed timestamp information corresponding to the service request, not only the target time code can be determined, but also the target sequence code corresponding to the service request can be determined.
[0064] Specifically, first determine the target moment corresponding to the timestamp information. For example, determine the moment when the distributed system receives a service request. Then, in the service request list of the system, determine all the service requests received at this moment. Further, use the sequence code corresponding to the request before the current service request as the to-be-processed sequence code. After adding 1 to the value of this sequence code, the target sequence code corresponding to the current service request is obtained. In the actual application process, the target sequence code can be regarded as an auto-incrementing code. That is to say, when the number of service requests received within one second does not reach 1048576, the target sequence codes corresponding to each service request can increase sequentially in the order of system reception.
[0065] S250. Concatenate the target reserved code, target machine code, target time code, and target sequence code in sequence from left to right, and determine the obtained concatenation result as the target identifier.
[0066] In this embodiment, after determining the target reserved code, target machine code, target time code, and target sequence code, concatenate the above four pieces of information in sequence from left to right, and the target identifier corresponding to the service request is obtained. Among them, in the target identifier, the target reserved code occupies 2 bit positions, the target machine code occupies 10 bit positions, the target time code occupies 32 bit positions, and the target sequence code occupies 20 bit positions. Exemplarily, after determining that the target reserved code is 00, the target machine code is 0001110101, the target time code is 00011101101111101110110101110010, and the target sequence code is 00011101010100111100, concatenate the above information, and the obtained 00 000111010100011101101111101110110101110010 00011101010100111100 is the target identifier corresponding to the service request.
[0067] S260. When a new service request carrying to-be-processed timestamp information is received, detect the target sequence code. When the value corresponding to the target sequence code is less than the preset threshold, accumulate the target sequence code to obtain the target sequence code corresponding to the new service request.
[0068] It should be noted that after determining the to-be-processed timestamp information corresponding to the new service request, if it is determined that the number of generated IDs at this moment is less than the preset threshold of 1048576, the target sequence code can be accumulated based on the previously generated ID to obtain the target sequence code corresponding to the new service request. When the number of IDs has reached 1048576, for the new service request, it is necessary to block the target program code until the next second to generate the corresponding target sequence code.
[0069] The technical solution of this embodiment re-divides the 64-bit ID constructed by the existing Snowflake algorithm, and adopts the form of 2-bit target reserved bits, 10-bit target machine code, 32-bit target time code, and 20-bit target sequence code. In seconds, it can not only reflect the application service corresponding to the service request, thus meeting the business needs of enterprises, but also enables the algorithm after variant optimization to support a longer time limit while retaining the original self-increasing and efficient characteristics of the Snowflake algorithm.
[0070] Embodiment III
[0071] Figure 3 As shown in the structural block diagram of an identifier generation device provided in Embodiment III of the present invention, it can execute the identifier generation method provided in any embodiment of the present invention, and has the corresponding functional modules and beneficial effects for executing the method. As Figure 3 shown, the device specifically includes: a target reserved code determination module 310, a target machine code determination module 320, a target time code determination module 330, and a target identifier determination module 340.
[0072] The target reserved code determination module 310 is configured to determine a target reserved code corresponding to the service request when receiving the service request.
[0073] The target machine code determination module 320 is configured to obtain a target server address corresponding to the service request, and determine a target machine code corresponding to the service request based on the target server address.
[0074] The target time code determination module 330 is configured to determine a target time code corresponding to the service request according to the to-be-processed timestamp information carried in the service request and preset timestamp information.
[0075] The target identifier determination module 340 is configured to determine a target sequence code corresponding to the service request according to the to-be-processed timestamp information, and construct a target identifier corresponding to the service request according to the target reserved code, the target machine code, the target time code, and the target sequence code.
[0076] Optionally, the target reserved code determination module 310 is further configured to determine a target process corresponding to the service request in a distributed system when receiving the service request, and determine the target reserved code according to the last two digits of the number of the target process.
[0077] Optionally, the target machine code determination module 320 is further configured to obtain the target server address, and determine the target machine code based on pre-deployed service components; or, obtain the target server address, and convert the target server address into a corresponding binary address field; combine a pre-edited specified field and the last eight bits of the binary address field to obtain the target machine code.
[0078] Based on the above technical solutions, the target time code determination module 330 includes a target timestamp information determination unit and a target time code determination unit.
[0079] The target timestamp information determination unit is configured to calculate the difference between the to-be-processed timestamp information and the preset timestamp information to obtain target timestamp information, where the unit of each timestamp information is seconds.
[0080] The target time code determination unit is configured to convert the target timestamp information into a corresponding binary time field, and determine the binary time field as the target time code.
[0081] Based on the above technical solutions, the target identifier determination module 340 includes a target sequence code determination unit and a target identifier determination unit.
[0082] The target sequence code determination unit is configured to determine a target moment according to the to-be-processed timestamp information, and determine the to-be-processed sequence code corresponding to the previous service request of the service request in the service request list associated with the target moment; accumulate the to-be-processed sequence code to obtain the target sequence code.
[0083] The target identifier determination unit is configured to splice the target reserved code, the target machine code, the target time code, and the target sequence code in sequence from left to right, and determine the obtained splicing result as the target identifier; where, in the target identifier, the target reserved code occupies 2 bit positions, the target machine code occupies 10 bit positions, the target time code occupies 32 bit positions, and the target sequence code occupies 20 bit positions.
[0084] Based on the above technical solutions, the identifier generation device further includes a detection module.
[0085] The detection module is configured to, when receiving a new service request carrying the to-be-processed timestamp information, detect the target sequence code, and when the value corresponding to the target sequence code is less than a preset threshold, accumulate the target sequence code to obtain a target sequence code corresponding to the new service request.
[0086] In the technical solution provided in this embodiment, when a service request is received, a target reservation code corresponding to the service request is determined, a target server address corresponding to the service request is obtained, a target machine code corresponding to the service request is determined based on the target server address, a target time code corresponding to the service request is determined according to the timestamp information to be processed carried in the service request and the preset timestamp information, a target sequence code corresponding to the service request is determined according to the timestamp information to be processed, and finally, the above various pieces of information are fused, that is, a target identifier corresponding to the service request is constructed according to the target reservation code, the target machine code, the target time code, and the target sequence code. Through the implementation of a variant of the Snowflake algorithm, the composition of each part of the generated primary key is changed. This not only reduces the number of IDs generated per second and avoids the waste of a large number of IDs, but also uses the server address to generate the target machine code, getting rid of the dependence on the data center ID and the data node ID in the traditional primary key generation method, and enhancing the flexibility of the algorithm.
[0087] The identifier generation device provided in an embodiment of the present invention can execute the identifier generation method provided in any embodiment of the present invention, and has functional modules and beneficial effects corresponding to the execution of the method.
[0088] It should be noted that the various units and modules included in the above device are only divided according to functional logic, but are not limited to the above division, as long as the corresponding functions can be realized; in addition, the specific names of the functional units are only for the convenience of mutual distinction and do not limit the protection scope of the embodiments of the present invention.
[0089] Embodiment 4
[0090] Figure 4 FIG. is a schematic structural diagram of an electronic device provided in Embodiment 4 of the present invention. Figure 4 FIG. shows a block diagram of an exemplary electronic device 40 suitable for implementing the implementation manner of the embodiment of the present invention. Figure 4 The shown electronic device 40 is only an example and should not bring any limitation to the functions and usage scope of the embodiments of the present invention.
[0091] As Figure 4 shown, the electronic device 40 is presented in the form of a general-purpose computing device. The components of the electronic device 40 may include, but are not limited to: one or more processors or processing units 401, a system memory 402, and a bus 403 connecting different system components (including the system memory 402 and the processing unit 401).
[0092] The bus 403 represents one or more of several types of bus architectures, including a memory bus or memory controller, a peripheral bus, an accelerated graphics port, a processor bus, or a local bus using any of the several bus architectures. By way of example, and not limitation, these architectures include the 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.
[0093] The electronic device 40 typically includes a variety of computer system readable media. These media can be any available media that can be accessed by the electronic device 40, including both volatile and nonvolatile media, removable and non-removable media.
[0094] The system memory 402 can include computer system readable media in the form of volatile memory, such as random access memory (RAM) 404 and / or cache memory 405. The electronic device 40 can further include other removable / non-removable, volatile / nonvolatile computer system storage media. By way of example only, a storage system 406 can be provided for reading from and writing to non-removable, nonvolatile magnetic media ( Figure 4 not shown and typically called a “hard disk drive”). Although Figure 4 not shown in the figures, a disk drive for reading from and writing to a removable nonvolatile disk (e.g., a “floppy disk”), and an optical disk drive for reading from and writing to a removable nonvolatile optical disk (e.g., a CD-ROM, DVD-ROM, or other optical media) can be provided. In these cases, each drive can be connected to the bus 403 by one or more data media interfaces. The memory 402 can include at least one program product having a set (e.g., at least one) of program modules that are configured to carry out the functions of the embodiments of the present invention.
[0095] A program / utility 408 having a set (at least one) of program modules 407 can be stored in, for example, the memory 402, such program modules 407 including, but not limited to, an operating system, one or more application programs, other program modules, and program data, each of which examples or some combination thereof may include an implementation of a network environment. The program modules 407 typically carry out the functions and / or methods of the embodiments described herein.
[0096] The electronic device 40 can also communicate with one or more external devices 409 (such as a keyboard, a pointing device, a display 410, etc.), and can also communicate with one or more devices that enable a user to interact with the electronic device 40, and / or communicate with any device that enables the electronic device 40 to communicate with one or more other computing devices (such as a network card, a modem, etc.). Such communication can be carried out through an input / output (I / O) interface 411. Moreover, the electronic device 40 can also communicate with one or more networks (such as a local area network (LAN), a wide area network (WAN), and / or a public network, such as the Internet) through a network adapter 412. As shown in the figure, the network adapter 412 communicates with other modules of the electronic device 40 through a bus 403. It should be understood that although Figure 4 not shown in the figure, other hardware and / or software modules can be used in combination with the electronic device 40, including but not limited to: microcode, device drivers, redundant processing units, external disk drive arrays, RAID systems, tape drives, and data backup storage systems, etc.
[0097] The processing unit 401 executes various functional applications and data processing by running programs stored in the system memory 402, for example, implementing the identity generation method provided by the embodiments of the present invention.
[0098] Embodiment Five
[0099] Embodiment Five of the present invention also provides a storage medium containing computer-executable instructions, and the computer-executable instructions are used to execute the identity generation method when executed by a computer processor.
[0100] The method includes:
[0101] When receiving a service request, determining a target reservation code corresponding to the service request;
[0102] Obtaining a target server address corresponding to the service request, and determining a target machine code corresponding to the service request based on the target server address;
[0103] According to the to-be-processed timestamp information carried in the service request and preset timestamp information, determining a target time code corresponding to the service request;
[0104] Determining a target sequence code corresponding to the service request according to the to-be-processed timestamp information, and constructing a target identity corresponding to the service request according to the target reservation code, the target machine code, the target time code, and the target sequence code.
[0105] The computer storage medium of the embodiments of the present invention may adopt any combination of one or more computer-readable media. The computer-readable media may be computer-readable signal media or computer-readable storage media. The computer-readable storage media may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination of the above. More specific examples (a non-exhaustive list) of the computer-readable storage media include: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In this document, the computer-readable storage media may be any tangible medium that contains or stores a program, which can be used by or in combination with an instruction execution system, apparatus, or device.
[0106] The computer-readable signal media may include data signals propagated in a baseband or as part of a carrier wave, which carry computer-readable program code. Such propagated data signals may take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. The computer-readable signal media may also be any computer-readable media other than the computer-readable storage media, which can send, propagate, or transmit a program for use by or in combination with an instruction execution system, apparatus, or device.
[0107] The program code contained on the computer-readable media may be transmitted by any appropriate medium, including but not limited to wireless, wire, optical cable, RF, etc., or any suitable combination of the above.
[0108] The computer program code for performing the operations of the embodiments of the present invention may be written in one or more programming languages or combinations thereof. The programming languages include object-oriented programming languages such as Java, Smalltalk, C++, and also include conventional procedural programming languages such as the "C" language or similar programming languages. The program code may be executed entirely on the user's computer, partially on the user's computer, executed as an independent software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In the case of a remote computer, the remote computer may be connected to the user's computer through any type of network, including a local area network (LAN) or a wide area network (WAN), or may be connected to an external computer (e.g., by using an Internet service provider to connect through the Internet).
[0109] Note that the above is only a preferred embodiment of the present invention and the technical principles applied. Those skilled in the art will understand that the present invention is not limited to the specific embodiments described herein, and various obvious changes, re-adjustments and substitutions can be made by those skilled in the art without departing from the protection scope of the present invention. Therefore, although the present invention has been described in more detail through the above embodiments, the present invention is not limited to the above embodiments. Without departing from the concept of the present invention, more other equivalent embodiments can be included, and the scope of the present invention is determined by the scope of the appended claims.
Claims
1. A method for generating an identifier, characterized in that, including: When receiving a service request, determining a target reservation code corresponding to the service request; Obtaining a target server address corresponding to the service request, and determining a target machine code corresponding to the service request based on the target server address; Determining a target time code corresponding to the service request according to the to-be-processed timestamp information carried in the service request and preset timestamp information; Determining a target sequence code corresponding to the service request according to the to-be-processed timestamp information, and constructing a target identifier corresponding to the service request according to the target reservation code, the target machine code, the target time code, and the target sequence code; The target sequence code is used to represent the counting serial number of the service request at the current moment; When receiving a new service request, detecting the target sequence code corresponding to the previous service request. When the value of the target sequence code corresponding to the previous service request is greater than or equal to a preset threshold, blocking the new service request until the next moment to generate a target sequence code corresponding to the new service request.
2. The method according to claim 1, wherein The step of, when receiving a service request, determining a target reservation code corresponding to the service request includes: When receiving the service request, determining a target process corresponding to the service request in a distributed system, and determining the target reservation code according to the last two digits of the number of the target process.
3. The method according to claim 1, wherein The step of obtaining a target server address corresponding to the service request and determining a target machine code corresponding to the service request based on the target server address includes: Obtaining the target server address and determining the target machine code based on a pre-deployed service component; or Obtaining the target server address and converting the target server address into a corresponding binary address field; Combining a pre-edited specified field and the last eight bits of the binary address field to obtain the target machine code.
4. The method according to claim 1, wherein The step of determining a target time code corresponding to the service request according to the to-be-processed timestamp information carried in the service request and preset timestamp information includes: Calculating the difference between the to-be-processed timestamp information and the preset timestamp information to obtain target timestamp information, where the unit of each timestamp information is seconds; Converting the target timestamp information into a corresponding binary time field, and determining the binary time field as the target time code.
5. The method according to claim 1, characterized in that, The step of determining a target sequence code corresponding to the service request according to the to-be-processed timestamp information includes: Determining a target moment according to the to-be-processed timestamp information, and determining the to-be-processed sequence code corresponding to the previous service request of the service request in a service request list associated with the target moment; Accumulating the to-be-processed sequence code to obtain the target sequence code.
6. The method according to claim 1, characterized in that, The step of constructing a target identifier corresponding to the service request according to the target reservation code, the target machine code, the target time code, and the target sequence code includes: Concatenate the target reserved code, the target machine code, the target time code, and the target sequence code in sequence from left to right, and determine the obtained concatenation result as the target identifier; Among them, in the target identifier, the target reserved code occupies 2 bit positions, the target machine code occupies 10 bit positions, the target time code occupies 32 bit positions, and the target sequence code occupies 20 bit positions.
7. The method according to claim 1, characterized in that, It further includes: When receiving a new service request carrying the to-be-processed timestamp information, detect the target sequence code. When the value corresponding to the target sequence code is less than a preset threshold, accumulate the target sequence code to obtain a target sequence code corresponding to the new service request.
8. An identification generation device, characterized in that, It includes: A target reserved code determination module, configured to determine a target reserved code corresponding to the service request when receiving the service request; A target machine code determination module, configured to obtain a target server address corresponding to the service request, and determine a target machine code corresponding to the service request based on the target server address; A target time code determination module, configured to determine a target time code corresponding to the service request according to the to-be-processed timestamp information carried in the service request and preset timestamp information; A target identifier determination module, configured to determine a target sequence code corresponding to the service request according to the to-be-processed timestamp information, and construct a target identifier corresponding to the service request according to the target reserved code, the target machine code, the target time code, and the target sequence code; The target sequence code is used to represent the counting sequence number of the service request at the current moment; A target sequence code determination module, configured to detect the target sequence code corresponding to the previous service request when receiving a new service request. When the value of the target sequence code corresponding to the previous service request is greater than or equal to the preset threshold, block the new service request until the next moment to generate a target sequence code corresponding to the new service request.
9. An electronic device, characterized in that, The electronic device includes: One or more processors; A storage device, configured to store one or more programs, When the one or more programs are executed by the one or more processors, the one or more processors implement the identifier generation method according to any one of claims 1-7.
10. A storage medium containing computer-executable instructions, where the computer-executable instructions are used to execute the identifier generation method according to any one of claims 1-7 when executed by a computer processor.
Citation Information
Patent Citations
Distributed database main key generation method and apparatus
CN108241715A
Method and device for optimizing snowflake algorithm, equipment and storage medium
CN113656172A