spring boot 中使用redis session
spring boot 默认的httpsession是存在内存中。这种默认方式有几个缺点:1、当分布式部署时,存在session不一致的问题;2、当服务重启时session就会丢失,这时候用户就需要重新登陆,可能导致用户数据丢失。通常会使用redis来保存session。
在spring boot中利用redis来保存session是非常简单。只需要简单的几步就可以了。可以参考官方教程。https://docs.spring.io/spring-session/docs/current/reference/html5/guides/boot-redis.html
配置流程:
1、pom文件中添加依赖
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
注意:如果pom文件没有引入 spring-boot-starter-data-redis,则需要引入,否则无法自动配置redis,出现这个错误'org.springframework.data.redis.connection.RedisConnectionFactory' that could not be found.
2、在配置文件中添加相关配置
#配置sesion使用redis
spring.session.store-type=redis
设置session的一些属性
server.servlet.session.cookie.http-only=true
server.servlet.session.timeout= 15000
#设置session在redis中的Namespace,避免和其他key冲突
spring.session.redis.namespace=spring:session
配置redis的链接
spring.redis.host=localhost
spring.redis.password=
spring.redis.port=6379
到这里,其实已经配置好了,可以直接使用httpsession了,没有特别的地方。
开发过程遇到的问题。
本地测试通过之后部署到测试环境发现无法正常运行,报如下异常:
Caused by: io.lettuce.core.RedisCommandExecutionException: ERR unknown command 'CONFIG'
at io.lettuce.core.protocol.AsyncCommand.completeResult(AsyncCommand.java:118) ~[lettuce-core-5.0.3.RELEASE.jar:na]
at io.lettuce.core.protocol.AsyncCommand.complete(AsyncCommand.java:109) ~[lettuce-core-5.0.3.RELEASE.jar:na]
at io.lettuce.core.protocol.CommandHandler.complete(CommandHandler.java:601) ~[lettuce-core-5.0.3.RELEASE.jar:na]
at io.lettuce.core.protocol.CommandHandler.decode(CommandHandler.java:559) ~[lettuce-core-5.0.3.RELEASE.jar:na]
at io.lettuce.core.protocol.CommandHandler.channelRead(CommandHandler.java:511) ~[lettuce-core-5.0.3.RELEASE.jar:na]
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) ~[netty-transport-4.1.23.Final.jar:4.1.23.Final]
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) ~[netty-transport-4.1.23.Final.jar:4.1.23.Final]
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) ~[netty-transport-4.1.23.Final.jar:4.1.23.Final]
at io.netty.channel.ChannelInboundHandlerAdapter.channelRead(ChannelInboundHandlerAdapter.java:86) ~[netty-transport-4.1.23.Final.jar:4.1.23.Final]
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) ~[netty-transport-4.1.23.Final.jar:4.1.23.Final]
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) ~[netty-transport-4.1.23.Final.jar:4.1.23.Final]
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) ~[netty-transport-4.1.23.Final.jar:4.1.23.Final]
at io.netty.channel.ChannelInboundHandlerAdapter.channelRead(ChannelInboundHandlerAdapter.java:86) ~[netty-transport-4.1.23.Final.jar:4.1.23.Final]
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) ~[netty-transport-4.1.23.Final.jar:4.1.23.Final]
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) ~[netty-transport-4.1.23.Final.jar:4.1.23.Final]
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) ~[netty-transport-4.1.23.Final.jar:4.1.23.Final]
at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1434) ~[netty-transport-4.1.23.Final.jar:4.1.23.Final]
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) ~[netty-transport-4.1.23.Final.jar:4.1.23.Final]
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) ~[netty-transport-4.1.23.Final.jar:4.1.23.Final]
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:965) ~[netty-transport-4.1.23.Final.jar:4.1.23.Final]
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163) ~[netty-transport-4.1.23.Final.jar:4.1.23.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:645) ~[netty-transport-4.1.23.Final.jar:4.1.23.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:580) ~[netty-transport-4.1.23.Final.jar:4.1.23.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:497) ~[netty-transport-4.1.23.Final.jar:4.1.23.Final]
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:459) ~[netty-transport-4.1.23.Final.jar:4.1.23.Final]
at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:886) ~[netty-common-4.1.23.Final.jar:4.1.23.Final]
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.23.Final.jar:4.1.23.Final]
at java.lang.Thread.run(Thread.java:748) ~[na:1.8.0_171]
在github上有相关的issue,https://github.com/spring-projects/spring-session/issues/124,并且给了解决办法。
一般是因为安全问题redis服务端禁用了CONFIG命令,而RedisHttpSessionConfiguration需要使用这个命令进行一些初始化。导致无法初始化。
一个简单的解决方式是添加一个jiava配置
@Bean
public static ConfigureRedisAction configureRedisAction() {
return ConfigureRedisAction.NO_OP;
}
spring boot 中使用redis session的更多相关文章
- Spring Boot中使用Redis小结
Spring Boot中除了对常用的关系型数据库提供了优秀的自动化支持之外,对于很多NoSQL数据库一样提供了自动化配置的支持,包括:Redis, MongoDB, 等. Redis简单介绍 Redi ...
- Spring Boot中使用redis的发布/订阅模式
原文:https://www.cnblogs.com/meetzy/p/7986956.html redis不仅是一个非常强大的非关系型数据库,它同时还拥有消息中间件的pub/sub功能,在sprin ...
- Spring Boot 中集成 Redis 作为数据缓存
只添加注解:@Cacheable,不配置key时,redis 中默认存的 key 是:users::SimpleKey [](1.redis-cli 中,通过命令:keys * 查看:2.key:缓存 ...
- spring boot 中使用 Redis 与 Log
spring boot + mybatis + redis 配置 1.application.yml #配置访问的URLserver: servlet-path: /web port: spring: ...
- Spring Boot中使用Redis数据库
引入依赖 Spring Boot提供的数据访问框架Spring Data Redis基于Jedis.可以通过引入spring-boot-starter-redis来配置依赖关系. <depend ...
- 学习Spring Boot:(十七)Spring Boot 中使用 Redis
前言 Redis 1 是一个由Salvatore Sanfilippo写的key-value存储系统. edis是一个开源的使用ANSI C语言编写.遵守BSD协议.支持网络.可基于内存亦可持久化的日 ...
- Spring Boot中使用Redis
一.定义工程 创建一个spring boot模块 二.修改pom文件 在pom文件中添加Spring Boot与Redis整合依赖 <dependencies> <!--spring ...
- spring boot中集成Redis
1 pom.xml文件中添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <arti ...
- 【redis】spring boot中 使用redis hash 操作 --- 之 使用redis实现库存的并发有序操作
示例: @Autowired StringRedisTemplate redisTemplate; @Override public void dealRedis(Dealer dealer) { d ...
随机推荐
- Android ListView 几个重要属性
Android ListView 几个重要属性http://blog.csdn.net/avenleft/article/details/7334060 android:transcriptMode= ...
- redis3.2装完后 其它机子访问爆protocol error, got 'n' as reply type byte
服务器上装了reids3.2版本,配置文件中已将bind的选项注释掉, linux的iptables的redis端口也开放 其它机子的PHP访问redis爆“protocol error, got ' ...
- go数据类型之基本类型
基本数据类型 数值型 整数 int 64位操作系统默认为int64,32位操作系统为int32,但是类型检查时时int int8 (byte 1字节) int16 (short 2字节) int32 ...
- eclipse安装activiti5.18.0工作流插件 以及安装过程中activiti插件出现的问题及解决
转: eclipse安装activiti5.18.0工作流插件 以及安装过程中activiti插件出现的问题及解决 2017年05月04日 18:44:21 JJ_nan 阅读数:2773 版权声 ...
- BRIEF特征点描述子
简介 BRIEF是2010年的一篇名为<BRIEF:Binary Robust Independent Elementary Features>的文章中提出,BRIEF是对已检测到的特征点 ...
- linux Git版本控制学习与Git服务器搭建
来源地址 要随时掌握工作区的状态,使用git status命令. 如果git status告诉你有文件被修改过,用git diff可以查看修改内容. 初始化一个Git仓库,使用git init命令. ...
- ubuntu thrift
1.下载 http://www.apache.org/dyn/closer.cgi?path=/thrift/0.9.2/thrift-0.9.2.tar.gz 2.解压 tar -xvf thrif ...
- timeshift 安装使用说明
https://blog.csdn.net/hunter___/article/details/79751379 这里介绍一个可视化的备份软件:Timeshift,它不只能备份你的个人文件夹或应用程序 ...
- Java基础-Collection子接口之Set接口
Java基础-Collection子接口之Set接口 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 学习Collection接口时,记得Collection中可以存放重复元素,也可 ...
- day15 接口与异常
接口 是一种独立于类的新机制,它关注的是行为. 接口的意义就体现在——让没有继承关系的类共享这些行为,各有各的具体实现. 设计上:当我们发现一个或多个类的方法相同,又没有继承关系,则考虑接口. 命名: ...