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)
Spring Boot集成Spring Data Reids和Spring Session实现Session共享的更多相关文章
- 86. Spring Boot集成ActiveMQ【从零开始学Spring Boot】
在Spring Boot中集成ActiveMQ相对还是比较简单的,都不需要安装什么服务,默认使用内存的activeMQ,当然配合ActiveMQ Server会更好.在这里我们简单介绍怎么使用,本节主 ...
- 84. Spring Boot集成MongoDB【从零开始学Spring Boot】
至于MongoDB网上有很多相关的资料,所以在这里不进行过多的介绍,我们在这里主要是介绍下如何将mongodb与spring boot结合使用.本节大纲: (1) 准备工作: (2) 新建一个mave ...
- 85. Spring Boot集成RabbitMQ【从零开始学Spring Boot】
这一节我们介绍下Spring Boot整合RabbitMQ,对于RabbitMQ这里不过多的介绍,大家可以参考网络上的资源进行安装配置,本节重点是告诉大家如何在Spring Boot中使用Rabbit ...
- spring boot集成shiro-redis时,分布式根据seesionId获取session报错排查总结
昨天在集成shiro-redis的时候,使用sessionId在其他微服务获取用户的session时,发生错误:There is no session with id [xxx]. 查遍了所有资料,基 ...
- Spring Boot 集成 WebSocket 实现服务端推送消息到客户端
假设有这样一个场景:服务端的资源经常在更新,客户端需要尽量及时地了解到这些更新发生后展示给用户,如果是 HTTP 1.1,通常会开启 ajax 请求询问服务端是否有更新,通过定时器反复轮询服务端响应的 ...
- MyBatis初级实战之一:Spring Boot集成
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- Spring Boot集成Springfox Swagger3和简单应用
摘要:Springfox Swagger可以动态生成 API 接口供前后端进行交互和在线调试接口,Spring Boot 框架是目前非常流行的微服务框架,所以,在Spring Boot 项目中集成Sp ...
- spring boot 集成 zookeeper 搭建微服务架构
PRC原理 RPC 远程过程调用(Remote Procedure Call) 一般用来实现部署在不同机器上的系统之间的方法调用,使得程序能够像访问本地系统资源一样,通过网络传输去访问远程系统资源,R ...
- Spring Boot集成JPA的Column注解命名字段无效的问题
偶然发现,Spring Boot集成jpa编写实体类的时候,默认使用的命名策略是下划线分隔的字段命名. Spring Boot版本:1.5.4.release 数据表: id int, userNam ...
随机推荐
- Spring框架中的aop操作之二 通过配置文件实现增强
aop表达式写法 配置文件代码: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&q ...
- ios之UIToolBar
toolbar除了可以和navigationController一起用之外,也可以独立用到view里.工具栏UIToolbar – 一般显示在底部,用于提供一组选项,让用户执行一些功能,而并非用于在完 ...
- c++ 计算彩票中奖概率
操作方法: 输入两个数字,第一个数字是备选总数,第二个数字是选择总数,然后返回中将概率. 可以投注多次,结束的时候返回总的中将概率. #include <iostream> using n ...
- Git学习——工作区和暂存区
工作区就是我们的电脑上的git初始化目录.版本库就是我们工作区中的隐藏目录.git.版本库中分为两个部分:(1)stage(index)暂存区:git add <file>命令后file就 ...
- IE6,7,8,9还有火狐浏览器的兼容
/*FF.Opear等支持Web标准的浏览器*/#header { margin-top: 23px; margin-bottom: 23px;}/*IE6浏览器*/*ht ...
- python爬虫基础01-HTTP协议
深入浅出了解HTTP协议 HTTP(HyperText Transfer Protocol,超文本传输协议)是互联网上应用最为广泛的一种网络协议.目前使用最普遍的一个版本是HTTP 1.1. HTTP ...
- Django 模版语法 三
使用自定义simple_tag 在 app01 下面创建 templatetags 文件夹,在创建 my_tag.py 文件,内容如下: from django import template fro ...
- getParameter getAttribute
URL:http://localhost:8888/Test/index.jsp?test=123 <body> ${test} ${requestScope.test} <%req ...
- 【java基础 11】java集合框架学习
导读:本篇博客主要是从整体上了解java的集合框架,然后主要介绍几个自己在项目中用到的结构,比如说:hashtable.hashmap.hashset.arraylist等! 一.宏观预览 从宏观上看 ...
- 【JavaScript 10—应用总结】:连缀
一.总体概述 1.1,什么叫连缀 所谓连缀,最简单的理解就是一句话同时设置一个或多个节点两个或两个以上的操作.比如: <span style="font-size:18px;" ...