A single number generation method and device, a server, and a storage medium
By adjusting the serial number and timestamp, the problem of poor serial number balance in the existing technology is solved, the balanced distribution and uniqueness of the serial numbers in the order number generation server are achieved, and the balance of data in the sharded databases and tables is ensured.
Patent Information
- Application Number
- CN202110320327.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-03-25
- Publication Date
- 2025-10-21
- Estimated Expiration
- 2041-03-25
AI Technical Summary
In the existing technology, when the concurrency is not high, the serial number in the generated order number will always be 0, resulting in poor data balance and inability to evenly distribute data to each shard database and table.
By obtaining the current timestamp and the serial number to be adjusted, it is determined whether to adjust the serial number or add the timestamp based on the preset serial number threshold to generate unique and evenly distributed order numbers. Server information such as machine ID and data center ID is used to further ensure uniqueness and balance.
It achieves balanced distribution of serial numbers while ensuring the uniqueness of order numbers, improves the balance of data in sharded databases and tables, and avoids the occurrence of identical serial numbers within the same timestamp.
Smart Images

Figure CN115129726B_ABST
Abstract
Description
Technical Field
[0001] Embodiments of the present invention relate to the field of computer applications, and particularly to a single number generation method, device, server, and storage medium. Background Art
[0002] In an actual production environment, a single number is an essential piece of information in an item acquisition task. In the currently commonly used single number generation scheme, the generated single number includes a serial number, which can be understood as a sequence of incrementing identity identification numbers (identification, ID). This single number generation scheme can support a single machine to generate multiple IDs within the same millisecond to ensure the uniqueness of each single number.
[0003] In the process of implementing the present invention, the inventors found the following technical problems in the prior art: When the concurrency is not high, the serial number in the single number generated based on the existing single number generation scheme will always be 0, which will cause all data to be distributed to the same sub-database and sub-table, resulting in poor data distribution balance. Summary of the Invention
[0004] Embodiments of the present invention provide a single number generation method, device, server, and storage medium to achieve the effect of evenly distributing the serial numbers in each single number while ensuring the uniqueness of each single number.
[0005] In a first aspect, embodiments of the present invention provide a single number generation method, which is applied to a single number generation server and may include:
[0006] When a single number generation event is detected, obtain the current timestamp corresponding to the currently detected single number generation event and the to-be-adjusted serial number, where the to-be-adjusted serial number includes the serial number in the previous single number generated when the previous single number generation event was detected;
[0007] If it is determined to increase the current timestamp based on the to-be-adjusted serial number and a preset serial number threshold, increase the current timestamp and adjust the to-be-adjusted serial number to obtain an adjusted serial number;
[0008] Generate a current single number corresponding to the currently detected single number generation event based on the increased current timestamp and the adjusted serial number.
[0009] Optionally, the preset serial number threshold is the maximum value by which the to-be-adjusted serial number can be adjusted. If it is determined to increase the current timestamp based on the to-be-adjusted serial number and the preset serial number threshold, increasing the current timestamp includes:
[0010] Determine the maximum value according to the number of digits occupied by the serial number of the to-be-adjusted serial number;
[0011] If the to-be-adjusted serial number is greater than or equal to the maximum value, determine to increase the current timestamp;
[0012] Accordingly, adjusting the sequence number to be adjusted to obtain the adjusted sequence number may include:
[0013] The minimum value to which the sequence number to be adjusted can be adjusted is determined, and the sequence number to be adjusted is adjusted to the minimum value to obtain an adjusted sequence number.
[0014] Optionally, generating a current order number corresponding to the currently monitored order number generation event based on the incremented current timestamp and the adjusted sequence number may include:
[0015] Obtain the server information of the order number generation server, and generate the current order number corresponding to the currently monitored order number generation event based on the server information, the increased current timestamp and the adjusted serial number, wherein the server information includes the machine identifier of the order number generation server and / or the data center identifier of the server cluster where the order number generation server is located.
[0016] On this basis, optionally, the above-mentioned method for generating a tracking number may further include:
[0017] Obtaining the Internet Protocol address of the order number generating server, the preset number of machine occupancy bits of the machine identifier, and the preset number of data center occupancy bits of the data center identifier;
[0018] Generates a machine ID based on the Internet Protocol address, the number of bits occupied by the machine, and the number of bits occupied by the data center.
[0019] On this basis, optionally, a machine identifier is generated based on the Internet Protocol address, the number of bits occupied by the machine, and the number of bits occupied by the data center, which may include:
[0020] Assemble the individual digits of an Internet Protocol address into an integer;
[0021] A common identifier is generated according to an integer, the number of bits occupied by the machine, and the number of bits occupied by the data center, and a machine identifier is generated according to the common identifier and the number of bits occupied by the machine.
[0022] Optionally, adjusting the sequence number to be adjusted to obtain the adjusted sequence number may include:
[0023] Adjust the sequence number to be adjusted based on the first preset adjustment direction to obtain a first adjusted sequence number;
[0024] Accordingly, based on the incremented current timestamp and the adjusted sequence number, a current order number corresponding to the currently monitored order number generation event is generated, which may include:
[0025] Generate a current order number corresponding to the currently monitored order number generation event based on the incremented current timestamp and the first adjusted sequence number;
[0026] Accordingly, the above-mentioned method for generating a tracking number may further include:
[0027] Obtaining a second preset adjustment direction opposite to the first preset adjustment direction;
[0028] If it is determined that the current timestamp has not been increased according to the sequence number to be adjusted and the preset sequence number threshold, adjusting the sequence number to be adjusted based on a second preset adjustment direction to obtain a second adjusted sequence number;
[0029] Generate a current order number corresponding to the currently monitored order number generation event based on the current timestamp and the second adjusted sequence number.
[0030] Optionally, before determining to increase the current timestamp based on the sequence number to be adjusted and the preset sequence number threshold, the above-mentioned method for generating a tracking number may further include:
[0031] Get the last timestamp in the last order number;
[0032] If the current timestamp is less than the previous timestamp, the current timestamp is adjusted to the previous timestamp, and the current timestamp is updated according to the adjustment result of the current timestamp.
[0033] In a second aspect, an embodiment of the present invention further provides a tracking number generation device, which is configured on a tracking number generation server and may include:
[0034] The module for obtaining the sequence number to be adjusted is used to obtain the current timestamp and the sequence number to be adjusted corresponding to the currently monitored order number generation event when the order number generation event is monitored. The sequence number to be adjusted includes the sequence number of the previous order number generated when the order number generation event was last monitored;
[0035] An adjusted sequence number obtaining module is configured to, if it is determined that the current timestamp should be increased based on the sequence number to be adjusted and a preset sequence number threshold, increase the current timestamp and adjust the sequence number to be adjusted to obtain an adjusted sequence number;
[0036] The current order number generation module is used to generate a current order number corresponding to the currently monitored order number generation event based on the increased current timestamp and the adjusted sequence number.
[0037] In a third aspect, an embodiment of the present invention further provides a tracking number generation server, which may include:
[0038] one or more processors;
[0039] a memory for storing one or more programs;
[0040] When one or more programs are executed by one or more processors, the one or more processors implement the method for generating a tracking number provided by any embodiment of the present invention.
[0041] In a fourth aspect, an embodiment of the present invention further provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the order number generation method provided by any embodiment of the present invention.
[0042] The technical solution of the embodiment of the present invention obtains the current timestamp and the sequence number to be adjusted corresponding to the currently monitored order number generation event, so as to subsequently adjust the sequence number to be adjusted to obtain the current order number corresponding to the currently monitored order number generation event. Since the sequence number to be adjusted includes the sequence number of the previous order number generated when the order number generation event was last monitored, this continuous sequence number adjustment method is not adjusted based on time, but is adjusted based on the order number generation server that generates each order number. This ensures a balanced distribution of sequence numbers in each order number generated by the order number generation server. On this basis, to further ensure the uniqueness of each order number generated by the order number generation server, it is determined whether the sequence number to be adjusted can continue to be adjusted within the current timestamp based on the sequence number to be adjusted and a preset sequence number threshold. If not, the current timestamp is increased to continue adjusting the sequence number to be adjusted within the increased current timestamp to obtain an adjusted sequence number, thereby avoiding the situation where the same sequence number exists within the same timestamp. Then, based on the increased current timestamp and the adjusted sequence number, the current order number corresponding to the currently monitored order number generation event is generated. The above technical solution achieves the effect of balanced distribution of serial numbers in each order number while ensuring the uniqueness of each order number generated by the same order number generation server. This balanced distribution of serial numbers can evenly distribute data to each sub-library and sub-table, and the data distribution is more balanced. BRIEF DESCRIPTION OF THE DRAWINGS
[0043] Figure 1 This is a flow chart of a method for generating a tracking number in the first embodiment of the present invention;
[0044] Figure 2 This is a flow chart of a method for generating a tracking number in the second embodiment of the present invention;
[0045] Figure 3 This is a flow chart of a method for generating a tracking number in Embodiment 3 of the present invention;
[0046] Figure 4 This is a schematic diagram of an optional example of a method for generating a tracking number in the third embodiment of the present invention;
[0047] Figure 5 This is a structural block diagram of a tracking number generating device in the fourth embodiment of the present invention;
[0048] Figure 6This is a structural diagram of a ticket number generation server in Example 5 of the present invention. DETAILED DESCRIPTION
[0049] The present invention will be further described in detail below with reference to the accompanying drawings and examples. It will be understood that the specific embodiments described herein are intended only to illustrate the present invention and are not intended to limit the present invention. It should also be noted that, for ease of description, the accompanying drawings only illustrate portions relevant to the present invention, not all structures.
[0050] Example 1
[0051] Figure 1 This is a flow chart of a method for generating a tracking number, provided in Example 1 of the present invention. This embodiment is applicable to generating tracking numbers, and is particularly suitable for generating tracking numbers with a uniform distribution of sequence numbers within each tracking number. This method can be performed by the tracking number generation device provided in this embodiment of the present invention. This device can be implemented in software and / or hardware and can be integrated into a tracking number generation server.
[0052] See also Figure 1 The method of the embodiment of the present invention specifically includes the following steps:
[0053] S110. When an order number generation event is detected, obtain a current timestamp and a sequence number to be adjusted corresponding to the currently detected order number generation event, wherein the sequence number to be adjusted includes the sequence number of the previous order number generated when the order number generation event was last detected.
[0054] Among them, the order number generation event can be an event triggered by the order number calling device for notifying the order number generation server to generate an order number, such as an event triggered when the order number calling device sends an order number calling request to the order number generation server, an event triggered when calling the order number acquisition service on the order number generation server, etc. The order number calling device can be a order number calling terminal, an order number calling server, etc. It should be noted that each time the order number generation server monitors an order number generation event, it can generate a corresponding order number, that is, each order number generation event corresponds to its own order number. In order to distinguish between the various order numbers, the order number generated based on the currently monitored order number generation event can be called the current order number, and the order number generated based on the last monitored order number generation event can be called the last order number. The last monitored order number generation event is relative to the currently (i.e., this time) monitored order number generation event.
[0055] The current timestamp can be the timestamp when the order number generation event is monitored, or it can be the timestamp when the order number call request is received or the order number acquisition service is called. In actual applications, optionally, the timestamp occupies M bits for each timestamp. When M is large enough, it can be accurate to the millisecond level and can be applied for many years, such as a 41-bit timestamp can be applied for 69 years. The serial number to be adjusted can be the serial number of the previous order number generated when the order number generation event was last monitored. In actual applications, optionally, the serial number occupies N bits, that is, the order number generation method described in the embodiment of the present invention can support the same machine (i.e., the order number generation server) to generate 2 in the same millisecond. N For example, when N=12, the same machine can generate 4096 serial numbers in the same millisecond.
[0056] S120: If it is determined to increase the current timestamp based on the sequence number to be adjusted and the preset sequence number threshold, the current timestamp is increased and the sequence number to be adjusted is adjusted to obtain an adjusted sequence number.
[0057] Among them, in order to ensure the balanced distribution of serial numbers in each generated order number and avoid the situation where the serial numbers in each order number are all 0 when the concurrency is not high, the previous serial number in the previous order number generated when the order number generation event was last monitored (that is, the serial number to be adjusted) can be adjusted to obtain the adjusted serial number used to generate the current order number, that is, the adjusted serial number in the current order number generated in the subsequent steps can be the serial number obtained after adjustment on the basis of the serial number to be adjusted. This continuous adjustment scheme of the serial number is not adjusted based on the time dimension, but is adjusted based on the order number generation server that generates each order number, that is, the serial numbers in each order number generated in turn by this order number generation server are adjusted in turn, thereby ensuring the balanced distribution of the serial numbers in each order number generated by it.
[0058] On this basis, in order to further avoid the situation where the same serial number exists in the same timestamp when the concurrency is high, that is, to ensure the uniqueness of each order number, it is possible to determine whether the serial number to be adjusted can be adjusted based on the preset adjustment direction based on the numerical relationship between the serial number to be adjusted and the preset serial number threshold, and then determine whether to increase the current timestamp and whether to continue to adjust the serial number to be adjusted based on the preset adjustment direction based on the judgment result.
[0059] For example, the preset adjustment direction is to increase the sequence number to be adjusted. When the sequence number to be adjusted is greater than or equal to the preset sequence number threshold, the sequence number to be adjusted cannot be increased. The preset sequence number threshold at this time can be the maximum value to which the sequence number to be adjusted can be adjusted (such as 2 N); For example, the preset adjustment direction includes reducing the sequence number to be adjusted. When the sequence number to be adjusted is less than or equal to the preset sequence number threshold, the sequence number to be adjusted cannot be reduced any further. The preset sequence number threshold at this time may be the minimum value to which the sequence number to be adjusted can be adjusted (such as 0). The above two examples are both based on the situation where the corresponding preset adjustment direction has adjusted the sequence number to be adjusted to the extreme. At this time, the sequence number to be adjusted can only be adjusted in the direction opposite to the preset adjustment direction. For example, when the preset adjustment direction is to increase the sequence number to be adjusted, the sequence number to be adjusted is reduced. For another example, when the preset adjustment direction is to reduce the sequence number to be adjusted, the sequence number to be adjusted is increased. It should be emphasized that when adjusting the sequence number to be adjusted in the direction opposite to the preset adjustment direction, the same adjusted sequence number to be adjusted (i.e., the adjusted sequence number) may appear in the same timestamp. In order to ensure that the combination of timestamp and serial number in the currently generated order number is different from the combination of timestamp and serial number in the historically generated order number, the current timestamp can be increased while adjusting the sequence number to be adjusted in the direction opposite to the preset adjustment direction. The implementation method of increasing the current timestamp can be to increase O milliseconds (ms) based on the current timestamp. O can be an integer greater than 0, such as 1, 2, 3, etc. For example, if O=1 and the preset adjustment direction is to increase the sequence number to be adjusted, 1ms is added to the current timestamp. This is because each ms can only support 2 N If there are more than 2 serial numbers N In this case, the sequence number threshold is set to the maximum or minimum value for illustrative purposes only. It can also be a value between the maximum and minimum values, which is not specifically defined here. For example, on the same order number generation server, assuming only one order number is generated at 400ms and 401ms, and the sequence number at 400ms is 0, then the sequence number at 401ms is 1.
[0060] S130: Generate a current order number corresponding to the currently monitored order number generation event based on the increased current timestamp and the adjusted sequence number.
[0061] Among them, there are many ways to generate the current order number corresponding to the currently monitored order number generation event based on the increased current timestamp and adjusted serial number, such as using the increased current timestamp and adjusted serial number as part of the current order number to generate the order number; for example, processing the current timestamp and / or adjusted serial number, and generating the current order number based on the processing results; etc., which are not specifically limited here.
[0062] The technical solution of the embodiment of the present invention obtains the current timestamp and the sequence number to be adjusted corresponding to the currently monitored order number generation event, so as to subsequently adjust the sequence number to be adjusted to obtain the current order number corresponding to the currently monitored order number generation event. Since the sequence number to be adjusted includes the sequence number of the previous order number generated when the order number generation event was last monitored, this continuous sequence number adjustment method is not adjusted based on time, but is adjusted based on the order number generation server that generates each order number. This ensures a balanced distribution of sequence numbers in each order number generated by the order number generation server. On this basis, to further ensure the uniqueness of each order number generated by the order number generation server, it is determined whether the sequence number to be adjusted can continue to be adjusted within the current timestamp based on the sequence number to be adjusted and a preset sequence number threshold. If not, the current timestamp is increased to continue adjusting the sequence number to be adjusted within the increased current timestamp to obtain an adjusted sequence number, thereby avoiding the situation where the same sequence number exists within the same timestamp. Then, based on the increased current timestamp and the adjusted sequence number, the current order number corresponding to the currently monitored order number generation event is generated. The above technical solution achieves the effect of balanced distribution of serial numbers in each order number while ensuring the uniqueness of each order number generated by the same order number generation server. This balanced distribution of serial numbers can evenly distribute data to each sub-library and sub-table, and the data distribution is more balanced.
[0063] An optional technical solution, wherein the preset sequence number threshold includes the maximum value to which the sequence number to be adjusted can be adjusted. If it is determined to increase the current timestamp based on the sequence number to be adjusted and the preset sequence number threshold, then increasing the current timestamp may include: determining the maximum value based on the number of sequence number digits occupied by the sequence number to be adjusted; if the sequence number to be adjusted is greater than or equal to the maximum value, then determining to increase the current timestamp; accordingly, adjusting the sequence number to be adjusted to obtain the adjusted sequence number may include: determining the minimum value to which the sequence number to be adjusted can be adjusted, adjusting the sequence number to the minimum value, and obtaining the adjusted sequence number. The number of sequence number digits occupied may be the number of digits occupied by the sequence number to be adjusted in the current order number, that is, the number of digits occupied by the adjusted sequence number in the current order number. The more digits occupied by the sequence number, the larger the maximum value. For example, in combination with the application scenarios that may be involved in the embodiments of the present invention, the maximum value may be 2^the number of sequence number digits occupied or 2^the number of sequence number digits occupied-1. When the sequence number to be adjusted is greater than or equal to the maximum value, it means that the sequence number to be adjusted cannot be increased further within the current timestamp. In this case, the current timestamp can be increased, and the sequence number to be adjusted can be adjusted to its minimum adjustable value within the increased current timestamp. This can simultaneously ensure the uniqueness of each generated order number and the balanced accumulation of sequence numbers in each order number.
[0064] The above technical solution is to adjust the sequence number to be adjusted in the direction of small to large. If the sequence number to be adjusted is adjusted in the direction of large to small, the preset sequence number threshold may include the minimum value to which the sequence number to be adjusted can be adjusted. If it is determined to increase the current timestamp based on the sequence number to be adjusted and the preset sequence number threshold, then increasing the current timestamp may include: determining the minimum value based on the number of bits occupied by the sequence number to be adjusted; if the sequence number to be adjusted is less than or equal to the minimum value, determining to increase the current timestamp; accordingly, adjusting the sequence number to be adjusted to obtain the adjusted sequence number may include: determining the maximum value to which the sequence number to be adjusted can be adjusted, adjusting the sequence number to the maximum value, and obtaining the adjusted sequence number.
[0065] An optional technical solution, regardless of whether to increase the current timestamp based on the preset sequence number threshold and the sequence number to be adjusted, the sequence number to be adjusted needs to be adjusted, but the numerical adjustment direction may be different in different situations. Specifically, for the case where the current timestamp is increased based on the preset sequence number threshold and the sequence number to be adjusted, the sequence number to be adjusted can be adjusted based on the first preset adjustment direction to obtain the first adjusted sequence number; and then, based on the increased current timestamp and the first adjusted sequence number, the current order number corresponding to the currently monitored order number generation event is generated. The optional example of this situation has been illustrated above and will not be repeated here. It should be noted that the preset adjustment direction explained above can be called the second preset adjustment direction, and the direction opposite to the preset adjustment direction can be called the first preset adjustment direction.
[0066] In the case where it is determined that the current timestamp has not been increased based on the preset sequence number threshold and the sequence number to be adjusted, the sequence number to be adjusted can be adjusted based on a second preset adjustment direction opposite to the first preset adjustment direction to obtain a second adjusted sequence number; and then, based on the current timestamp and the second adjusted sequence number, a current order number corresponding to the currently monitored order number generation event is generated. For example, assuming that the first preset adjustment direction includes decreasing the sequence number to be adjusted, then the second preset adjustment direction includes increasing the sequence number to be adjusted. When the sequence number to be adjusted is less than the preset sequence number threshold, the sequence number to be adjusted can continue to be increased. At this time, the preset sequence number threshold can be the maximum value to which the sequence number to be adjusted can be adjusted (such as 2 N); For example, assuming that the first preset adjustment direction is to increase the sequence number to be adjusted, then the second preset adjustment direction is to decrease the sequence number to be adjusted. When the sequence number to be adjusted is greater than the preset sequence number threshold, the sequence number to be adjusted can continue to be decreased. At this time, the preset sequence number threshold can be the minimum value to which the sequence number to be adjusted can be adjusted (such as 0). The above two examples are both based on the situation where the corresponding second preset adjustment direction has not adjusted the sequence number to be adjusted to the extreme. The sequence number to be adjusted can continue to be adjusted based on the second preset adjustment direction, and the situation where the combination of the timestamp and sequence number in the currently generated order number is the same as the combination of the timestamp and sequence number in the historically generated order number will not occur. Therefore, at this time, only the sequence number to be adjusted can be adjusted and there is no need to increase the current timestamp.
[0067] Example 2
[0068] Figure 2 It is a flow chart of a method for generating an order number provided in the second embodiment of the present invention. This embodiment is optimized based on the above-mentioned technical solutions. In this embodiment, optionally, based on the increased current timestamp and the adjusted serial number, a current order number corresponding to the currently monitored order number generation event is generated, which may include: obtaining the server information of the order number generation server, and generating the current order number corresponding to the currently monitored order number generation event based on the server information, the increased current timestamp and the adjusted serial number, wherein the server information includes the machine identifier of the order number generation server and / or the data center identifier of the server cluster where the order number generation server is located. Among them, the explanations of the terms that are the same as or corresponding to the above-mentioned embodiments are not repeated here.
[0069] See also Figure 2 The method of this embodiment may specifically include the following steps:
[0070] S210. When an order number generation event is detected, obtain a current timestamp and a sequence number to be adjusted corresponding to the currently detected order number generation event, wherein the sequence number to be adjusted includes the sequence number of the previous order number generated when the order number generation event was last detected.
[0071] S220: If it is determined to increase the current timestamp based on the sequence number to be adjusted and the preset sequence number threshold, the current timestamp is increased and the sequence number to be adjusted is adjusted to obtain an adjusted sequence number.
[0072] S230. Obtain the server information of the order number generation server, and generate a current order number corresponding to the currently monitored order number generation event based on the server information, the increased current timestamp, and the adjusted serial number, wherein the server information includes the machine identifier of the order number generation server and / or the data center identifier of the server cluster where the order number generation server is located.
[0073] Among them, in actual applications, the number of order number generation servers may be one, two or more, and each order number generation server may constitute one, two or more server clusters, and the number of order number generation servers in each server cluster is limited. Since these order number generation servers can work at the same time, in order to ensure that the order numbers generated by these order number generation servers are unique, that is, not only the order numbers generated by the same order number generation server are unique, but also the order numbers generated by different order number generation servers are unique, when generating the current order number, in addition to considering the added current timestamp and the adjusted serial number, the server information of the order number generation server used to generate the current order number can also be considered. The server information can be the machine ID of the order number generation server and / or the data center ID of the server cluster where the order number generation server is located. Specifically, the machine ID can be the unique ID of the order number generation server. Assuming that the number of bits occupied by the machine ID is P, in order to ensure the uniqueness of the order numbers containing the machine ID, there can be 2 N The order generation servers work simultaneously. For example, when P=10, 1024 order generation servers can be deployed. The data center identifier can be the unique identifier of the server cluster where the order generation server is located. Assuming that the number of bits occupied by the data center identifier is Q, in order to ensure the uniqueness of each order number containing the machine identifier and the data center identifier, there can be 2 Q Server clusters work simultaneously, and each server cluster can have 2 N The order number generation servers work simultaneously.
[0074] The technical solution of the embodiment of the present invention, when generating the current order number, not only considers the added current timestamp and the adjusted serial number, but also considers the machine identifier and / or the data center identifier, thereby ensuring that when multiple order number generation servers and / or multiple server clusters work at the same time, the uniqueness of the order numbers generated by different order number generation servers in the same server cluster or different server clusters can still be guaranteed, and the serial number in each order number is determined based on the data center and / or machine as the dimension, thereby achieving the effect of balanced distribution of the serial numbers.
[0075] An optional technical solution is that the above-mentioned machine identifier can be automatically generated in advance through the following steps: obtaining the Internet Protocol (IP) address of the order number generating server, the preset number of machine occupancy bits of the machine identifier, and the preset number of data center occupancy bits of the data center identifier; generating the machine identifier based on the Internet Protocol address, the number of machine occupancy bits, and the number of data center occupancy bits. Among them, the number of machine occupancy bits can be the number of bits occupied by the preset machine identifier in the current order number, and the number of data center occupancy bits can be the number of bits occupied by the preset data center identifier in the current order number. There are many ways to generate the machine identifier based on the Internet Protocol address, the number of machine occupancy bits, and the number of data center occupancy bits, such as assembling the various digits in the Internet Protocol address into an integer; generating a common identifier based on the integer, the number of machine occupancy bits, and the number of data center occupancy bits, and generating the machine identifier based on the common identifier and the number of machine occupancy bits. When the number of order number generating servers is huge, compared with manually configuring machine identifiers, the above-mentioned technical solution that can automatically generate machine identifiers for each order number server greatly reduces labor costs and effectively ensures the accuracy and efficiency of machine identifier generation. On this basis, the automatic generation process of the data center identifier is similar, such as generating the data center identifier based on the common identifier and the number of data center occupancy bits.
[0076] To better understand the process of generating the aforementioned machine and data center identifiers, the following provides an illustrative example. For example, the IP address of the order number generation server is obtained and the four digits of the Internet Protocol version 4 (IP4) address in the IP address are assembled into a long integer. For example, if the IP4 address is 192.168.1.1, this can be assembled into a long integer of type 19216811. This integer is similar to a key, and the advantage of the long type is that it has a large number of bits. Furthermore, the data center identifier is generated according to the following formula: the bitwise AND of the integer and 2^(number of data center occupancy bits + number of machine occupancy bits) exponential power - 1, and then divided by 2^data center occupancy exponential power to obtain the divisor as the data center identifier. The bitwise AND of the integer and 2^(number of data center occupancy bits + number of machine occupancy bits) exponential power - 1 can be called a common identifier. When the number of data center occupancy bits is R, a data center identifier not greater than 2^R can be generated; the machine identifier is generated according to the following formula: the remainder obtained by dividing the common identifier by 2^number of machine center occupancy bits exponential power is used as the machine center identifier.
[0077] Example 3
[0078] Figure 3This is a flowchart of a method for generating an order number provided in Example 3 of the present invention. This embodiment is optimized based on the above-mentioned technical solutions. In this embodiment, optionally, before determining to increase the current timestamp based on the sequence number to be adjusted and the preset sequence number threshold, the above-mentioned order number generation method may also include: obtaining the previous timestamp in the previous order number; if the current timestamp is less than the previous timestamp, adjusting the current timestamp to the previous timestamp, and updating the current timestamp based on the adjustment result of the current timestamp. Among them, the explanations of the terms that are the same as or corresponding to the above-mentioned embodiments are not repeated here.
[0079] See also Figure 3 The method of this embodiment may specifically include the following steps:
[0080] S310: When an order number generation event is detected, obtain a current timestamp and a sequence number to be adjusted corresponding to the currently detected order number generation event, wherein the sequence number to be adjusted includes the sequence number of the previous order number generated when the order number generation event was last detected.
[0081] S320. Obtain the previous timestamp in the previous order number. If the current timestamp is less than the previous timestamp, adjust the current timestamp to the previous timestamp, and update the current timestamp based on the adjustment result of the current timestamp.
[0082] Among them, the timestamp of the order number generation server is dynamically adjusted, and it is possible that the timestamp will run to the previous few milliseconds. This situation can be called time rollback. In actual application, if the timestamp of the order number generation server is normal, then the current timestamp is greater than or equal to the previous timestamp in the previous order number. That is, if the current timestamp is less than the previous timestamp, this means that the order number generation server has experienced a time rollback. At this time, if the current order number is directly generated based on such a current timestamp, the problem of duplicate order numbers is likely to occur. In order to solve this problem, an optional solution is to throw an exception when the order number generation server experiences a time rollback, but this will cause the order number calling device to be unable to call the current order number for a period of time, thereby affecting the use of the production environment. Accordingly, in order to solve the time rollback problem, ensure the normal use of the production environment, and ensure the uniqueness of each generated order number, the current timestamp can be adjusted to the previous timestamp, and the current timestamp can be updated based on the adjustment result of the current timestamp.
[0083] S330: If it is determined to increase the current timestamp based on the sequence number to be adjusted and the preset sequence number threshold, the current timestamp is increased and the sequence number to be adjusted is adjusted to obtain an adjusted sequence number.
[0084] S340: Generate a current order number corresponding to the currently monitored order number generation event based on the increased current timestamp and the adjusted sequence number.
[0085] The technical solution of the embodiment of the present invention determines whether a time rollback problem occurs in the order number generation server by whether the current timestamp is smaller than the previous timestamp. If so, the current timestamp can be set to the previous timestamp, that is, the order number is generated based on the previous timestamp, thereby achieving the effect of normal and non-duplicate generation of order numbers under abnormal circumstances.
[0086] In order to better understand the specific implementation process of the above steps, the following is an exemplary description of the method for generating a tracking number in each embodiment with reference to specific examples. Figure 4 :
[0087] 1. Get the IP address of the order number generation server and assemble the four digits in IP4 in the IP address into a long integer.
[0088] 2. Generate a data center ID and a machine center ID based on the long type integer, the number of bits occupied by the data center, and the number of bits occupied by the machine.
[0089] 3. Get the current timestamp,
[0090] A) If the current timestamp is less than the previous timestamp, set the current timestamp to the previous timestamp and execute the following steps; otherwise, execute the following steps directly;
[0091] B) If the sequence number to be adjusted is greater than or equal to the maximum value, 1ms is added to the current timestamp, the current timestamp is updated based on the added value, and the sequence number to be adjusted is adjusted to 0 to obtain the adjusted sequence number; otherwise, 1 is added to the sequence number to obtain the adjusted sequence number.
[0092] 4. Generate current order number
[0093] Generates an 18-digit long current order number based on the data center ID, machine ID, current timestamp, and adjusted serial number, and returns the current order number to the order number calling device.
[0094] Example 4
[0095] Figure 5 This is a structural block diagram of the tracking number generation device provided in the fourth embodiment of the present invention. The device is used to execute the tracking number generation method provided in any of the above embodiments. The device and the tracking number generation method of the above embodiments belong to the same inventive concept. For details not fully described in the embodiments of the tracking number generation device, please refer to the embodiments of the above tracking number generation method. Figure 5 The device can be configured in a tracking number generating server, and specifically can include: a module for obtaining a sequence number to be adjusted 410, a module for obtaining an adjusted sequence number 420, and a module for generating a current tracking number 430.
[0096] The sequence number to be adjusted acquisition module 410 is configured to, upon detecting a tracking number generation event, acquire the current timestamp and the sequence number to be adjusted corresponding to the currently detected tracking number generation event, wherein the sequence number to be adjusted includes the sequence number of the previous tracking number generated when the tracking number generation event was last detected.
[0097] An adjusted sequence number obtaining module 420 is configured to, if it is determined that the current timestamp should be increased based on the sequence number to be adjusted and a preset sequence number threshold, increase the current timestamp and adjust the sequence number to be adjusted to obtain an adjusted sequence number;
[0098] The current order number generating module 430 is configured to generate a current order number corresponding to the currently monitored order number generating event according to the incremented current timestamp and the adjusted sequence number.
[0099] Optionally, the preset sequence number threshold is the maximum value to which the sequence number to be adjusted can be adjusted. The adjusted sequence number obtaining module 420 may specifically include:
[0100] The maximum value determining unit is used to determine the maximum value based on the number of bits occupied by the sequence number to be adjusted; the current timestamp increasing unit is used to determine to increase the current timestamp if the sequence number to be adjusted is greater than or equal to the maximum value; the adjusted sequence number obtaining unit is used to determine the minimum value to which the sequence number to be adjusted can be adjusted, adjust the sequence number to the minimum value, and obtain the adjusted sequence number.
[0101] Optionally, the current order number generating module 430 may specifically include:
[0102] The first current order number generation unit is used to obtain server information of the order number generation server, and generate a current order number corresponding to the currently monitored order number generation event based on the server information, the increased current timestamp and the adjusted serial number, wherein the server information includes the machine identifier of the order number generation server and / or the data center identifier of the server cluster where the order number generation server is located.
[0103] On this basis, optionally, the above-mentioned order number generating device may further include:
[0104] The occupied digits acquisition module is used to obtain the Internet Protocol address of the order number generation server, the preset machine occupied digits of the machine identifier, and the preset data center occupied digits of the data center identifier; the machine identifier generation module is used to generate the machine identifier based on the Internet Protocol address, the machine occupied digits and the data center occupied digits.
[0105] On this basis, the optional machine identification generation module may specifically include:
[0106] The digital assembly unit is used to assemble the individual numbers in the Internet Protocol address into an integer; the machine identification generation unit is used to generate a common identification based on the integer, the number of bits occupied by the machine and the number of bits occupied by the data center, and to generate a machine identification based on the common identification and the number of bits occupied by the machine.
[0107] Optionally, the adjusted serial number obtaining module 420 may include:
[0108] A first adjusted sequence number obtaining unit, configured to adjust the sequence number to be adjusted based on a first preset adjustment direction to obtain a first adjusted sequence number;
[0109] Accordingly, the current order number generating module 430 may specifically include:
[0110] a second current order number generating unit, configured to generate a current order number corresponding to a currently monitored order number generation event based on the incremented current timestamp and the first adjusted sequence number;
[0111] Accordingly, the above-mentioned order number generating device may further include:
[0112] A second preset adjustment direction acquisition module is used to obtain a second preset adjustment direction that is opposite to the first preset adjustment direction; a second adjusted serial number acquisition module is used to adjust the serial number to be adjusted based on the second preset adjustment direction to obtain a second adjusted serial number if it is determined based on the serial number to be adjusted and the preset serial number threshold that the current timestamp has not been increased; and a current order number regeneration module is used to generate a current order number corresponding to the currently monitored order number generation event based on the current timestamp and the second adjusted serial number.
[0113] Optionally, the above-mentioned order number generating device may further include:
[0114] A previous timestamp acquisition module is used to obtain the previous timestamp in the previous order number before the current timestamp is increased based on the sequence number to be adjusted and the preset sequence number threshold;
[0115] The current timestamp update module adjusts the current timestamp to the previous timestamp if the current timestamp is less than the previous timestamp, and updates the current timestamp according to the adjustment result of the current timestamp.
[0116] The order number generation device provided by the fourth embodiment of the present invention obtains the current timestamp and the sequence number to be adjusted corresponding to the order number generation event monitored this time through the sequence number to be adjusted acquisition module, so as to obtain the current order number corresponding to the order number generation event monitored this time after adjusting the sequence number to be adjusted. Since the sequence number to be adjusted includes the sequence number in the previous order number generated when the order number generation event was monitored last time, this continuous adjustment method of the sequence number is not adjusted based on time, but is adjusted based on the order number generation server that generates each order number, thereby ensuring the sequence number in each order number generated by the order number generation server. Balanced distribution; on this basis, in order to further ensure the uniqueness of each order number generated by the order number generation server, the adjusted serial number obtaining module determines whether the sequence number to be adjusted can continue to be adjusted within the current timestamp based on the sequence number to be adjusted and the preset sequence number threshold. If not, the current timestamp is increased to continue adjusting the sequence number to be adjusted within the increased current timestamp to obtain the adjusted sequence number, thereby avoiding the situation where the same sequence number exists within the same timestamp; furthermore, the current order number generation module generates the current order number corresponding to the currently monitored order number generation event based on the increased current timestamp and the adjusted sequence number. The above device, under the premise of ensuring the uniqueness of each order number generated by the same order number generation server, achieves the effect of balanced distribution of serial numbers in each order number. This balanced distribution of serial numbers can make the data evenly distributed to each sub-database and sub-table, and the data distribution is more balanced.
[0117] The order number generating device provided in the embodiment of the present invention can execute the order number generating method provided in any embodiment of the present invention, and has the corresponding functional modules and beneficial effects of the execution method.
[0118] It is worth noting that in the embodiment of the above-mentioned order number generating device, the various units and modules included are only divided according to functional logic, but are not limited to the above-mentioned division, as long as the corresponding functions can be achieved; in addition, the specific names of the functional units are only for the convenience of distinguishing each other, and are not used to limit the scope of protection of the present invention.
[0119] Example 5
[0120] Figure 6 A schematic diagram of the structure of a ticket generation server provided in the fifth embodiment of the present invention is shown as follows: Figure 6 As shown, the order number generating server includes a memory 510, a processor 520, an input device 530 and an output device 540. The number of processors 520 in the order number generating server can be one or more. Figure 6In the example of a processor 520; the memory 510, the processor 520, the input device 530 and the output device 540 in the order number generating server can be connected via a bus or other means, Figure 6 The connection via bus 550 is taken as an example.
[0121] Memory 510, as a computer-readable storage medium, can be used to store software programs, computer-executable programs, and modules, such as the program instructions / modules corresponding to the tracking number generation method in the embodiments of the present invention (e.g., module 410 for obtaining the serial number to be adjusted, module 420 for obtaining the adjusted serial number, and module 430 for generating the current tracking number in the tracking number generation device). Processor 520 executes the software programs, instructions, and modules stored in memory 510 to execute various functional applications and data processing of the tracking number generation server, thereby implementing the aforementioned tracking number generation method.
[0122] The memory 510 may mainly include a program storage area and a data storage area, wherein the program storage area may store an operating system and at least one application required for a function; the data storage area may store data created based on the use of the order number generation server, etc. In addition, the memory 510 may include a high-speed random access memory and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, or other non-volatile solid-state storage device. In some instances, the memory 510 may further include a memory remotely located relative to the processor 520, and these remote memories may be connected to the device via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.
[0123] The input device 530 may be used to receive input digital or character information and generate key signal input related to user settings and function control of the device. The output device 540 may include a display device such as a display screen.
[0124] Example 6
[0125] Embodiment 6 of the present invention provides a storage medium containing computer-executable instructions. When executed by a computer processor, the computer-executable instructions are used to perform a method for generating a tracking number. The method is applied to a tracking number generation server and may include:
[0126] When a tracking number generation event is detected, obtain the current timestamp and the sequence number to be adjusted corresponding to the currently detected tracking number generation event, where the sequence number to be adjusted includes the sequence number of the previous tracking number generated when the tracking number generation event was last detected;
[0127] If it is determined that the current timestamp should be increased based on the sequence number to be adjusted and the preset sequence number threshold, the current timestamp is increased and the sequence number to be adjusted is adjusted to obtain the adjusted sequence number;
[0128] Based on the incremented current timestamp and the adjusted sequence number, a current order number corresponding to the currently monitored order number generation event is generated.
[0129] Of course, the storage medium containing computer-executable instructions provided in an embodiment of the present invention is not limited to the method operations described above, and can also execute related operations in the order number generation method provided in any embodiment of the present invention.
[0130] Through the above description of the implementation methods, those skilled in the art can clearly understand that the present invention can be implemented with the help of software and necessary general-purpose hardware, and of course it can also be implemented by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art, can be embodied in the form of a software product, which can be stored in a computer-readable storage medium, such as a computer's floppy disk, read-only memory (ROM), random access memory (RAM), flash memory (FLASH), hard disk or optical disk, etc., including a number of instructions for enabling a computer device (which can be a personal computer, server, or network device, etc.) to execute the methods described in each embodiment of the present invention.
[0131] Note that the above are only preferred embodiments of the present invention and the technical principles employed. Those skilled in the art will understand that the present invention is not limited to the specific embodiments described herein, and that various obvious changes, readjustments, and substitutions can be made by those skilled in the art without departing from the scope of protection of the present invention. Therefore, although the present invention has been described in detail through the above embodiments, the present invention is not limited to the above embodiments and may include many other equivalent embodiments without departing from the concept of the present invention. The scope of the present invention is determined by the scope of the appended claims.
Claims
1. A method for generating a tracking number, characterized in that: Applied to a tracking number generation server, the method includes: When a tracking number generation event is detected, obtaining a current timestamp and a sequence number to be adjusted corresponding to the currently detected tracking number generation event, wherein the sequence number to be adjusted includes the sequence number of the previous tracking number generated when the tracking number generation event was last detected; Obtain the last timestamp in the last order number; If the current timestamp is less than the previous timestamp, the current timestamp is adjusted to the previous timestamp, and the current timestamp is updated according to the adjustment result of the current timestamp; if it is determined to increase the current timestamp based on the sequence number to be adjusted and the preset sequence number threshold, the current timestamp is increased and the sequence number to be adjusted is adjusted to obtain an adjusted sequence number, wherein the increase of the current timestamp is based on the current timestamp; Generating a current order number corresponding to the currently monitored order number generation event according to the incremented current timestamp and the adjusted sequence number; If it is determined according to the sequence number to be adjusted and the preset sequence number threshold that the current timestamp is not increased, adjusting the sequence number to be adjusted based on a second preset adjustment direction to obtain a second adjusted sequence number; A current order number corresponding to the currently monitored order number generation event is generated according to the current timestamp and the second adjusted sequence number.
2. The method according to claim 1, characterized in that The preset sequence number threshold is a maximum value to which the sequence number to be adjusted can be adjusted, and if it is determined to increase the current timestamp based on the sequence number to be adjusted and the preset sequence number threshold, increasing the current timestamp includes: Determine the maximum value according to the number of bits occupied by the sequence number to be adjusted; If the sequence number to be adjusted is greater than or equal to the maximum value, determining to increase the current timestamp; Accordingly, adjusting the sequence number to be adjusted to obtain the adjusted sequence number includes: The minimum value to which the sequence number to be adjusted can be adjusted is determined, and the sequence number to be adjusted is adjusted to the minimum value to obtain an adjusted sequence number.
3. The method according to claim 1, characterized in that Generating a current order number corresponding to the currently monitored order number generation event according to the increased current timestamp and the adjusted sequence number includes: Obtain the server information of the order number generation server, and generate a current order number corresponding to the currently monitored order number generation event based on the server information, the increased current timestamp and the adjusted serial number, wherein the server information includes the machine identifier of the order number generation server and / or the data center identifier of the server cluster where the order number generation server is located.
4. The method according to claim 3, characterized in that Also includes: Obtaining the Internet Protocol address of the order number generating server, the preset number of machine occupied bits of the machine identifier, and the preset number of data center occupied bits of the data center identifier; The machine identifier is generated according to the Internet Protocol address, the number of bits occupied by the machine, and the number of bits occupied by the data center.
5. The method according to claim 4, characterized in that Generating the machine identifier according to the Internet Protocol address, the number of bits occupied by the machine, and the number of bits occupied by the data center includes: Assembling the individual digits in the Internet Protocol address into an integer; A common identifier is generated according to the integer, the number of bits occupied by the machine, and the number of bits occupied by the data center, and the machine identifier is generated according to the common identifier and the number of bits occupied by the machine.
6. The method according to claim 1, characterized in that The adjusting the sequence number to be adjusted to obtain the adjusted sequence number includes: Adjusting the to-be-adjusted sequence number based on a first preset adjustment direction to obtain a first adjusted sequence number, wherein the first preset adjustment direction is opposite to the second preset adjustment direction; Accordingly, generating a current order number corresponding to the currently monitored order number generation event according to the increased current timestamp and the adjusted sequence number includes: A current order number corresponding to the currently monitored order number generation event is generated according to the increased current timestamp and the first adjusted sequence number.
7. A tracking number generating device, characterized in that: Configured on the order number generating server, the device includes: A sequence number to be adjusted acquisition module is used to acquire, when a tracking number generation event is detected, a current timestamp and a sequence number to be adjusted corresponding to the currently detected tracking number generation event, wherein the sequence number to be adjusted includes the sequence number of the previous tracking number generated when the tracking number generation event was last detected; an adjusted sequence number obtaining module, configured to, if it is determined based on the sequence number to be adjusted and a preset sequence number threshold that the current timestamp should be increased, increase the current timestamp, and adjust the sequence number to be adjusted to obtain an adjusted sequence number, wherein the increasing of the current timestamp is based on the current timestamp; a current order number generating module, configured to generate a current order number corresponding to the currently monitored order number generating event based on the incremented current timestamp and the adjusted sequence number; A last timestamp acquisition module, used to obtain the last timestamp in the last order number; a current timestamp updating module, configured to adjust the current timestamp to the previous timestamp if the current timestamp is less than the previous timestamp, and update the current timestamp according to the adjustment result of the current timestamp; a second adjusted sequence number obtaining module configured to adjust the sequence number to be adjusted based on a second preset adjustment direction to obtain a second adjusted sequence number if it is determined that the current timestamp has not been increased according to the sequence number to be adjusted and a preset sequence number threshold; The current order number regeneration module is used to generate a current order number corresponding to the currently monitored order number generation event according to the current timestamp and the second adjusted sequence number.
8. A ticket number generation server, characterized in that: include: one or more processors; a memory for storing 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 order number generation method as described in any one of claims 1 to 6.
9. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the method for generating a tracking number as described in any one of claims 1 to 6 is implemented.
Citation Information
Patent Citations
Global ID generation method and device for service entity
CN110737806A