Serial number generation method, device, computer equipment and readable storage medium
By receiving and generating business serial number acquisition requests in a distributed system, querying the target business serial number rules and the maximum value of the number segment, and generating batch serial number sets, the problem of high database performance load is solved and the efficiency and uniqueness of serial number generation are improved.
Patent Information
- Application Number
- CN202411178307.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-26
- Publication Date
- 2025-09-30
- Estimated Expiration
- 2044-08-26
AI Technical Summary
In distributed systems, existing sequence number generation methods result in high database performance requirements, excessive load, and slow generation efficiency.
By receiving the target business serial number acquisition request from the business end, querying the target business serial number rules and the maximum value of the number segment, generating the first business serial number set of the number of serial numbers, reducing the number of requests to the database, and using the target business serial number rules and the maximum value of the number segment to generate batch serial numbers.
It reduces the performance requirements and load on the database, improves the efficiency of serial number generation, and ensures the uniqueness and generation efficiency of the serial number.
Smart Images

Figure CN119066069B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a method, apparatus, computer device, computer-readable storage medium, and computer program product for generating a serial number. Background Art
[0002] When running applications in a distributed system, various services are executed, and the execution of each service requires service serial numbers as data support. Therefore, during the service execution process, service serial numbers are generated using a serial number generation method.
[0003] In the current method of generating a sequence number, the server sends a request to generate a target service sequence number to the database. In response to the request, the database generates a target service sequence number and returns the target service sequence number to the server.
[0004] However, current methods for generating serial numbers require multiple servers in a distributed system, each of which needs to generate multiple serial numbers. Consequently, each server sends a large number of generation requests to the database, placing high demands on database performance and placing excessive database load. This also results in slower serial number generation efficiency. Summary of the Invention
[0005] Based on this, it is necessary to provide a serial number generation method, device, computer equipment, computer-readable storage medium and computer program product to address the above technical problems.
[0006] In a first aspect, the present application provides a method for generating a sequence number, the method comprising:
[0007] Receive a target service serial number acquisition request sent by a service end; the target service serial number acquisition request includes a target service number and an acquisition quantity;
[0008] Query the target service sequence number rule according to the target service number and obtain the maximum value of the number segment;
[0009] The number of service sequence numbers to be generated is determined based on the acquired quantity, and a first service sequence number set of the number of sequence numbers is generated based on the maximum value of the number segment and the target service sequence number rule.
[0010] In one embodiment, before receiving the target service sequence number acquisition request sent by the service end, the method further includes:
[0011] Receive the target business sequence number rule of the target business sequence number sent by the management end;
[0012] A target service sequence number queue corresponding to the target service sequence number is established based on the target service sequence number rule; the target service sequence number queue is used to store the target service sequence number.
[0013] In one embodiment, after receiving the target service sequence number acquisition request sent by the service end, the method further includes:
[0014] Querying a target service sequence number queue according to the target service number, and determining the number of target service sequence numbers in the target service sequence number queue as the number of queue sequence numbers;
[0015] Determine whether the number of queue sequence numbers is less than the acquired number;
[0016] If the number of queue sequence numbers is less than the acquisition number, it is determined that the target service sequence number acquisition request meets the preset sequence number generation condition.
[0017] In one embodiment, querying the target service sequence number rule according to the target service number and obtaining the maximum value of the number segment includes:
[0018] In the case where the target service sequence number acquisition request meets the sequence number generation condition, querying the target service sequence number rule according to the target service number;
[0019] Sending a status acquisition request to the database; the status acquisition request is used to query the process lock status of the maximum value of the number segment;
[0020] Receive the process lock status of the maximum value of the number segment returned by the database, and obtain the maximum value of the number segment based on the process lock status.
[0021] In one embodiment, obtaining the maximum value of the number segment based on the process lock state includes:
[0022] If the process lock state is the locked state, executing the step of sending a state acquisition request to the database according to a preset time period until the process lock state is the unlocked state;
[0023] If the process lock state is unlocked, the maximum value of the number segment is obtained and the maximum value of the number segment is locked.
[0024] In one embodiment, generating the first service sequence number set of the number of sequence numbers based on the maximum value of the number segment and the target service sequence number rule includes:
[0025] Determine the starting target service sequence number based on the maximum value of the number segment and the step size in the target service sequence number rule;
[0026] Starting with the starting target service sequence number, generating the target service sequence number of the sequence number based on the target service sequence number rule;
[0027] A first service sequence number set is constructed according to each of the target service sequence numbers.
[0028] In one embodiment, after generating the first service sequence number set of the number of sequence numbers based on the maximum value of the number segment and the target service sequence number rule, the method further includes:
[0029] Determining the largest target service sequence number in the first service sequence number set as the maximum value of the target number segment, and performing unlocking processing on the maximum value of the number segment;
[0030] The maximum value of the unlocked number segment is updated according to the maximum value of the target number segment.
[0031] In a second aspect, the present application further provides a serial number generating device, comprising:
[0032] A receiving module, configured to receive a target service serial number acquisition request sent by a service end; the target service serial number acquisition request includes a target service number and an acquisition quantity;
[0033] An acquisition module is used to query the target service sequence number rule according to the target service number and obtain the maximum value of the number segment;
[0034] A generation module is used to determine the number of service sequence numbers to be generated based on the acquired quantity, and to generate a first service sequence number set of the number of sequence numbers based on the maximum value of the number segment and the target service sequence number rule.
[0035] In a third aspect, the present application further provides a computer device comprising a memory and a processor, wherein the memory stores a computer program, and when the processor executes the computer program, the following steps are implemented:
[0036] Receive a target service serial number acquisition request sent by a service end; the target service serial number acquisition request includes a target service number and an acquisition quantity;
[0037] Query the target service sequence number rule according to the target service number and obtain the maximum value of the number segment;
[0038] The number of service sequence numbers to be generated is determined based on the acquired quantity, and a first service sequence number set of the number of sequence numbers is generated based on the maximum value of the number segment and the target service sequence number rule.
[0039] In a fourth aspect, the present application further provides a computer-readable storage medium having a computer program stored thereon, wherein when the computer program is executed by a processor, the following steps are implemented:
[0040] Receive a target service serial number acquisition request sent by a service end; the target service serial number acquisition request includes a target service number and an acquisition quantity;
[0041] Query the target service sequence number rule according to the target service number and obtain the maximum value of the number segment;
[0042] The number of service sequence numbers to be generated is determined based on the acquired quantity, and a first service sequence number set of the number of sequence numbers is generated based on the maximum value of the number segment and the target service sequence number rule.
[0043] In a fifth aspect, the present application further provides a computer program product, comprising a computer program, which, when executed by a processor, implements the following steps:
[0044] Receive a target service serial number acquisition request sent by a service end; the target service serial number acquisition request includes a target service number and an acquisition quantity;
[0045] Query the target service sequence number rule according to the target service number and obtain the maximum value of the number segment;
[0046] The number of service sequence numbers to be generated is determined based on the acquired quantity, and a first service sequence number set of the number of sequence numbers is generated based on the maximum value of the number segment and the target service sequence number rule.
[0047] The above-mentioned sequence number generation method, apparatus, computer equipment, computer-readable storage medium and computer program product receive a target business sequence number acquisition request sent by a business end; the target business sequence number acquisition request includes a target business number and an acquisition quantity; the target business sequence number rule is queried according to the target business number, and the maximum value of the number segment is acquired; the sequence number of the business sequence numbers to be generated is determined based on the acquisition quantity, and the first business sequence number set of the sequence number is generated based on the maximum value of the number segment and the target business sequence number rule. By adopting this method, the sequence number of the business sequence numbers to be generated is determined based on the acquisition quantity in the target business sequence number acquisition request, and the first business sequence number set of the sequence number is generated based on the maximum value of the number segment and the target business sequence number rule. Multiple target business sequence numbers can be generated in batches, which reduces the number of requests sent to the database, thereby reducing the performance requirements and database load on the database. At the same time, the efficiency of sequence number generation is improved. BRIEF DESCRIPTION OF THE DRAWINGS
[0048] In order to more clearly illustrate the technical solutions in the embodiments of the present application or related technologies, the following briefly introduces the drawings required for use in the embodiments of the present application or related technical descriptions. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other related drawings can be obtained based on these drawings without paying any creative work.
[0049] Figure 1 This is an application environment diagram of a sequence number generation method in one embodiment;
[0050] Figure 2 Schematic diagram of a flow chart of a method for generating a sequence number in one embodiment;
[0051] Figure 3 Schematic diagram of a process for receiving target service sequence number rules in one embodiment;
[0052] Figure 4 A flowchart of determining whether a target service sequence number acquisition request satisfies sequence number generation conditions in one embodiment;
[0053] Figure 5 A schematic diagram of a process for querying target service sequence number rules and obtaining the maximum value of a number segment in one embodiment;
[0054] Figure 6 A schematic diagram of a process for obtaining the maximum value of a number segment in one embodiment;
[0055] Figure 7 A schematic diagram of a process for generating a first service sequence number set in one embodiment;
[0056] Figure 8 1. A schematic diagram of a process for updating the maximum value of a number segment in one embodiment;
[0057] Figure 9 Schematic diagram of the architecture of a method for generating a sequence number in one embodiment;
[0058] Figure 10 is a structural block diagram of a sequence number generating device in one embodiment;
[0059] Figure 11 FIG. 1 is a diagram showing the internal structure of a computer device in one embodiment. DETAILED DESCRIPTION
[0060] In order to make the purpose, technical solutions and advantages of this application more clear, the following further describes this application in detail with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain this application and are not intended to limit this application.
[0061] The serial number generation method provided in the embodiment of the present application can be applied to Figure 1In the distributed system 100 shown. The distributed system 100 includes a management terminal 110, multiple service terminals 120, a business terminal 130 and a database 140. The management terminal 110 is used to generate a target sequence number rule configuration module. The service terminal 120 batch generates business sequence numbers for target businesses according to the target business sequence number rules. The business terminal 130 is used to execute the target business according to the business sequence number. The management terminal 110 can be, but is not limited to, various personal computers, laptops, smart phones, and tablet computers. The server terminal 120 or the business terminal 130 can be an independent physical server, a server cluster or distributed system composed of multiple physical servers, or a cloud server that provides cloud computing services.
[0062] In an exemplary embodiment, Figure 2 As shown, a method for generating a sequence number is provided, which is applied to Figure 1 The server 120 (hereinafter referred to as the server) in FIG. 1 is used as an example to illustrate the process, which includes the following steps 202 to 206. In which:
[0063] Step 202: Receive a target service sequence number acquisition request sent by the service end.
[0064] The target service serial number acquisition request includes the target service serial number and the acquisition quantity. The target service serial number is an identifier of the target service serial number rule, which is used to identify the target service serial number rule and the target service serial number to be generated.
[0065] In practice, when the target service number is insufficient on the service side, the service side constructs a target service number acquisition request based on the target service number and acquisition quantity, and sends the request to the server. The server receives the request, parses it, and obtains the target service number and acquisition quantity.
[0066] Specifically, the service end is pre-set with a target acquisition quantity. While executing the target service based on the target service sequence number, the service end simultaneously monitors the number of target service sequence numbers in the service end. When the number of sequence numbers meets the acquisition condition, the service end constructs a target service sequence number acquisition request for the target service sequence number based on the target service number and the acquisition quantity. The service end then sends the target service sequence number acquisition request to the server end. The server end receives the target service sequence number acquisition request, parses the request, and obtains the target service number and the acquisition quantity.
[0067] In one exemplary embodiment, the service end presets a retrieval quantity of 100 and a target service number of ORD0100. While executing the target service based on the target service number, the service end simultaneously monitors the number of target service numbers. When the number of numbers falls below a preset alarm threshold, the service end constructs an HTTP (Hypertext Transfer Protocol) request to retrieve the target service number based on the retrieval quantity of 100 and the target service number ORD0100. The server receives the request over the network and parses it to obtain the retrieval quantity of 100 and the target service number ORD0100.
[0068] Optionally, the number of acquisitions is determined based on the execution frequency of the target business, and the embodiment of the present application does not limit the number of acquisitions.
[0069] Step 204: query the target service sequence number rule according to the target service number, and obtain the maximum value of the number segment.
[0070] In implementation, the server queries the target service sequence number queue based on the target service number. Then, based on the target service sequence number queue and the number of requests, the server determines whether the target service sequence number request meets the preset sequence number generation conditions. If the target service sequence number request meets the sequence number generation conditions, the server searches the various service sequence number rules for the target service number. The server then sends a status request to the database and obtains the maximum value of the number segment based on the status request.
[0071] Specifically, the server queries the target service sequence number queue based on the target service number. The server then determines the number of sequence numbers in the target service sequence number queue as the queue sequence number. Based on the queue sequence number and the acquired sequence number, the server determines whether the target service sequence number request meets the preset sequence number generation conditions. If the target service sequence number request meets the sequence number generation conditions, the server determines the target service sequence number rule from the various service sequence number rules based on the target service number. The server then sends a status acquisition request to the database to obtain the process lock status and, based on the process lock status, obtains the maximum value of the number segment.
[0072] Step 206: Determine the number of service serial numbers to be generated based on the acquired quantity, and generate a first service serial number set of the number of serial numbers based on the maximum value of the number segment and the target service serial number rule.
[0073] In implementation, the server determines the number of service numbers to be generated based on the acquired quantity. The server then determines the starting target service number based on the maximum value of the number segment and generates the target service numbers for that number based on the target service number rule and the starting target service number. The server then constructs a first service number set based on each target service number.
[0074] Specifically, the server determines the acquired quantity as the number of service sequence numbers to be generated. Then, the server determines the starting target service sequence number based on the maximum value of the number segment, and generates the target service sequence number according to the target service sequence number rule and the starting target service sequence number. The server constructs a first service sequence number set based on each target service sequence number. If the target service requires the target service sequence number to be continuous, the server updates the target service sequence number queue based on the first service sequence number set. Then, the server determines the acquired quantity of target service sequence numbers from the updated target service sequence number queue, and constructs a second service sequence number set based on the acquired quantity of target service sequence numbers. Then, the server sends the second service sequence number set to the service end via a network connection. If the target service requires the target service sequence number to be discontinuous, the server directly sends the first service sequence number set to the service end via a network connection. In the case where the target service sequence number is continuous, for example, only one server can execute the target service. In the case where the target service sequence number is not continuous, for example, only multiple servers can execute the target service.
[0075] In an optional embodiment, the server performs a difference operation between the acquired number and the number of queue sequence numbers to obtain the number of service sequence numbers to be generated. The server determines the starting target service sequence number based on the maximum value of the number segment and generates a target service sequence number based on the target service sequence number rule and the starting target service sequence number. The server constructs a first service sequence number set based on each target service sequence number. The server then directly sends the first service sequence number set to the service client via a network connection.
[0076] In the above sequence number generation method, the number of service sequence numbers to be generated is determined based on the acquisition quantity in the target service sequence number acquisition request, and a first service sequence number set is generated based on the maximum value of the number segment and the target service sequence number rule. This method can generate multiple target service sequence numbers in batches, reducing the number of requests sent to the database, thereby reducing the performance requirements and database load. At the same time, it improves the efficiency of sequence number generation.
[0077] In an exemplary embodiment, before querying the target service sequence number rule, it is necessary to first obtain the target service sequence number rule from the management end. Figure 3 As shown, before step 202 is executed, the specific processing process of the serial number generation method further includes steps 302 to 304. Among them:
[0078] Step 302: Receive the target service sequence number rule of the target service sequence number sent by the management terminal.
[0079] In implementation, the management terminal sends the target service sequence number rule of the target service sequence number to the server terminal through a network connection. The server terminal receives the target service sequence number rule of the target service sequence number sent by the management terminal and generates the target service sequence number according to the target service sequence number rule.
[0080] Specifically, when the management end completes constructing the target service sequence number rule of the target service sequence number, the management end sends the target service sequence number rule to the server end through the network connection. The server end receives the target service sequence number rule and saves the target service sequence number rule.
[0081] In an exemplary embodiment, the target service sequence number is an order service sequence number. The target service sequence number rule is an order service sequence number rule. When the management terminal completes constructing the order service sequence number rule for the order service sequence number, the management terminal sends the order service sequence number rule to the server terminal via a network connection. The server terminal receives the order service sequence number rule and saves it.
[0082] Step 304: Establish a target service sequence number queue corresponding to the target service sequence number based on the target service sequence number rule.
[0083] The target service sequence number queue is used to store the target service sequence number. The target service sequence number rule includes the target service number and the pre-generated quantity.
[0084] During implementation, the server establishes a target business sequence number queue corresponding to the target business sequence number based on the target business number and the pre-generated quantity.
[0085] Specifically, the server uses the pre-generated quantity as a reference to establish a target service number queue that can store the pre-generated number of target service numbers. Then, the server establishes an association between the target service number and the target service queue, making it easier to query the target service number queue based on the target service number.
[0086] In an exemplary embodiment, the target business number is ORD0100, the target business sequence number is the order business sequence number, and the pre-generated quantity is 100. The server uses the pre-generated quantity as a reference to establish an order business sequence number queue capable of storing 100 order business sequence numbers. Then, the server establishes an association between ORD0100 and the order business sequence number queue.
[0087] Optionally, the pre-generated quantity is generally the same as the acquired quantity at the business end, and is set according to the execution frequency of the target business. The embodiment of the present application does not limit the pre-generated quantity.
[0088] In this embodiment, by receiving the target service sequence number rule, it is convenient to generate the target service sequence number according to the target service sequence number rule. Then, by establishing a target service sequence number queue to store the target service sequence number, the target service sequence number can be sent to the service end immediately, improving the efficiency of sequence number acquisition.
[0089] In an exemplary embodiment, after receiving the target service sequence number acquisition request, it is necessary to determine whether the target service sequence number acquisition request meets the sequence number generation condition. Figure 4 As shown, after step 202 is executed, the specific processing process of the serial number generation method further includes steps 402 to 406. Among them:
[0090] Step 402: query the target service sequence number queue according to the target service number, and determine the number of target service sequence numbers in the target service sequence number queue as the queue sequence number number.
[0091] In implementation, the server queries the target service sequence number queue corresponding to the target service number, and then counts the target service sequence numbers in the target service sequence number queue to obtain the number of queue sequence numbers.
[0092] Specifically, the server searches each service sequence number queue for a target service sequence number queue associated with the target service number. The server then counts the target service sequence numbers in the target service sequence number queue to obtain the total number of target service sequence numbers in the target service sequence number queue, and determines this number as the queue sequence number number.
[0093] In an exemplary embodiment, the target business number is ORD0100. The server searches each business number queue for the order business number queue associated with ORD0100. The server then counts the order business numbers in the order business number queue, obtaining a total of 50 order business numbers in the order business number queue, and determines 50 as the queue number.
[0094] Step 404: Determine whether the number of queue sequence numbers is less than the acquired number.
[0095] During implementation, the server determines whether the number of queued sequence numbers is less than the number obtained. If so, this indicates that there are not enough target service sequence numbers in the target service sequence number queue to send to the client, and a target service sequence number needs to be generated. If the number of queued sequence numbers is greater than or equal to the number obtained, this indicates that there are enough target service sequence numbers in the target service sequence number queue to send to the client.
[0096] In an optional embodiment, if the number of queue sequence numbers is greater than or equal to the acquired number, the server determines the acquired number of target service sequence numbers in the target service sequence number queue and constructs a third service sequence number set based on the acquired number of target service sequence numbers. The server then sends the third service sequence number set to the service client via a network connection.
[0097] Step 406: If the number of queue sequence numbers is less than the acquisition number, it is determined that the target service sequence number acquisition request meets the preset sequence number generation condition.
[0098] The sequence number generation condition is that the number of queue sequence numbers is less than the number of acquisitions.
[0099] In implementation, if the number of queue sequence numbers is less than the acquisition number, the server determines that the target service sequence number acquisition request meets the sequence number generation condition and executes the following step 502.
[0100] Specifically, the server pre-sets a sequence number generation condition. If the number of queue sequence numbers is less than the acquisition number, the server determines to trigger the sequence number generation process and determines whether the target service sequence number acquisition request meets the preset sequence number generation condition.
[0101] In this embodiment, by judging whether the number of queue sequence numbers is less than the acquired number, it is determined whether the current service end has sufficient number of service sequence numbers to send to the service end, and when the number of queue sequence numbers is less than the acquired number, the sequence number generation process is triggered.
[0102] In an exemplary embodiment, Figure 5 As shown, the specific processing process of step 204 also includes steps 502 to 506. Among them:
[0103] Step 502: When the target service sequence number acquisition request satisfies the sequence number generation condition, the target service sequence number rule is searched according to the target service number.
[0104] In implementation, when the target business sequence number acquisition request meets the sequence number generation conditions, the server searches for the target business sequence number rule corresponding to the target business number in each business sequence number rule.
[0105] Specifically, when the target service sequence number acquisition request meets the sequence number generation condition, the server searches for the target service sequence number rule associated with the target service number in each service sequence number rule.
[0106] In an exemplary embodiment, the target business number is ORD0100. When the target business serial number acquisition request meets the serial number generation conditions, the server triggers the serial number generation process. The server searches for the order business serial number rule associated with ORD0100 in each business serial number rule.
[0107] Step 504: Send a status acquisition request to the database.
[0108] The status acquisition request is used to query the process lock status of the maximum value of the segment.
[0109] During implementation, the server sends a status request to the database to obtain the process lock status of the maximum number segment in the database. If the maximum number segment in the database is in the locked state, the database returns the process lock status of the maximum number segment as locked to the server. If the maximum number segment in the database is in the unlocked state, the database returns the process lock status of the maximum number segment as unlocked to the server.
[0110] Specifically, there are multiple servers and a database in a distributed system. While one server sends a status acquisition request to the database, another server is generating business serial numbers in batches based on the maximum value of the segment. At this time, the process lock status of the maximum value of the segment in the database is in a locked state. The database returns the process lock status of the maximum value of the segment to the server as a locked state. If, at the same time as a server sends a status acquisition request to the database, there is no server generating business serial numbers in the distributed system, the maximum value of the segment is in an unlocked state. The database returns the process lock status of the maximum value of the segment to the server as an unlocked state. Among them, the process lock here is an exclusive lock, that is, only one server is allowed to read and use the maximum value of the segment at the same time.
[0111] Step 506: Receive the process lock status of the maximum value of the segment returned by the database, and obtain the maximum value of the segment based on the process lock status.
[0112] In implementation, the server receives the process lock status of the maximum value of the segment returned by the database. Then, the server obtains the maximum value of the segment from the database based on the process lock status.
[0113] Specifically, the server receives the process lock status of the maximum value of the segment from the database via a network connection. The process lock status ensures whether the maximum value of the segment is locked by the process lock. If the maximum value of the segment is locked by the process lock, the process lock status is locked. At this time, the server cannot read the maximum value of the segment from the database. The server executes the step of sending a status acquisition request to the database according to a preset time period until the process lock status is unlocked. If the maximum value of the segment is not locked by the process lock, the process lock status is unlocked. The server directly reads the maximum value of the segment from the database and locks the maximum value of the segment.
[0114] In this embodiment, by obtaining the process lock status, it is determined whether another server in the distributed system is generating service sequence numbers. Furthermore, when the process lock status is unlocked, the maximum value of the number segment is obtained. This ensures that the server is the only device generating the target service sequence number based on the maximum value of the number segment, avoiding service sequence number conflicts and ensuring the uniqueness of the service sequence number.
[0115] In an exemplary embodiment, Figure 6As shown, the specific processing process of obtaining the maximum value of the number segment based on the process lock state in step 506 also includes steps 602 to 604. Among them:
[0116] Step 602: If the process lock state is the locked state, the step of sending a state acquisition request to the database is executed according to a preset time period until the process lock state is the unlocked state.
[0117] In practice, a time period is pre-set on the server. If the process lock state is locked, the maximum value of the number segment is locked by the process lock and cannot be read by the server. Therefore, the server executes the step of sending a status acquisition request to the database at intervals corresponding to the preset time period until the process lock state is unlocked. Sending the status acquisition request to the database is step 504. The specific processing process of step 504 has been described in detail in the above embodiment and will not be repeated here in this embodiment.
[0118] Step 604: If the process lock state is unlocked, obtain the maximum value of the number segment and perform locking processing on the maximum value of the number segment.
[0119] In practice, if the process lock state is unlocked, the server directly obtains the maximum value of the segment from the database. Then, the server locks the maximum value of the segment according to the process lock, making the process lock state of the maximum value of the segment locked.
[0120] Specifically, if the process lock is unlocked, the server can use the maximum value of the segment. Therefore, the server reads the maximum value of the segment from the database. The server then locks the maximum value of the segment based on the process lock, changing the process lock state of the maximum value of the segment to locked. This prevents other servers from reading the maximum value of the segment to generate service numbers, which could cause confusion in the service numbers.
[0121] In this embodiment, when the process lock state is unlocked, the maximum sequence number is obtained and locked, which prevents other servers from obtaining the maximum sequence number and ensures the uniqueness of the target business sequence number.
[0122] In an exemplary embodiment, Figure 7 As shown, the specific processing process of generating the first service sequence number set based on the maximum value of the number segment and the target service sequence number rule in step 206 includes steps 702 to 706. Among them:
[0123] Step 702: Determine the starting target service sequence number based on the maximum value of the number segment and the step size in the target service sequence number rule.
[0124] The step size is the interval between two target service numbers.
[0125] In practice, the server determines the step size in the target service sequence number rule and adds the maximum value of the segment and the step size to obtain the starting target service sequence number.
[0126] In an exemplary embodiment, the step size is 1, and the maximum value of the number segment is 1000. The server performs an addition operation on the maximum value of the number segment 1000 and the step size to obtain the starting target service sequence number 10001.
[0127] Step 704 , starting with the initial target service sequence number, generates a target service sequence number based on the target service sequence number rule.
[0128] During implementation, the server starts with the starting target service sequence number and generates target service sequence numbers in sequence according to the target service sequence number rules.
[0129] Specifically, the target service serial number rule includes a string module, a date module, and a numeric module. The numeric module is used to generate the numeric component of the service serial number. The server begins with the starting target service serial number and increments it according to the step size specified in the target service serial number rule to obtain the numeric component of the serial number. The server then generates the date component based on the date module and the string component based on the string module. The server then concatenates the string component, the date component, and each numeric component to obtain the target service serial number.
[0130] In an exemplary embodiment, the target business sequence number rule is an order business sequence number rule. The order business sequence number includes a string module, a date module, and a numeric module. The starting target business sequence number is 1001, with a step size of 1. The sequence number is 100. The numeric module is used to generate the numeric component of the business sequence number. Starting with 1001, the server increments 1001 according to the step size of 1 in the target business sequence number rule to obtain the numeric component of the sequence number. The numeric components of the sequence number are a set of 1001 to 1100. Then, the server generates a date component of 20240814 based on the date module and a string component of ORD based on the string module. The server concatenates the string component, the date component, and each numeric component to obtain the target business sequence number. The target business sequence number is a set of ORD202408141001 to ORD202408141100.
[0131] Step 706: Construct a first service sequence number set according to each target service sequence number.
[0132] During implementation, the server combines the target service sequence numbers into a first service sequence number set according to the size order of the target service sequence numbers.
[0133] In this embodiment, batch generation of service sequence numbers is accomplished by determining the maximum value of the number segment and the number of target service sequence numbers according to the target service sequence number rules. This reduces the number of database accesses, database requirements, and database load. Furthermore, by generating the target service sequence numbers in the server's memory, reliance on the database is reduced. Furthermore, by adjusting the number of sequence numbers to accommodate different service requirements, the sequence number generation method exhibits excellent scalability.
[0134] In an exemplary embodiment, after the first service sequence number set is generated, it is also necessary to unlock the maximum value of the number segment in the database and update the maximum value of the number segment. Figure 8 As shown, after step 206 is executed, the specific processing process of the serial number generation method further includes steps 802 to 804. Among them:
[0135] Step 802: Determine the largest target service sequence number in the first service sequence number set as the maximum value of the target number segment, and unlock the maximum value of the number segment.
[0136] In implementation, the server determines the target service sequence number with the largest value in the first service sequence number set as the target segment maximum value. Then, the server unlocks the segment maximum value based on the process lock.
[0137] Specifically, the server determines the target service sequence number with the largest value in the first service sequence number set and determines the numerical component of the target service sequence number. The server then determines the numerical component of the target service sequence number with the largest value as the target number segment maximum value. The server unlocks the maximum number segment value in the database based on the process lock to obtain the unlocked maximum number segment value.
[0138] Step 804: Update the maximum value of the unlocked number segment according to the maximum value of the target number segment.
[0139] During implementation, the server determines the maximum value of the target number segment as the new maximum value of the number segment to obtain the updated maximum value of the number segment.
[0140] Specifically, the server constructs a segment maximum value update request based on the target segment maximum value. The server then sends the segment maximum value update request to the database. The database receives the segment maximum value update request and, based on the segment maximum value update request, determines the target segment maximum value as the new segment maximum value.
[0141] In an exemplary embodiment, the target maximum value of the number segment is 1100. The server constructs a number segment maximum value update request based on 1100. The server then sends the number segment maximum value update request to the database. The database receives the number segment maximum value update request and, based on the number segment maximum value update request, determines the target maximum value of 1100 as the new number segment maximum value.
[0142] In this embodiment, by unlocking the maximum value of the sequence number and updating the maximum value of the number segment, the server can subsequently generate service sequence numbers based on the updated maximum value of the number segment, ensuring the uniqueness of the service sequence number. Furthermore, the database only needs to store the maximum value of the number segment, reducing database resource usage. Furthermore, the implementation of this sequence number generation method is relatively simple, requiring only the design of the database table structure for storing the number segments and the corresponding application and update logic.
[0143] In an exemplary embodiment, Figure 9 FIG. 1 is a schematic diagram of the architecture of a method for generating a sequence number in an embodiment. Figure 9 As shown, the distributed system 100 includes a control terminal 110, a server (serial number server) 120, a business terminal (application terminal) 130 and a database 140. The control terminal 110 is responsible for setting the serial number rules, supporting the combination of multiple serial number rules, and can realize personalized configuration of serial number generation strategies. The configured business serial number rules are saved in the database and the server 120. The server 120 is responsible for generating serial numbers according to the business serial number rules set by the control terminal, and generates a certain number of business serial numbers each time. Each time a batch of serial numbers is generated, the database 140 is updated only once (the maximum value of the number segment of this batch of serial numbers is updated). The business terminal 130 connects to the server 120 to obtain the business serial numbers for use. Each time the obtained number of business serial numbers is obtained, the business serial numbers are cached locally. The next time it is used, it is directly obtained from the cache. When the serial numbers are exhausted, go to the server 120 to obtain a batch. The business terminal 130 saves the cache type and data structure of the business serial numbers. For example, a multi-queue method or a data table method is used to save the business serial numbers. According to Figure 9 The complete process of generating business sequence numbers in the distributed system shown is:
[0144] 1. The management terminal 110 determines each target component factor from each component factor according to the characteristic information of the target service.
[0145] 2. The management terminal 110 constructs a target service sequence number rule based on each target component factor, saves the target service sequence number rule to the database, and notifies the server 120.
[0146] 3. The service end 130 monitors the service sequence number status of each queue or table in the cache to determine whether a service sequence number needs to be generated for the current number segment. If it is detected that the sequence number needs to be supplemented, it sends a target service sequence number acquisition request to the server end 120 based on its own service sequence number and acquisition quantity to obtain the target service sequence number.
[0147] 4. The server 120 queries the target business sequence number rule according to the business sequence number.
[0148] 5. Server 120 obtains the maximum value of the number segment and determines the number of service sequence numbers to be generated based on the obtained value. Server 120 then generates the target service sequence number based on the maximum value of the number segment and the target service sequence number rule. The database's distributed locking capabilities are used to obtain the maximum value of the number segment. Multiple threads or processes simultaneously acquire locks when acquiring sequence numbers to ensure that each number segment can only be acquired by one thread.
[0149] 6. The server 120 sends a batch of generated target service serial numbers to the service terminal 130.
[0150] It should be understood that, although the various steps in the flowcharts involved in the various embodiments described above are displayed in sequence according to the instructions of the arrows, these steps are not necessarily executed in sequence in the order indicated by the arrows. Unless otherwise specified herein, there is no strict order restriction on the execution of these steps, and these steps can be executed in other orders. Moreover, at least a portion of the steps in the flowcharts involved in the various embodiments described above can include multiple steps or multiple stages, and these steps or stages are not necessarily executed and completed at the same time, but can be executed at different times, and the execution order of these steps or stages is not necessarily to be carried out in sequence, but can be executed in turn or alternately with other steps or at least a portion of steps or stages in other steps.
[0151] Based on the same inventive concept, embodiments of the present application also provide a sequence number generation device for implementing the aforementioned sequence number generation method. The solution provided by this device is similar to the solution described in the aforementioned method. Therefore, the specific limitations of one or more sequence number generation device embodiments provided below can be found in the aforementioned limitations of the sequence number generation method and will not be further elaborated here.
[0152] In an exemplary embodiment, Figure 10 As shown, a sequence number generating device 1000 is provided, comprising: a receiving module 1001, an obtaining module 1002 and a generating module 1003, wherein:
[0153] The receiving module 1001 is used to receive a target service serial number acquisition request sent by a service end; the target service serial number acquisition request includes a target service number and an acquisition quantity.
[0154] The acquisition module 1002 is used to query the target service sequence number rule according to the target service number and obtain the maximum value of the number segment.
[0155] The generating module 1003 is used to determine the number of service sequence numbers to be generated based on the acquired quantity, and to generate a first service sequence number set of the number of sequence numbers based on the maximum value of the number segment and the target service sequence number rule.
[0156] In an exemplary embodiment, the serial number generation module 1000 further includes:
[0157] The second receiving module is used to receive the target service sequence number rule of the target service sequence number sent by the management terminal.
[0158] An establishment module is used to establish a target business sequence number queue corresponding to the target business sequence number based on the target business sequence number rule; the target business sequence number queue is used to store the target business sequence number.
[0159] In an exemplary embodiment, the serial number generation module 1000 further includes:
[0160] The query module is used to query the target business sequence number queue according to the target business number, and determine the number of target business sequence numbers in the target business sequence number queue as the queue sequence number number.
[0161] The judgment module is used to judge whether the number of queue sequence numbers is less than the acquisition number.
[0162] The first determining module is configured to determine whether the target service sequence number acquisition request satisfies a preset sequence number generation condition if the number of queue sequence numbers is less than the acquisition number.
[0163] In an exemplary embodiment, the acquisition module 1002 includes:
[0164] The first query submodule is used to query the target business sequence number rule according to the target business number when the target business sequence number acquisition request meets the sequence number generation condition.
[0165] The first sending submodule is used to send a status acquisition request to the database; the status acquisition request is used to query the process lock status of the maximum value of the number segment.
[0166] The first receiving submodule is used to receive the process lock status of the maximum value of the number segment returned by the database, and obtain the maximum value of the number segment based on the process lock status.
[0167] In an exemplary embodiment, the first receiving submodule includes a second receiving submodule and a first acquiring submodule. The first acquiring submodule includes:
[0168] The second sending submodule is configured to, if the process lock state is in the locked state, execute the step of sending a status acquisition request to the database according to a preset time period until the process lock state is in the unlocked state.
[0169] The second acquisition submodule is used to obtain the maximum value of the number segment if the process lock state is unlocked, and lock the maximum value of the number segment.
[0170] In an exemplary embodiment, the generation module 1003 includes a first determination submodule and a first generation submodule. The first generation submodule includes:
[0171] The second determining submodule is used to determine the starting target service sequence number based on the maximum value of the number segment and the step size in the target service sequence number rule.
[0172] The second generating submodule is configured to start with the initial target service sequence number and generate a target service sequence number based on a target service sequence number rule.
[0173] The first construction submodule is used to construct a first service sequence number set according to each target service sequence number.
[0174] In an exemplary embodiment, the serial number generation module 1000 further includes:
[0175] The unlocking module is used to determine the largest target service sequence number in the first service sequence number set as the maximum value of the target number segment, and to unlock the maximum value of the number segment.
[0176] The update module is used to update the maximum value of the unlocked number segment according to the maximum value of the target number segment.
[0177] Each module in the above-mentioned serial number generation device can be implemented in whole or in part by software, hardware, or a combination thereof. Each module can be embedded in or independent of a processor in a computer device in the form of hardware, or can be stored in a memory in the computer device in the form of software, so that the processor can call and execute the corresponding operations of each module.
[0178] In an exemplary embodiment, a computer device is provided. The computer device may be a server, and its internal structure diagram may be as shown in FIG. Figure 11As shown. The computer device includes a processor, a memory, an input / output interface (Input / Output, abbreviated as I / O) and a communication interface. The processor, memory and input / output interface are connected through a system bus, and the communication interface is connected to the system bus through the input / output interface. The processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system, a computer program and a database. The internal memory provides an environment for the operation of the operating system and computer program in the non-volatile storage medium. The database of the computer device is used to store data used in the serial number generation method. The input / output interface of the computer device is used to exchange information between the processor and an external device. The communication interface of the computer device is used to communicate with an external terminal through a network connection. When the computer program is executed by the processor, a serial number generation method is implemented.
[0179] Those skilled in the art will understand that Figure 11 The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than shown in the figure, or combine certain components, or have a different component arrangement.
[0180] In one embodiment, a computer device is further provided, including a memory and a processor. The memory stores a computer program, and the processor implements the steps in the above method embodiments when executing the computer program.
[0181] In one embodiment, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the steps in the above-mentioned method embodiments are implemented.
[0182] In one embodiment, a computer program product is provided, including a computer program, which implements the steps in the above method embodiments when executed by a processor.
[0183] Those skilled in the art will appreciate that all or part of the processes in the above-mentioned embodiment methods can be implemented by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to memory, database or other media used in the embodiments provided in this application may include at least one of non-volatile memory and volatile memory. Non-volatile memory may include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetic random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory may include random access memory (RAM) or external cache memory, etc. By way of illustration and not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM). The database involved in the various embodiments provided herein may include at least one of a relational database and a non-relational database. Non-relational databases may include, but are not limited to, distributed databases based on blockchains. The processor involved in the various embodiments provided herein may be, but are not limited to, a general-purpose processor, a central processing unit (CPU), a graphics processing unit (GPU), a digital signal processor (DSP), a programmable logic unit (PLC), a data processing logic unit based on quantum computing, an artificial intelligence (AI) processor, and the like.
[0184] The technical features of the above embodiments can be combined arbitrarily. In order to make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this application.
[0185] The above-described embodiments merely represent several implementation methods of the present application. While the descriptions are relatively specific and detailed, they should not be construed as limiting the scope of the present application. It should be noted that a person of ordinary skill in the art may make various modifications and improvements without departing from the spirit of the present application, and these modifications and improvements fall within the scope of protection of the present application. Therefore, the scope of protection of the present application shall be determined by the appended claims.
Claims
1. A method for generating a sequence number, characterized in that: The method comprises: Receive a target service serial number acquisition request sent by a service end; the target service serial number acquisition request includes a target service number and an acquisition quantity; Query the target service sequence number rule according to the target service number and obtain the maximum value of the number segment; The querying of the target service sequence number rule according to the target service number and obtaining the maximum value of the number segment includes: If the target service sequence number acquisition request meets the sequence number generation conditions, query the target service sequence number rules according to the target service number; Sending a status acquisition request to the database; the status acquisition request is used to query the process lock status of the maximum value of the number segment; Receive the process lock status of the maximum value of the number segment returned by the database, and obtain the maximum value of the number segment based on the process lock status; Determining the number of service sequence numbers to be generated based on the acquired quantity, and generating a first service sequence number set of the number of sequence numbers based on the maximum value of the number segment and the target service sequence number rule; If the target service sequence numbers corresponding to the target service are continuous, updating the target service sequence number queue according to the first service sequence number set; Determining the acquired number of target service sequence numbers from the updated target service sequence number queue, and constructing a second service sequence number set according to the acquired number of target service sequence numbers; Sending the second service sequence number set to the service end via the network connection; If the target service sequence numbers corresponding to the target services are not continuous, the first service sequence number set is sent to the service end via the network connection.
2. The method according to claim 1, characterized in that Before receiving the target service sequence number acquisition request sent by the service end, the method further includes: Receive the target business sequence number rule of the target business sequence number sent by the management end; A target service sequence number queue corresponding to the target service sequence number is established based on the target service sequence number rule; the target service sequence number queue is used to store the target service sequence number.
3. The method according to claim 2, characterized in that The target service sequence number rule includes a target service number and a pre-generated quantity, and establishing a target service sequence number queue corresponding to the target service sequence number based on the target service sequence number rule includes: Taking the pre-generated quantity as a reference, establishing a target service sequence number queue capable of storing the pre-generated quantity of target service sequence numbers; Establish an association relationship between the target service number and the target service sequence number queue.
4. The method according to claim 1, wherein After receiving the target service sequence number acquisition request sent by the service end, the method further includes: Querying a target service sequence number queue according to the target service number, and determining the number of target service sequence numbers in the target service sequence number queue as the number of queue sequence numbers; Determine whether the number of queue sequence numbers is less than the acquired number; If the number of queue sequence numbers is less than the acquisition number, it is determined that the target service sequence number acquisition request meets the preset sequence number generation condition.
5. The method according to claim 1, wherein The obtaining of the maximum value of the number segment based on the process lock state includes: If the process lock state is the locked state, executing the step of sending a state acquisition request to the database according to a preset time period until the process lock state is the unlocked state; If the process lock state is unlocked, the maximum value of the number segment is obtained and the maximum value of the number segment is locked.
6. The method according to claim 1, characterized in that The generating of the first service sequence number set having the number of sequence numbers based on the maximum value of the number segment and the target service sequence number rule includes: Determine the starting target service sequence number based on the maximum value of the number segment and the step size in the target service sequence number rule; Starting with the starting target service sequence number, generating the target service sequence number of the sequence number based on the target service sequence number rule; A first service sequence number set is constructed according to each of the target service sequence numbers.
7. The method according to claim 1, characterized in that After generating the first service sequence number set with the number of sequence numbers based on the maximum value of the number segment and the target service sequence number rule, the method further includes: Determining the largest target service sequence number in the first service sequence number set as the maximum value of the target number segment, and performing unlocking processing on the maximum value of the number segment; The maximum value of the unlocked number segment is updated according to the maximum value of the target number segment.
8. A serial number generating device, characterized in that: The device comprises: A receiving module, configured to receive a target service serial number acquisition request sent by a service end; the target service serial number acquisition request includes a target service number and an acquisition quantity; An acquisition module is used to query the target service sequence number rule according to the target service number and obtain the maximum value of the number segment; The acquisition module includes: A first query submodule is configured to query a target service sequence number rule according to the target service number when the target service sequence number acquisition request satisfies a sequence number generation condition; The first sending submodule is used to send a status acquisition request to the database; the status acquisition request is used to query the process lock status of the maximum value of the number segment; A first receiving submodule is configured to receive a process lock status of a maximum value of a number segment returned by a database, and obtain the maximum value of the number segment based on the process lock status; A generating module, configured to determine the number of service sequence numbers to be generated based on the acquired quantity, and generate a first service sequence number set of the number of sequence numbers based on the maximum value of the number segment and the target service sequence number rule; The device is also used to update the target business sequence number queue according to the first business sequence number set if the target business sequence number corresponding to the target business is continuous; determine the acquired number of target business sequence numbers from the updated target business sequence number queue, and construct a second business sequence number set based on the acquired number of target business sequence numbers; send the second business sequence number set to the business end through a network connection; if the target business sequence number corresponding to the target business is not continuous, send the first business sequence number set to the business end through a network connection.
9. A computer device comprising a memory and a processor, wherein the memory stores a computer program, wherein: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 7 are implemented.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 7 are implemented.
Citation Information
Patent Citations
Distributed serial number generation method and device, equipment and readable storage medium
CN114281872A
Serial number distribution method and device, computer equipment and storage medium
CN117076519A