Distributed identification generation method, device, electronic device and storage medium
By locking the target node and querying the maximum identification number and number segment number, batch identification intervals are generated, which solves the problem of inefficient identification generation in the prior art and realizes efficient batch identification generation.
Patent Information
- Application Number
- CN202110129272.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-01-29
- Publication Date
- 2025-04-15
- Estimated Expiration
- 2041-01-29
AI Technical Summary
In the prior art, the identification generation method cannot generate identification in batches, and requires multiple access to the database, resulting in inefficiency.
By obtaining the service identification generation request, locking the target node, and querying the maximum identification number and number number segment number of the previous batch, generating and storing the identification interval of the current batch, and generating the identification number in batches.
It greatly reduces the number of accesses to the database, improves the efficiency of identity generation, and can generate identity in batches.
Smart Images

Figure CN113821521B_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of computer technology, and in particular to a distributed identification generation method, device, electronic device and storage medium. Background Art
[0002] In most network services, ID (identity) is usually needed to identify various data objects, such as user accounts, online shopping order numbers, online ticket purchase order numbers, etc.
[0003] In the related art, the ID can be generated using methods such as UUID (Universally Unique Identifier), database primary key auto-increment, and Snowflake algorithm.
[0004] Among them, the purpose of UUID is to allow all elements in a distributed system to have unique identification information, but the ID it generates is unordered, containing letters and numbers, etc., without increment or trend. The ID cannot contain business identifiers and has poor readability. It generates a string, which has poor storage performance and slow query efficiency. The data is too long and is not suitable for storage, which consumes database performance and is not friendly to large distributed systems.
[0005] Based on the method of database primary key auto-increment, taking MySQL (relational database management system) as an example, when creating a new table, specify the primary key to automatically increase and generate in the form of auto_increment, or specify a growth step. This is not applicable in distributed high-concurrency systems. Distributed systems involve sharding, cross-machine and even cross-computer room deployment environments. The database auto-increment method cannot meet business needs. If the database goes down, it cannot continue to be used. At the same time, under the condition of high-concurrency and large-scale access, the database's tolerance is limited and it is difficult to support.
[0006] The ID generated by the snowflake algorithm is 64 bits long, of which the first identifier is not used and marked as 0, the next 41 bits are timestamps, which are used to store the difference in timestamps; the next 10 bits are machine codes, which can identify 1024 machine nodes. If the machine is deployed in a separate computer room (IDC), these 10 bits can be split, for example, 5 bits represent the computer room ID and 5 bits represent the machine ID, so there are 32*32=1024 combinations; the last 12 bits are random sequences, which are used to record millisecond counts. A node can generate 4096 ID numbers. However, this method is highly dependent on the machine clock. If the clock is dialed back, it will cause duplicate ID generation, the system will be abnormal, and the ID cannot be generated anymore, which may cause the system to be unusable.
[0007] In summary, the identifier generation method in the related art cannot generate identifiers in batches and needs to access the database multiple times.
[0008] It should be noted that the information disclosed in the above background technology section is only used to enhance the understanding of the background of the present disclosure, and therefore may include information that does not constitute the prior art known to ordinary technicians in the field. Summary of the invention
[0009] The purpose of the present disclosure is to provide a distributed identification generation method, device, electronic device and storage medium, which can generate identifications in batches, greatly reducing the number of times the database is accessed and improving the efficiency of identification generation.
[0010] Other features and advantages of the present disclosure will become apparent from the following detailed description, or may be learned in part by the practice of the present disclosure.
[0011] The disclosed embodiment provides a distributed identifier generation method, including: obtaining an identifier generation request, the identifier generation request including a service identifier; responding to the identifier generation request, generating a node corresponding to the identifier generation request, and locking a target node from existing nodes; obtaining an identifier generation request corresponding to the target node as a target identifier generation request, the target identifier generation request including a target service identifier; querying and obtaining the maximum identifier number of the previous batch corresponding to the target service identifier and the number of number segments of each batch; generating and storing an identifier interval of the current batch corresponding to the target service identifier based on the maximum identifier number of the previous batch corresponding to the target service identifier and the number of number segments of each batch. The identifier interval includes identifiers of the number of number segments.
[0012] In some exemplary embodiments of the present disclosure, responding to an identifier generation request, generating a node corresponding to the identifier generation request, and locking a target node from existing nodes includes: responding to the identifier generation request, sending a node generation request to a distributed application coordination service, so that the distributed application coordination service generates a corresponding node for the identifier generation request, and the distributed application coordination service locks the target node according to the number of the existing node; receiving the number of the target node returned by the distributed application coordination service, so that the distributed application coordination service deletes the target node after returning the number of the target node.
[0013] In some exemplary embodiments of the present disclosure, querying to obtain the maximum identification number of the previous batch corresponding to the target business identifier and the number of number segments for each batch includes: querying the maximum identification number of the previous batch corresponding to the target business identifier and the number of number segments for each batch from a remote dictionary service; if the maximum identification number of the previous batch corresponding to the target business identifier and the number of number segments for each batch are not found in the remote dictionary service, querying the maximum identification number of the previous batch corresponding to the target business identifier and the number of number segments for each batch from a database; and synchronizing the maximum identification number of the previous batch corresponding to the target business identifier and the number of number segments for each batch found in the database to the remote dictionary service.
[0014] In some exemplary embodiments of the present disclosure, querying and obtaining the maximum identification number of the previous batch corresponding to the target business identifier and the number of number segments for each batch also includes: querying the maximum identification number of the previous batch corresponding to the target business identifier and the number of number segments for each batch from a remote dictionary service; querying the maximum identification number of the previous batch corresponding to the target business identifier and the number of number segments for each batch from a database; if the maximum identification number of the previous batch corresponding to the target business identifier and the number of number segments for each batch queried from the remote dictionary service are inconsistent with the maximum identification number of the previous batch corresponding to the target business identifier queried in the database, then using the maximum identification number of the previous batch corresponding to the target business identifier queried in the database and the number of number segments for each batch to update the remote dictionary service.
[0015] In some exemplary embodiments of the present disclosure, the method further includes: obtaining the maximum identification number of the current batch according to the maximum identification number of the previous batch and the number of number segments of each batch, and updating the remote dictionary service and database.
[0016] In some exemplary embodiments of the present disclosure, the method further includes: receiving an identification acquisition request sent by a client; responding to the identification acquisition request, sequentially acquiring identifications from the identification interval of the current batch, and returning the identifications to the client.
[0017] In some exemplary embodiments of the present disclosure, the method further includes: if the ratio of the number of used identifiers in the identifier interval of the current batch to the number of identifiers in the identifier interval of the current batch exceeds a preset ratio, generating a new identifier generation request.
[0018] The embodiment of the present disclosure provides a distributed identifier generation device, including: a first request acquisition module, used to obtain an identifier generation request, the identifier generation request includes a service identifier; a node generation module, used to respond to the identifier generation request, generate a node corresponding to the identifier generation request, and lock the target node from the existing nodes; a second request acquisition module, used to obtain the identifier generation request corresponding to the target node as a target identifier generation request, the target identifier generation request includes a target service identifier; a data query module, used to query and obtain the maximum identifier number of the previous batch corresponding to the target service identifier and the number of number segments of each batch; an identifier interval generation module, used to generate and store the identifier interval of the current batch corresponding to the target service identifier according to the maximum identifier number of the previous batch corresponding to the target service identifier and the number of number segments of each batch, the identifier interval including identifiers of the number of number segments.
[0019] An embodiment of the present disclosure provides an electronic device, comprising: at least one processor; and a storage device for storing at least one program, wherein when the at least one program is executed by the at least one processor, the at least one processor implements any one of the above-mentioned distributed identification generation methods.
[0020] An embodiment of the present disclosure provides a computer-readable storage medium having a computer program stored thereon. When the computer program is executed by a processor, any of the above-mentioned distributed identifier generation methods is implemented.
[0021] The distributed identifier generation method provided by the embodiment of the present disclosure responds to the identifier generation request, generates a node corresponding to the identifier generation request, locks the target node from the existing nodes, and executes the identifier generation request corresponding to the target node as the target identifier generation request, thereby ensuring the uniqueness of the identifier generation; according to the maximum identifier number of the previous batch corresponding to the target business identifier and the number of number segments in each batch, a batch of identifier intervals can be generated, that is, identifiers can be generated in batches, and after receiving the user's identifier acquisition request, the identifiers can be directly acquired in sequence from the stored identifier intervals of the current batch, without having to access the database each time the identifier is acquired, which greatly reduces the number of database accesses and improves the efficiency of identifier generation.
[0022] It is to be understood that the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the present disclosure. BRIEF DESCRIPTION OF THE DRAWINGS
[0023] The accompanying drawings herein are incorporated into the specification and constitute a part of the specification, illustrate embodiments consistent with the present disclosure, and together with the specification are used to explain the principles of the present disclosure. Obviously, the accompanying drawings described below are only some embodiments of the present disclosure, and for ordinary technicians in this field, other accompanying drawings can be obtained based on these accompanying drawings without creative work.
[0024] Figure 1 A schematic diagram showing an exemplary system architecture to which the distributed identification generation method according to an embodiment of the present disclosure can be applied.
[0025] Figure 2 The figure is a flow chart of a distributed identification generation method according to an exemplary embodiment.
[0026] Figure 3 is a flowchart of another distributed identification generation method according to an exemplary embodiment.
[0027] Figure 4 is a flowchart of another distributed identification generation method according to an exemplary embodiment.
[0028] Figure 5 is a flowchart of another distributed identification generation method according to an exemplary embodiment.
[0029] Figure 6 is a flowchart of another distributed identification generation method according to an exemplary embodiment.
[0030] Figure 7 A block diagram of a distributed identifier generation device is shown according to an exemplary embodiment.
[0031] Figure 8 is a schematic structural diagram of an electronic device according to an exemplary embodiment. DETAILED DESCRIPTION
[0032] Example embodiments will now be described more fully with reference to the accompanying drawings. However, example embodiments can be implemented in a variety of forms and should not be construed as limited to the examples set forth herein; rather, these embodiments are provided so that the disclosure will be more comprehensive and complete and to fully convey the concepts of the example embodiments to those skilled in the art. The described features, structures, or characteristics may be combined in any suitable manner in one or more embodiments.
[0033] In addition, the accompanying drawings are only schematic illustrations of the present disclosure and are not necessarily drawn to scale. The same reference numerals in the figures represent the same or similar parts, and their repeated description will be omitted. Some of the block diagrams shown in the accompanying drawings are functional entities and do not necessarily correspond to physically or logically independent entities. These functional entities can be implemented in software form, or implemented in one or more hardware modules or integrated circuits, or implemented in different networks and / or processor devices and / or microcontroller devices.
[0034] Figure 1A schematic diagram showing an exemplary system architecture to which the distributed identification generation method according to an embodiment of the present disclosure can be applied.
[0035] like Figure 1 As shown, the system architecture may include business applications, identification generation services, distributed application coordination services (Zookeeper), remote dictionary services (Redis, Remote Dictionary Server) and databases (DB, DataBase). Among them, business applications and identification generation services can be configured in terminal devices or in other devices. The present disclosure takes terminal devices as an example for explanation, but the present disclosure is not limited to this.
[0036] The terminal device may be a smart phone, a tablet computer, a portable computer, a desktop computer, a smart speaker, a wearable smart device, a virtual reality device, an augmented reality device, etc., but is not limited thereto.
[0037] In the present disclosure, Figure 1 As shown, business application 1, business application 2, business application 3 and business application 4 can send identification generation requests to the identification generation service respectively. Among them, business application 2 and business application 3 can also send identification generation requests together, for example, business application 1 sends identification generation request 1 to the identification generation service, business application 2 and business application 3 send identification generation request 2 to the identification generation service, and business application 4 sends identification generation request 3 to the identification generation service.
[0038] The identity generation service can obtain each identity generation request of each business application, and each identity generation request includes a business identity corresponding to the business application; the identity generation service can respond to the identity generation request and send a node generation request corresponding to the identity generation request to the distributed application coordination service; the distributed application coordination service can generate a unique node for each identity generation request, for example, generating node 1 for identity generation request 1, generating node 2 for identity generation request 2, and generating node 3 for identity generation request 3. If an identity generation request is received later, node 4 can continue to be generated for the identity generation request; the distributed application coordination service can determine the target node from the existing nodes and lock the target node, and return the number corresponding to the target node to the identity generation service; after returning the number corresponding to the target node, the distributed application coordination service can unlock the target node, for example, delete the target node; after deleting the target node, the distributed application can re-determine a new target node from the existing nodes and can wait in line.
[0039] The identification generation service can obtain the number of the target node. According to the number of the target node, the identification generation service can use the identification generation request corresponding to the target node as the target identification generation request. The target identification generation request is the request that the identification generation service currently needs to execute, wherein the business identifier in the target identification generation request is the target business identifier.
[0040] The identification generation service can query the remote dictionary service and database to obtain the maximum identification number of the previous batch corresponding to the target business identification and the number of number segments for each batch; and return the maximum identification number of the previous batch corresponding to the target business identification and the number of number segments for each batch to the business application corresponding to the target business identification.
[0041] The service application can generate and store the identification interval of the current batch corresponding to the target service identifier according to the maximum identification number of the previous batch corresponding to the target service identifier and the number of number segments of each batch, and the identification interval includes the number of number segment identifiers.
[0042] In the disclosed embodiment, the system architecture may further include a client (not shown in the figure). The user may send an identification acquisition request through the client, and the business application may respond to the identification acquisition request, sequentially acquire identifications from the identification interval of the current batch, and return the identifications to the client.
[0043] If the ratio of the number of used identifiers in the identifier interval of the current batch to the number of identifiers in the identifier interval of the current batch exceeds a preset ratio, the business application can generate a new identifier generation request, and the business application can send a new identifier generation request to the identifier generation service to generate the next batch of identifier intervals.
[0044] It should be understood that Figure 1 The number of business applications, identification generation services, distributed application coordination services, remote dictionary services and databases is merely illustrative, and any number of business applications, identification generation services, distributed application coordination services, remote dictionary services and databases may be used according to actual needs.
[0045] It should be noted that the above application scenarios are only shown to facilitate understanding of the spirit and principle of the present disclosure, and the embodiments of the present disclosure are not limited in this respect. On the contrary, the embodiments of the present disclosure can be applied to any applicable scenario.
[0046] Below, each step of the distributed identification generation method in the exemplary embodiment of the present disclosure will be described in more detail with reference to the accompanying drawings and embodiments.
[0047] Figure 2 is a flow chart of a distributed identification generation method according to an exemplary embodiment. The method provided by the embodiment of the present disclosure can be performed as follows Figure 1The process is executed in the client shown in the figure, but the present disclosure is not limited to this.
[0048] like Figure 2 As shown, the distributed identification generation method provided by the embodiment of the present disclosure may include the following steps.
[0049] In step S202, an identifier generation request is obtained, where the identifier generation request includes a service identifier.
[0050] In the embodiment of the present disclosure, one or more business applications may generate one or more identification generation requests corresponding to the business applications, and each identification generation request includes a business identification (bessiness_flag) for characterizing the business application.
[0051] Among them, the identifier (ID) can be used to represent various data objects, such as a user's account number, an order number for online shopping, and a character number in an online game, etc. The identifier can refer to a number or a string that represents uniqueness.
[0052] The business application may be, for example, a shopping application, a ticket purchase application, etc., which is not limited in the present disclosure. For example, a shopping application may use an identifier to indicate an order number of a user's online shopping.
[0053] In step S204, in response to the identifier generation request, a node corresponding to the identifier generation request is generated, and a target node is locked from existing nodes.
[0054] In the embodiment of the present disclosure, one or more identifier generation requests may be responded to and a node corresponding to each identifier generation request may be generated.
[0055] In the embodiment of the present disclosure, the target node can be locked from the existing nodes. For example, the node with the largest number among the existing nodes can be used as the target node, or the node with the smallest number among the existing nodes can be used as the target node.
[0056] In the disclosed embodiment, the target node is locked, and one of the identifier generation requests may be locked for execution, which can ensure the uniqueness of the identifier generation in the subsequent process.
[0057] In step S206, an identifier generation request corresponding to the target node is obtained as a target identifier generation request, where the target identifier generation request includes a target service identifier.
[0058] In the disclosed embodiment, an identifier generation request corresponding to a target node can be used as a target identifier generation request from one or more identifier generation requests, and subsequently a batch of identifiers can be generated based on the target identifier generation request to ensure the uniqueness of the identifier generation.
[0059] Among them, the service identifier in the target identifier generation request can be used as the target service identifier, and the target service identifier can be used to query the maximum identification number and number segment quantity corresponding to the target service identifier.
[0060] In step S208, the maximum identification number of the previous batch corresponding to the target service identification and the number of number segments of each batch are obtained by query.
[0061] In the disclosed embodiment, the maximum identification number (max_id) of the previous batch and the number of number segments (step) of each batch corresponding to the target service identifier can be obtained from a remote dictionary service or database according to the target service identifier.
[0062] Among them, the remote dictionary service and the database can pre-store multiple service identifiers, the maximum identification number of each batch corresponding to the service identifier, and the number of number segments.
[0063] It should be noted that in the embodiments of the present disclosure, the service identifier, the maximum identification number of each batch corresponding to the service identifier, and the number of number segments may also be stored in other types of memory or database, and the present disclosure does not impose any restrictions on this.
[0064] The number of number segments in each batch can be preset according to actual conditions. For example, the number of number segments in each batch can be set to 10,000, that is, 10,000 identifiers can be generated in each batch, or the number of number segments in each batch can be set separately, which is not limited in the present disclosure.
[0065] Among them, the initial maximum identification number can be set to 0. The maximum identification number of the first batch can be obtained based on the initial maximum identification number and the number of number segments of the first batch. The maximum identification number of the second batch can be obtained based on the maximum identification number of the first batch and the number of number segments of the second batch, and so on.
[0066] In step S210, based on the maximum identification number of the previous batch corresponding to the target service identification and the number of number segments in each batch, an identification interval of the current batch corresponding to the target service identification is generated and stored, and the identification interval includes identifications of the number of number segments.
[0067] In the disclosed embodiment, an identification interval [max_id+1, max_id+step] of the current batch corresponding to the target business identifier can be generated based on the maximum identification number (max_id) of the previous batch corresponding to the target business identifier and the number of number segments (step) of each batch. The identification interval includes step identifiers, and the identification interval of the current batch can be stored in the JVM (Java Virtual Machine). After receiving the user's identification acquisition request, the identifiers can be directly acquired in sequence from the stored identification interval of the current batch, avoiding the situation where the database has to be accessed each time the identifier is acquired, thereby greatly reducing the number of database accesses.
[0068] For example, the maximum identification number of the previous batch corresponding to the target service identification is 10000, and the number of number segments in each batch is 10000. Then the identification interval of the current batch corresponding to the target service identification can be generated as [10001, 20000], which includes 10000 identifications.
[0069] The distributed identifier generation method provided by the embodiment of the present disclosure responds to the identifier generation request, generates a node corresponding to the identifier generation request, locks the target node from the existing nodes, and executes the identifier generation request corresponding to the target node as the target identifier generation request, thereby ensuring the uniqueness of the identifier generation; according to the maximum identifier number of the previous batch corresponding to the target business identifier and the number of number segments in each batch, a batch of identifier intervals can be generated, that is, identifiers can be generated in batches, and after receiving the user's identifier acquisition request, the identifiers can be directly acquired in sequence from the stored identifier intervals of the current batch, without having to access the database each time the identifier is acquired, which greatly reduces the number of database accesses and improves the efficiency of identifier generation.
[0070] Figure 3 is a flowchart of another distributed identification generation method according to an exemplary embodiment.
[0071] In the disclosed embodiment, Figure 2 The difference between the distributed identity generation method shown is that Figure 3 The distributed identifier generation method shown further provides how to respond to the identifier generation request, generate a node corresponding to the identifier generation request, and lock the target node from the existing nodes, that is, provides an embodiment of the above step S204.
[0072] like Figure 3 As shown, step S204 may include the following steps.
[0073] In step S302, in response to the identifier generation request, a node generation request is sent to the distributed application coordination service, so that the distributed application coordination service generates a corresponding node for the identifier generation request, and the distributed application coordination service locks the target node according to the number of the existing node.
[0074] In the embodiments of the present disclosure, reference Figure 1 The identity generation service can respond to the identity generation request and send a node generation request corresponding to the identity generation request to the distributed application coordination service; the distributed application coordination service can generate a unique node for each identity generation request, for example, generate node 1 for identity generation request 1, generate node 2 for identity generation request 2, and generate node 3 for identity generation request 3. If the identity generation request is received later, node 4 can continue to be generated for the identity generation request.
[0075] In the embodiments of the present disclosure, reference Figure 1 , the distributed application coordination service can determine the target node from the existing nodes and lock the target node, and return the number corresponding to the target node to the identity generation service.
[0076] In the disclosed embodiment, the distributed application coordination service may determine a target node from existing nodes and lock the target node, or it may be considered that the target node has obtained a Zookeeper lock.
[0077] For example, the distributed application coordination service can select the node with the largest number from the existing nodes as the target node and lock the target node, that is, select node 4 as the target node and lock node 4. The distributed application coordination service can return the number of node 4 to the identity generation service, and the identity generation service can execute the identity generation request corresponding to node 4.
[0078] It should be noted that in the embodiments of the present disclosure, the node with the smallest number may also be used as the target node, or the node that meets other conditions may be used as the target node, and the present disclosure does not impose any limitation on this.
[0079] In step S304, the number of the target node returned by the distributed application coordination service is received, so that the distributed application coordination service deletes the target node after returning the number of the target node.
[0080] In the disclosed embodiment, after returning the number corresponding to the target node, the distributed application coordination service can unlock the target node, for example, delete the target node; after deleting the target node, the distributed application can re-determine a new target node from the existing nodes and can wait in queue.
[0081] For example, after returning the number of node 4, the distributed application coordination service can unlock node 4, that is, it can delete node 4. After deleting node 4, the distributed application can select the node with the largest number from the existing nodes as the target node again, that is, it can select node 3 as the target node, and can wait in line. After the identification generation request corresponding to node 4 is executed, the number of node 3 can be returned to the identification generation service to execute the identification generation request corresponding to node 3.
[0082] In the disclosed embodiment, the target node can be locked through the distributed application coordination service, and the target identifier generation request corresponding to the target node can be executed subsequently to generate a batch of identifiers to ensure the uniqueness of the identifier generation.
[0083] Figure 4 is a flowchart of another distributed identification generation method according to an exemplary embodiment.
[0084] In the disclosed embodiment, Figure 2 The difference between the distributed identity generation method shown is that Figure 3 The distributed identifier generation method shown further provides how to query and obtain the maximum identifier of the previous batch and the number of number segments of each batch corresponding to the target service identifier, that is, provides an embodiment of the above step S208.
[0085] like Figure 4 As shown, step S208 may include the following steps.
[0086] In step S402, the maximum identification number of the previous batch and the number of number segments of each batch corresponding to the target service identification are queried from the remote dictionary service.
[0087] In the disclosed embodiment, the maximum identification number of the previous batch and the number of segments of each batch corresponding to the target business identifier are queried from the remote dictionary service, and the response speed is fast. If the maximum identification number of the previous batch and the number of segments of each batch corresponding to the target business identifier are not queried in the remote dictionary service, step S404 can be executed; if the maximum identification number of the previous batch and the number of segments of each batch corresponding to the target business identifier are queried in the remote dictionary service, the maximum identification number of the previous batch and the number of segments of each batch corresponding to the target business identifier can be queried from the database, and it is determined whether the maximum identification number of the previous batch and the number of segments of each batch queried from the remote dictionary service are consistent with the maximum identification number of the previous batch and the number of segments of each batch queried from the database. If they are consistent, the maximum identification number of the previous batch and the number of segments of each batch can be returned. If they are inconsistent, the maximum identification number of the previous batch and the number of segments of each batch queried from the database can be used to update the maximum identification number of the previous batch and the number of segments of each batch in the remote dictionary service.
[0088] In the embodiments of the present disclosure, reference Figure 1 There can be one or more remote dictionary services, and data consistency can be ensured between remote dictionary services through data synchronization.
[0089] In step S404, if the maximum identification number of the previous batch and the number of segments of each batch corresponding to the target service identifier are not found in the remote dictionary service, the maximum identification number of the previous batch and the number of segments of each batch corresponding to the target service identifier are queried from the database.
[0090] In the disclosed embodiment, the data stored in the database is complete and comprehensive, and the database may store an identification batch record table. If the above data is not found from the remote dictionary service, the maximum identification number of the previous batch corresponding to the target business identification and the number of number segments of each batch may be found from the database.
[0091] In the embodiments of the present disclosure, reference Figure 1 There can be one or more databases, and the master-slave synchronization between databases can ensure data consistency and avoid data loss.
[0092] In the embodiment of the present disclosure, the design of the database table can be as follows:
[0093] CREATE TABLE`xfyl_service_batch_id_record`(
[0094] `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT `primary key`,
[0095] `bessiness_flag`varchar(128)NOT NULL COMMENT`Business flag`,
[0096] `max_id`bigint(20)NOT NULL DEFULT 1COMMENT`maxid`,
[0097] `step` int(11) DEFAULT NULL COMMENT `step`,
[0098] `yn`tinyint(4)DEFAULT`1`COMMENT` valid identifier 0-invalid 1-valid`,
[0099] `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT `create time`,
[0100] `up_time` datatime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATECURRENT_TIMESTAMP COMMENT `update time`,
[0101] PRIMARY KEY(`id`)USING BTREE,
[0102] KEY`idx_xfyl_service_batch_id_record_max_id`(`max_id`)USING BTREE
[0103] )ENGINE=InnoDB AUTO_INCREMENT=21DEFAULT CHARSET=utf8ROW_FORMAT=DYNAMIC COMMENT=`Service batch ID record table`;
[0104] Among them, id is the primary key, bessiness_flag identifies the specific business application, max_id records the maximum identification number of the batch, step indicates the number of number segments in each batch, yn indicates whether the record is valid, create_time indicates the record creation time, and update_time indicates the latest time of each record update.
[0105] In step S406, the maximum identification number of the previous batch corresponding to the target service identification queried from the database and the number of number segments in each batch are synchronized to the remote dictionary service.
[0106] In the disclosed embodiment, the maximum identification number of the previous batch corresponding to the target business identification queried from the database and the number of number segments in each batch are synchronized to the remote dictionary service, which can ensure data consistency. The next time the above data is queried, it can be obtained from the remote dictionary service, which can speed up the response.
[0107] In an exemplary embodiment, the above-mentioned distributed identification generation method may further include:
[0108] In step S408, the maximum identification number of the current batch is obtained according to the maximum identification number of the previous batch and the number of segments of each batch, and the remote dictionary service and database are updated.
[0109] In the disclosed embodiment, the maximum identification number of the previous batch and the number of number segments of each batch may be added together to obtain the maximum identification number of the current batch, and the maximum identification number of the current batch may be saved to the remote dictionary service and database.
[0110] For example, the maximum identification number of the previous batch is max_id, and the number of number segments in each batch is step, then the maximum identification number of the current batch can be updated to max_id=max_id+step.
[0111] Figure 5 is a flowchart of another distributed identification generation method according to an exemplary embodiment.
[0112] In the disclosed embodiment, Figure 2 The difference between the distributed identity generation method shown is that Figure 3 The distributed identifier generation method shown further provides how to query and obtain the maximum identifier of the previous batch and the number of number segments of each batch corresponding to the target service identifier, that is, provides an embodiment of the above step S208.
[0113] like Figure 5 As shown, step S208 may include the following steps.
[0114] In step S502, the maximum identification number of the previous batch and the number of number segments of each batch corresponding to the target service identification are queried from the remote dictionary service.
[0115] Step S502 is similar to step S402, and may refer to the above step S402, which will not be described in detail in the present disclosure.
[0116] In step S504, the maximum identification number of the previous batch and the number of number segments of each batch corresponding to the target service identification are queried from the database.
[0117] In the embodiment of the present disclosure, the maximum identification number of the previous batch and the number of number segments in each batch corresponding to the target business identifier can be queried from the remote dictionary service and the database respectively, and it can be determined whether the queried data are consistent. If not, step S506 can be executed. If consistent, the queried data can be returned.
[0118] In step S506, if the maximum identification number of the previous batch corresponding to the target business identifier and the number of number segments for each batch queried from the remote dictionary service are inconsistent with the maximum identification number of the previous batch corresponding to the target business identifier queried in the database and the number of number segments for each batch, the remote dictionary service is updated using the maximum identification number of the previous batch corresponding to the target business identifier queried in the database.
[0119] In the disclosed embodiment, the remote dictionary service is updated using the maximum identification number of the previous batch and the number of number segments of each batch corresponding to the target service identification queried in the database, so as to ensure the consistency of data.
[0120] In an exemplary embodiment, the above-mentioned distributed identification generation method may further include:
[0121] In step S508, the maximum identification number of the current batch is obtained according to the maximum identification number of the previous batch and the number of segments of each batch, and the remote dictionary service and database are updated.
[0122] Step S508 is similar to step S408, and may refer to the above step S408, which will not be described in detail in this disclosure.
[0123] Figure 6 is a flowchart of another distributed identification generation method according to an exemplary embodiment.
[0124] In the embodiment of the present disclosure, Figure 2 Based on the distributed identity generation method shown, Figure 6 The distributed identification generation method shown may also include the following steps.
[0125] In step S602, an identification acquisition request sent by a client is received.
[0126] In the disclosed embodiment, a user may send an identification acquisition request through a client, and a business application may receive the identification acquisition request.
[0127] For example, when a user uses a shopping application to shop online, after the user pays, an order number needs to be generated for each order. At this time, the order number generation request can be regarded as an identifier acquisition request.
[0128] In step S604, in response to the identifier acquisition request, identifiers are acquired in sequence from the identifier interval of the current batch, and the identifiers are returned to the client.
[0129] In the disclosed embodiment, the business application may respond to the identifier acquisition request, sequentially acquire identifiers from the stored identifier intervals of the current batch, and return the identifiers to the client.
[0130] For example, if the business application has stored 10,000 identifiers, and the stored identifier interval is [10001, 20000], then the identifiers can be obtained in sequence starting from identifier 10001.
[0131] In step S606, if the ratio of the number of used identifiers in the identifier interval of the current batch to the number of identifiers in the identifier interval of the current batch exceeds a preset ratio, a new identifier generation request is generated.
[0132] In the disclosed embodiment, if the ratio of the number of used identifiers in the identifier interval of the current batch to the number of identifiers in the identifier interval of the current batch exceeds a preset ratio, the business application can generate a new identifier generation request, and the business application can send a new identifier generation request to the identifier generation service to generate the identifier interval of the next batch. The identifier generation service can respond to the identifier generation request, generate a node corresponding to the identifier generation request, and lock the target node from the existing nodes; query and obtain the maximum identifier number of the current batch and the number of number segments of each batch corresponding to the target business identifier corresponding to the target node; based on the maximum identifier number of the current batch and the number of number segments of each batch corresponding to the target business identifier, generate and store the identifier interval of the next batch corresponding to the target business identifier, and the identifier interval includes the number of number segments of the identifiers.
[0133] The preset ratio can be set as needed, for example, the preset ratio can be 80%. If the number of used identifiers in the identifier interval of the current batch accounts for more than 80% of the number of identifiers in the identifier interval of the current batch, an asynchronous request can be made to generate the identifier interval of the next batch for subsequent use.
[0134] It should be noted that the above figures are only schematic illustrations of the processes included in the method according to the exemplary embodiments of the present disclosure, and are not intended to be limiting. It is easy to understand that the processes shown in the above figures do not indicate or limit the time sequence of these processes. In addition, it is also easy to understand that these processes can be performed synchronously or asynchronously, for example, in multiple modules.
[0135] The following are embodiments of the device disclosed herein, which can be used to execute the method embodiments disclosed herein. For details not disclosed in the device embodiments disclosed herein, please refer to the method embodiments disclosed herein.
[0136] Figure 7A block diagram of a distributed identifier generation device is shown according to an exemplary embodiment.
[0137] like Figure 7 As shown, the distributed identifier generation device 700 includes: a first request acquisition module 702, a node generation module 704, a second request acquisition module 706, a data query module 708 and an identifier interval generation module 710.
[0138] Among them, the first request acquisition module 702 can be used to obtain an identification generation request, which includes a business identification; the node generation module 704 can be used to respond to the identification generation request, generate a node corresponding to the identification generation request, and lock the target node from the existing nodes; the second request acquisition module 706 can be used to obtain the identification generation request corresponding to the target node as a target identification generation request, and the target identification generation request includes a target business identification; the data query module 708 can be used to query and obtain the maximum identification number of the previous batch corresponding to the target business identification and the number of number segments for each batch; the identification interval generation module 710 can be used to generate and store the identification interval of the current batch corresponding to the target business identification based on the maximum identification number of the previous batch corresponding to the target business identification and the number of number segments for each batch, and the identification interval includes the number of number segments identifiers.
[0139] In an exemplary embodiment, the node generation module 704 may include: a request response unit, which can be used to respond to the identifier generation request and send a node generation request to the distributed application coordination service so that the distributed application coordination service generates a corresponding node for the identifier generation request, and the distributed application coordination service locks the target node according to the number of the existing node; a number receiving unit, which can be used to receive the number of the target node returned by the distributed application coordination service so that the distributed application coordination service deletes the target node after returning the number of the target node.
[0140] In an exemplary embodiment, the data query module 708 may include: a first data query unit, which can be used to query the maximum identification number of the previous batch corresponding to the target business identifier and the number of number segments for each batch from a remote dictionary service; a second data query unit, which can be used to query the maximum identification number of the previous batch corresponding to the target business identifier and the number of number segments for each batch from a database if the maximum identification number of the previous batch corresponding to the target business identifier and the number of number segments for each batch are not found in the remote dictionary service; a data synchronization unit, which can be used to synchronize the maximum identification number of the previous batch corresponding to the target business identifier and the number of number segments for each batch found in the database to the remote dictionary service.
[0141] In an exemplary embodiment, the data query module 708 may also include: a third data query unit, which can be used to query the maximum identification number of the previous batch and the number of number segments of each batch corresponding to the target business identifier from a remote dictionary service; a fourth data query unit, which can be used to query the maximum identification number of the previous batch and the number of number segments of each batch corresponding to the target business identifier from a database; a data update unit, which can be used to update the remote dictionary service using the maximum identification number of the previous batch and the number of number segments of each batch corresponding to the target business identifier queried from the remote dictionary service if the maximum identification number and the number of number segments of each batch corresponding to the target business identifier queried from the remote dictionary service are inconsistent with the maximum identification number and the number of number segments of each batch corresponding to the target business identifier queried from the database.
[0142] In an exemplary embodiment, the distributed identification generating device 700 may further include: a data updating module, which may be used to obtain the maximum identification number of the current batch according to the maximum identification number of the previous batch and the number of number segments of each batch, and update the remote dictionary service and database.
[0143] In an exemplary embodiment, the distributed identifier generation device 700 may also include: a request receiving module, which can be used to receive an identifier acquisition request sent by a client; a request response module, which can be used to respond to the identifier acquisition request, obtain identifiers in sequence from the identifier interval of the current batch, and return the identifiers to the client.
[0144] In an exemplary embodiment, the distributed identifier generation device 700 may also include: a request generation module, which can be used to generate a new identifier generation request if the ratio of the number of used identifiers in the identifier interval of the current batch to the number of identifiers in the identifier interval of the current batch exceeds a preset ratio.
[0145] It should be noted that the block diagrams shown in the above figures are functional entities and do not necessarily correspond to physically or logically independent entities. These functional entities can be implemented in software form, or in one or more hardware modules or integrated circuits, or in different networks and / or processor devices and / or microcontroller devices.
[0146] Figure 8 A schematic diagram of the structure of an electronic device according to an exemplary embodiment is shown. It should be noted that: Figure 8 The electronic device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present invention.
[0147] like Figure 8As shown, the electronic device 800 includes a central processing unit (CPU) 801, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 802 or a program loaded from a storage part 808 into a random access memory (RAM) 803. In the RAM 803, various programs and data required for the operation of the system 800 are also stored. The CPU 801, the ROM 802, and the RAM 803 are connected to each other via a bus 804. An input / output (I / O) interface 805 is also connected to the bus 804.
[0148] The following components are connected to the I / O interface 805: an input section 806 including a keyboard, a mouse, etc.; an output section 807 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker, etc.; a storage section 808 including a hard disk, etc.; and a communication section 809 including a network interface card such as a LAN card, a modem, etc. The communication section 809 performs communication processing via a network such as the Internet. A drive 810 is also connected to the I / O interface 805 as needed. A removable medium 811, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 810 as needed, so that a computer program read therefrom is installed into the storage section 808 as needed.
[0149] In particular, according to an embodiment of the present invention, the process described above with reference to the flowchart can be implemented as a computer software program. For example, an embodiment of the present invention includes a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from a network through a communication section 809, and / or installed from a removable medium 811. When the computer program is executed by a central processing unit (CPU) 801, the above-mentioned functions defined in the system of the present invention are executed.
[0150] It should be noted that the computer-readable medium shown in the present invention may be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present invention, a computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in combination with an instruction execution system, device or device. In the present invention, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, which carries a computer-readable program code. This propagated data signal may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. Computer-readable signal media may also be any computer-readable medium other than computer-readable storage media, which may send, propagate or transmit a program for use by or in conjunction with an instruction execution system, apparatus or device. The program code contained on the computer-readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wire, optical cable, RF, etc., or any suitable combination of the above.
[0151] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present invention. In this regard, each box in the flow chart or block diagram can represent a module, a program segment, or a part of a code, and the above-mentioned module, program segment, or a part of a code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram or flow chart, and the combination of the boxes in the block diagram or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0152] The units involved in the embodiments of the present invention may be implemented in software or hardware. The units described may also be provided in a processor. For example, they may be described as follows: a processor includes a sending unit, an acquiring unit, a determining unit, and a first processing unit. The names of these units do not, in some cases, limit the units themselves. For example, the sending unit may also be described as a "unit that sends a request for obtaining an image to a connected server."
[0153] As another aspect, the present invention also provides a computer-readable medium, which may be included in the device described in the above embodiment; or it may exist independently and not be assembled into the device. The above computer-readable medium carries one or more programs, and when the above one or more programs are executed by a device, the device includes: obtaining an identification generation request, the identification generation request includes a service identification; responding to the identification generation request, generating a node corresponding to the identification generation request, and locking the target node from the existing nodes; obtaining the identification generation request corresponding to the target node as a target identification generation request, the target identification generation request includes a target service identification; querying and obtaining the maximum identification number of the previous batch corresponding to the target service identification and the number of number segments of each batch; generating and storing the identification interval of the current batch corresponding to the target service identification according to the maximum identification number of the previous batch corresponding to the target service identification and the number of number segments of each batch, and the identification interval includes the number of number segments.
[0154] The exemplary embodiments of the present invention are specifically shown and described above. It should be understood that the present invention is not limited to the detailed structure, configuration or implementation method described herein; on the contrary, the present invention is intended to cover various modifications and equivalent configurations included in the spirit and scope of the appended claims.
Claims
1. A distributed identifier generation method, characterized in that: include: Obtaining an identifier generation request, wherein the identifier generation request includes a service identifier; In response to the identifier generation request, a node corresponding to the identifier generation request is generated, and a target node is locked from existing nodes; Acquire an identifier generation request corresponding to the target node as a target identifier generation request, wherein the target identifier generation request includes a target service identifier; Query and obtain the maximum identification number of the previous batch corresponding to the target service identification and the number of number segments in each batch; Generate and store an identification interval of the current batch corresponding to the target service identifier according to the maximum identification number of the previous batch corresponding to the target service identifier and the number of number segments of each batch, wherein the identification interval includes the number of identifications of the number segment; The step of responding to the identifier generation request, generating a node corresponding to the identifier generation request, and locking a target node from existing nodes includes: In response to the identifier generation request, sending a node generation request to the distributed application coordination service, so that the distributed application coordination service generates a corresponding node for the identifier generation request, and the distributed application coordination service locks the target node according to the number of the existing node; The number of the target node returned by the distributed application coordination service is received, so that the distributed application coordination service deletes the target node after returning the number of the target node.
2. The method according to claim 1, characterized in that: The query obtains the maximum identification number of the previous batch corresponding to the target service identification and the number of number segments of each batch, including: Querying the maximum identification number of the previous batch and the number of number segments of each batch corresponding to the target service identification from the remote dictionary service; If the maximum identification number of the previous batch corresponding to the target service identifier and the number of number segments of each batch are not found in the remote dictionary service, the maximum identification number of the previous batch corresponding to the target service identifier and the number of number segments of each batch are found in the database; The maximum identification number of the previous batch corresponding to the target service identification queried from the database and the number of number segments of each batch are synchronized to the remote dictionary service.
3. The method according to claim 1, characterized in that The query obtains the maximum identification number of the previous batch corresponding to the target service identification and the number of number segments of each batch, and also includes: Querying the maximum identification number of the previous batch and the number of number segments of each batch corresponding to the target service identification from the remote dictionary service; Querying the database for the maximum identification number of the previous batch corresponding to the target service identification and the number of number segments of each batch; If the maximum identification number of the previous batch corresponding to the target service identifier and the number of number segments for each batch queried from the remote dictionary service are inconsistent with the maximum identification number of the previous batch corresponding to the target service identifier and the number of number segments for each batch queried from the database, the remote dictionary service is updated using the maximum identification number of the previous batch corresponding to the target service identifier and the number of number segments for each batch queried from the database.
4. The method according to claim 2 or 3, characterized in that: Also includes: According to the maximum identification number of the previous batch and the number of number segments of each batch, the maximum identification number of the current batch is obtained, and the remote dictionary service and the database are updated.
5. The method according to claim 1, characterized in that Also includes: Receive the identification acquisition request sent by the client; In response to the identifier acquisition request, identifiers are acquired in sequence from the identifier interval of the current batch, and the identifiers are returned to the client.
6. The method according to claim 5, characterized in that Also includes: If the ratio of the number of used identifiers in the identifier interval of the current batch to the number of identifiers in the identifier interval of the current batch exceeds a preset ratio, a new identifier generation request is generated.
7. A distributed identification generation device, characterized in that: include: A first request acquisition module, used to acquire an identifier generation request, wherein the identifier generation request includes a service identifier; A node generation module, used to respond to the identification generation request, generate a node corresponding to the identification generation request, and lock the target node from the existing nodes; wherein, responding to the identification generation request, generating a node corresponding to the identification generation request, and locking the target node from the existing nodes, includes: responding to the identification generation request, sending a node generation request to a distributed application coordination service, so that the distributed application coordination service generates a node corresponding to the identification generation request, and the distributed application coordination service locks the target node according to the number of the existing node; receiving the number of the target node returned by the distributed application coordination service, so that the distributed application coordination service deletes the target node after returning the number of the target node; A second request acquisition module, configured to acquire an identifier generation request corresponding to the target node as a target identifier generation request, wherein the target identifier generation request includes a target service identifier; A data query module, used to query and obtain the maximum identification number of the previous batch corresponding to the target service identification and the number of number segments of each batch; The identification interval generation module is used to generate and store the identification interval of the current batch corresponding to the target service identifier according to the maximum identification number of the previous batch corresponding to the target service identifier and the number of number segments in each batch, and the identification interval includes the number of identifications of the number segments.
8. An electronic device, characterized in that: include: at least one processor; A storage device, used to store at least one program, when the at least one program is executed by the at least one processor, enables the at least one processor to implement the method according to any one of claims 1 to 6.
9. A computer-readable storage medium having computer-executable instructions stored thereon, characterized in that: When the executable instructions are executed by a processor, the method according to any one of claims 1 to 6 is implemented.
Citation Information
Patent Citations
Method and device for generating numbers
CN104486407A
Order identifier generation method and device, server and storage medium
CN111694841A