CN111385356A - Distributed session processing method, device and system - Google Patents
Distributed session processing method, device and system Download PDFInfo
- Publication number
- CN111385356A CN111385356A CN202010141939.5A CN202010141939A CN111385356A CN 111385356 A CN111385356 A CN 111385356A CN 202010141939 A CN202010141939 A CN 202010141939A CN 111385356 A CN111385356 A CN 111385356A
- Authority
- CN
- China
- Prior art keywords
- session
- key
- data
- user terminal
- current
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Granted
Links
Images
Classifications
-
- H—ELECTRICITY
- H04—ELECTRIC COMMUNICATION TECHNIQUE
- H04L—TRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
- H04L67/00—Network arrangements or protocols for supporting network services or applications
- H04L67/01—Protocols
- H04L67/10—Protocols in which an application is distributed across nodes in the network
- H04L67/1001—Protocols in which an application is distributed across nodes in the network for accessing one among a plurality of replicated servers
-
- H—ELECTRICITY
- H04—ELECTRIC COMMUNICATION TECHNIQUE
- H04L—TRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
- H04L67/00—Network arrangements or protocols for supporting network services or applications
- H04L67/01—Protocols
- H04L67/10—Protocols in which an application is distributed across nodes in the network
- H04L67/1095—Replication or mirroring of data, e.g. scheduling or transport for data synchronisation between network nodes
-
- H—ELECTRICITY
- H04—ELECTRIC COMMUNICATION TECHNIQUE
- H04L—TRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
- H04L67/00—Network arrangements or protocols for supporting network services or applications
- H04L67/01—Protocols
- H04L67/10—Protocols in which an application is distributed across nodes in the network
- H04L67/1097—Protocols in which an application is distributed across nodes in the network for distributed storage of data in networks, e.g. transport arrangements for network file system [NFS], storage area networks [SAN] or network attached storage [NAS]
-
- H—ELECTRICITY
- H04—ELECTRIC COMMUNICATION TECHNIQUE
- H04L—TRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
- H04L67/00—Network arrangements or protocols for supporting network services or applications
- H04L67/14—Session management
- H04L67/142—Managing session states for stateless protocols; Signalling session states; State transitions; Keeping-state mechanisms
-
- H—ELECTRICITY
- H04—ELECTRIC COMMUNICATION TECHNIQUE
- H04L—TRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
- H04L67/00—Network arrangements or protocols for supporting network services or applications
- H04L67/14—Session management
- H04L67/146—Markers for unambiguous identification of a particular session, e.g. session cookie or URL-encoding
Landscapes
- Engineering & Computer Science (AREA)
- Computer Networks & Wireless Communication (AREA)
- Signal Processing (AREA)
- Computer And Data Communications (AREA)
Abstract
The embodiment of the invention provides a distributed session processing method, a distributed session processing device and a distributed session processing system, which relate to the technical field of cloud computing, wherein the method comprises the following steps: acquiring a session request sent by a user terminal, wherein the session request carries an identity of the user terminal; generating a locking key according to the identity of the user terminal, and acquiring a distributed lock corresponding to the locking key; generating a session key according to the identity, and detecting whether session data corresponding to the session key exists in a preset database by using the session key; if not, creating a current session corresponding to the session request; and when the current session is finished, generating a session aging key according to the identity, and backing up the session data and the session aging key generated by the current session to a preset database. The technical scheme provided by the embodiment of the invention can solve the problem that the session data cannot be acquired after the session expires in the prior art.
Description
[ technical field ] A method for producing a semiconductor device
The invention relates to the technical field of cloud computing, in particular to a distributed session processing method, device and system.
[ background of the invention ]
In the existing distributed session system, when a request is distributed to a certain server in a cluster in a load balancing manner, multiple requests of the same user are easily distributed to different servers of the cluster, so that the situation that session data cannot be acquired occurs. And recording management needs to be performed on the session due to business needs, and session data cannot be acquired after the session is expired.
[ summary of the invention ]
In view of this, embodiments of the present invention provide a distributed session processing method, apparatus, and system, so as to solve the problem in the prior art that session data cannot be acquired after a session expires.
In order to achieve the above object, according to an aspect of the present invention, there is provided a distributed session processing method, the method including:
acquiring a session request sent by a user terminal, wherein the session request carries an identity of the user terminal; generating a locking key according to the identity of the user terminal, and acquiring a distributed lock corresponding to the locking key; generating a session key according to the identity, and detecting whether session data corresponding to the session key exists in a preset database by using the session key; if not, creating a current session corresponding to the session request; and when the current session is finished, generating a session aging key according to the identity, and backing up the session data generated by the current session and the session aging key to the preset database, wherein the session aging key is used for defining the validity period of the session, and the validity period of the session key is greater than that of the session aging key.
Optionally, after generating a session key according to the identity and detecting whether session data corresponding to the session key exists in a preset database by using the session key, the method further includes: if so, acquiring the session data of the previous session of the user terminal and a session aging key of the user terminal stored in association with the session data from the preset database; determining whether the session data of the user terminal is valid data according to a session aging key of the user terminal; and if so, reading the session data of the user terminal and applying the session data to the current session.
Optionally, after the session data of the user terminal is read and applied to the current session, the method further includes: acquiring the session data and the session aging key of the previous session of the user terminal stored in the database; comparing whether the session data of the current session is consistent with the session data of the previous session; if not, merging the session data of the current session and the session data of the previous session; and backing up the merged session data to the database, and updating the session aging key of the previous session to the session aging key of the current session.
Optionally, the determining, according to the session aging key of the previous session, whether the session data of the user terminal is valid data includes: acquiring current time; determining whether the current time is within an effective period defined in a session aging key of the user terminal; when the current time exceeds the validity period, the session data of the user terminal is invalid data; and when the current time does not exceed the validity period, the session data of the user terminal is valid data.
Optionally, after the session aging key is generated according to the identity when the current session is completed, and the session data and the session aging key generated by the current session are backed up to the preset database, the method further includes: monitoring and filtering out an overdue event related to a session aging key in the preset database; obtaining the identity of the user terminal corresponding to the expired session aging key and the session validity period according to a preset mapping relation; generating the locking key according to the identity of the user terminal, and acquiring a distributed lock corresponding to the locking key; obtaining the session key according to the identity of the user terminal, and reading the session data stored in the preset database by using the session key; and recording the ending time of the session key and the session data in the preset database, and deleting the session key.
Optionally, after the generating a locking key according to the identity of the user terminal and acquiring the distributed lock corresponding to the locking key, the method further includes:
performing mutual exclusion operation by using the acquired distributed lock; before each mutual exclusion operation, judging whether the distributed lock is expired; if the time is out of date, the mutual exclusion operation is abandoned and corresponding lock losing processing is carried out; and if not, performing current mutual exclusion operation until the lock grabbing succeeds, and feeding back the lock grabbing success information to the user terminal through a preset interface.
Optionally, when the current session is completed, generating a session aging key according to the identity, and backing up the session data generated by the current session and the session aging key to the preset database includes: generating a session identifier according to the timestamp of the current session when the current session is created and the identity identifier of the user, and storing the session identifier into session data generated by the current session; generating a session aging key according to the identity of the user; and backing up the session data generated by the current session and the session aging key to a preset database.
In order to achieve the above object, according to an aspect of the present invention, there is provided a distributed session processing apparatus, the apparatus including:
the first obtaining unit is used for obtaining a session request sent by a user terminal, wherein the session request carries an identity of the user terminal;
the first generating unit is used for generating a locking key according to the identity of the user terminal and acquiring a distributed lock corresponding to the locking key;
the second generation unit is used for generating a session key according to the identity and detecting whether session data corresponding to the session key exists in a preset database or not by using the session key;
a creating unit, configured to create, if not present, a current session corresponding to the session request;
and the processing unit is used for generating a session aging key according to the identity identifier when the current session is completed, and backing up the session data generated by the current session and the session aging key to the preset database, wherein the session aging key is used for defining the validity period of the session, and the validity period of the session key is greater than that of the session aging key.
To achieve the above object, according to one aspect of the present invention, there is provided a distributed session processing system including one or more session servers and a distributed database, wherein the session servers include a memory, a processor and a computer program stored in the memory and executable on the processor, and the processor implements the steps of the distributed session processing method when executing the computer program.
In order to achieve the above object, according to an aspect of the present invention, there is provided a computer nonvolatile storage medium including a stored program that, when executed, controls a device in which the storage medium is located to execute the above-described distributed session processing method.
In the scheme, a distributed database (redis database) is established based on a distributed session management system to provide a service for reading and storing session data, when a user accesses the server through a user terminal, the server generates a lock key (lock: user ID) according to an identity (namely a user ID) of the user, and since the identity of the user is unique, the distributed lock is obtained by using the lock key, the distributed lock is used for preventing coverage caused by repeatedly creating sessions with the same user ID during concurrency, and a session key and a session aging key are introduced, so that the validity period of the session key is set longer than the session aging key, and the corresponding session data can be obtained when the session aging key is overtime.
[ description of the drawings ]
In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings needed to be used in the embodiments will be briefly described below, and it is obvious that the drawings in the following description are only some embodiments of the present invention, and it is obvious for those skilled in the art to obtain other drawings based on these drawings without inventive labor.
FIG. 1 is an architecture diagram of an alternative distributed session processing system provided by embodiments of the present invention;
fig. 2 is a flowchart of an alternative distributed session processing method according to an embodiment of the present invention;
fig. 3 is a functional block diagram of an alternative distributed session processing apparatus according to an embodiment of the present invention;
fig. 4 is a schematic diagram of an alternative session server according to an embodiment of the present invention.
[ detailed description ] embodiments
For better understanding of the technical solutions of the present invention, the following detailed descriptions of the embodiments of the present invention are provided with reference to the accompanying drawings.
It should be understood that the described embodiments are only some embodiments of the invention, and not all embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
The terminology used in the embodiments of the invention is for the purpose of describing particular embodiments only and is not intended to be limiting of the invention. As used in the examples of the present invention and the appended claims, the singular forms "a," "an," and "the" are intended to include the plural forms as well, unless the context clearly indicates otherwise.
It should be understood that the term "and/or" as used herein is merely one type of association that describes an associated object, meaning that three relationships may exist, e.g., a and/or B may mean: a exists alone, A and B exist simultaneously, and B exists alone. In addition, the character "/" herein generally indicates that the former and latter related objects are in an "or" relationship.
It should be understood that although the terms first, second, third, etc. may be used to describe the terminals in the embodiments of the present invention, the terminals should not be limited by these terms. These terms are only used to distinguish one terminal from another. For example, a first terminal may also be referred to as a second terminal, and similarly, a second terminal may also be referred to as a first terminal, without departing from the scope of embodiments of the present invention.
The word "if" as used herein may be interpreted as "at … …" or "when … …" or "in response to a determination" or "in response to a detection", depending on the context. Similarly, the phrases "if determined" or "if detected (a stated condition or event)" may be interpreted as "when determined" or "in response to a determination" or "when detected (a stated condition or event)" or "in response to a detection (a stated condition or event)", depending on the context.
For convenience of understanding, an application scenario to which the embodiments of the present specification may be applied is introduced first, and please refer to fig. 1, which is a schematic structural diagram of a distributed session system provided in the embodiments of the present specification. The distributed session system includes one or more session servers (session server clusters) 100, user terminals 200, a distributed database 300, and a load balancer 400.
It can be understood that the session request of the user terminal 200 is distributed to a session server in the session server cluster in a load balancing manner, which may cause that multiple requests of the same user are distributed to different session servers, and the session data (session data) cannot be obtained.
The session synchronization refers to the mutual synchronization of a plurality of servers, each server contains all sessions, the bandwidth consumption of the network is high, the data volume is limited by the memory, and the horizontal expansion cannot be realized.
The user terminal storage method is to store the session in the cookie of the browser, and the session needs to be transmitted every request, so that the bandwidth consumption of the foreign network is high, and the session is easy to be tampered.
The reverse proxy hash consistency is that the reverse proxy server utilizes the website of the user to perform hash calculation to ensure that the request of the same website is distributed to the same server, and by the scheme, if the server is restarted, part of session may be lost, or after the server is horizontally expanded, the request cannot be distributed to the original server after the hash calculation is performed again according to the website.
The backend centralized storage method is to store the session in a storage system behind a server. Compared with the previous schemes, the scheme can be horizontally expanded, the session cannot be lost when the server is restarted or expanded, but the problem of repeated session creation or session coverage caused by multiple incoming lines of the same user cannot be guaranteed in concurrent access.
Illustratively, the user terminal 200 is an electronic device such as a mobile phone, a tablet computer, a Personal Computer (PC), a Personal Digital Assistant (PDA), a netbook, and the like. And are not limited herein.
Fig. 2 is a flowchart of a distributed session processing method according to an embodiment of the present invention, applied to the session server 100, as shown in fig. 2, the method includes:
step S01, obtaining a session request sent by a user terminal, wherein the session request carries an identity of the user terminal;
step S02, generating a locking key according to the identity of the user terminal, and acquiring a distributed lock corresponding to the locking key;
step S03, generating a session key according to the identity, and detecting whether session data corresponding to the session key exists in a preset database by using the session key;
step S04, if not, creating a current session corresponding to the session request;
and step S05, when the current session is completed, generating a session aging key according to the identity, and backing up the session data and the session aging key generated by the current session to a preset database, wherein the session aging key is used for defining the validity period of the session, and the validity period of the session key is greater than that of the session aging key.
Specifically, the session data is stored in the database in a key-value form, the session key is a key, and the session data is a value.
Sessions are used to track sessions for each user, identified using a key generated by the Session server 100 to distinguish between users. The Session data is stored in a memory (redis database) of the server, so that when a user sends a Session request, the Session server 100 generates a key using the user ID, and obtains a distributed lock using the key, thereby finding the Session data corresponding to the user for operation.
When the user terminal 200 accesses the session server 100, the session server 100 generates a lock key (lock: userid) according to the identity (i.e. user ID) of the user, and since the identity of the user is unique, a distributed lock is obtained by using the lock key, the distributed lock is to prevent the coverage caused by repeatedly creating sessions with the same user ID during concurrency, and solve the problem that the session data of the session request which is ended later in two requests covers the session data of the session request which is ended first due to the same session data being loaded simultaneously in the two session requests; and a session key and a session aging key are introduced, and the validity period of the session key is set to be longer than that of the session aging key, so that the corresponding session data can be acquired when the session aging key is overtime.
The method is established based on a distributed session system, a preset database provides reading and storing services of session data for a distributed database (redis database), the session data of each user is stored in the redis database as key-value pairs, the key is a session key, and the value is the session data required to be stored by the user. It can be appreciated that using a redis database to store session data centrally and uniformly does not need to worry about requests being distributed to different servers resulting in failure to route to session data. Moreover, the user terminal does not need to store the session data, the redis database is convenient for horizontal expansion, and the session data can not be lost after the server is restarted.
The following describes in detail a specific technical solution of the distributed session processing method provided in this embodiment.
Step S01, acquiring a session request sent by the user terminal, where the session request carries an identity of the user terminal.
In this embodiment, the request is a session request, and in other embodiments, the request may also be a registration request or a logout request.
The user terminal may be a first user terminal (mobile phone), a second user terminal (notebook computer), or a third user terminal (desktop computer), and of course, the first user terminal, the second user terminal, and the third user terminal may all be mobile phones. In this embodiment, when multiple user terminals access a server at the same time, the problem of session coverage occurring when multiple user terminals enter a line at the same time is avoided by using a distributed lock technology.
The session request may be, for example, an http request, and when the user terminal initiates the http request, a cookie is taken on a header of the http request, and the cookie includes an identity (user ID) of the user.
And step S02, generating a locking key according to the identity of the user terminal, and acquiring the distributed lock corresponding to the locking key.
The user terminal can only safely access the server on the premise of acquiring the distributed lock. The lock key has globally unique characteristics and can therefore be used as a key for a distributed lock. In this embodiment, the key is a key generated based on the identity of the user, that is, the key and the distributed lock have a mapping relationship.
In one embodiment, the distributed lock is stored in a distributed database, and when the server receives the request, the server triggers the distributed lock to rob the lock creation operation.
The validity period of the distributed lock is a Session (Session) validity period of the user terminal, and the Session validity period can be obtained through an interface (Session expiration Time form API) for obtaining the expiration Time, and the interface returns the expiration Time of the Session of the user terminal, that is, the validity period of the distributed lock can be obtained. For example, the validity period of the session is set to 30 minutes, that is, the maximum inactivity interval of the session is set to 30 minutes, if the system has no activity within 30 minutes (no http request), the session expires, and if the system has an http request, the session expires after 30 minutes (that is, the expiration time is increased by 30 minutes based on the request time). Then the validity period of the distributed lock is also 30 minutes.
Through the distributed locking technology, the exclusivity of session data in the whole session can be realized, and the coverage of other terminals on the same session data is avoided.
Further, after step S02, the method further includes:
performing mutual exclusion operation by using the acquired distributed lock;
before each mutual exclusion operation, judging whether the distributed lock is expired;
if the time is out of date, the mutual exclusion operation is abandoned and corresponding lock losing processing is carried out;
and if not, performing current mutual exclusion operation until the lock grabbing succeeds, and feeding back the lock grabbing success information to the user terminal through a preset interface.
Specifically, in the process of preempting the lock, the session server 100 will first determine whether the distributed lock has expired; if the time is out of date, the mutual exclusion operation is abandoned and corresponding lock losing processing is carried out; and if not, carrying out mutual exclusion operation until the lock robbing is successful. Judging whether the distributed lock is expired, directly comparing the current time with the validity period of the distributed lock, and when the current time exceeds the validity period of the distributed lock, determining that the distributed lock is expired currently and the lock is lost when the current time is expired.
It can be understood that, after the session server cluster receives the session request message, the session request message is distributed to the server corresponding to the current session of the user terminal, and the server receives the session-related request. Specifically, a mechanism of a second write failure of the same lock, that is, mapping between one lock key and one distributed lock, is used to determine whether the session can acquire the distributed lock, and if the session cannot acquire the distributed lock, the state of attempting to acquire the distributed lock is maintained until the distributed lock is acquired, and when the state of attempting to acquire the distributed lock is maintained, the corresponding thread of the server is also in a blocking state.
Further, when the session server acquires the distributed lock, the server can set the time efficiency of the distributed lock, and if the time is out, the server does not release the distributed lock, the distributed lock is automatically released, so that deadlock is avoided.
After the distributed lock is obtained, if other requests apply for the distributed lock of the same session data, the obtaining fails before the server releases the distributed lock, so that the server enters a blocking state, waits for the distributed lock, and ensures the consistency of the session data. It can be understood that, when the lock key generated based on the user's id is used to obtain the distributed lock, it can be ensured that only one session exists at the same time for the same user.
And step S03, generating a session key according to the identity, and detecting whether session data corresponding to the session key exists in a preset database by using the session key.
The session data is stored in the database in a key-value form, the session key is a key, and the session data is a value.
The session key is a key generated based on an identity (user ID) of a user, and is "userid," for example: sessionkey is "123456". The validity period of the session key is slightly longer than the validity period of the session. The validity period of the session is obtained by the negotiation between the user terminal and the server, for example, the validity period of the session key is 40 minutes, and the validity period of the session is 30 minutes. The validity period of the Session may be obtained using an API interface in the form of Session Expire Time.
It can be understood that the server acquires the current session data according to the session key generated by the user ID. If the session data exists, the server represents that the requested user logs in, and the server returns the requested session data to the user terminal according to the authority of the logged-in user.
In the embodiment, the redis database is adopted to store the session data in a unified and centralized manner, so that the problem that the request is distributed to different servers to cause the request cannot be routed to the session data is avoided. The distributed session management mechanism based on redis utilizes the session key as a key to search corresponding session data, and since the redis database is stored in a memory, the access efficiency is high, and the performance is good, the reading speed is high under the condition of high concurrency of large users, and the system access efficiency can be effectively improved in the face of multi-user supported clusters.
Step S04, if not present, creates a current session corresponding to the session request. I.e. a session is newly established.
It will be appreciated that, when a session is created, the session data for the current session is by default backed up into the database. In this embodiment, the session data includes a session identifier, and the session identifier is generated according to the identity identifier of the user and a timestamp during session creation, and is used to identify the same session of the same user.
Step S05, when the current conversation is finished, generating a session aging key according to the identity, and backing up the session data and the session aging key generated by the current conversation to a preset database, including:
generating a session identifier according to a timestamp of the current session when the current session is created and an identity identifier of a user, and storing the session identifier into session data generated by the current session;
generating a session aging key according to the identity of the user;
and backing up the session data and the session aging key generated by the current session to a preset database.
It can be understood that a session validity key is introduced, the validity period of the session is used as the validity period of the session validity key, and when the session data is stored, the session data is stored in the database in a key-value form, and the validity period of the session key is greater than the validity period of the session, i.e., greater than the validity period of the session validity key, so that it is ensured that the corresponding session data can be acquired for data recording when the session validity key is overtime. In the embodiment, the key-lock, the session key and the session aging key are generated based on the user identification, so that different users are ensured not to be affected by each other, and only one session (session) can exist in the same user at the same time.
After step S03, the method further includes:
and step S06, if yes, acquiring the session data of the previous session of the user terminal and the session aging key of the user terminal stored in association with the session data from a preset database.
For example, the session data of the user is already stored in the redis database, which proves that the session is not a newly-established session, and the session data in the redis database can be applied to the current session.
And step S07, determining whether the session data of the user terminal is valid data according to the session aging key of the user terminal.
And step S08, if yes, reading session data of the user terminal and applying the session data to the current session.
When the user has created session data, it is necessary to ensure that the session data in the redis database is normal, i.e., not expired or stale. When the life cycle of the session data itself is up, the session data needs to be cleared from the redis database.
Further, the step of determining whether the session data of the user terminal is valid data according to the session aging key of the previous session includes:
acquiring current time; determining whether the current time is within an effective period defined in a session aging key of the user terminal; if the current time exceeds the validity period, the session data of the user terminal is invalid data; and if the current time does not exceed the validity period, the session data of the user terminal is valid data.
For example, the effective deadline defined by the session aging key is 2020-9-1-10-30+30 minutes, i.e. the request time is 10 am and half am on 9/1/2020, the session aging key is 2020-9-1-11-00, i.e. 11 am on 9/1/2020, and if the current time is 2020-9-1-10-45, then the session data is valid data in the valid period.
In this embodiment, the validity period of the session key is greater than the validity period of the session aging key.
After step S08, the method further includes: acquiring session data and a session aging key of a previous session of a user terminal, which are stored in a database; comparing whether the session data of the current session is consistent with the session data of the previous session; if not, merging the session data of the current session and the session data of the previous session; and backing up the merged session data to a database, and updating the session aging key of the previous session to the session aging key of the current session.
It can be understood that, when the session data of the previous session is stored in the database, when the latest session of the user is ended, the session data and the session aging key should be updated, where the update of the session aging key refers to updating the time limit value in the session aging key, for example, the session aging key of the previous session is 2020-9-1-10-30+30 minutes, and the session aging key of the current session is 2020-9-25+30 minutes, then the session aging key of the updated session data is 2020-9-25+30 minutes.
Further, after step S05, the method further includes:
monitoring and filtering out an overdue event related to a session aging key in a preset database;
obtaining the identity of the user terminal corresponding to the expired session aging key and the session validity period according to a preset mapping relation;
generating a locking key according to the identity of the user terminal, and acquiring a distributed lock corresponding to the locking key;
obtaining a session key according to the identity of the user terminal, and reading session data stored in a preset database by using the session key;
and recording the ending time of the session key and the session data in a preset database, and deleting the session key.
It can be understood that the user just enters the line by monitoring the expiration event of the session aging key, which results in inaccurate session status records in the database.
An embodiment of the present invention provides a distributed session processing apparatus, as shown in fig. 3, the apparatus includes a first obtaining unit 10, a first generating unit 20, a second generating unit 30, a creating unit 40, and a processing unit 50.
A first obtaining unit 10, configured to obtain a session request sent by a user terminal, where the session request carries an identity of the user terminal;
the first generating unit 20 is configured to generate a locking key according to the identity of the user terminal, and acquire a distributed lock corresponding to the locking key;
the second generating unit 30 is configured to generate a session key according to the identity, and detect whether session data corresponding to the session key exists in a preset database by using the session key;
a creating unit 40 configured to create, if not, a current session corresponding to the session request;
and the processing unit 50 is configured to generate a session aging key according to the identity when the current session is completed, and backup the session data and the session aging key generated by the current session to a preset database, where the session aging key is used to define a validity period of the session, and the validity period of the session key is greater than that of the session aging key.
Specifically, the session data is stored in the database in a key-value form, the session key is a key, and the session data is a value.
Sessions are used to track each user's Session, identified using a server-generated key lock, to distinguish between users. The Session data is stored in a memory (redis database) of the server, so that when a user sends a Session request, the server generates a key lock by using the user ID, and obtains a distributed lock by using the key lock, thereby finding the Session data corresponding to the user for operation.
When a user accesses a server through a user terminal, the server generates a lock key (lock: userid) according to the identity (namely the user ID) of the user, and the identity of the user is unique, so that a distributed lock is obtained by using the lock key, the distributed lock is used for preventing the coverage caused by repeatedly creating sessions with the same user ID during concurrency, and the problem that the session data of the session request which is ended later in two requests covers the session data of the session request which is ended first in the two requests because the same session data is simultaneously loaded in the two session requests is solved.
The method is established based on a distributed session system, a preset database provides reading and storing services of session data for a distributed database (redis database), the session data of each user is stored in the redis database as key-value pairs, the key is a session key, and the value is the session data required to be stored by the user. It can be appreciated that using a redis database to store session data centrally and uniformly does not need to worry about requests being distributed to different servers resulting in failure to route to session data. Moreover, the user terminal does not need to store the session data, the redis database is convenient for horizontal expansion, and the session data can not be lost after the server is restarted.
The processing unit 50 includes a storage subunit, a first generation subunit, and a first backup subunit.
The storage subunit is used for generating a session identifier according to the timestamp of the current session when the session is created and the identity identifier of the user, and storing the session identifier into session data generated by the current session;
the first generation subunit is used for generating a session aging key according to the identity of the user;
and the first backup subunit is used for backing up the session data and the session aging key generated by the current session to a preset database.
It can be understood that a session validity key is introduced, the validity period of the session is used as the validity period of the session validity key, and when the session data is stored, the session data is stored in the database in a key-value form, and the validity period of the session key is greater than the validity period of the session, i.e., greater than the validity period of the session validity key, so that it is ensured that the corresponding session data can be acquired for data recording when the session validity key is overtime. In the embodiment, the key-lock, the session key and the session aging key are generated based on the user identification, so that different users are ensured not to be affected by each other, and only one session (session) can exist in the same user at the same time.
The device also comprises a second acquisition unit, a determination unit and a reading unit.
And the second acquisition unit is used for acquiring the session data of the previous session of the user terminal and the session aging key of the user terminal stored in association with the session data from a preset database if the session data of the previous session of the user terminal exists.
For example, the session data of the user is already stored in the redis database, which proves that the session is not a newly-established session, and the session data in the redis database can be applied to the current session.
And the determining unit is used for determining whether the session data of the user terminal is valid data according to the session aging key of the user terminal.
And the reading unit is used for reading the session data of the user terminal and applying the session data to the current session if the session data is positive.
When the user has created session data, it is necessary to ensure that the session data in the redis database is normal, i.e., not expired or stale. When the life cycle of the session data itself is up, the session data needs to be cleared from the redis database.
Further, the determining unit comprises an acquiring subunit, a determining subunit, a first confirming subunit and a second confirming subunit.
An obtaining subunit, configured to obtain a current time; a determining subunit, configured to determine whether the current time is within a validity period defined in a session validity key of the user terminal; the first confirming subunit is used for determining that the session data of the user terminal is invalid data if the current time exceeds the validity period; and the second confirming subunit is used for determining that the session data of the user terminal is valid data if the current time does not exceed the validity period.
For example, the effective deadline defined by the session aging key is 2020-9-1-10-30+30 minutes, i.e. the request time is 10 am and half am on 9/1/2020, the session aging key is 2020-9-1-11-00, i.e. 11 am on 9/1/2020, and if the current time is 2020-9-1-10-45, then the session data is valid data in the valid period.
Further, the device also comprises a monitoring unit, a third acquisition unit and a second processing unit.
The monitoring unit is used for monitoring and filtering out an overdue event related to a session aging key in a preset database;
the third obtaining unit is used for obtaining the identity of the user terminal corresponding to the expired session aging key and the session validity period according to the preset mapping relation;
the first generating unit 20 is configured to generate a locking key according to the identity of the user terminal, and acquire a distributed lock corresponding to the locking key;
a second generating unit 30, configured to obtain a session key according to the identity of the user terminal, and read session data stored in a preset database by using the session key;
and the second processing unit is used for recording the ending time of the session key and the session data in a preset database and deleting the session key.
It can be understood that the user just enters the line by monitoring the expiration event of the session aging key, which results in inaccurate session status records in the database.
The embodiment of the invention provides a non-volatile storage medium of a computer, wherein the storage medium comprises a stored program, and when the program runs, equipment where the storage medium is located is controlled to execute the following steps:
acquiring a session request sent by a user terminal, wherein the session request carries an identity of the user terminal; generating a locking key according to the identity of the user terminal, and acquiring a distributed lock corresponding to the locking key; generating a session key according to the identity, and detecting whether session data corresponding to the session key exists in a preset database by using the session key; if not, creating a current session corresponding to the session request; when the current session is finished, generating a session aging key according to the identity, and backing up the session data and the session aging key generated by the current session to a preset database, wherein the session aging key is used for defining the validity period of the session, and the validity period of the session key is greater than that of the session aging key
Optionally, after controlling, during the running of the program, an apparatus in which the storage medium is located to execute a process of generating a session key according to the identity, and detecting whether session data corresponding to the session key exists in a preset database by using the session key, the method includes:
if the session data exists, acquiring the session data of the previous session of the user terminal and a session aging key of the user terminal stored in association with the session data from a preset database; determining whether the session data of the user terminal is valid data according to a session aging key of the user terminal; if yes, the session data of the user terminal is read and applied to the current session.
Optionally, after controlling the device in which the storage medium is located to read session data of the user terminal and apply the session data to the current session when the program runs, the method includes: acquiring session data and a session aging key of a previous session of a user terminal, which are stored in a database; comparing whether the session data of the current session is consistent with the session data of the previous session; if not, merging the session data of the current session and the session data of the previous session; and backing up the merged session data to a database, and updating the session aging key of the previous session to the session aging key of the current session.
Optionally, when the program runs, controlling the device where the storage medium is located to execute, when the current session is completed, generating a session aging key according to the identity, and after the session data and the session aging key generated by the current session are backed up to a preset database, the method includes:
monitoring and filtering out an overdue event related to a session aging key in a preset database; obtaining the identity of the user terminal corresponding to the expired session aging key and the session validity period according to a preset mapping relation; generating a locking key according to the identity of the user terminal, and acquiring a distributed lock corresponding to the locking key; obtaining a session key according to the identity of the user terminal, and reading session data stored in a preset database by using the session key; and recording the ending time of the session key and the session data in a preset database, and deleting the session key.
Optionally, when the program runs, controlling the device where the storage medium is located to execute generating a session aging key according to the identity, and backing up session data and the session aging key generated by the current session to a preset database includes:
generating a session identifier according to the timestamp of the current session when the session is created and the identity identifier of the user, and storing the session identifier into session data generated by the current session; generating a session aging key according to the identity of the user; and backing up the session data and the session aging key generated by the current session to a preset database.
Fig. 4 is a schematic diagram of a session server according to an embodiment of the present invention. As shown in fig. 4, the session server 100 of this embodiment includes: the processor 101, the memory 102, and the computer program 103 stored in the memory 102 and capable of running on the processor 101, where the processor 101 implements the distributed session processing method in the embodiment when executing the computer program 103, and in order to avoid repetition, details are not repeated here. Alternatively, the computer program is executed by the processor 101 to implement the functions of each model/unit in the distributed session processing apparatus in the embodiment, which are not described herein again to avoid redundancy.
The session server 100 may be a computing device such as a desktop computer, a notebook, a palm top computer, and a cloud server. The computer device may include, but is not limited to, a processor 101, a memory 102. Those skilled in the art will appreciate that fig. 4 is merely an example of a computing device 100 and is not intended to limit the computing device 100 and that it may include more or less components than those shown, or some of the components may be combined, or different components, e.g., the computing device may also include input output devices, network access devices, buses, etc.
The Processor 101 may be a Central Processing Unit (CPU), other general purpose Processor, a Digital Signal Processor (DSP), an Application Specific Integrated Circuit (ASIC), a Field Programmable Gate Array (FPGA) or other Programmable logic device, discrete Gate or transistor logic device, discrete hardware component, or the like. A general purpose processor may be a microprocessor or the processor may be any conventional processor or the like.
The storage 102 may be an internal storage unit of the computer device 100, such as a hard disk or a memory of the computer device 100. The memory 102 may also be an external storage device of the computer device 100, such as a plug-in hard disk, a Smart Media Card (SMC), a Secure Digital (SD) Card, a Flash memory Card (Flash Card), etc., provided on the computer device 100. Further, the memory 102 may also include both internal storage units and external storage devices of the computer device 100. The memory 102 is used for storing computer programs and other programs and data required by the computer device. The memory 102 may also be used to temporarily store data that has been output or is to be output.
It is clear to those skilled in the art that, for convenience and brevity of description, the specific working processes of the above-described systems, apparatuses and units may refer to the corresponding processes in the foregoing method embodiments, and are not described herein again.
In the embodiments provided in the present invention, it should be understood that the disclosed system, apparatus and method may be implemented in other ways. For example, the above-described apparatus embodiments are merely illustrative, and for example, the division of the units is only one logical division, and there may be other divisions in actual implementation, for example, a plurality of units or components may be combined or integrated into another system, or some features may be omitted, or not executed. In addition, the shown or discussed mutual coupling or direct coupling or communication connection may be an indirect coupling or communication connection through some interfaces, devices or units, and may be in an electrical, mechanical or other form.
The units described as separate parts may or may not be physically separate, and parts displayed as units may or may not be physical units, may be located in one place, or may be distributed on a plurality of network units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution of the embodiment.
In addition, functional units in the embodiments of the present invention may be integrated into one processing unit, or each unit may exist alone physically, or two or more units are integrated into one unit. The integrated unit can be realized in a form of hardware, or in a form of hardware plus a software functional unit.
The integrated unit implemented in the form of a software functional unit may be stored in a computer readable storage medium. The software functional unit is stored in a storage medium and includes several instructions for causing a computer device (which may be a personal computer, a server, or a network device) or a Processor (Processor) to execute some steps of the methods according to the embodiments of the present invention. And the aforementioned storage medium includes: various media capable of storing program codes, such as a usb disk, a removable hard disk, a Read-Only Memory (ROM), a Random Access Memory (RAM), a magnetic disk, or an optical disk.
The above description is only for the purpose of illustrating the preferred embodiments of the present invention and is not to be construed as limiting the invention, and any modifications, equivalents, improvements and the like made within the spirit and principle of the present invention should be included in the scope of the present invention.
Claims (10)
1. A distributed session processing method, the method comprising:
acquiring a session request sent by a user terminal, wherein the session request carries an identity of the user terminal;
generating a locking key according to the identity of the user terminal, and acquiring a distributed lock corresponding to the locking key;
generating a session key according to the identity, and detecting whether session data corresponding to the session key exists in a preset database by using the session key;
if not, creating a current session corresponding to the session request;
and when the current session is finished, generating a session aging key according to the identity, and backing up the session data generated by the current session and the session aging key to the preset database, wherein the session aging key is used for defining the validity period of the session, and the validity period of the session key is greater than that of the session aging key.
2. The method of claim 1, wherein after generating a session key according to the identity and detecting whether session data corresponding to the session key exists in a preset database by using the session key, the method further comprises:
if so, acquiring the session data of the previous session of the user terminal and a session aging key of the user terminal stored in association with the session data from the preset database;
determining whether the session data of the user terminal is valid data according to a session aging key of the user terminal;
and if so, reading the session data of the user terminal and applying the session data to the current session.
3. The method of claim 2, wherein after reading session data of the user terminal and applying the session data in the current session, the method further comprises:
acquiring the session data and the session aging key of the previous session of the user terminal stored in the database;
comparing whether the session data of the current session is consistent with the session data of the previous session;
if not, merging the session data of the current session and the session data of the previous session;
and backing up the merged session data to the database, and updating the session aging key of the previous session to the session aging key of the current session.
4. The method of claim 2, wherein the determining whether the session data of the ue is valid data according to the session aging key of the previous session comprises:
acquiring current time;
determining whether the current time is within an effective period defined in a session aging key of the user terminal;
when the current time exceeds the validity period, the session data of the user terminal is invalid data;
and when the current time does not exceed the validity period, the session data of the user terminal is valid data.
5. The method according to claim 1, wherein after the generating a session aging key according to the identity when the current session is completed, and backing up session data and the session aging key generated by the current session to the preset database, the method further comprises:
monitoring and filtering out an overdue event related to a session aging key in the preset database;
obtaining the identity of the user terminal corresponding to the expired session aging key and the session validity period according to a preset mapping relation;
generating the locking key according to the identity of the user terminal, and acquiring a distributed lock corresponding to the locking key;
obtaining the session key according to the identity of the user terminal, and reading the session data stored in the preset database by using the session key;
and recording the ending time of the session key and the session data in the preset database, and deleting the session key.
6. The method according to claim 1, wherein after the generating a key according to the identity of the user terminal and acquiring the distributed lock corresponding to the key, the method further comprises:
performing mutual exclusion operation by using the acquired distributed lock;
before each mutual exclusion operation, judging whether the distributed lock is expired;
if the time is out of date, the mutual exclusion operation is abandoned and corresponding lock losing processing is carried out;
and if not, performing current mutual exclusion operation until the lock grabbing succeeds, and feeding back the lock grabbing success information to the user terminal through a preset interface.
7. The method according to any one of claims 1 to 6, wherein when the current session is completed, generating a session aging key according to the identity, and backing up session data generated by the current session and the session aging key to the preset database includes:
generating a session identifier according to the timestamp of the current session when the current session is created and the identity identifier of the user, and storing the session identifier into session data generated by the current session;
generating a session aging key according to the identity of the user;
and backing up the session data generated by the current session and the session aging key to a preset database.
8. A distributed session processing apparatus, the apparatus comprising:
the first obtaining unit is used for obtaining a session request sent by a user terminal, wherein the session request carries an identity of the user terminal;
the first generating unit is used for generating a locking key according to the identity of the user terminal and acquiring a distributed lock corresponding to the locking key;
the second generation unit is used for generating a session key according to the identity and detecting whether session data corresponding to the session key exists in a preset database or not by using the session key;
a creating unit, configured to create, if not present, a current session corresponding to the session request;
and the processing unit is used for generating a session aging key according to the identity identifier when the current session is completed, and backing up the session data generated by the current session and the session aging key to the preset database, wherein the session aging key is used for defining the validity period of the session, and the validity period of the session key is greater than that of the session aging key.
9. A computer non-volatile storage medium, wherein the storage medium includes a stored program, and when the program runs, the apparatus on which the storage medium is located is controlled to execute the distributed session processing method according to any one of claims 1 to 7.
10. A distributed session processing system comprising one or more session servers and a distributed database, said session servers comprising a memory, a processor and a computer program stored in said memory and executable on said processor, wherein said processor when executing said computer program implements the steps of the distributed session processing method of any one of claims 1 to 7.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202010141939.5A CN111385356B (en) | 2020-03-04 | 2020-03-04 | Distributed session processing method, device and system |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202010141939.5A CN111385356B (en) | 2020-03-04 | 2020-03-04 | Distributed session processing method, device and system |
Publications (2)
Publication Number | Publication Date |
---|---|
CN111385356A true CN111385356A (en) | 2020-07-07 |
CN111385356B CN111385356B (en) | 2023-06-23 |
Family
ID=71218582
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202010141939.5A Active CN111385356B (en) | 2020-03-04 | 2020-03-04 | Distributed session processing method, device and system |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN111385356B (en) |
Cited By (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN113297305A (en) * | 2020-07-16 | 2021-08-24 | 阿里巴巴集团控股有限公司 | Session state processing method, device, equipment and storage medium |
Citations (5)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN104753987A (en) * | 2013-12-26 | 2015-07-01 | 北京东方通科技股份有限公司 | Distributed session management method and system |
CN107145396A (en) * | 2016-03-01 | 2017-09-08 | 阿里巴巴集团控股有限公司 | Distributed lock implementation method and equipment |
CN107517227A (en) * | 2016-06-15 | 2017-12-26 | 阿里巴巴集团控股有限公司 | Session implementation method and device for distributed consensus system |
US20180025145A1 (en) * | 2015-02-10 | 2018-01-25 | Bundesdruckerei Gmbh | Computer-implemented method for controlling access |
CN110213356A (en) * | 2019-05-21 | 2019-09-06 | 深圳壹账通智能科技有限公司 | A kind of login process method and relevant device based on data processing |
-
2020
- 2020-03-04 CN CN202010141939.5A patent/CN111385356B/en active Active
Patent Citations (5)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN104753987A (en) * | 2013-12-26 | 2015-07-01 | 北京东方通科技股份有限公司 | Distributed session management method and system |
US20180025145A1 (en) * | 2015-02-10 | 2018-01-25 | Bundesdruckerei Gmbh | Computer-implemented method for controlling access |
CN107145396A (en) * | 2016-03-01 | 2017-09-08 | 阿里巴巴集团控股有限公司 | Distributed lock implementation method and equipment |
CN107517227A (en) * | 2016-06-15 | 2017-12-26 | 阿里巴巴集团控股有限公司 | Session implementation method and device for distributed consensus system |
CN110213356A (en) * | 2019-05-21 | 2019-09-06 | 深圳壹账通智能科技有限公司 | A kind of login process method and relevant device based on data processing |
Cited By (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN113297305A (en) * | 2020-07-16 | 2021-08-24 | 阿里巴巴集团控股有限公司 | Session state processing method, device, equipment and storage medium |
Also Published As
Publication number | Publication date |
---|---|
CN111385356B (en) | 2023-06-23 |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
CN110213356B (en) | Login processing method based on data processing and related equipment | |
US11288253B2 (en) | Allocation method and device for a distributed lock | |
US11868324B2 (en) | Remote durable logging for journaling file systems | |
CN111814133A (en) | Unified login method and device for mobile application | |
CN110309161B (en) | Data synchronization method and device and server | |
US9910881B1 (en) | Maintaining versions of control plane data for a network-based service control plane | |
CN113010818A (en) | Access current limiting method and device, electronic equipment and storage medium | |
CN108234122B (en) | Token checking method and device | |
CN111245817A (en) | Automatic refreshing method and device for validity period certificate | |
CN113301390B (en) | Data processing method, device and server for calling virtual resources | |
JP2012146083A (en) | Session management system, session management apparatus, server device and session management method | |
CN106462472B (en) | Obtaining resource leases using multiple lease servers | |
CN113760976A (en) | Service processing method, device, equipment and storage medium | |
CN111385356B (en) | Distributed session processing method, device and system | |
CN109428924B (en) | Application online state maintenance method, access layer assembly, application system and equipment | |
CN113852631A (en) | Method and device for determining access token | |
CN111327680A (en) | Authentication data synchronization method, device, system, computer equipment and storage medium | |
CN116010926A (en) | Login authentication method, login authentication device, computer equipment and storage medium | |
CN114968503A (en) | Service timing processing method and device, electronic equipment and storage medium | |
US9197519B2 (en) | Tracking objects within dynamic environments | |
CN114048443A (en) | User identity verification method and device and computer storage medium | |
CN116954933A (en) | Interface calling method and device, storage medium and electronic equipment | |
CN113630273A (en) | Account logout system, method, device and storage medium | |
CN112463748A (en) | Storage system file lock identification method, system, terminal and storage medium | |
CN106375354B (en) | Data processing method and device |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination | ||
GR01 | Patent grant | ||
GR01 | Patent grant |