A distributed lock implementation method, distributed lock and system based on Nacos
By using the prefix unique identification string to generate self-increment data records in the Nacos configuration center, the problem of the need to introduce third-party tools for distributed lock implementation in distributed systems is solved, and efficient and low-cost distributed lock management is realized, ensuring that high-quality and high-quality lock management is achieved. Stability and mutual exclusion in concurrency and downtime situations.
Patent Information
- Application Number
- CN202410033985.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-01-09
- Publication Date
- 2024-12-24
- Estimated Expiration
- 2044-01-09
AI Technical Summary
The prior art requires the introduction of third-party tools to implement distributed locks in distributed systems, resulting in increased framework complexity and increased computer resource consumption, thereby increasing distributed deployment costs.
The distributed lock is implemented through the Nacos configuration center, and the self-incremental data records are generated using the prefix unique identification string, sorting and comparing, ensuring mutex, and improving the acquisition efficiency of distributed locks under high concurrency.
Reduces the cost of distributed deployment, improves the efficiency of distributed lock acquisition, ensures real-time and mutual exclusion in high concurrency situations, and avoids data loss caused by downtime through persistent storage.
Smart Images

Figure CN118170557B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of information technology, and in particular to a method for implementing a distributed lock based on Nacos, a distributed lock and a system. Background Art
[0002] Traditional centralized systems, based on the excellent performance and good stability of mainframes, deploy services on a mainframe and use the high performance of the mainframe to process or store large amounts of data.
[0003] However, the mainframes in the existing centralized system are expensive, complex to operate and maintain, and when a single point of failure occurs, the entire service will be unavailable, causing serious losses. At the same time, due to the explosive development of Internet applications, the demand for performance is getting higher and higher. Simply improving the performance of mainframes will only lead to higher and higher costs, and once a single point of failure occurs, it will cause very large losses.
[0004] Therefore, the existing technology distributes services on different computers through a distributed system, that is, a computer can be distributed arbitrarily in space. Because the services are dispersed to different computers, the high performance of a single computer is no longer required, thereby reducing high costs, operational complexity, and single point failures that cause the entire service to be unavailable.
[0005] Among them, Nacos (Naming and Configuration Service) is an open source distributed service discovery and configuration management system based on Alibaba. Its purpose is to help developers simplify the process of service registration, discovery and configuration management in distributed systems.
[0006] But at the same time, no matter what form or platform the distributed system is deployed in, it will face the situation where multiple processes or threads or computers or clients may access shared resources at the same time. If not properly controlled, it may lead to problems such as data inconsistency or competition conditions. Summary of the invention
[0007] Based on this, the purpose of the present invention is to provide a method for implementing a distributed lock based on Nacos.
[0008] A method for implementing a distributed lock based on Nacos, comprising the steps of:
[0009] S1. Receive a request to access a shared resource and generate a unique identification string with a prefix corresponding to the request;
[0010] S2. Push the unique identifier string with prefix to the Nacos configuration center;
[0011] S3, Nacos configuration center automatically generates self-incrementing data records based on the pushed unique identifier string with prefix;
[0012] S4. Sort all data records with prefixes in the Nacos configuration center to obtain the minimum data record;
[0013] S5, comparing the minimum data record with the prefix identifier to see if they are consistent, if yes, proceed to the next step, otherwise continue to compare and wait;
[0014] S6, enabling the device sending the request to access the shared resource, and performing an operation corresponding to the request on the shared resource;
[0015] S7: After performing operations on shared resources, the minimum data record of the Nacos configuration center is deleted.
[0016] Compared with the prior art, the present invention solves the problem that the implementation of distributed locks through Nacos requires the introduction of third-party tools, thereby increasing the complexity of the original framework and increasing computer resource consumption, thereby increasing the cost of distributed deployment.
[0017] Furthermore, the prefix is "lock_", and the unique identification string is a character string represented by 32 hexadecimal digits.
[0018] Among them, by setting a prefix, the distributed lock identification string can be quickly filtered and located by the prefix in the Nacos configuration center.
[0019] Furthermore, the push operation is to update the Nacos server configuration using the unique identification string with the prefix as dataId;
[0020] After the Nacos configuration center updates the configuration, it generates a data record, and the data record is provided with a self-incrementing id, and the configuration is automatically synchronized to all clients;
[0021] The sorting operation uses the prefix as a condition to query all data records with the prefix, and sorts the data records by size according to the id in the data records, so as to obtain the minimum data record with the minimum id.
[0022] Among them, the change of dataId will trigger the dynamic update and synchronization function of Nacos configuration center. Therefore, by pushing the prefixed unique identification string to Nacos configuration center in the form of dataId, its characteristics can be cleverly utilized to make full use of the resources of Nacos configuration center.
[0023] At the same time, compared with redis single-threaded request processing, Nacos multi-threaded concurrency can process requests from multiple clients at the same time, so that the calculation of the minimum data record is accurate and real-time. Therefore, the query of all data records with prefixes can also be executed concurrently, so that multiple clients can obtain the information of the minimum data record in real time, and then the time taken by the client to obtain the distributed lock under high concurrency is shorter than that of redis.
[0024] In one embodiment, before deleting the data record of the current Nacos configuration center, the steps are also included:
[0025] Compare the prefixed unique identification string in the data record of the Nacos configuration center with the prefixed unique identification string of the current access shared resource request to see if they are consistent. Only if they are consistent can the deletion operation be performed, otherwise feedback information is sent to further ensure the mutual exclusivity of the distributed lock.
[0026] A distributed lock device includes a prefix unique identification string generation unit, an identification string push unit, a Nacos configuration center, a sorting unit, a comparison unit, an access unit, and a release unit;
[0027] The prefixed unique identification string generating unit is used to receive a request for accessing a shared resource and generate a prefixed unique identification string corresponding to the request;
[0028] The identification string pushing unit pushes the unique identification string with a prefix to the Nacos configuration center;
[0029] The Nacos configuration center automatically generates a self-incrementing data record according to the pushed unique identification string with a prefix;
[0030] The sorting unit sorts all data records with prefixes in the Nacos configuration center to obtain the minimum data record;
[0031] The comparison unit compares the minimum data record with the prefix identifier to see if they are consistent, if yes, executes the next step, otherwise continues to compare and wait;
[0032] The access unit enables the device sending the request to access the shared resource and perform the operation corresponding to the request on the shared resource;
[0033] The release unit deletes the minimum data record of the Nacos configuration center after performing the operation on the shared resource.
[0034] A distributed lock system comprises a Nacos server and a distributed lock device, wherein the server is composed of multiple groups of servers and / or storage media and is provided with shared resources, wherein the shared resources are resources shared by multiple clients; and the distributed lock device is used to execute the Nacos-based distributed lock implementation method according to any one of claims 1 to 4.
[0035] For better understanding and implementation, the present invention is described in detail below with reference to the accompanying drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0036] Figure 1 This is a schematic diagram of the distributed lock system structure;
[0037] Figure 2 Figure 1 The overall structural diagram of the distributed locking device shown;
[0038] Figure 3 This is an overall flow chart of the implementation method based on Nacos distributed lock proposed in the present invention. DETAILED DESCRIPTION
[0039] In the process of using Nacos to implement distributed configuration, technicians found that in order to ensure that access to a shared resource is mutually exclusive in a distributed environment, it is necessary to introduce a distributed lock based on a third-party tool to ensure that only one process or thread or computer or client can access the shared resource at the same time. However, the third-party tool is generally a database framework or service framework, such as MySQL, Redis and other databases or ZooKeeper, Eureka and other services. Therefore, the introduction of a third-party database framework or service framework only to implement a distributed lock will increase the complexity of the original framework and increase computer resource consumption, thereby increasing the cost of distributed deployment.
[0040] The technicians designed a distributed lock based on the Nacos configuration center to achieve dynamic synchronization through long polling. Taking the configuration synchronization between the client and the server as an example, the client initiates a long polling request at regular intervals and confirms whether the server's configuration has changed through the server's dataId. If it has changed, the request is returned immediately to synchronize the client's configuration with the server, and the client and server are updated synchronously. If there is no change, it will continue to wait until the timeout returns, and then initiate a long polling request again after the return.
[0041] Therefore, after the above analysis and design, the present invention proposes an implementation method of a distributed lock based on Nacos, and forms a distributed lock device and system based on this method.
[0042] See also Figure 1 , Figure 1This is a schematic diagram of the distributed lock system structure.
[0043] The system includes a Nacos server and a distributed lock device, wherein the Nacos server is built based on Nacos and consists of multiple groups of servers and / or storage media, and is provided with shared resources, which are resources shared by multiple clients. The distributed lock device is used to receive a request from a client and generate a unique lock according to the request, ensuring that only clients with the corresponding unique lock can access the shared resources of the server at the same time.
[0044] Please also see Figure 2 and Figure 3 , Figure 2 for Figure 1 The overall structural diagram of the distributed lock device shown in FIG. Figure 3 This is an overall flow chart of the implementation method based on Nacos distributed lock proposed in the present invention.
[0045] The distributed lock device includes a prefix unique identification string generation unit 1, an identification string push unit 2, a Nacos configuration center 3, a sorting unit 4, a comparison unit 5, an access unit 6 and a release unit 7.
[0046] The prefixed unique identification string generating unit 1 is used to execute step S1: receiving a request for accessing a shared resource, and generating a prefixed unique identification string corresponding to the request.
[0047] Specifically, when a client needs to access the shared resources of the Nacos server, a unique identification string with a prefix is generated, that is, a unique lock is generated.
[0048] Since there is a lot of data on the Nacos configuration center, setting a prefix allows the Nacos configuration center to quickly filter and locate the identification string of the distributed lock through the prefix.
[0049] In this embodiment, the prefix is "lock_", the unique identification string is a string that identifies an information element in a distributed system and is unique across all computers and networks in the distributed system, and the string is represented by 32 hexadecimal digits.
[0050] The identification string pushing unit 2 is used to execute step S2: pushing the unique identification string with a prefix to the Nacos configuration center.
[0051] Specifically, the push operation is to update the configuration of the Nacos server using the unique identification string with the prefix as dataId.
[0052] Among them, the change of dataId will trigger the dynamic update and synchronization function of Nacos configuration center. Therefore, by pushing the prefixed unique identification string to Nacos configuration center in the form of dataId, its characteristics can be cleverly utilized to make full use of the resources of Nacos configuration center.
[0053] The Nacos configuration center 3 is used to execute step S3: the Nacos configuration center automatically generates a self-incrementing data record according to the pushed unique identification string with a prefix.
[0054] Specifically, based on the dynamic update feature of the Nacos configuration center, that is, after the Nacos configuration center updates the configuration, a data record will be generated, and the data record is provided with a self-incrementing id, and the configuration is automatically synchronized to all clients.
[0055] The auto-increment id is accumulated based on the data record information on the Nacos configuration center. Since Nacos processes requests in multiple threads concurrently, when there are a large number of requests at the same time and a large number of unique identifier strings with prefixes are pushed to the Nacos configuration center, it is more convenient to determine the order by auto-increment id than by time.
[0056] The sorting unit 4 is used to execute step S4: sorting all data records with prefixes in the Nacos configuration center to obtain the minimum data record.
[0057] Specifically, the prefix is "lock_", and the sorting operation uses the prefix as a condition to query all data records with the prefix, so as to quickly filter and locate the corresponding data records in numerous configuration contents, and sort by size according to the id in the filtered and located data records to obtain the minimum data record with the minimum id.
[0058] Compared with redis single-threaded request processing, Nacos multi-threaded concurrency can process requests from multiple clients at the same time, so that the calculation of the minimum data record is accurate and real-time. Therefore, querying all data records with prefixes can also be executed concurrently, so that multiple clients can obtain the information of the minimum data record in real time, and then the time taken by the client to obtain the distributed lock under high concurrency is shorter than that of redis.
[0059] The comparison unit 5 is used to execute step S5: compare the minimum data record with the prefix identifier to see if they are consistent. If yes, execute the next step; otherwise, continue to compare and wait.
[0060] Specifically, the comparison is to compare the prefixed unique identification string in the smallest data record of the Nacos configuration center with the prefixed unique identification string of the current request to see if they are consistent. If they are consistent, it means that the current client has obtained the lock and can execute the next operation, otherwise continue to queue and wait.
[0061] Among them, since the Nacos configuration center is based on database storage data records, that is, distributed locks, it has the characteristics of persistent data. Compared with Redis, which stores distributed lock data through memory cache, it is easy to cause lock data loss when a crash occurs, and then multiple requests obtain distributed locks at the same time. Based on the Nacos configuration center, it can ensure that data will not be lost even if the data record crashes, thereby ensuring that the mutual exclusivity of the lock is not affected by the crash.
[0062] The access unit 6 is used to execute step S6: enabling the device sending the request to access the shared resource and perform an operation corresponding to the request on the shared resource.
[0063] Among them, when the client that sent the request obtained the distributed lock and executed the operation corresponding to the request on the shared resource, a downtime event occurred. Based on the characteristics of the Nacos configuration center storing data records in the database, even if a downtime occurs, the minimum data record remains unchanged. Therefore, after the downtime ends, the client obtains the distributed lock again based on the client's unique identification string and the same minimum data record, and continues to execute the operation corresponding to the request on the shared resource, ensuring the continuity of the client's operation on the shared resource.
[0064] The release unit 7 is used to execute step S7: after performing the operation on the shared resource, the minimum data record of the Nacos configuration center is deleted.
[0065] Specifically, the lock is released by deleting the data record of the Nacos configuration center. At the same time, according to the dynamic update feature of the Nacos configuration center, the operation is synchronized for all clients.
[0066] In one embodiment, when deleting the data record of the Nacos configuration center, the prefixed unique identification string in the data record of the Nacos configuration center is compared with the prefixed unique identification string of the current access shared resource request to see if they are consistent. Only if they are consistent can the deletion operation be performed, otherwise feedback information is sent to further ensure the mutual exclusivity of the lock.
[0067] Compared with the prior art, the present invention can process requests for access or operation of shared resources on the server side under high concurrency, and will not take too long to acquire distributed locks under high concurrency. At the same time, the data of the Nacos distributed lock of the present invention is directly persisted in the database, ensuring that data will not be lost even if the system crashes. In addition, the present invention solves the problem that the implementation of distributed locks through Nacos requires the introduction of third-party tools, thereby increasing the complexity of the original framework and improving the use of computer resources.
[0068] Based on the same inventive concept, the present application also provides an electronic device, which may be a terminal device such as a server, a desktop computing device or a mobile computing device (e.g., a laptop computing device, a handheld computing device, a tablet computer, a netbook, etc.). The device includes one or more processors and a memory, wherein the processor is used to execute a program to implement the implementation method of the distributed lock based on Nacos according to an embodiment of the present invention; and the memory is used to store a computer program executable by the processor.
[0069] Based on the same inventive concept, the present application also provides a computer-readable storage medium, corresponding to the embodiment of the aforementioned distributed lock implementation method based on Nacos, wherein the computer-readable storage medium stores a computer program thereon, and when the program is executed by a processor, the steps of the distributed lock implementation method based on Nacos recorded in any of the aforementioned embodiments are implemented.
[0070] The present application may take the form of a computer program product implemented on one or more storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing program code. Computer-usable storage media include permanent and non-permanent, removable and non-removable media, and information storage can be achieved by any method or technology. Information can be computer-readable instructions, data structures, modules of programs, or other data. Examples of computer storage media include but are not limited to: phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, read-only compact disk read-only memory (CD-ROM), digital versatile disk (DVD) or other optical storage, magnetic cassettes, tape disk storage or other magnetic storage devices or any other non-transmission medium that can be used to store information that can be accessed by a computing device.
[0071] The above-mentioned embodiments only express several implementation methods of the present invention, and the description is relatively specific and detailed, but it cannot be understood as limiting the scope of the invention patent. It should be pointed out that for ordinary technicians in this field, several modifications and improvements can be made without departing from the concept of the present invention, and the present invention is also intended to include these modifications and modifications.
Claims
1. A method for implementing a distributed lock based on Nacos, characterized in that: The following steps are involved: S1. Receive a request to access a shared resource and generate a unique identification string with a prefix corresponding to the request; S2. Push the unique identifier string with prefix to the Nacos configuration center; The push operation is to update the Nacos server configuration using the unique identifier string with the prefix as dataId; After the Nacos configuration center updates the configuration, it generates a data record, and the data record is provided with a self-incrementing id, and the configuration is automatically synchronized to all clients; The sorting operation uses the prefix as a condition to query all data records with the prefix, and sorts them by size according to the id in the data record, so as to obtain the minimum data record with the minimum id; S3, Nacos configuration center automatically generates self-incrementing data records based on the pushed unique identifier string with prefix; S4. Sort all data records with prefixes in the Nacos configuration center to obtain the minimum data record; S5, comparing the minimum data record with the prefix identifier to see if they are consistent, if yes, proceed to the next step, otherwise continue to compare and wait; S6, enabling the device sending the request to access the shared resource, and performing an operation corresponding to the request on the shared resource; S7: After performing operations on shared resources, the minimum data record of the Nacos configuration center is deleted.
2. The method according to claim 1, characterized in that: The prefix is "lock_", and the unique identification string is a string represented by 32 hexadecimal digits.
3. A method according to claim 1, characterized in that Before deleting the data records of the current Nacos configuration center, the following steps are also included: Compare the prefixed unique identification string in the data record of the Nacos configuration center with the prefixed unique identification string of the current access shared resource request to see if they are consistent. Only if they are consistent can the deletion operation be performed, otherwise a feedback message is sent.
4. A distributed lock device, characterized in that: It includes a prefix unique identification string generation unit, an identification string push unit, a Nacos configuration center, a sorting unit, a comparison unit, an access unit, and a release unit; The prefixed unique identification string generating unit is used to receive a request for accessing a shared resource and generate a prefixed unique identification string corresponding to the request; The identification string pushing unit pushes the prefixed unique identification string to the Nacos configuration center; wherein the pushing operation of the identification string pushing unit is to update the configuration of the server side using the prefixed unique identification string as dataId; After the Nacos configuration center updates the configuration, it generates a data record, and the data record is provided with a self-incrementing id, and the configuration is automatically synchronized to all clients; The sorting operation of the sorting unit uses the prefix as a condition to query all data records with the prefix, and sorts the data records by size according to the id in the data records, so as to obtain the minimum data record with the minimum id; The Nacos configuration center automatically generates a self-incrementing data record according to the pushed unique identification string with a prefix; The sorting unit sorts all data records with prefixes in the Nacos configuration center to obtain the minimum data record; The comparison unit compares the minimum data record with the prefix identifier to see if they are consistent, if yes, executes the next step, otherwise continues to compare and wait; The access unit enables the device sending the request to access the shared resource and perform the operation corresponding to the request on the shared resource; The release unit deletes the minimum data record of the Nacos configuration center after performing the operation on the shared resource.
5. A device according to claim 4, characterized in that: The prefix in the prefixed unique identification string generating unit is "lock_", and the unique identification string is a string represented by 32 hexadecimal digits.
6. A device according to claim 4, characterized in that: Before the release unit deletes the data record of the current Nacos configuration center, it also includes: Compare the prefixed unique identification string in the data record of the Nacos configuration center with the prefixed unique identification string of the current access shared resource request to see if they are consistent. Only if they are consistent can the deletion operation be performed, otherwise a feedback message is sent.
7. A distributed lock system, characterized in that: It includes a Nacos server and a distributed lock device, wherein the server is composed of multiple groups of servers and / or storage media and is provided with shared resources, and the shared resources are resources shared by multiple clients; the distributed lock device is used to execute the implementation method of the Nacos-based distributed lock according to any one of claims 1 to 3.
8. An electronic device comprising: A memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the distributed lock implementation method based on Nacos as described in any one of claims 1 to 3 when executing the computer program.
Citation Information
Cited By
Information resource efficient collaborative optimization method for distributed computer system
CN120162118A
