Distributed cluster session sharing method and device, equipment and medium
By storing session data to the key-value storage system in a distributed cluster and utilizing data sharding and replication mechanisms, cross-server sharing and consistency problems are solved, and global sharing and high availability of session data are achieved.
Patent Information
- Application Number
- CN202411541809.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-31
- Publication Date
- 2025-01-10
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
In a distributed cluster environment, traditional session management technology cannot realize cross-server sharing of Session, it is difficult to ensure the consistency of Session data, and there is a risk of a single point of failure.
By storing session data to preset key-value storage systems, such as Redis, it uses data sharding and replication mechanisms for management, unified storage and cross-server sharing of session data is realized, and automatic failover is performed when node failures.
It realizes global sharing of session data, ensures that the session state is consistent when users switch between different nodes, solves the session consistency problem in a distributed environment, and improves the stability and reliability of the system.
Smart Images

Figure CN119299453A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of distributed cluster technology, and in particular to a distributed cluster session sharing method, device, equipment and medium. Background Art
[0002] With the rapid development of the Internet, Web applications are facing challenges such as a surge in the number of users and a sharp increase in data access. In order to meet these challenges, distributed cluster systems have become the mainstream architecture for building Internet applications due to their high availability and scalability. Under this architecture, user session management becomes particularly important. Traditional session management technology usually relies on the memory of a single server to store session information, but this method has obvious limitations in a distributed environment, such as the inability to share sessions across servers, the difficulty in ensuring the consistency of session data, and the risk of single point failure. Therefore, how to efficiently share and manage session sessions in a distributed cluster system is a problem that technicians in this field currently need to solve. Summary of the invention
[0003] In view of this, the purpose of the present invention is to provide a distributed cluster session sharing method, device, equipment and medium, which can realize the unified storage and cross-server sharing of session data and ensure the consistency of session data in the distributed cluster system. The specific scheme is as follows:
[0004] In a first aspect, the present application discloses a distributed cluster session sharing method, which is applied to each application server node in a distributed cluster, comprising:
[0005] Obtaining a service request sent by a client, and determining a session associated with the service request;
[0006] Storing the session data corresponding to the session in a preset key-value storage system so that the preset key-value storage system manages the session data based on a data sharding mechanism and a replication mechanism;
[0007] If there is currently an operation request for the session data, the preset key-value storage system is called according to the operation request to perform a corresponding operation on the session data.
[0008] Optionally, obtaining a service request sent by a client and determining a session associated with the service request includes:
[0009] A service request sent by a client through a load balancer based on a preset data distribution strategy is obtained, and a session associated with the service request is determined.
[0010] Optionally, storing the session data corresponding to the session in a preset key-value storage system so that the preset key-value storage system manages the session data based on a data sharding mechanism and a replication mechanism includes:
[0011] The session data corresponding to the session is stored in a Redis cluster, so that the Redis cluster manages the session data based on a data sharding mechanism and a replication mechanism.
[0012] Optionally, storing the session data corresponding to the session in a preset key-value storage system so that the preset key-value storage system manages the session data based on a data sharding mechanism and a replication mechanism includes:
[0013] The session data corresponding to the session is stored in a preset key-value storage system so that the preset key-value storage system disperses the session data into multiple slots based on a data sharding mechanism to manage the session data; wherein each slot corresponds to a master node for management, and the slave node associated with the master node backs up the data in the slot accordingly through a replication mechanism.
[0014] Optionally, the distributed cluster session sharing method further includes:
[0015] Acquire system load information currently corresponding to the preset key-value storage system, and compare the system load information with a preset system load threshold to obtain a comparison result;
[0016] When it is determined according to the comparison result that the preset key-value storage system meets the preset load expansion condition, the nodes in the preset key-value storage system are horizontally expanded.
[0017] Optionally, the distributed cluster session sharing method further includes:
[0018] When it is monitored that a master node in the preset key-value storage system fails, a target node is re-elected from slave nodes in the preset key-value storage system to determine a new master node, and the session data is operated on the new master node.
[0019] Optionally, if there is currently an operation request for the session data, calling the preset key-value storage system according to the operation request to perform a corresponding operation on the session data includes:
[0020] If there is currently a read request, a write request for the session data, and any one or a combination of operation requests for creation, update, and destruction of the session, the preset key-value storage system is called according to the operation request to perform corresponding operations on the session data.
[0021] In a second aspect, the present application discloses a distributed cluster session sharing device, which is applied to each application server node in a distributed cluster, including:
[0022] A request acquisition module, used to acquire a service request sent by a client and determine a session associated with the service request;
[0023] A session data storage module, used for storing session data corresponding to the session in a preset key-value storage system, so that the preset key-value storage system manages the session data based on a data sharding mechanism and a replication mechanism;
[0024] The session data operation module is used to call the preset key-value storage system to perform corresponding operations on the session data according to the operation request if there is currently an operation request for the session data.
[0025] In a third aspect, the present application discloses an electronic device, comprising a processor and a memory; wherein the memory is used to store a computer program, and the computer program is loaded and executed by the processor to implement the distributed cluster session sharing method as described above.
[0026] In a fourth aspect, the present application discloses a computer-readable storage medium for storing a computer program; wherein the computer program, when executed by a processor, implements the distributed cluster session sharing method as described above.
[0027] The present application provides a distributed cluster session sharing method, which is applied to each application server node in a distributed cluster, including: obtaining a service request sent by a client, and determining a session associated with the service request; storing the session data corresponding to the session in a preset key-value storage system, so that the preset key-value storage system manages the session data based on a data sharding mechanism and a replication mechanism; if there is currently an operation request for the session data, calling the preset key-value storage system according to the operation request to perform a corresponding operation on the session data. The beneficial technical effect of the present application is: by storing the session data of each application server node in a distributed cluster in a preset key-value storage system, all server instances can access the session data stored in the preset key-value storage system, thereby realizing global sharing of session data, ensuring that the session state can remain consistent when the user switches between different nodes, and solving the session consistency problem in a distributed environment.
[0028] In addition, a distributed cluster session sharing device, equipment and storage medium provided in the present application correspond to the above-mentioned distributed cluster session sharing method and have the same effect as above. BRIEF DESCRIPTION OF THE DRAWINGS
[0029] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on the provided drawings without paying creative work.
[0030] Figure 1 A flow chart of a distributed cluster session sharing method disclosed in this application;
[0031] Figure 2 A specific session sharing process diagram disclosed in this application;
[0032] Figure 3 A schematic diagram of the structure of a distributed cluster session sharing device disclosed in this application;
[0033] Figure 4 This is a structural diagram of an electronic device disclosed in this application. DETAILED DESCRIPTION
[0034] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0035] Traditional session management technology usually relies on the memory of a single server to store session information, but this method has obvious limitations in a distributed environment, such as the inability to share sessions across servers, difficulty in ensuring the consistency of session data, and the risk of single point failure.
[0036] To this end, the present application provides a distributed cluster session sharing solution that can achieve session sharing and management across application servers and solve the session consistency problem in a distributed environment.
[0037] The embodiment of the present invention discloses a distributed cluster session sharing method, which is applied to each application server node in a distributed cluster. Figure 1 As shown, the method includes:
[0038] Step S11: Obtain a service request sent by the client, and determine a session associated with the service request.
[0039] In the embodiment of the present application, it is applied to each application server node in the distributed cluster. The client sends a service request to the load balancer, and the load balancer distributes the service request to different application server nodes according to the configured policy. Specifically, the service request sent by the client through the load balancer based on the preset data distribution policy is obtained, and the session associated with the service request is determined.
[0040] The preset data distribution strategy is a strategy configured by a load balancer. For example, each application server node may be polled to distribute the service request; or the service request may be distributed according to the minimum number of connections of the application server node. The resource utilization and response speed are optimized by load balancing technology. When each application server node receives the distributed service request, the Session associated with the service request is determined.
[0041] Step S12: storing the session data corresponding to the session in a preset key-value storage system, so that the preset key-value storage system manages the session data based on a data sharding mechanism and a replication mechanism.
[0042] In an embodiment of the present application, in order to solve the problem of inconsistent Session status in a distributed cluster system, the session data originally distributed on each application server node is centrally stored in a preset key-value storage system, thereby transforming the traditional distributed Session management into centralized management to ensure that the Session status can remain consistent when the user switches between different nodes.
[0043] In a feasible implementation, the preset key-value storage system can be a Redis cluster. Redis is a high-performance key-value storage system that provides atomic operations, persistence, high availability, and rich data structures, and is very suitable for use as a storage medium for Session sharing. Therefore, Session data can be stored in Redis, so that all server instances can access the Session data stored in Redis to achieve global sharing and management of Sessions across application servers, solving the problem of session consistency in a distributed environment. It can be understood that Redis is only a specific implementation of the preset key-value storage system, and other storage solutions with the characteristics of fastness and lightness can be applied to the present invention as preset key-value storage systems.
[0044] It should be noted that when the session data is managed by the preset key-value storage system, the consistency of the data is maintained through a data sharding mechanism and a replication mechanism. Specifically, the session data corresponding to the session is stored in the preset key-value storage system, so that the preset key-value storage system distributes the session data to multiple slots based on the data sharding mechanism to manage the session data; wherein each slot is managed by a corresponding master node, and the slave nodes associated with the master node back up the data in the slot accordingly through a replication mechanism.
[0045] The following steps are all explained with the preset key-value storage system as Redis cluster as an example, which will not be repeated later. The Redis cluster mode is selected to maintain data consistency through data sharding and replication mechanisms. In the Redis cluster, data is divided into multiple slots, each slot is assigned to a specific master node for management, and each master node has one or more slave nodes associated with it, and the slave nodes will copy the data in the master node's slot. Therefore, the session data is stored in different slots, and the master node is responsible for each corresponding slot, and the data is replicated through its slave nodes, so as to store data in a distributed manner on different nodes. This data sharding mechanism enables the Redis cluster to scale horizontally. As the system load increases, the Redis cluster can be horizontally expanded to further enhance the ability of session sharing.
[0046] Specifically, the system load information currently corresponding to the preset key-value storage system is obtained, and the system load information is compared with a preset system load threshold to obtain a comparison result; when it is determined according to the comparison result that the preset key-value storage system meets the preset load expansion condition, the nodes in the preset key-value storage system are horizontally expanded.
[0047] Moreover, based on the master-slave replication mechanism of the Redis cluster, the data of the slots managed by each master node will be copied to multiple slave nodes in the Redis cluster to achieve high availability and failover of the data. This replication mechanism ensures data redundancy and fault recovery capabilities. Even if a master node fails, a complete copy of the data is still stored on its corresponding slave node, which effectively manages the session data while ensuring the consistency and reliability of the Session data. Once a failure of the master node is detected, one of the slave nodes will be elected as the new master node, and the application server will continue to perform relevant operations of the Session data on the new master node. It can be seen that the present invention can automatically perform failover when a node fails, thereby reducing the downtime of the system, avoiding data loss caused by single point failures, and improving the overall stability and reliability of the system.
[0048] Specifically, when it is monitored that a master node in the preset key-value storage system fails, a target node is re-elected from slave nodes in the preset key-value storage system to determine a new master node, and the session data is operated on the new master node.
[0049] It can be seen that Redis's high availability and persistence mechanism ensure the security of Session data. Even if some servers go down, Session data will not be lost, and it is fault-tolerant.
[0050] Step S13: If there is currently an operation request for the session data, the preset key-value storage system is called according to the operation request to perform a corresponding operation on the session data.
[0051] In the embodiment of the present application, since the Session data originally distributed on various application server nodes are uniformly stored in Redis, if there is an operation request for the session data, the Redis cluster will be called to perform Session-related operations on the nodes of the Redis cluster.
[0052] It should be noted that the operation request for the session data includes a read request, a write request, and any one or a combination of operation requests for the creation, update, and destruction of the session data. The creation, update, and destruction operations of each user session are directly operated on Redis, and when the application server needs to read or write Session data, it also directly operates on Redis, so as to ensure centralized management and consistency of data.
[0053] The present application provides a distributed cluster session sharing method, which is applied to each application server node in a distributed cluster, including: obtaining a service request sent by a client, and determining a session associated with the service request; storing the session data corresponding to the session in a preset key-value storage system, so that the preset key-value storage system manages the session data based on a data sharding mechanism and a replication mechanism; if there is currently an operation request for the session data, calling the preset key-value storage system according to the operation request to perform a corresponding operation on the session data. The beneficial technical effect of the present application is: by storing the session data of each application server node in a distributed cluster in a preset key-value storage system, all server instances can access the session data stored in the preset key-value storage system, thereby realizing global sharing of session data, ensuring that the session state can remain consistent when the user switches between different nodes, and solving the session consistency problem in a distributed environment.
[0054] like Figure 2The figure shows a schematic diagram of a distributed cluster Session sharing process based on Redis provided by this embodiment. First, the client sends a request to the load balancer; then the load balancer distributes the request to different application servers according to the configured strategy (such as polling, minimum number of connections, etc.); When the application server needs to read or write Session data, it will call the Redis cluster; the Redis cluster can store the Session data in different slots according to the data sharding mechanism, and the data of each slot will be copied to the slave node in the Redis cluster to achieve high availability and failover of the data. Once the master node is detected to have a failure, one of the slave nodes will be elected as the new master node, and the application server will continue to read and write the Session on the new master node. It can be seen that the high availability and data sharding mechanism of the Redis cluster are used to achieve unified storage and cross-server sharing of Session data. It not only solves the single point failure problem and improves the stability and reliability of the system, but also optimizes resource utilization and response speed through load balancing technology, while ensuring data consistency and security, providing an efficient and scalable Session management solution for distributed applications.
[0055] Correspondingly, the embodiment of the present application also discloses a distributed cluster session sharing device, which is applied to each application server node in the distributed cluster, see Figure 3 As shown, the device comprises:
[0056] The request acquisition module 11 is used to acquire the service request sent by the client and determine the session associated with the service request;
[0057] A session data storage module 12, used for storing the session data corresponding to the session in a preset key-value storage system, so that the preset key-value storage system manages the session data based on a data sharding mechanism and a replication mechanism;
[0058] The session data operation module 13 is used to call the preset key-value storage system to perform corresponding operations on the session data according to the operation request if there is currently an operation request for the session data.
[0059] Among them, for more specific working processes of the above-mentioned modules, please refer to the corresponding contents disclosed in the aforementioned embodiments, which will not be repeated here.
[0060] It can be seen that the above scheme of this embodiment is applied to each application server node in a distributed cluster, including: obtaining the service request sent by the client, and determining the session associated with the service request; storing the session data corresponding to the session in a preset key-value storage system, so that the preset key-value storage system manages the session data based on the data sharding mechanism and the replication mechanism; if there is currently an operation request for the session data, calling the preset key-value storage system according to the operation request to perform corresponding operations on the session data. The beneficial technical effect of the present application is: by storing the session data of each application server node in a distributed cluster in a preset key-value storage system, all server instances can access the session data stored in the preset key-value storage system, realizing global sharing of session data, ensuring that the session state can remain consistent when the user switches between different nodes, and solving the problem of session consistency in a distributed environment.
[0061] Furthermore, the present application also discloses an electronic device. Figure 4 This is a structural diagram of an electronic device 20 according to an exemplary embodiment, and the content in the diagram cannot be considered as any limitation on the scope of use of the present application.
[0062] Figure 4 A schematic diagram of the structure of an electronic device 20 provided in an embodiment of the present application. The electronic device 20 may specifically include: at least one processor 21, at least one memory 22, a power supply 23, a communication interface 24, an input / output interface 25, and a communication bus 26. The memory 22 is used to store a computer program, which is loaded and executed by the processor 21 to implement the relevant steps in the distributed cluster session sharing method disclosed in any of the aforementioned embodiments. In addition, the electronic device 20 in this embodiment may specifically be a server.
[0063] In this embodiment, the power supply 23 is used to provide working voltage for each hardware device on the electronic device 20; the communication interface 24 can create a data transmission channel between the electronic device 20 and the external device, and the communication protocol it follows is any communication protocol that can be applied to the technical solution of the present application, and is not specifically limited here; the input and output interface 25 is used to obtain external input data or output data to the outside world, and its specific interface type can be selected according to specific application needs and is not specifically limited here.
[0064] In addition, the memory 22 as a carrier for resource storage may be a read-only memory, a random access memory, a disk or an optical disk, etc. The resources stored thereon may include an operating system 221, a computer program 222 and data 223, etc. The data 223 may include various data. The storage method may be temporary storage or permanent storage.
[0065] The operating system 221 is used to manage and control the hardware devices and computer programs 222 on the electronic device 20, which can be Windows Server, Unix, Linux, etc. In addition to including a computer program that can be used to complete the distributed cluster session sharing method performed by the electronic device 20 disclosed in any of the aforementioned embodiments, the computer program 222 can further include a computer program that can be used to complete other specific tasks.
[0066] Furthermore, the embodiment of the present application also discloses a computer-readable storage medium, where the computer-readable storage medium includes a random access memory (RAM), a memory, a read-only memory (ROM), an electrically programmable ROM, an electrically erasable programmable ROM, a register, a hard disk, a magnetic disk or an optical disk, or any other form of storage medium known in the technical field. Wherein, when the computer program is executed by the processor, the aforementioned distributed cluster session sharing method is implemented. For the specific steps of the method, reference can be made to the corresponding contents disclosed in the aforementioned embodiment, and no further description will be given here.
[0067] In this specification, each embodiment is described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the embodiments can be referred to each other. For the device disclosed in the embodiment, since it corresponds to the method disclosed in the embodiment, the description is relatively simple, and the relevant parts can be referred to the method part.
[0068] The steps of the distributed cluster session sharing method or algorithm described in conjunction with the embodiments disclosed herein can be implemented directly using hardware, a software module executed by a processor, or a combination of the two. The software module can be placed in a random access memory (RAM), a memory, a read-only memory (ROM), an electrically programmable ROM, an electrically erasable programmable ROM, a register, a hard disk, a removable disk, a CD-ROM, or any other form of storage medium known in the technical field.
[0069] Finally, it should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the presence of other identical elements in the process, method, article or device including the elements.
[0070] The above is a detailed introduction to the distributed cluster session sharing method, device, equipment and medium provided by the present invention. Specific examples are used in this article to illustrate the principles and implementation methods of the present invention. The description of the above embodiments is only used to help understand the method of the present invention and its core idea; at the same time, for general technical personnel in this field, according to the idea of the present invention, there will be changes in the specific implementation method and application scope. In summary, the content of this specification should not be understood as a limitation on the present invention.
Claims
1. A distributed cluster session sharing method, characterized in that: Applicable to each application server node in the distributed cluster, including: Obtaining a service request sent by a client, and determining a session associated with the service request; Storing the session data corresponding to the session in a preset key-value storage system so that the preset key-value storage system manages the session data based on a data sharding mechanism and a replication mechanism; If there is currently an operation request for the session data, the preset key-value storage system is called according to the operation request to perform a corresponding operation on the session data.
2. The distributed cluster session sharing method according to claim 1, characterized in that: The obtaining of the service request sent by the client and determining the session associated with the service request includes: A service request sent by a client through a load balancer based on a preset data distribution strategy is obtained, and a session associated with the service request is determined.
3. The distributed cluster session sharing method according to claim 1, characterized in that: The storing the session data corresponding to the session in a preset key-value storage system so that the preset key-value storage system manages the session data based on a data sharding mechanism and a replication mechanism includes: The session data corresponding to the session is stored in a Redis cluster, so that the Redis cluster manages the session data based on a data sharding mechanism and a replication mechanism.
4. The distributed cluster session sharing method according to claim 1, characterized in that: The storing the session data corresponding to the session in a preset key-value storage system so that the preset key-value storage system manages the session data based on a data sharding mechanism and a replication mechanism includes: The session data corresponding to the session is stored in a preset key-value storage system so that the preset key-value storage system disperses the session data into multiple slots based on a data sharding mechanism to manage the session data; wherein each slot corresponds to a master node for management, and the slave node associated with the master node backs up the data in the slot accordingly through a replication mechanism.
5. The distributed cluster session sharing method according to claim 4, characterized in that: Also includes: Acquire system load information currently corresponding to the preset key-value storage system, and compare the system load information with a preset system load threshold to obtain a comparison result; When it is determined according to the comparison result that the preset key-value storage system meets the preset load expansion condition, the nodes in the preset key-value storage system are horizontally expanded.
6. The distributed cluster session sharing method according to claim 4, characterized in that: Also includes: When it is monitored that a master node in the preset key-value storage system fails, a target node is re-elected from slave nodes in the preset key-value storage system to determine a new master node, and the session data is operated on the new master node.
7. The distributed cluster session sharing method according to any one of claims 1 to 6, characterized in that: If there is currently an operation request for the session data, calling the preset key-value storage system according to the operation request to perform a corresponding operation on the session data includes: If there is currently a read request, a write request for the session data, and any one or a combination of operation requests for creation, update, and destruction of the session, the preset key-value storage system is called according to the operation request to perform corresponding operations on the session data.
8. A distributed cluster session sharing device, characterized in that: Applicable to each application server node in the distributed cluster, including: A request acquisition module, used to acquire a service request sent by a client and determine a session associated with the service request; A session data storage module, used for storing session data corresponding to the session in a preset key-value storage system, so that the preset key-value storage system manages the session data based on a data sharding mechanism and a replication mechanism; The session data operation module is used to call the preset key-value storage system to perform corresponding operations on the session data according to the operation request if there is currently an operation request for the session data.
9. An electronic device, characterized in that: The electronic device comprises a processor and a memory; wherein the memory is used to store a computer program, and the computer program is loaded and executed by the processor to implement the distributed cluster session sharing method according to any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that: Used to store computer programs; wherein when the computer program is executed by a processor, the distributed cluster session sharing method according to any one of claims 1 to 7 is implemented.
Citation Information
Patent Citations
Method and device for sharing session object in distributed cluster
CN109150936A
Sharing method of Session session in distributed cluster system
CN110633256A
Session control sharing method and system in distributed cluster system
CN114979234A
Session data processing method, device and system, electronic equipment and storage medium
CN116155922A
Cited By
Server cluster session management method and device, equipment and storage medium
CN121924120A
Data processing method, system and device, computer equipment and storage medium
CN122222037A