Spring Boot集成Spring Data Reids和Spring Session实现Session共享
首先,需要先集成Redis的支持,参考:http://www.cnblogs.com/EasonJim/p/7805665.html
Spring Boot集成Spring Data Redis+Spring Session非常的简单,也不用担心版本问题,只需要引入相应的包即可。再次感叹一下Spring Boot对于版本的控制做的真的太好了。
小提示:如果在做Spring MVC时如果问题出现在版本上出现网上找不到解决方案的BUG时,可以参考Spring Boot引入的版本来知道Redis和Session用的是什么版本。比如这个项目上使用的1.4.7的Spring Boot,那么MVC用的是4.3.9,Redis为1.7.11,Jedis为2.8.2。
集成步骤:
POM:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.jsoft.springboottest</groupId> <artifactId>springboottest1</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboottest1</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <!-- Inherit defaults from Spring Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.7.RELEASE</version> </parent> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <!-- Add typical dependencies for a web application --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 热部署模块 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <!-- 这个需要为 true 热部署才有效 --> </dependency> <!-- Redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- Session --> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> </dependency> </dependencies> <!-- Package as an executable jar --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
使用@EnableRedisHttpSession开启Redis的Session支持,直接新建一个类。
package com.jsoft.springboottest.springboottest1; import org.springframework.context.annotation.Configuration; import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; @Configuration @EnableRedisHttpSession(maxInactiveIntervalInSeconds = 60 * 60 * 24) public class SessionConfig { }
maxInactiveIntervalInSeconds为过期时间,单位为秒。
通过上面基本完成了,Redis集成部分参考上一篇文章,集群和单机的都行。
示例工程:https://github.com/easonjim/5_java_example/tree/master/springboottest/springboottest7
参考:
http://www.jb51.net/article/112923.htm
http://blog.csdn.net/zl18310999566/article/details/54290994
http://blog.csdn.net/l1028386804/article/details/65081718
http://blog.csdn.net/xiaoyu411502/article/details/48603597
https://www.cnblogs.com/mengmeng89012/p/5519698.html
http://www.cnblogs.com/ityouknow/p/5748830.html
https://projects.spring.io/spring-session/(Spring Session官网,一切都是这个入口,Spring Session Data Redis只是其中一个小模块)
https://docs.spring.io/spring-session/docs/current/reference/html5/guides/boot.html(官方Spring Boot集成文档)
https://docs.spring.io/spring-session/docs/current/reference/html5/(官方总配置文档,下面有个表格可以进入查看)
https://docs.spring.io/spring-session/docs/current/reference/html5/guides/custom-cookie.html(官方文档,实现Session的key修改和作用域)
https://docs.spring.io/spring-session/docs/current/reference/html5/guides/users.html(官方文档,实现多个Session)