前言

今天你们将再一次领略到SpringBoot的开发到底有多快,以及SpringBoot的思想(默认配置)

我们将使用redis存放用户的session,用户session存放策略有很多,有存放到内存的,有存放数据库的,也有存放redis。这里我们使用redis存放,目的是,当服务器重启,用户的session信息也没有丢失。

已经加入我的github模版中:https://github.com/LinkinStars/springBootTemplate

实现

添加gradle依赖

compile group: 'org.springframework.session', name: 'spring-session-data-redis', version: '1.3.1.RELEASE'

新增类

import org.springframework.context.annotation.Configuration;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; /**
* session存放redis*/
@Configuration
@EnableRedisHttpSession
public class RedisSessionConfig {
}

在controller中添加验证代码

//测试session存放redis测试
HttpSession session = request.getSession();
System.out.println("session数据:" + session.getAttribute("xxx"));
session.setAttribute("xxx","123");
System.out.println("session数据:" + session.getAttribute("xxx"));

然后本地启动redis,启动springboot

就可以了

对,你没有听错,就是可以了。这就是springboot的魅力,项目中添加一个功能非常的简单,而且耦合度非常低,如果你有一天不用了,直接把这个类删除就可以了。然后一点,之所以完成了,是因为springboot默认了很多的配置,如默认redis地址为本地,默认端口号为6379等

如果你需要配置当然也是没有问题的,在application.yml进行配置就可以,更多的配置在这里就不列举了。

spring:

#redis相关配置
    redis:

host: localhost

port: 6379

问题和参考

针对redis的使用在之后会进行更新,下面记录出现的一个问题

(error) MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk.

解决方案是:

127.0.0.1:6379> config set stop-writes-on-bgsave-error no

OK

参考来自:https://www.cnblogs.com/anny-1980/p/4582674.html

https://www.cnblogs.com/sweetchildomine/p/6984596.html

在SpringBoot中存放session到Redis的更多相关文章

  1. SpringBoot中Shiro缓存使用Redis、Ehcache

    在SpringBoot中Shiro缓存使用Redis.Ehcache实现的两种方式实例 SpringBoot 中配置redis作为session 缓存器. 让shiro引用 本文是建立在你是使用这sh ...

  2. springboot中使用cache和redis

    知识点:springboot中使用cache和redis (1)springboot中,整合了cache,我们只需要,在入口类上加 @EnableCaching 即可开启缓存 例如:在service层 ...

  3. springboot中各个版本的redis配置问题

    今天在springboot中使用数据库,springboot版本为2.0.2.RELEASE,通过pom引入jar包,配置文件application.properties中的redis配置文件报错,提 ...

  4. springboot 中 集成druid ,redis

    1,导入druid jar包 <!--引入drud--> <dependency> <groupId>com.alibaba</groupId> < ...

  5. SpringBoot中整合Redis、Ehcache使用配置切换 并且整合到Shiro中

    在SpringBoot中Shiro缓存使用Redis.Ehcache实现的两种方式实例 SpringBoot 中配置redis作为session 缓存器. 让shiro引用 本文是建立在你是使用这sh ...

  6. springboot中,使用redisTemplate操作redis

    知识点: springboot中整合redis springboot中redisTemplate的使用 redis存数据时,key出现乱码问题 一:springboot中整合redis (1)pom. ...

  7. 实例讲解Springboot以Repository方式整合Redis

    1 简介 Redis是高性能的NoSQL数据库,经常作为缓存流行于各大互联网架构中.本文将介绍如何在Springboot中整合Spring Data Redis,使用Repository的方式操作. ...

  8. springboot整合redis存放session

    y进入maven依赖: <!--spring boot 与redis应用基本环境配置 --> <dependency> <groupId>org.springfra ...

  9. springboot中使用spring-session实现共享会话到redis(二)

    上篇文章介绍了springboot中集成spring-session实现了将session分布式存到redis中.这篇在深入介绍一些spring-session的细节. 1.session超时: 在t ...

随机推荐

  1. Linux指令--ping

    Linux系统的ping命令是常用的网络命令,它通常用来测试与目标主机的连通性,我们经常会说"ping一下某机器,看是不是开着".不能打开网页时会说"你先ping网关地址 ...

  2. linkin大话设计模式--常用模式总结

    linkin大话设计模式--常用模式总结 一,常用设计模式定义 Abstract Factory(抽象工厂模式):提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类. Adapter( ...

  3. Unity3d 基本设计开发 原则(提高代码可读性)

    参考:http://blog.csdn.net/qq_34134078/article/details/51780356 1.单一原则 即:明确类的定义.通俗来讲,让他们只做一件事,而不是多件事. 提 ...

  4. CRM项目-1模型与站点管理

    一.项目环境 语言:python3 IDE:pycharm 组件:bootstarp,jQuery 二.模型 2.1编写模型 联合唯一 class Meta:显示中文名. 认证使用django自带的U ...

  5. wpf动画同步闪烁

    public class BlinkAnimation : Animatable { /// <summary> /// 单例,保持所有闪烁的动画同步 /// </summary&g ...

  6. web.xml 文件中一般包括 servlet, spring, filter, listenr的配置的加载顺序

    首先可以肯定 加载顺序与他们在web.xml 文件中的先后顺序无关. web.xml 中 listener 和 serverlet 的加载顺序为 先 listener 后serverlet最终得出结果 ...

  7. 学习H5仿制网站时遇到的问题

    学习H5的第二周,开始仿制网站,在本过程中也发现了自己存在的更多问题. 1.TOP块:在制作top中,遇到的第一个问题,一旦top左右的背景色不同该怎样去调整 解决方式:建立两个div取代一个div ...

  8. 使用sed修改配置项的值

    起先我的想法是根据等号来求得配置项所在的行号, sed -n '/aaa/=' config.ini 然后根据行号删除这一行,再增加一行比如行号是9 sed -i  '9d' config.ini s ...

  9. error: Autoconf version 2.67 or higher is required

    error: Autoconf version 2.67 or higher is required 今天linux下遇到这种错误,顺便记录下来. #rpm -qf /usr/bin/autoconf ...

  10. JMETER_16个逻辑控制器详解

    Jmeter逻辑控制器(Logic Controller)介绍: 1. Jmeter官网对逻辑控制器的解释是:"Logic Controllers determine the order i ...