A preliminary study of spring-session one of spring-session
1. What is spring-session
1.1 Background - session session sharing
HttpSession
It is created and managed by the servlet container, like Tomcat/Jetty is stored in memory.
But if we build the application into a distributed cluster and use LVS or Nginx to do it 负载均衡
, then Http requests from the same user will likely be distributed to two different applications.
The question is , how to ensure that different web sites can share the same session data?
The simplest idea is to save the session data to a unified place outside the memory, such as Memcached/Redis and other databases.
Then the question comes again , how to replace the implementation of Servlet container creation and management HttpSession
?
-
Use the Servlet container to
插件功能
customize the creation and management strategy of HttpSession, and replace the default strategy by configuration.In fact, there have already been open source projects in this regard, such as
memcached-session-manager
(currently our official mall uses this), andtomcat-redis-session-manager
.
However, this method has a disadvantage, that is, it needs to couple the code of Servlet containers such as Tomcat/Jetty. -
Design a Filter,
利用HttpServletRequestWrapper
implement your own getSession() method, and take over the work of creating and managing Session data.spring-session
It is through this way of thinking.
1.2 spring-session
Introduction
spring-session
is one of Spring's projects,
- Official website address: http://projects.spring.io/spring-session/
- GitHub address: https://github.com/spring-projects/spring-session
spring-session
Provides a set of solutions for creating and managing Servlet HttpSession. spring-session
Provides the clustered Session (Clustered Sessions) function. By default, external Redis is used to store Session data to solve the problem of Session sharing.
The following is a feature introduction from the official website:
Spring Session provides the following features:
- API and implementations for managing a user's session
- HttpSession - allows replacing the HttpSession in an application container (i.e. Tomcat) neutral way
- Clustered Sessions - Spring Session makes it trivial to support clustered sessions without being tied to an application container specific solution.
- Multiple Browser Sessions - Spring Session supports managing multiple users' sessions in a single browser instance (i.e. multiple authenticated accounts similar to Google).
- RESTful APIs - Spring Session allows providing session ids in headers to work with RESTful APIs
- WebSocket - provides the ability to keep the HttpSession alive when receiving WebSocket messages
2. Self-study source code usage (take redis as an example)
Steps (refer to http://docs.spring.io/spring-session/docs/current/reference/html5/guides/httpsession.html ):
- First download the source code from
GitHub
https://github.com/spring-projects/spring-session - Local installation
gradle
(omitted), refer to http://feitianbenyue.iteye.com/blog/2326268 - Install locally
redis
(need 2.8+) and start (omitted) - Based on
spring-session
the root directory, executegradlew :samples:httpsession-xml:tomcatRun
Then you can open http://localhost:8080, you can see the following interface, and you can use this page to set the session attribute
3. The key in redis
open redis-cli
, enter
keys *
result:
It can be seen that redis
the spring-session
relevant key in it spring:session:
starts with
and has the following keys
127.0.0.1:6379> keys spr*
1) "spring:session:expirations:1474542840000"
2) "spring:session:sessions:c3ae0dba-058e-4eca-99ad-6ec5225fd6e0"
3) "spring:session:sessions:expires:c3ae0dba-058e-4eca-99ad-6ec5225fd6e0"
至此,我们大概知道 spring-session 是什么东东, 大概怎么玩, 下面我们来聊聊项目中如何使用他
4.参考
- spring-session github地址
- spring-session 官网地址
- 利用spring session解决共享Session问题
- http://feitianbenyue.iteye.com/blog/2326268
- http://docs.spring.io/spring-session/docs/current/reference/html5/guides/httpsession.html
---to be continued