Simple Example of Spring Session to implement distributed session, springsession example
Previously, tomcat-redis-session-manager was used to implement distributed session management, but it has some limitations, mainly because it is too tight to bind with tomcat, here, we use Spring Session to manage distributed sessions. Spring session is completely unrelated to specific containers. If you need to know how to use tomcat-redis-Session-manager to implement distributed sessions, please refer to my previous article. The following is the topic for setting up the Spring Session project.
1. Introduce Spring Session maven dependency
<!-- spring session begin --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.5.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session</artifactId> <version>1.3.1.RELEASE</version> </dependency> <!-- spring session end -->
2. Add Spring Session-related configurations to the Spring configuration file (Spring Session is highlighted here, So redis-related configurations are not listed. You need to refer to the instance code)
<!-- Spring Session begin --> <bean id="redisHttpSessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"> <property name="maxInactiveIntervalInSeconds" value="1800" /> </bean> <!-- Spring Session end -->
3. Configure the Spring Session Filter in web. xml, which must be placed before all filters.
<! -- Add a session proxy filter to wrap the Servlet's getSession, it must be placed at the top of all filter chains --> <filter-name> springSessionRepositoryFilter </filter-name> <filter-class> org. springframework. web. filter. delegatingFilterProxy </filter-class> </filter> <filter-mapping> <filter-name> springSessionRepositoryFilter </filter-name> <url-pattern>/* </url-pattern> </filter-mapping>
This is almost all the steps, isn't it easy? Hurry up and try it on your own. It seems that the distributed Session on the tall architecture is handled by Spring Session!
The following is my github Source Code address:
Https://github.com/13babybear/bounter-springsession
Thank you for your valuable comments!