A Weakly Centralized Distributed System Networking Method and System
By using the Redis SET command to set a distributed lock at system startup, a central node is determined, and other services take over in case of failure, thus solving the single point of failure problem of centralized systems and realizing a simple and reliable weakly centralized distributed system.
Patent Information
- Application Number
- CN202111542881.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-16
- Publication Date
- 2024-08-20
- Estimated Expiration
- 2041-12-16
AI Technical Summary
Centralized systems suffer from single points of failure and poor scalability. Blockchain technology or distributed system consensus protocols are complex and difficult to implement in a simple, weakly centralized system.
In the initial stage of system startup, the Redis SET command with EX and NX parameters is used to set a distributed lock, determine the central node, and in the event of a failure of the central service, other services will select a new central node to take over the system.
This solves the single point of failure problem of the central service, realizes a simple weakly centralized system, and ensures system reliability and scalability.
Smart Images

Figure CN114257591B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of Internet technology, and in particular to a weakly centralized distributed system networking method and system. Background Art
[0002] Weakly centralized systems are relative to centralized systems. A centralized system is one in which the entire system is deployed on one node. The advantage of a centralized system is that it can save resources to the maximum extent. However, centralized systems have obvious single point failure problems and poor scalability. Once the central service cannot continue to provide services due to any problem, the entire system will be paralyzed. Blockchain technology or some distributed system consistency protocols are relatively complex to implement, requiring each service to do a lot of work. The simple weakly centralized system solves the problem of single point failure of the central service very well, and is relatively simple to implement. Summary of the invention
[0003] In order to solve the deficiencies of the prior art, the present invention proposes a weakly centralized distributed system networking method and system, which allows the services in the system to try to set and obtain a distributed lock using the redis set command plus EX and NX parameters in the initial startup. The node that obtains the distributed lock is determined as the center. When other services are started, if they do not obtain this permission and the distributed lock information is not for this service, they will automatically serve as node services. When the central service fails, other services in the system will select a service as the new center, and the new center will take over the entire system and provide services to the outside world.
[0004] To achieve the above objectives, the technical solutions adopted by the present invention include:
[0005] A weakly centralized distributed system networking method, comprising:
[0006] S1. Set a distributed lock in redis and set the survival time of the distributed lock;
[0007] S2. Obtain the connection information of redis, start all node services indiscriminately, and grab the distributed lock;
[0008] S3. Determine the central node and the common node according to the distributed lock snatching situation, and regularly update the survival time of the distributed lock;
[0009] S4, the common nodes are connected to the central node;
[0010] S5, judging whether the common node is successfully connected to the central node;
[0011] If the common node is successfully connected to the central node, register with the central node;
[0012] Determine whether the registration is successful. If the registration is successful, the ordinary node starts to provide node services;
[0013] If the registration is unsuccessful, the distributed lock is re-grabbed;
[0014] If the ordinary node fails to connect to the central node, the distributed lock is re-grabbed.
[0015] Further, the determining of the central node and the common node according to the distributed lock snatching situation includes:
[0016] Determine whether the node successfully grabs the distributed lock;
[0017] If a node successfully grabs the distributed lock, the node that successfully grabs the distributed lock is set as the central node;
[0018] If the node fails to successfully grab the distributed lock, the information of the distributed lock is obtained to determine whether the information belongs to the address and interface of the node;
[0019] If the information belongs to the address and interface of this node, this node is set as the central node, and other nodes are set as ordinary nodes.
[0020] Furthermore, the central node is used to receive connections from clients, register and process client requests and registrations of common nodes, and send task requests to common nodes.
[0021] Furthermore, the distributed lock is set using the SETNX command.
[0022] Furthermore, when the ordinary node fails to register or fails to connect to the central node, the delay for re-snatching the distributed lock is set to 3 seconds.
[0023] The present invention also relates to a weakly centralized distributed system networking system, comprising:
[0024] Distributed lock setting module, used to set distributed locks and survival time;
[0025] The acquisition module is used to obtain redis connection information, distributed lock information and client requests;
[0026] A judgment module is used to judge whether the node has successfully seized the distributed lock, whether the ordinary node is connected and / or registered with the central node successfully, and if all nodes have failed to successfully seize the distributed lock, whether the information of the distributed lock belongs to the address and interface of the node;
[0027] A processing module, used to determine the central node and the common node, regularly update the survival time of the distributed lock, receive the connection and registration of the client, and receive and process the request of the client;
[0028] The storage module is used to store the information of the distributed lock, the address and interface information of the node and the request of the client.
[0029] The present invention also relates to a computer-readable storage medium, on which a computer program is stored, and the computer program implements the above method when executed by a processor.
[0030] The present invention also relates to an electronic device, characterized in that it comprises a processor and a memory;
[0031] The memory is used to store the information of the distributed lock, the address and interface information of the node and the request of the client;
[0032] The processor is used to execute a computer program stored in a memory to perform a method.
[0033] The beneficial effects of the present invention are:
[0034] The weakly centralized distributed system networking method and system of the present invention is adopted. By allowing the service in the system to try to set and obtain a distributed lock by using the redis set command plus EX and NX parameters at the initial startup, the node that obtains the distributed lock is determined as the center. When other services are started, those that do not obtain this authority automatically serve as node services. When the central service fails, other services in the system will select a service as the new center, and the new center will take over the entire system and provide services to the outside world. The single point failure problem of the central service is solved, and the implementation is relatively simple. BRIEF DESCRIPTION OF THE DRAWINGS
[0035] Figure 1 The figure is a flow chart of the weakly centralized distributed system networking method of the present invention.
[0036] Figure 2 This is a schematic diagram of the network structure of the weakly centralized distributed system of the present invention. DETAILED DESCRIPTION
[0037] In order to more clearly understand the content of the present invention, it will be described in detail with reference to the drawings and embodiments.
[0038] The first aspect of the present invention relates to a process of steps as follows Figure 1 The weakly centralized distributed system networking method shown includes:
[0039] S1. Set a distributed lock in redis and set the survival time of the distributed lock;
[0040] Specifically, use the set command in redis to set the distributed lock, for example
[0041] setkeyvalue[EXseconds|PXmilliseconds][NX|XX]
[0042] EXsecond: Set the expiration time of the distributed lock to second seconds. SETkeyvalueEXsecond has the same effect as SETEXkeysecondvalue
[0043] PXmillisecond: Sets the expiration time of the distributed lock to millisecond milliseconds. SETkeyvaluePXmillisecond has the same effect as PSETEXkeymillisecondvalue.
[0044] NX: Set the operation only when the distributed lock does not exist. SETkeyvalueNX has the same effect as SETNXkeyvalue.
[0045] XX: Set the operation only when the distributed lock already exists.
[0046] We only used the EX and NX parameters.
[0047] For example: setcenter192.168.12.11:8000EX3NX
[0048] The meaning is: set the key to center and the value to 192.168.12.11:8000. If the key does not exist, the setting is successful. If it exists, the setting fails. If the setting is successful, the key will survive for 3 seconds.
[0049] S2. Obtain the connection information of redis, start all node services indiscriminately, and grab the distributed lock;
[0050] setNX(key, 1) When a thread executes setNX and returns 1, it means that the key does not exist originally and the thread successfully obtains the distributed lock. When other threads execute setNX and return 0, it means that the key already exists and the thread fails to grab the lock.
[0051] S3. Determine the central node and the common node according to the distributed lock snatching situation, and regularly update the survival time of the distributed lock; if a thread that obtains the lock dies during the execution of a task and there is no time to explicitly release the lock, the resource will be locked forever, and other threads will never be able to enter. Therefore, the key of setNX must be set with a timeout to ensure that even if it is not explicitly released, the lock will be automatically released after a certain period of time. setNX does not support the timeout parameter, so the EX parameter is required.
[0052] Specifically, determine whether the node successfully grabs the distributed lock;
[0053] If a node successfully grabs the distributed lock, the node that successfully grabs the distributed lock is set as the central node;
[0054] If the node fails to successfully grab the distributed lock, the information of the distributed lock is obtained to determine whether the information belongs to the address and interface of the node;
[0055] If the information belongs to the address and interface of this node, this node is set as the central node, and other nodes are set as ordinary nodes.
[0056] S4, the common nodes are connected to the central node;
[0057] S5, judging whether the common node is successfully connected to the central node;
[0058] If the common node is successfully connected to the central node, register with the central node;
[0059] Determine whether the registration is successful. If the registration is successful, the ordinary node starts to provide node services;
[0060] If the registration is unsuccessful, the distributed lock is re-grabbed;
[0061] If the ordinary node fails to connect to the central node, the distributed lock is re-grabbed.
[0062] When the ordinary node fails to register or fails to connect to the central node, the delay for re-snatching the distributed lock is set to 3 seconds.
[0063] Since the distributed lock has a survival time, the remaining survival time must be updated regularly. When the central service dies, the distributed lock will become invalid after a survival time. The central node will not actively give up its central identity. When the ordinary node fails to connect to the central node or register, it will try to grab the distributed lock again and finally confirm its identity again.
[0064] Another aspect of the present invention also relates to a weakly centralized distributed system networking system, the structure of which is as follows: Figure 2 As shown, including:
[0065] Distributed lock setting module, used to set distributed locks and survival time;
[0066] The acquisition module is used to obtain redis connection information, distributed lock information and client requests;
[0067] A judgment module is used to judge whether the node has successfully seized the distributed lock, whether the ordinary node is connected and / or registered with the central node successfully, and if all nodes have failed to successfully seize the distributed lock, whether the information of the distributed lock belongs to the address and interface of the node;
[0068] A processing module, used to determine the central node and the common node, regularly update the survival time of the distributed lock, receive the connection and registration of the client, and receive and process the request of the client;
[0069] The storage module is used to store the information of the distributed lock, the address and interface information of the node and the request of the client.
[0070] By using this system, the above-mentioned operation processing method can be executed and the corresponding technical effects can be achieved.
[0071] An embodiment of the present invention also provides a computer-readable storage medium capable of implementing all the steps in the weakly centralized distributed system networking method in the above embodiment, wherein a computer program is stored on the computer-readable storage medium, and when the computer program is executed by a processor, all the steps in the weakly centralized distributed system networking method in the above embodiment are implemented.
[0072] An embodiment of the present invention also provides an electronic device for executing the above method. As an implementation device of the method, the electronic device has at least a processor and a memory, and in particular, the memory stores data and related computer programs required for executing the method, such as information of distributed locks, node addresses and interface information, and client requests, etc., and the processor calls the data and programs in the memory to execute all the steps of the implementation method and obtains the corresponding technical effects.
[0073] Preferably, the electronic device may include a bus architecture, the bus may include any number of interconnected buses and bridges, and the bus will include various circuits linked together by one or more processors and memories. The bus may also link together various other circuits such as peripherals, voltage regulators, and power management circuits, which are all well known in the art and are therefore not further described herein. The bus interface provides an interface between the bus and the receiver and transmitter. The receiver and transmitter may be the same component, i.e., a transceiver, providing a unit for communicating with various other systems over a transmission medium. The processor is responsible for managing the bus and general processing, while the memory may be used to store data used by the processor when performing operations.
[0074] In addition, the electronic device may further include components such as a communication module, an input unit, an audio processor, a display, and a power supply. The processor (or controller, operation control) used may include a microprocessor or other processor device and / or a logic device, which receives input and controls the operation of various components of the electronic device; the memory may be one or more of a cache, a flash memory, a hard drive, a removable medium, a volatile memory, a non-volatile memory, or other suitable devices, which may store the above-mentioned related data information, and may also store a program for executing related information, and the processor may execute the program stored in the memory to realize information storage or processing, etc.; the input unit is used to provide input to the processor, for example, it may be a key or a touch input device; the power supply is used to provide power to the electronic device; the display is used to display display objects such as images and text, for example, it may be an LCD display. The communication module is a transmitter / receiver that sends and receives signals via an antenna. The communication module (transmitter / receiver) is coupled to the processor to provide input signals and receive output signals, which may be the same as the case of a conventional mobile communication terminal. Based on different communication technologies, multiple communication modules may be provided in the same electronic device, such as a cellular network module, a Bluetooth module and / or a wireless local area network module, etc. The communication module (transmitter / receiver) is also coupled to a speaker and a microphone via an audio processor to provide an audio output via the speaker and receive an audio input from the microphone, thereby realizing a common telecommunication function. The audio processor may include any suitable buffer, decoder, amplifier, etc. In addition, the audio processor is also coupled to a central processor, so that recording can be performed on the machine through the microphone, and the sound stored on the machine can be played through the speaker.
[0075] Those skilled in the art will appreciate that embodiments of the present invention may be provided as methods, systems, or computer program products. Therefore, the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0076] The present invention is described with reference to flowcharts and / or block diagrams of methods, devices (systems), and computer program products according to embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A system that specifies the functions of a box or multiple boxes.
[0077] These computer program instructions may also be stored in a computer-readable memory that can direct a computer or other programmable data processing device to work in a specific manner, so that the instructions stored in the computer-readable memory produce a product including an instruction system, which is implemented in the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0078] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps of the functions specified in a box or multiple boxes. Although the preferred embodiments of the present invention have been described, those skilled in the art may make additional changes and modifications to these embodiments once they know the basic creative concept. Therefore, the attached claims are intended to be interpreted as including the preferred embodiments and all changes and modifications that fall within the scope of the present invention.
[0079] The above is only a preferred specific embodiment of the present invention, but the protection scope of the present invention is not limited thereto. Any changes or substitutions that can be easily thought of by a person skilled in the art within the technical scope disclosed by the present invention should be included in the protection scope of the present invention. Therefore, the protection scope of the present invention should be based on the protection scope of the claims.
Claims
1. A weakly centralized distributed system networking method, characterized in that: include: S1. Set a distributed lock in redis and set the survival time of the distributed lock; S2. Obtain the connection information of redis, start all node services indiscriminately, and grab the distributed lock; S3. Determine the central node and the common node according to the distributed lock snatching situation, and regularly update the survival time of the distributed lock; Determining the central node and the common node according to the distributed lock snatching situation includes: Determine whether the node successfully grabs the distributed lock; If a node successfully grabs the distributed lock, the node that successfully grabs the distributed lock is set as the central node; If the node fails to successfully grab the distributed lock, the information of the distributed lock is obtained to determine whether the information belongs to the address and interface of the node; If the information belongs to the address and interface of this node, this node is set as the central node and other nodes are set as ordinary nodes; S4, the common nodes are connected to the central node; S5, judging whether the common node is successfully connected to the central node; If the common node is successfully connected to the central node, register with the central node; Determine whether the registration is successful. If the registration is successful, the ordinary node starts to provide node services; If the registration is unsuccessful, the distributed lock is re-grabbed; If the ordinary node fails to connect to the central node, the distributed lock is re-grabbed.
2. The method according to claim 1, characterized in that The central node is used to receive connections from clients, register and process client requests and registration of common nodes, and send task requests to common nodes.
3. The method according to claim 1, characterized in that The distributed lock is set using the SETNX command.
4. The method according to claim 1, characterized in that When the ordinary node fails to register or fails to connect to the central node, the delay for re-snatching the distributed lock is set to 3 seconds.
5. A weakly centralized distributed system networking system, characterized in that: include: Distributed lock setting module, used to set distributed locks and survival time; The acquisition module is used to obtain redis connection information, distributed lock information and client requests; A judgment module, used to judge whether the node has successfully seized the distributed lock; If a node successfully grabs the distributed lock, the node that successfully grabs the distributed lock is set as the central node; If the node fails to successfully grab the distributed lock, the information of the distributed lock is obtained to determine whether the information belongs to the address and interface of the node; If the information belongs to the address and interface of this node, this node is set as the central node and other nodes are set as ordinary nodes; It is also used to determine whether the ordinary node is successfully connected to the central node; If the common node is successfully connected to the central node, register with the central node; Determine whether the registration is successful. If the registration is successful, the ordinary node starts to provide node services; If the registration is unsuccessful, the distributed lock is re-grabbed; If the ordinary node fails to connect to the central node, the distributed lock is re-grabbed; A processing module, used to determine the central node and the common node, regularly update the survival time of the distributed lock, receive the connection and registration of the client, and receive and process the request of the client; The storage module is used to store the information of the distributed lock, the address and interface information of the node and the request of the client.
6. A computer-readable storage medium, characterized in that: The storage medium stores a computer program, and when the computer program is executed by a processor, the method according to any one of claims 1 to 4 is implemented.
7. An electronic device, characterized in that: including a processor and a memory; The memory is used to store the information of the distributed lock, the address and interface information of the node and the request of the client; The processor is configured to execute a computer program stored in a memory to perform the method according to any one of claims 1 to 4.
Citation Information
Patent Citations
Distributed timing method and system and server
CN108733459A
A timed task scheduling method and device
CN109558234A
Distributed lock management method and device
CN113076187A
Distributed lock coordination method and device, equipment and storage medium
CN113660350A
Method for high availability of services in cloud computing systems
US10185597B1
