Main node election method and device based on Redis, medium and product

By setting up Redis sentinels and Redis nodes in the Redis cluster, automatic master node election is achieved, which solves the inefficiency problem of manually setting up master nodes after Redis failure, and improves fault recovery efficiency and business security.

CN119583317APending Publication Date: 2025-03-07GUANGZHOU BAOLUN ELECTRONICS CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411790785.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-06
Publication Date
2025-03-07

AI Technical Summary

Technical Problem

In the prior art, the main node needs to be manually set up after Redis failure, which is slow, low efficiency, high labor cost, and difficult to meet business needs.

Method used

The method of automatic master node election is realized by setting Redis sentinels and Redis nodes in the Redis cluster. The specific steps include: determining that the connection to the Redis sentry cluster failed, and connecting to the Redis sentry in the Redis cluster successively; if the connection is successful, obtain the master node information, and connect to the Redis node based on the master node information to determine the master node.

Benefits of technology

It realizes automatic selection of master nodes after Redis failure, avoids the inefficiency problem of manually setting up master nodes, improves the efficiency and speed of Redis failure recovery, reduces maintenance costs, and improves business security.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119583317A_ABST
    Figure CN119583317A_ABST
Patent Text Reader

Abstract

The invention provides a main node election method and device based on Redis, a medium and a product, and relates to the technical field of data storage. Each network machine in a Redis cluster is provided with a Redis sentry and a Redis node, the Redis sentries form a Redis sentry cluster, and the main node election method comprises the following steps: determining that the connection with the Redis sentry cluster fails, and successively connecting the Redis sentries in the Redis cluster; if it is determined that the connection with the Redis sentry is successful, master node information in the Redis sentry is obtained, and the master node information comprises whether a master node is empty or not; and connecting the Redis node according to the main node information, and determining the main node based on a connection result. According to the embodiment of the invention, automatic setting of the master node can be realized, the situation that the master node needs to be set manually is avoided, the Redis fault recovery efficiency and speed are improved, the labor cost of Redis maintenance is effectively reduced, and the service security of Redis is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of data storage technology. Specifically, the present application relates to a master node election method, device, medium and product based on Redis. Background Art

[0002] With the rapid development of science and technology, Internet companies are dealing with increasingly complex customer needs while also generating a huge amount of data information. Redis is widely used on big data platforms and can be used as a counter for data statistics or to store data. Redis occupies the memory of a single physical machine or virtual machine, and memory resources are limited, so capacity needs to be adjusted according to business conditions, which requires the use of Redis clusters.

[0003] At present, in the process of using redis, in order to improve the reliability of redis, master-slave nodes are set in different servers and managed. When a fault occurs, the slave node is switched to the master node, and a notification message is sent to each business network element that needs to access redis, so that each business network element modifies the stored redis IP address, so that the business network element can access the switched master node according to the modified redis Internet Protocol (IP) address. However, in the prior art, the master node is often manually set when a fault occurs. This node setting method is slow, inefficient, and costly and difficult to meet business needs. Summary of the invention

[0004] The embodiments of the present application provide a master node election method, device, medium and product based on Redis, which can solve the problems of slow speed, low efficiency, high labor cost and difficulty in meeting business needs of manually setting the master node after the existing failure. In order to achieve this purpose, the embodiments of the present application provide the following solutions.

[0005] According to one aspect of an embodiment of the present application, a master node election method based on Redis is provided, each network machine in a Redis cluster is provided with a Redis sentinel and a Redis node, and the Redis sentinels constitute a Redis sentinel cluster, and the method includes:

[0006] Determine that the connection with the Redis Sentinel cluster fails, and connect to the Redis Sentinels in the Redis cluster one by one;

[0007] If it is determined that the connection with the Redis Sentinel is successful, the master node information in the Redis Sentinel is obtained, and the master node information includes whether the master node is empty;

[0008] Connect to the Redis node according to the master node information, and determine the master node based on the connection result, where the connection result includes at least one of whether the connected Redis node is the master node and whether the Redis node is connected normally.

[0009] In a possible implementation, determining that the connection with the Redis Sentinel cluster fails includes:

[0010] Determine the connected Redis sentinel and send preset information to the Redis sentinel;

[0011] If it is determined that the feedback information corresponding to the preset information is not received, it is determined that the address of the Redis Sentinel is unreachable and the connection with the Redis Sentinel cluster fails.

[0012] In a possible implementation, the sequentially connecting to the Redis sentinels in the Redis cluster includes:

[0013] Determine the target Redis sentinel currently connected, and send the preset information to the target Redis sentinel;

[0014] If it is determined that feedback information corresponding to the preset information is received, it is determined that the connection with the target Redis sentinel is successful;

[0015] If it is determined that feedback information corresponding to the preset information is not received, a next target Redis sentinel connected in the Redis cluster is determined, and the preset information is sent to the next target Redis sentinel.

[0016] In a possible implementation, the master node information includes that the master node is empty, and connecting the Redis node according to the master node information includes:

[0017] Connect to Redis nodes one by one;

[0018] If it is determined to be connected to the Redis node, the master node is set according to the type of the Redis node connected to.

[0019] In a possible implementation, setting the master node according to the type of the Redis node connected to includes:

[0020] If none of the Redis nodes connected to are master nodes, the currently connected Redis node is set as the master node.

[0021] In a possible implementation, the master node information includes that the master node is not empty, and determining the master node based on the connection result includes:

[0022] If it is determined according to the connection result that the master node connection is abnormal, the fault repair information of the Redis sentinel is obtained, and the master node is determined according to the fault repair information.

[0023] In a possible implementation, the fault repair information includes a repair timeout, and determining the master node according to the fault repair information includes:

[0024] If it is determined that the slave node information is obtained from the Redis sentinel, the Redis node corresponding to the slave node information is connected, and the master node is determined according to the connection status with the Redis node.

[0025] According to one aspect of an embodiment of the present application, an electronic device is provided, including a memory, a processor, and a computer program stored in the memory, wherein the processor executes the computer program to implement the steps of the method described above.

[0026] According to one aspect of an embodiment of the present application, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the steps of the method described above are implemented.

[0027] According to one aspect of an embodiment of the present application, a computer program product is provided, including a computer program, and when the computer program is executed by a processor, the steps of the method described above are implemented.

[0028] The beneficial effects of the technical solution provided by the embodiment of the present application are:

[0029] The Redis-based master node election method provided by the present application connects to the Redis sentinels in the Redis cluster one by one when it is determined that the connection with the Redis sentinel cluster has failed; if it is determined that the connection with the Redis sentinel is successful, the master node information in the Redis sentinel is obtained; the Redis node is connected according to the master node information, and the master node is determined based on the connection result. After a Redis failure, the embodiment of the present application automatically selects the master node according to the current Redis information, thereby realizing automatic setting of the master node, avoiding the need to manually set the master node, improving the efficiency and speed of Redis fault recovery, effectively reducing the labor cost of Redis maintenance, and improving the business security of Redis. BRIEF DESCRIPTION OF THE DRAWINGS

[0030] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the following is a brief introduction to the drawings required for use in describing the embodiments of the present application.

[0031] Figure 1 A flowchart of a master node election method based on Redis provided in an embodiment of the present application;

[0032] Figure 2 A schematic diagram of a Redis cluster provided in an embodiment of the present application;

[0033] Figure 3 A flowchart for connecting to a Redis Sentinel provided in an embodiment of the present application;

[0034] Figure 4 A flowchart of the master node setting when the master node is empty provided in an embodiment of the present application;

[0035] Figure 5 A flowchart of the master node setting when the master node is not empty provided in an embodiment of the present application;

[0036] Figure 6 A structural diagram of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0037] The embodiments of the present application are described below in conjunction with the drawings in the present application. It should be understood that the implementation methods described below in conjunction with the drawings are exemplary descriptions for explaining the technical solutions of the embodiments of the present application and do not constitute a limitation on the technical solutions of the embodiments of the present application.

[0038] It will be understood by those skilled in the art that, unless specifically stated, the singular forms "one", "said", and "the" used herein may also include plural forms. It should be further understood that the terms "including" and "comprising" used in the embodiments of the present application refer to that the corresponding features can be implemented as the presented features, information, data, steps, operations, elements and / or components, but do not exclude the implementation of other features, information, data, steps, operations, elements, components and / or combinations thereof supported by the present technical field. It should be understood that when we say that an element is "connected" or "coupled" to another element, the one element may be directly connected or coupled to the other element, or it may refer to that the one element and the other element establish a connection relationship through an intermediate element. In addition, the "connection" or "coupling" used herein may include wireless connection or wireless coupling. The term "and / or" used herein indicates at least one of the items defined by the term, for example, "A and / or B" indicates that it is implemented as "A", or is implemented as "A", or is implemented as "A and B".

[0039] In order to make the objectives, technical solutions and advantages of the present application clearer, the implementation methods of the present application will be further described in detail below with reference to the accompanying drawings.

[0040] The following describes several exemplary embodiments to illustrate the technical solutions of the embodiments of the present application and the technical effects produced by the technical solutions of the present application. It should be noted that the following embodiments can refer to, draw on or combine with each other, and the same terms, similar features and similar implementation steps in different embodiments will not be described repeatedly.

[0041] The Redis-based master node election method, device, medium and product provided in this application are intended to solve at least one technical problem existing in the prior art.

[0042] In an embodiment of the present application, a master node election method based on Redis is provided, and objects to which the method can be applied can be network machines in a Redis cluster, terminals related to the Redis cluster such as mobile phones, laptops, servers, cloud platforms, and other smart terminals.

[0043] Optionally, the application environment of the master node election method of the present application is a Redis cluster, wherein each network machine in the Redis cluster is provided with a Redis sentinel and a Redis node, and the Redis sentinels constitute a Redis sentinel cluster, which monitors the status of each node in the Redis cluster and stores information about the master node and slave node in the Redis cluster through the Redis sentinels in the Redis sentinel cluster.

[0044] Optionally, the Redis cluster includes at least two network machines, each of which has a corresponding Redis node and a Redis sentinel. When only one network machine is online, the Redis sentinel corresponding to the network machine can still elect the Redis node corresponding to the network machine as the master node according to the communication status with other Redis sentinels, and can restore communication with other Redis sentinels; once other Redis sentinels are online, the Redis sentinels find each other through pre-stored information and re-elect the master node. This structure and master node election method can build a high-availability environment (Redis sentinel cluster) when there are only two network machines.

[0045] In one embodiment, Figure 2 As shown, the network machine includes network machine A and network machine B. A set of redis sentinels and redis nodes are built on network machine A and network machine B respectively, and the redis node on network machine A is set as the master node. The master node of redis sentinel 1 and Redis sentinel 2 on network machine A and network machine B is set to network machine A, thus forming a redis cluster + redis master-slave high availability environment.

[0046] like Figure 1-Figure 5 As shown, the master node election method based on Redis of the present application includes:

[0047] S101: Determine that the connection with the Redis Sentinel cluster fails, and connect to the Redis Sentinels in the Redis cluster one by one.

[0048] Optionally, determining that the connection with the Redis Sentinel cluster fails includes: determining the connected Redis Sentinel, and sending preset information to the Redis Sentinel; if it is determined that feedback information corresponding to the preset information is not received, determining that the address of the Redis Sentinel is unreachable and the connection with the Redis Sentinel cluster fails.

[0049] Optionally, the preset information may be a ping packet and other information that can be used to detect whether the address of the Redis sentinel is reachable. When the preset information is a ping packet, the ping packet is sent to the Redis sentinel according to the address of the Redis sentinel.

[0050] Optionally, when the preset information is a ping packet, the feedback information may be a pong packet. If it is determined that the pong packet is received, it is determined that the address of the Redis Sentinel is correct, the Redis Sentinel can be connected, and the Redis Sentinel cluster is normal. Wherein, when the preset information is information other than a ping packet, the feedback information may also be feedback indicating whether the preset information is received, feedback on whether the connection with the Redis Sentinel cluster is allowed, and other information that can indicate whether the connection is successful.

[0051] In one embodiment, Figure 3 As shown, when the network machine is started, it can be detected whether the connection with the Redis Sentinel cluster fails. Among them, when each network machine is started, the Redis node built on the network machine can try to connect with the Redis Sentinel built on the network machine, send a ping packet to the Redis Sentinel, and if the pong packet sent by the Redis Sentinel is received, it means that the address of the Redis Sentinel is correct and can be connected. If the pong packet is not received, it is determined that the address of the Redis Sentinel is unreachable on the network, and the connection with the Redis Sentinel cluster fails.

[0052] Optionally, after sending the preset information, it can be detected whether the corresponding feedback information is received within the preset time period. If the feedback information is not received within the preset time period, it is determined that the connection with the Redis Sentinel cluster has failed. Among them, the preset time period time point can be the time point when the preset information is sent. The length of the preset time period can be a preset value, and can also be determined according to one or more parameters of the current network status, the location of the Redis Sentinel to receive the preset information, and the information transmission delay.

[0053] Alternatively, if Figure 3As shown, after the connection with the Redis sentinel fails, you can connect to other sentinels in the Redis sentinel cluster one by one to detect whether the connection with all Redis in the sentinel cluster fails. Connecting to the Redis sentinels in the Redis cluster one by one includes: determining the target Redis sentinel currently connected, sending the preset information to the target Redis sentinel; if it is determined that feedback information corresponding to the preset information is received, it is determined that the connection with the target Redis sentinel is successful; if it is determined that feedback information corresponding to the preset information is not received, then determine the next target Redis sentinel connected in the Redis cluster, and send the preset information to the next target Redis sentinel.

[0054] Optionally, if it is determined that after sending the preset information to each Redis sentinel, no feedback information can be received, it is determined that all Redis sentinels in the Redis cluster are unreachable and the network machine program startup fails. After the program startup fails, an alarm message can be sent to a preset object (such as a designated operator or an alarm information display device) to remind the Redis cluster failure. It can also be related to the startup of this program, such as the number of connections, the preset information sent, the startup time, the Redis sentinel address used when sending the preset information, and other information related to the Redis cluster failure.

[0055] S102: If it is determined that the connection with the Redis Sentinel is successful, the master node information in the Redis Sentinel is obtained.

[0056] Optionally, Redis Sentinel automatically monitors the status of the master node in the Redis cluster, identifies whether the master node is faulty, and records the master node information based on the identification result.

[0057] Optionally, after determining that the connection with the Redis sentinel is successful, the operation of sending preset information to the Redis sentinel may no longer be performed, and the master node information may be extracted from the successfully connected Redis sentinel.

[0058] Optionally, after receiving the feedback information from the Redis sentinel, it is determined that the connection with the Redis sentinel is successful. The master node information is extracted from the connected Redis sentinel. The master node information can be extracted from the connected Redis sentinel through a command line.

[0059] Optionally, the master node information includes whether the master node is empty, wherein the master node information can be obtained from the connected Redis sentinel. If the master node is empty, it means that the current master node environment is abnormal.

[0060] S103: Connect to the Redis node according to the master node information, and determine the master node based on the connection result.

[0061] Optionally, the Redis node connection method adopted when the master node is empty is different from the Redis node connection method adopted when the master node is not empty. Wherein, when the master node information includes that the master node is empty, connecting the Redis node according to the master node information includes: connecting to the Redis node one by one; if it is determined to connect to the Redis node, setting the master node according to the type of the connected Redis node.

[0062] In one embodiment, Figure 4 As shown in the figure, when the master node data is empty, it is determined that the data of the current Redis Sentinel cluster has been lost and needs to be repaired. At this time, you can try to connect to the Redis nodes one by one according to the address of the Redis node, and determine whether the connection with the Redis node is successful based on the connection status.

[0063] Optionally, if all Redis nodes cannot be connected, it is determined that the current Redis Sentinel cluster network is unreachable and the network machine program startup fails.

[0064] Optionally, when connecting to a Redis node, the type of the Redis node (master node, slave node) can be obtained, and the master node can be set based on the type. Among them, the master node is set according to the type of the Redis node connected to, including: if none of the Redis nodes connected to are master nodes, the currently connected Redis node is set as the master node. In addition, the master nodes corresponding to other Redis nodes are also set as the currently connected Redis node. It is also possible to select one of the Redis nodes as the master node based on the status of the connected Redis node (communication efficiency with other Redis nodes, supported bandwidth, location, working environment, performance of the hardware used, and other parameters).

[0065] Optionally, if the connected Redis node is the master node, continue to set the Redis node as the master node of the Redis cluster.

[0066] In one embodiment, Figure 4As shown, connect to the Redis nodes in the Redis cluster one by one. If connected to the Redis node, detect whether the Redis node is the master node. If the Redis node is the master node, continue to determine the Redis node as the master node. If all the Redis nodes connected to are not master nodes, the Redis node currently connected to can be determined as the master node. And the master nodes corresponding to other Redis nodes are also set as the currently connected Redis nodes. And, after the master node is set, the connection between the master node and the slave node is made based on the master node to achieve the reconnection of the Redis cluster. After the Redis cluster is reconnected, the connection status of the Redis node and the Redis Sentinel cluster is detected at all times. After determining that the connection with the Redis Sentinel cluster fails, the master node election method based on Redis of this application is executed again.

[0067] Optionally, when the master node information includes that the master node is not empty (i.e., the information of the master node can be extracted), determining the master node based on the connection result includes: if it is determined according to the connection result that the master node connection is abnormal, obtaining the fault repair information of the Redis sentinel, and determining the master node based on the fault repair information.

[0068] Optionally, the master node can be determined through the master node information to determine whether the master node is subjectively offline (a certain Redis sentinel considers the master node unavailable) or objectively offline (multiple Redis sentinels consider the master node unavailable). If so, it is determined that the master node connection is abnormal. If not, it is determined that the master node connection is normal and the program of the network machine is running normally.

[0069] Optionally, after determining that the master node connection is abnormal, the automatic fault repair operation of the Redis sentinel is used to perform fault repair. Fault repair information is obtained, wherein the fault repair information includes whether the fault repair has timed out, whether the fault repair is completed, and other information related to the fault repair.

[0070] Optionally, when the fault repair information includes fault repair timeout information, determining the master node according to the fault repair information includes: if it is determined that the slave node information is obtained from the Redis sentinel, connecting to the Redis node corresponding to the slave node information, and determining the master node according to the connection status with the Redis node.

[0071] Optionally, if no slave node information is obtained from the connected Redis sentinel, the Redis node of the current network machine or the Redis node corresponding to the network machine started by the current program is determined as the master node monitored by the Redis sentinel, and the node is also used as the master node corresponding to other Redis nodes.

[0072] Alternatively, if Figure 5As shown, if the slave node information is obtained from the connected Redis sentinel, it is determined whether the slave node corresponding to the slave node information is connected normally. If it is determined that the connection is normal, the slave node is set as the master node, and the information of the master node in Redis is reset based on the information of the master node, and the master node is also set as the master node corresponding to other Redis. If it is determined that the slave node corresponding to the slave node information is not connected normally (such as connection failure), the Redis node of this network machine or the Redis node corresponding to the network machine started by the current program is set as the master node monitored by the currently connected Redis sentinel. In this way, the use of Redis sentinel clusters and Redis nodes can be realized when only the Redis sentinel and the master node are also offline.

[0073] Optionally, when the fault repair information indicates that the repair has not timed out, you can wait for the Redis sentinel to be repaired, and after the repair is completed, continue to detect whether the master node is connected normally.

[0074] Optionally, after the master node is determined and the master node connection is normal, the Redis cluster is reconnected. After the Redis cluster is reconnected, the Redis Sentinel cluster is continuously checked to see if it is normal. When a connection failure with the Redis Sentinel cluster is detected, the master node election method is executed again.

[0075] The Redis-based master node election method provided by the present application connects to the Redis sentinels in the Redis cluster one by one when it is determined that the connection with the Redis sentinel cluster has failed; if it is determined that the connection with the Redis sentinel is successful, the master node information in the Redis sentinel is obtained; the Redis node is connected according to the master node information, and the master node is determined based on the connection result. After a Redis failure, the embodiment of the present application automatically selects the master node according to the current Redis information, which can realize automatic setting of the master node, avoid the need to manually set the master node, improve the efficiency and speed of Redis fault recovery, effectively reduce the labor cost of Redis maintenance, and improve the business security of Redis.

[0076] In an optional embodiment, an electronic device is provided, such as Figure 6 As shown, Figure 6 The electronic device 4000 shown includes: a processor 4001 and a memory 4003. The processor 4001 and the memory 4003 are connected, such as through a bus 4002. Optionally, the electronic device 4000 may also include a transceiver 4004, which may be used for data interaction between the electronic device and other electronic devices, such as data transmission and / or data reception. It should be noted that in actual applications, the transceiver 4004 is not limited to one, and the structure of the electronic device 4000 does not constitute a limitation on the embodiments of the present application.

[0077] Processor 4001 may be a CPU (Central Processing Unit), a general-purpose processor, a DSP (Digital Signal Processor), an ASIC (Application Specific Integrated Circuit), an FPGA (Field Programmable Gate Array), or other programmable logic devices, transistor logic devices, hardware components, or any combination thereof. It may implement or execute various exemplary logic blocks, modules, and circuits described in conjunction with the disclosure of this application. Processor 4001 may also be a combination that implements computing functions, such as a combination of one or more microprocessors, a combination of a DSP and a microprocessor, and the like.

[0078] The bus 4002 may include a path to transmit information between the above components. The bus 4002 may be a PCI (Peripheral Component Interconnect) bus or an EISA (Extended Industry Standard Architecture) bus. The bus 4002 may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, only one thick line is used in the figure, but it does not mean that there is only one bus or one type of bus.

[0079] Memory 4003 can be ROM (Read Only Memory) or other types of static storage devices that can store static information and instructions, RAM (Random Access Memory) or other types of dynamic storage devices that can store information and instructions, or it can be EEPROM (Electrically Erasable Programmable Read Only Memory), CD-ROM (Compact Disc Read Only Memory) or other optical disk storage, optical disk storage (including compressed optical disk, laser disc, optical disk, digital versatile disc, Blu-ray disc, etc.), magnetic disk storage medium, other magnetic storage devices or any other medium that can be used to carry or store computer programs and can be read by a computer, without limitation here.

[0080] The memory 4003 is used to store the computer program for executing the embodiment of the present application, and the execution is controlled by the processor 4001. The processor 4001 is used to execute the computer program stored in the memory 4003 to implement the steps shown in the above method embodiment.

[0081] Among them, the electronic device can be any electronic product that can interact with an object, such as a personal computer, a tablet computer, a smart phone, a personal digital assistant (PDA), a game console, an interactive network television (IPTV), a smart wearable device, etc.

[0082] The electronic device may also include a network device and / or an object device, wherein the network device includes, but is not limited to, a single network server, a server group consisting of multiple network servers, or a cloud consisting of a large number of hosts or network servers for cloud computing.

[0083] The network where the electronic device is located includes but is not limited to the Internet, a wide area network, a metropolitan area network, a local area network, a virtual private network (Virtual Private Network, VPN), etc.

[0084] An embodiment of the present application provides a computer-readable storage medium, which includes a computer program. When the computer program is executed by a processor, the steps and corresponding contents of the aforementioned method embodiment can be implemented.

[0085] An embodiment of the present application provides a computer program product, which includes a computer program. When the computer program is executed by a processor, the steps and corresponding contents of the aforementioned method embodiment can be implemented.

[0086] The terms "first", "second", "third", "fourth", "1", "2", etc. (if any) in the specification and claims of this application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the numbers used in this way can be interchanged where appropriate, so that the embodiments of the present application described herein can be implemented in an order other than that shown or described in the drawings.

[0087] It should be understood that, although each operation step is indicated by arrows in the flowchart of the embodiment of the present application, the implementation order of these steps is not limited to the order indicated by the arrows. Unless clearly stated herein, in some implementation scenarios of the embodiment of the present application, the implementation steps in each flowchart can be performed in other orders according to demand. In addition, some or all of the steps in each flowchart may include multiple sub-steps or multiple stages based on actual implementation scenarios. Some or all of these sub-steps or stages may be executed at the same time, and each sub-step or stage in these sub-steps or stages may also be executed at different times respectively. In different scenarios of execution time, the execution order of these sub-steps or stages may be flexibly configured according to demand, and the embodiment of the present application does not limit this.

[0088] The above is only an optional implementation method for some implementation scenarios of the present application. It should be pointed out that for ordinary technicians in this technical field, without departing from the technical concept of the solution of the present application, other similar implementation methods based on the technical ideas of the present application are also within the protection scope of the embodiments of the present application.

Claims

1. A master node election method based on Redis, characterized in that: Each network machine in the Redis cluster is provided with a Redis sentinel and a Redis node, and the Redis sentinels constitute a Redis sentinel cluster. The method comprises: Determine that the connection with the Redis Sentinel cluster fails, and connect to the Redis Sentinels in the Redis cluster one by one; If it is determined that the connection with the Redis Sentinel is successful, the master node information in the Redis Sentinel is obtained, and the master node information includes whether the master node is empty; Connect to the Redis node according to the master node information, and determine the master node based on the connection result, where the connection result includes at least one of whether the connected Redis node is the master node and whether the Redis node is connected normally.

2. According to the Redis-based master node election method of claim 1, it is characterized in that: The determination of connection failure with the Redis Sentinel cluster includes: Determine the connected Redis sentinel and send preset information to the Redis sentinel; If it is determined that the feedback information corresponding to the preset information is not received, it is determined that the address of the Redis Sentinel is unreachable and the connection with the Redis Sentinel cluster fails.

3. The master node election method based on Redis according to claim 2 is characterized in that: The step of successively connecting to the Redis sentinels in the Redis cluster includes: Determine the target Redis sentinel currently connected, and send the preset information to the target Redis sentinel; If it is determined that feedback information corresponding to the preset information is received, it is determined that the connection with the target Redis sentinel is successful; If it is determined that feedback information corresponding to the preset information is not received, a next target Redis sentinel connected in the Redis cluster is determined, and the preset information is sent to the next target Redis sentinel.

4. The master node election method based on Redis according to claim 1 is characterized in that: The master node information includes that the master node is empty, and connecting the Redis node according to the master node information includes: Connect to Redis nodes one by one; If it is determined to be connected to the Redis node, the master node is set according to the type of the Redis node connected to.

5. The master node election method based on Redis according to claim 4 is characterized in that: Set the master node according to the type of Redis node connected to, including: If none of the Redis nodes connected to are master nodes, the currently connected Redis node is set as the master node.

6. The master node election method based on Redis according to claim 1 is characterized in that: The master node information includes that the master node is not empty, and the determining of the master node based on the connection result includes: If it is determined according to the connection result that the master node connection is abnormal, the fault repair information of the Redis sentinel is obtained, and the master node is determined according to the fault repair information.

7. The master node election method based on Redis according to claim 6 is characterized in that: The fault repair information includes a repair timeout, and determining the master node according to the fault repair information includes: If it is determined that the slave node information is obtained from the Redis sentinel, the Redis node corresponding to the slave node information is connected, and the master node is determined according to the connection status with the Redis node.

8. An electronic device comprising a memory, a processor and a computer program stored in the memory, characterized in that: The processor executes the computer program to implement the steps of the method according to any one of claims 1 to 7.

9. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 7 are implemented.

10. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 7 are implemented.