Spring Session - 使用Redis存储HttpSession例子
目的
使用Redis存储管理HttpSession;
添加pom.xml
该工程基于Spring Boot,同时我们将使用Spring IO Platform来维护依赖版本号;
引入的依赖有spring-session、spring-boot-starter-web、spring-boot-starter-redis,pom文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<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.example</groupId>
<artifactId>helloworld</artifactId>
<version>0.0.1-SNAPSHOT</version> <dependencyManagement>
<dependencies>
<dependency>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>Athens-SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency> <dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.4.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <!-- Additional lines to be added here... -->
<dependencies>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.4.3.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build> </project>
配置Spring Session
配置比较简单,主要是添加@EnableRedisHttpSession注解即可,该注解会创建一个名字叫springSessionRepositoryFilter的Spring Bean,其实就是一个Filter,这个Filter负责用Spring Session来替换原先的默认HttpSession实现,在这个例子中,Spring Session是用Redis来实现的。
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; @EnableRedisHttpSession
public class HttpSessionConfig { }
操作HttpSession
这里我们将实现两个操作,一个是往Session中写入数据,另一个是查询数据,如下所示:
@RestController
public class Example { @RequestMapping("/set")
String set(HttpServletRequest req) {
req.getSession().setAttribute("testKey", "testValue");
return "设置session:testKey=testValue";
} @RequestMapping("/query")
String query(HttpServletRequest req) {
Object value = req.getSession().getAttribute("testKey");
return "查询Session:\"testKey\"=" + value;
} }
编写main方法
编写main方法,使用@SpringBootApplication注解标注,如果查看该注解源码的话,会发现相当于添加了@SpringBootConfiguration@EnableAutoConfiguration@ComponentScan等注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class APP {
public static void main(String[] args) throws Exception {
SpringApplication.run(APP.class, args);
}
}
运行程序,测试验证
1、本地启动redis服务;

2、打开redis客户端,输入flushall清空缓存;

3、浏览器输入http://localhost:8080/set,设置Session

redis客户端输入keys *命令, 可以发现redis中确实有数据插入:

4、浏览器输入http://localhost:8080/query,查询Session

5、清空redis缓存,浏览器输入http://localhost:8080/query,再次查询Session

发现HttpSession中已经无数据。

最后,如果查看浏览器的cookie的话,会发现有一个name为“SESSION”的cookie,其值为redis中spring session key的一部分。

另外,还可以在redis客户端输入HGETALL来查看spring session具体的值,如下:

结论:以上测试结果全部符合预期,HttpSession的实现成功被Spring Session替换,操作HttpSession等同于操作redis中的数据。
工程源码参考
https://github.com/peterchenhdu/spring-session-example
参考资料
http://docs.spring.io/spring-session/docs/1.3.1.BUILD-SNAPSHOT/reference/html5/
Spring Session - 使用Redis存储HttpSession例子的更多相关文章
- 170222、使用Spring Session和Redis解决分布式Session跨域共享问题
使用Spring Session和Redis解决分布式Session跨域共享问题 原创 2017-02-27 徐刘根 Java后端技术 前言 对于分布式使用Nginx+Tomcat实现负载均衡,最常用 ...
- 使用Spring Session和Redis解决分布式Session跨域共享问题
http://blog.csdn.net/xlgen157387/article/details/57406162 使用Spring Session和Redis解决分布式Session跨域共享问题
- django 安装redis及session使用redis存储
环境:centos 7.4 第一:安装redis 下载redis并安装: wget http://download.redis.io/releases/redis-5.0.5.tar.gz yum - ...
- Spring Session加Redis
session是一个非常常见的概念.session的作用是为了辅助http协议,因为http是本身是一个无状态协议.为了记录用户的状态,session机制就应运而生了.同时session也是一个非常老 ...
- Spring Session加Redis(山东数漫江湖)
session是一个非常常见的概念.session的作用是为了辅助http协议,因为http是本身是一个无状态协议.为了记录用户的状态,session机制就应运而生了.同时session也是一个非常老 ...
- PHP session用redis存储
redis的官方github这么说: phpredis can be used to store PHP sessions. To do this, configure session.save_ha ...
- php session之redis存储
前提:redis已安装好. php代码: <?php ini_set("session.save_handler", "redis"); ini_set( ...
- Nginx+Tomcat搭建集群,Spring Session+Redis实现Session共享
小伙伴们好久不见!最近略忙,博客写的有点少,嗯,要加把劲.OK,今天给大家带来一个JavaWeb中常用的架构搭建,即Nginx+Tomcat搭建服务集群,然后通过Spring Session+Redi ...
- SpringBoot学习笔记(13)----使用Spring Session+redis实现一个简单的集群
session集群的解决方案: 1.扩展指定server 利用Servlet容器提供的插件功能,自定义HttpSession的创建和管理策略,并通过配置的方式替换掉默认的策略.缺点:耦合Tomcat/ ...
随机推荐
- 选择困难症的福音——团队Scrum冲刺阶段-Day5(补发 那天csshow)
选择困难症的福音--团队Scrum冲刺阶段-Day 5 今日进展 编写提问部分 游戏分类的界面 将之前错误的图标改正 关于我们的俄罗斯方块,今天有了新的进展 NextBlockView(定义了下一个方 ...
- ipcam
ipcam也叫ip network camera,就是基于internet protocol的网络摄像机,同普通摄像头或者网眼的主要区别是ipcam实际上是一台视频服务器和摄像头的集成.ipcam只要 ...
- 在Linux服务器上配置Transmission来离线下载BT种子
Transmission简介 Transmission是一种BitTorrent客户端,特点是跨平台的后端和简洁的用户界面,硬件资源消耗极少,支持包括Linux.BSD.Solaris.Mac OS ...
- URL与URI的区别和联系
转自:https://www.cnblogs.com/chengdabelief/p/6635045.html 简单理解是这样的:理解URI和URL的区别,我们引入URN这个概念. URI = Uni ...
- ActiveMQ_6持久化
activemq持久化 ActiveMQ提供了插件式的消息存储,主要有有如下几种: 1.AMQ消息存储-基于文件的存储方式,是以前的默认消息存储 2.KahaDB消息存储-提供了容量的提升和恢复能力, ...
- 机器学习面试--一句话概括传统ML算法
机器学习过程主要包括:数据的特征提取.数据预处理.训练模型.测试模型.模型评估改进等几部分 传统机器学习算法主要包括以下五类: 回归:建立一个回归方程来预测目标值,用于连续型分布预测 分类:给定大量带 ...
- OpenGL Compute Shader靠谱例子及读取二进制Shader,SPIR-V
学OpenGL以来一直苦恼没有像DX那样可以读取二进制Shader使用的方法,除去有时不想公开自己写的牛逼Shader的心理(虽然目前还从没写过什么牛逼的Shader), 主要是不用现场编译,加快读取 ...
- IntelliJ IDEA 2017版 spring-boot2.0.2 搭建 JPA springboot DataSource JPA sort排序方法使用方式, 添加关联表的 order by
1.sort可以直接添加在命名格式的字段中 List<BomMain> findAllByDeleted(Integer deleted, Sort sort); 2.可以作为pageab ...
- javabean的特点
javabean的三个基础条件 1.拥有私有的属性 2.共有的get,set方法 3.默认的构造方法
- php中 curl, fsockopen ,file_get_contents 三个函数
赵永斌:有些时候用file_get_contents()调用外部文件,容易超时报错.换成curl后就可以.具体原因不清楚curl 效率比file_get_contents()和fsockopen()高 ...