Background technique
Session, that is, session control represents a conversation procedure between application server and client, is generally used for controlling
The time-out time that user processed logs in, and storage user log in relevant information.In single application server, Session information
There may be in the jvm memory of application server, read-write is all by same application server.But in distributed environment
Under, the request of client, which can be supported, is balanced to more application servers, it is therefore desirable to have a kind of mechanism to solve application service
Session data sharing problem between device.
Existing distribution Session Managed Solution one: the Session based on browser Cookie is shared.This scheme will
Session information is stored into the Cookie of browser, also known as client Session, by Cookie when the page initiates to request
In content be sent to Web server together.
Existing distribution Session Managed Solution two: the Session based on traditional database is shared.This scheme will
Session information is stored into relevant database, and different application server realizes that Session believes by accessing the database
Breath is shared.The program is realized simply, such as the PersistentManager of Tomcat directly supports JDBC database purchase.
Existing distribution Session Managed Solution three: it is based on application server/Servlet container Clustering machine
System.This scheme will be present the Session information on separate unit application server and is synchronized in cluster by Session replicanism
Other application server.The Clustering mechanism of some common application servers and Servlet container may be implemented
The function of Session duplication, such as Tomcat Clustering/Session Replication, Jboss buddy
replication。
Existing distribution Session Managed Solution four: the Session based on NFS is shared.This scheme is similar with scheme two,
By the storage of Session information into NFS, each server only needs the disk of the storage Session of carry shared server i.e.
Can, it realizes relatively simple.
Existing distribution Session Managed Solution five: the Session based on Redis is shared.This scheme believes Session
Breath is stored in Redis memory database, and Redis guarantees the High Availabitity of service using the framework of one master and multiple slaves, and read-write is reflected respectively
It is mapped on principal and subordinate, the data between principal and subordinate are synchronous to be carried out by asynchronous system.
In existing scheme, the shortcomings that scheme one is the data that Cookie is merely able to the fundamental types such as storage character string, numerical value,
And there is limitation in size, quantity;Due to requesting that cookie information will be transmitted every time, bandwidth and network transmission performance have one
Fixing is rung;It is problematic in terms of safety, it is easy to be stolen, distort.
In scheme two, database server is more difficult extension relative to application server and resource is more valuable, in high concurrent
Web application in, maximum performance bottleneck is generally placed upon database server.So if by Session storage to database
Table, high concurrent easily cause database table contention and locking when operating, influence service feature.
The shortcomings that scheme three is that the Session replication performance based on Clustering is poor, and scalability is not high.
The shortcomings that scheme four is that the performance that NFS reads and writes high concurrent is not high, is existed on Hard Disk I/O performance and network bandwidth
Larger bottleneck, especially for the scene of the frequent read-write operation of small documents as Session.
The shortcomings that scheme five is under the configuration of read and write abruption, and the data of write operation are not complete from master to slave synchronization
Cheng Shi the to be synchronized complete or selection such as can only select to read legacy data (redis configuration if client will read the data
It determines).Another drawback, if master failure, can be likely to cause and write in master into slave synchronizing process
The loss of data of operation.
In conclusion the prior art is difficult to meet simultaneously performance and data consistency requirement under high concurrent scene.
Summary of the invention
To solve under data inconsistence problems present in existing Session management method and high concurrent scene, client
The problems such as holding service feature low, the embodiment of the present invention provide the method and dress of a kind of distributed Session management based on NoSQL
It sets.
In a first aspect, the embodiment of the present invention provides a kind of method of distributed Session management based on NoSQL, the party
Method includes:
The session request that client is sent is received, and obtains the action type of the session request in the session request
With the sessionID of the session request;
If the session request is write operation, the session request is routed to the master node of cluster server,
So that the master node stores described in the session data of the sessionID and session request carrying and confession
The sessionID and the session data are synchronized to each slave node of cluster server by master node;
If the session request is read operation, know that the corresponding session data of the sessionID are complete in judgement
After synchronizing, the session request is routed into slave node according to preset load balancing, for the client root
The session data corresponding with the sessionID are read on the slave node according to the sessionID.
Second aspect, the embodiment of the present invention provide a kind of device of distributed Session management based on NoSQL, the dress
It sets and includes:
Receiving unit receives the session request that client is sent, and obtains the session request in the session request
Action type and the session request sessionID;
The session request is routed to cluster server if the session request is write operation by the first routing unit
Master node, so that the master node stores the session number that the sessionID and the session request carry
The sessionID and the session data are synchronized to each of cluster server according to and for the master node
Slave node;
Secondary route unit knows that the sessionID is corresponding in judgement if the session request is read operation
Session data are completed synchronize after, the session request is routed into slave node according to preset load balancing,
For the client read on the slave node according to the sessionID it is corresponding with the sessionID described
Session data.
The third aspect, the embodiment of the present invention provide a kind of electronic equipment, which includes:
Memory and processor, the processor and the memory complete mutual communication by bus;It is described to deposit
Reservoir is stored with the program instruction that can be executed by the processor, and it is as follows that the processor calls described program instruction to be able to carry out
Method: receiving the session request that client is sent, and obtain in the session request session request action type and
The sessionID of the session request;If the session request is write operation, the session request is routed into cluster service
The master node of device, for the session that the master node stores the sessionID and the session request carries
The sessionID and the session data are simultaneously synchronized to each of cluster server for the master node by data
Slave node;If the session request is read operation, the corresponding session data of the sessionID have been known in judgement
After completion synchronizes, the session request is routed into slave node according to preset load balancing, for the client
The session data corresponding with the sessionID are read on the slave node according to the sessionID.
Fourth aspect, the embodiment of the present invention provide a kind of computer readable storage medium, are stored thereon with computer program,
The computer program realizes following method when being executed by processor: receiving the session request that client is sent, and in the meeting
The action type of the session request and the sessionID of the session request are obtained in words request;If the session request is
The session request is then routed to the master node of cluster server by write operation, so that the master node stores institute
State the session data that sessionID and the session request carry and for the master node by the sessionID and
The session data are synchronized to each slave node of cluster server;If the session request is read operation, sentencing
It is disconnected know that the corresponding session data of the sessionID are completed and synchronize after, by the session request according to preset load
Balance policy routes to slave node, so that the client is read on the slave node according to the sessionID
The session data corresponding with the sessionID.
The method and device of distributed Session management provided in an embodiment of the present invention based on NoSQL, by client
Increase a kind of middleware device between end and cluster server, bridge is established between cluster server and client, according to client
The type of the session request sent and the synchronous regime of client's Session data to be accessed are held, for the session request road
By to different server nodes, to solve that service feature under data inconsistence problems and high concurrent scene is not high to ask
Topic;In addition, lifting function by providing master gravity treatment, additionally it is possible to solve when the master node of cluster server breaks down
Caused by data loss problem.
Specific embodiment
In order to make the object, technical scheme and advantages of the embodiment of the invention clearer, below in conjunction with the embodiment of the present invention
In attached drawing, technical solution in the embodiment of the present invention is explicitly described, it is clear that described embodiment is the present invention
A part of the embodiment, instead of all the embodiments.Based on the embodiments of the present invention, those of ordinary skill in the art are not having
Every other embodiment obtained under the premise of creative work is made, shall fall within the protection scope of the present invention.
Fig. 1 is the distributed Session management architecture figure provided in an embodiment of the present invention based on NoSQL.NoSQL
(Not Only SQL), refers to non-relational database.NoSQL database is made of NoSQL cluster, which includes one
Platform master server and Duo Tai slave server.The NoSQL database provides the reading and storage service of Session data,
Namely provide read/write service.The session data of each user are like that key-value pair is equally stored in NoSQL database,
In, key is exactly sessionID, and value is exactly the session data that user needs to store.Cluster server is exactly to pass through
SessionID distinguishes the session data of different user storage.
As shown in Figure 1, middleware device is between client and NoSQL server, between NoSQL cluster and client
Establish bridge.
Fig. 2 is the process signal of the method for the distributed Session management provided in an embodiment of the present invention based on NoSQL
Figure.As shown in Fig. 2, method includes the following steps:
S201, the session request that client is sent is received, and obtains the behaviour of the session request in the session request
Make the sessionID of type and the session request;
Specifically, when user passes through individual terminal devices (calling user terminal in the following text) first time access client, client meeting
A sessionID is distributed for user terminal, and sessionID is back to user terminal, user terminal can save the sessionID,
In this way, the access request that client receives just will include sessionID when user's access client again.Also, client
It is read operation to be carried out or write operation that end, which also judges the access request that user terminal is sent,.Therefore, client is according to user's
Access request and the session request that generates includes at least two information: action type and sessionID.Middleware device receives
After the session request that client is sent, the action type and sessionID of the session request are obtained.
If S202, the session request are write operation, the session request is routed to the master of cluster server
Node, so that the master node stores the session data of the sessionID and session request carrying and for institute
State each slave node that the sessionID and the session data are synchronized to cluster server by master node;
Specifically, if middleware device knows that the session request is write operation, middleware device goes to the session request
Master server (i.e. master node), after master server receives the session request, it will talk about request
SessionID and session data are stored in the database of master server in the form of key-value pair, and master takes
Above-mentioned two information can be synchronized to respectively in the database of each slave server (i.e. slave node) by business device.
Optionally, master server is completed to return to operating result to client after data store.After completing storage
Master server can also locally record storage operation, in this way, if master server goes out during storing data
When existing failure, middleware can check the corresponding session data of the sessionID on master server by operation note
Storage state.
If S203, the session request are read operation, the corresponding session number of the sessionID is known in judgement
According to after being completed and synchronizing, the session request is routed into slave node according to preset load balancing, for the visitor
Family end the session number corresponding with the sessionID is read on the slave node according to the sessionID
According to.
Specifically, if middleware device knows that the session request is read operation, middleware device can be sentenced according to sessionID
The synchronous regime for its corresponding session data of breaking, if judgement knows that the session data are write by all slave servers
Enter in the database of itself, then the session request is gone into slave server, after slave server receives the session request,
Corresponding session data for customer end is found according to the sessionID of session request to read.
Fig. 3 is the illustrative timing diagram of read operation provided in an embodiment of the present invention.
From above embodiment as can be seen that middleware device uses read and write abruption strategy, write if middleware device receives
Operation, also referred to as CUD (newly-built/update/deletion) operation, is routed to master node, when receiving R (reading) operation, load balancing is arrived
Each slave node, to ensure that the service feature under high concurrent scene;Also, by read operation go to each slave node it
Before, the synchronous regime for the session data that can be first accessed client judges, can guarantee that client is accessed in this way
Correct session data, rather than stale data.
The method of distributed Session management provided in an embodiment of the present invention based on NoSQL, by client sum aggregate
Increase a kind of middleware device between group's server, bridge is established between cluster server and client, is sent according to client
Session request type and client's Session data to be accessed synchronous regime, routed to not for the session request
Same server node, to solve the problems, such as that the service feature under data inconsistence problems and high concurrent scene is not high.
On the basis of the above embodiments, this method is further comprising the steps of:
Receive the feedback that the slave node is sent after completing the sessionID and session data storage
Information;
Specifically, above-mentioned two information can be synchronized to respectively the database of each slave server by master server
It is interior, and each slave server is notified that middleware device, middleware device receive slave node after completing data and synchronizing
Feedback information.
If receiving the feedback information that all slave nodes are sent, it is determined that the corresponding session of the sessionID
Synchronization is completed in data.
Specifically, middleware device can know the sum of slave server in cluster server, be received by statistics
The number of feedback information mean sessionID pairs if the number of feedback information is identical as slave server sum
The session data answered by master server sync to all slave servers, the database of master server and
The data that the database of each slave server is stored are consistent, and ensure that the session data that client needs to access
Correctness.
Fig. 4 is the illustrative timing diagram of write operation provided in an embodiment of the present invention.
It is provided in an embodiment of the present invention based on NoSQL distributed Session management method, by middleware device with
The interaction of each slave server, the synchronous regime for each session data that can timely update guarantee the strong by one of session data
Cause property.
On the basis of the various embodiments described above, this method is further comprising the steps of:
According to the feedback information, the corresponding session data of the sessionID are updated in operation log
Synchronously complete state.
Specifically, middleware device locally can be corresponding to record sessionID by way of updating operation log
Session data synchronously complete state on each slave server.
For example, the data record form of operation log can are as follows: sessionID and synchronously complete state.Work as middleware device
When receiving write operation, middleware device can inquire the sessionID in operation log, then corresponding synchronously complete at its
" unfinished " is recorded at state;Then middleware device receives the feedback information of each slave server, if middleware device connects
The feedback information of all slave servers is received, then will synchronously complete state and be updated to " being completed ";Conversely, then synchronously completing
State is still " unfinished ".
The method of distributed Session management provided in an embodiment of the present invention based on NoSQL, operates day by setting
Will can the direct inquiring client terminal session data that are accessed using operation log when middleware device receives read operation
State is synchronously completed, and then determines the session request being routed to master server or slave server, so guarantee number
According to strong consistency.
On the basis of the various embodiments described above, this method further include:
If the session request is read operation, know that the corresponding session data of the sessionID do not complete in judgement
After synchronizing, then the session request is routed into the master node, so that the client exists according to the sessionID
The session data corresponding with the sessionID are read on the master node.
Specifically, it is synchronized if middleware device judgement knows that the session data that client to be accessed do not complete also,
I.e. there are also part slave servers also in synchronous process, which is then routed to master service by middleware device
At device, the newest session data of master server storage are read, without completing number until all slave servers
According to synchronization, could be read on slave server.
As can be seen from the above-described embodiment, the distributed Session provided in an embodiment of the present invention based on NoSQL is managed
Method can save the read access time of client, in turn ensure client compared with the session secret sharing based on Redis
The accuracy of the session data of reading.
On the basis of the various embodiments described above, this method further include:
Receive the master Node registry request that each server is sent in the cluster server;
Specifically, when NoSQL cluster server starts, each server can send master section to middleware device
Point registration request, middleware device then receive the master Node registry request that each server is sent.
Optionally, when slave Server Restart or when having new slave server that cluster is added, slave server to
Master server initiates full dose synchronization request, and entire memory image is sent to slave server, restarted by master server
Afterwards or new addition slave server can also send master node note after synchronously completing memory image to middleware device
Volume request.
After receiving the master Node registry request, if not inquiring master Node registry letter in register list
Breath then sets master node for the server for sending the master Node registry request, remaining server is set as
Slave node.
Specifically, after middleware device receives the request of master Node registry, each clothes can be registered in register list
It is engaged in the information of device, such as IP address and and the port of server attribute (master or slave) in the cluster, server
Address.When middleware device receives first registration request, due to not yet storing the letter of any server in the register list
Breath, namely master node is not present at this time, then the server for sending the registration request is then set as by middleware device
Master node;Next, middleware device will continue to receive other registration requests you because master node has been registered,
Master server has generated, then other servers are set as slave node by middleware device.
The method of distributed Session management provided in an embodiment of the present invention based on NoSQL, by cluster server
Master election is carried out, the availability of NoSQL database is improved.
On the basis of the various embodiments described above, this method further include:
The heartbeat message that the master node is sent is received every preset duration;
Specifically, master server periodically can send heartbeat message to middleware device, and correspondingly, middleware device is then
Each preset duration receives the heartbeat message.Middleware device would know that the state of master server is just in this way
Often work or failure.If being more than preset duration, middleware device does not receive heartbeat message also, and middleware device is just recognized
It breaks down for master server or master server has been deleted.
If being more than that the preset duration does not receive the heartbeat message, to the institute for subscribing to master knot removal event
There is server to send master node reselection information, so that each server is sent out after receiving the master node reselection information
Master Node registry is sent to request.
Specifically, cluster server can subscribe to master knot removal event in middleware device, in this way, if middleware fills
The heartbeat message for not receiving existing master server more than preset duration is set, i.e., existing master server breaks down
When, middleware device can send master node reselection information to the Servers-all for subscribing to master knot removal event, and
The server for receiving the reselection information can then send the request of master Node registry to middleware device.
As can be seen from the above-described embodiment, middleware device can be monitored master server, work as discovery
When master server failure, master server is re-elected from remaining server, further improves NoSQL data
The availability in library.
Optionally, when the load balancing in the various embodiments described above includes: polling schemas, randomized policy and minimum response
Between tactful at least one.
Specifically, polling schemas refers to that each session request is sequentially allocated according to the time sequencing of request to slave server.
Minimum corresponding time parameter method refers to that middleware device can be distributed according to the speed of the response speed of each slave server, it will words
Request assignment to Best-case Response Time, namely that slave server that response is most fast.
For example, middleware device has received 3 session requests according to sequencing: session request 1, session request 2 and
Session request 3;If there is 5 slave servers: slave1-slave5 in cluster;According to polling schemas, middleware device can be incited somebody to action
Above-mentioned 3 session requests route to slave1, slave2 and slave3 respectively.According to minimum response strategy, middleware device
The response time of each slave server can be calculated in real time, and then above-mentioned session request 1 is distributed to and responds most fast slave clothes
Business device similarly, then session request 2 and 3 is also allocated to and responds most fast slave server.
The method of distributed Session management provided in an embodiment of the present invention based on NoSQL, provides client access
Read and write abruption, load-balancing function when NoSQL database improve the overall performance and efficiency of Session management.
Fig. 5 is the structural representation of the device of the distributed Session management provided in an embodiment of the present invention based on NoSQL
Figure.As shown in figure 5, the middleware device includes: receiving unit 501, the first routing unit 502 and secondary route unit 503.
Wherein:
Receiving unit 501 receives the session request that client is sent, and obtains the session in the session request and ask
The sessionID of the action type and the session request asked;If the session request of the first routing unit 502 is write operation,
The session request is then routed to the master node of cluster server, for described in master node storage
SessionID and the session data of session request carrying simultaneously supply the master node by the sessionID and institute
State each slave node that session data are synchronized to cluster server;If the session request of secondary route unit 503 is
Read operation, then after judgement knows that the corresponding session data of the sessionID are completed and synchronize, by the session request
Route to slave node according to preset load balancing, for the client according to the sessionID described
The session data corresponding with the sessionID are read on slave node.
Specifically, using read and write abruption strategy, when receiving unit 501 receives write operation, the first routing unit 502 should
Session request is routed to master node;When receiving unit 501 receives read operation, if the judgement of secondary route unit 503 is known
Session data have synchronously completed, then by session request load balancing to each slave node.It should be noted that the present invention is real
The middleware device for applying example offer is to realize that above method embodiment, function specifically refer to above method implementation
Example, details are not described herein again.
The device of distributed Session management provided in an embodiment of the present invention based on NoSQL, passes through receiving unit, the
One routing unit and secondary route unit establish bridge between cluster server and client, are asked according to the session that client is sent
The synchronous regime of the type and client asked the Session data to be accessed, different services is routed to for the session request
Device node, to solve the problems, such as that the service feature under data inconsistence problems and high concurrent scene is not high.
Fig. 6 is the structural block diagram of electronic equipment provided in an embodiment of the present invention.As shown in fig. 6, the electronic equipment includes: to deposit
Reservoir 602 and processor 601, the processor 601 and the memory 602 complete mutual communication by bus 603;Institute
It states memory 602 and is stored with the program instruction that can be executed by the processor, the processor 601 calls described program to instruct energy
Enough execute following method, for example, receive the session request that client is sent, and obtain the meeting in the session request
Talk about the action type of request and the sessionID of the session request;If the session request is write operation, by the session
Request routes to the master node of cluster server, so that the master node stores the sessionID and the meeting
The session data of words request carrying are simultaneously synchronous with the session data by the sessionID for the master node
To each slave node of cluster server;If the session request is read operation, the sessionID is known in judgement
Corresponding session data are completed synchronize after, the session request is routed into slave according to preset load balancing
Node, for the client read on the slave node according to the sessionID it is corresponding with the sessionID
The session data.
The embodiment of the present invention also provides a kind of computer program product, and the computer program product is non-temporary including being stored in
Computer program on state computer readable storage medium, the computer program include program instruction, when described program instructs
When being computer-executed, computer is able to carry out method provided by above-mentioned each method embodiment, for example, receives client
The session request of transmission, and obtain in the session request action type and the session request of the session request
sessionID;If the session request is write operation, the master that the session request routes to cluster server is saved
Point, so that the master node stores described in the session data of the sessionID and session request carrying and confession
The sessionID and the session data are synchronized to each slave node of cluster server by master node;If
The session request is read operation, then after judgement knows that the corresponding session data of the sessionID are completed and synchronize,
The session request is routed into slave node according to preset load balancing, so that the client is according to
SessionID reads the session data corresponding with the sessionID on the slave node.
The embodiment of the present invention also provides a kind of computer readable storage medium, the computer-readable recording medium storage meter
The instruction of calculation machine, the computer instruction make the computer execute method provided by above-mentioned each method embodiment, for example,
The session request that client is sent is received, and obtains the action type and the meeting of the session request in the session request
Talk about the sessionID of request;If the session request is write operation, the session request is routed into cluster server
Master node, for the session data that the master node stores the sessionID and the session request carries
And the sessionID and the session data are synchronized to each slave of cluster server for the master node
Node;If the session request is read operation, know that the corresponding session data of the sessionID are completed in judgement
After synchronizing, the session request is routed into slave node according to preset load balancing, for the client according to
The sessionID reads the session data corresponding with the sessionID on the slave node.
Finally, it should be noted that the above embodiments are merely illustrative of the technical solutions of the present invention, rather than its limitations;Although
Present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that: it still may be used
To modify the technical solutions described in the foregoing embodiments or equivalent replacement of some of the technical features;
And these are modified or replaceed, technical solution of various embodiments of the present invention that it does not separate the essence of the corresponding technical solution spirit and
Range.