1.介绍

有关Session的管理方式这里就不再进行讨论,目前无非就是三种单机Session(基于单机内存,无法部署多台机器)、基于Cookie(安全性差)、基于全局的统一Session管理(redis、mysql)等多种方式
针对于像淘宝这种超大型网站来说Session如何管理的就无从得知了、但是可以通过yy的方式想象一下,这种大型架构都需要部署多台认证Server,但是一般来说集中式Session无法存储那么多的Session
那么就可以通过UID分片的形式来存储,不同UID分布在不同的Server上认证即可(纯属猜测丷)

2.快速开始

这里采用的是redis进行集中式Session管理,核心如下依赖

            <dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
<version>1.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>

完整pom.xml


<?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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>springboot-4</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
<version>1.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.4.1.RELEASE</version>
</plugin>
</plugins>
</build> </project>

application.yaml配置

这块主要是通过application.yaml去配置redis的链接信息


server:
port: 8080
spring:
redis:
database: 1
host: localhost
pool:
max-active: 20

开启@EnableRedisHttpSession

通过加上@EnableRedisHttpSession注解,开启redis集中式session管理,所有的session都存放到了redis中


@SpringBootApplication
@EnableRedisHttpSession
public class AppApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(AppApplication.class, args);
}
}

通过源码可知、可以通过设置maxInactiveIntervalInSeconds来设定session的统一过期时间,


@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@Target({ java.lang.annotation.ElementType.TYPE })
@Documented
@Import(RedisHttpSessionConfiguration.class)
@Configuration
public @interface EnableRedisHttpSession {
int maxInactiveIntervalInSeconds() default 1800; String redisNamespace() default ""; RedisFlushMode redisFlushMode() default RedisFlushMode.ON_SAVE;
}

通过redis集中式管理session这种方式在使用上面对客户端是透明的,无需自己操作redis,在使用HttpSession对象的时候直接使用即可


@RestController
public class IndexController {
@GetMapping("/index")
public ResponseEntity index(HttpSession httpSession) {
httpSession.setAttribute("user", "helloword");
return ResponseEntity.ok("ok");
} @GetMapping("/helloword")
public ResponseEntity hello(HttpSession httpSession) {
return ResponseEntity.ok(httpSession.getAttribute("user"));
}
}

3. 其他扩展

SpringBoot Session集中式管理不仅仅限于redis这种方式,目前内部支持HttpSession with Pivotal GemFire、HttpSession with JDBC、HttpSession with Mongo
等多种方式具体可以参考http://docs.spring.io/spring-session/docs/1.2.2.RELEASE/reference/html5/#httpsession-jdbc
本文代码

https://git.oschina.net/wangkang_daydayup/SpringBoot-Learn/tree/master

springboot-4

SpringBoot初始教程之Redis集中式Session管理的更多相关文章

  1. SpringBoot系列教程之Redis集群环境配置

    之前介绍的几篇redis的博文都是基于单机的redis基础上进行演示说明的,然而在实际的生产环境中,使用redis集群的可能性应该是大于单机版的redis的,那么集群的redis如何操作呢?它的配置和 ...

  2. SpringBoot初始教程之Servlet、Filter、Listener配置(七)

    1.介绍 通过之前的文章来看,SpringBoot涵盖了很多配置,但是往往一些配置是采用原生的Servlet进行的,但是在SpringBoot中不需要配置web.xml的 因为有可能打包之后是一个ja ...

  3. SpringBoot初始教程之Servlet、Filter、Listener配置详解

    1.介绍 通过之前的文章来看,SpringBoot涵盖了很多配置,但是往往一些配置是采用原生的Servlet进行的,但是在SpringBoot中不需要配置web.xml的 因为有可能打包之后是一个ja ...

  4. SpringBoot初始教程之Servlet、Filter、Listener配置

    1.介绍通过之前的文章来看,SpringBoot涵盖了很多配置,但是往往一些配置是采用原生的Servlet进行的,但是在SpringBoot中不需要配置web.xml的 因为有可能打包之后是一个jar ...

  5. SpringBoot系列教程之Bean加载顺序之错误使用姿势辟谣

    在网上查询 Bean 的加载顺序时,看到了大量的文章中使用@Order注解的方式来控制 bean 的加载顺序,不知道写这些的博文的同学自己有没有实际的验证过,本文希望通过指出这些错误的使用姿势,让观文 ...

  6. SpringBoot系列教程之Bean之指定初始化顺序的若干姿势

    上一篇博文介绍了@Order注解的常见错误理解,它并不能指定 bean 的加载顺序,那么问题来了,如果我需要指定 bean 的加载顺序,那应该怎么办呢? 本文将介绍几种可行的方式来控制 bean 之间 ...

  7. HDFS集中式缓存管理(Centralized Cache Management)

    Hadoop从2.3.0版本号開始支持HDFS缓存机制,HDFS同意用户将一部分文件夹或文件缓存在HDFS其中.NameNode会通知拥有相应块的DataNodes将其缓存在DataNode的内存其中 ...

  8. redis的hash操作在集中式session中的应用

    在集群部署时,为了高可用性的目的,往往把session进行共享,共享分为两种:session复制和集中式管理. redis在session集中式管理中可以起到比较大的作用. 制约session集中式共 ...

  9. tomcat redis 集群 session共享

    jcoleman/tomcat-redis-session-manager: Redis-backed non-sticky session store for Apache Tomcathttps: ...

随机推荐

  1. Kafka设计解析(十四)Kafka producer介绍

    转载自 huxihx,原文链接 Kafka producer介绍 Kafka 0.9版本正式使用Java版本的producer替换了原Scala版本的producer.本文着重讨论新版本produce ...

  2. MySQL(五)SELECT语句执行顺序

    上一篇讲述了Oracle的SELECT语法的执行顺序,这篇讲述MySQL的SELECT语法的执行顺序.MySQL的SELECT语法的执行顺序和Oracle的基本相同,只是增加了MySQL独有的LIMI ...

  3. oracle 表的创建与管理 约束

    在 Oracle 之中数据表就被称为数据库对象,而对象的操作语法一共有三种:· 创建对象:CREATE 对象类型 对象名称 [选项]:· 删除对象:DROP 对象类型 对象名称 [选项]:· 修改对象 ...

  4. koa2学习笔记02 - 给koa2添加系统日志 —— node日志管理模块log4js

    前言 没有日志系统的后台应用是没有灵魂的, 平时工作中每次我们遇到接口报错的时候, 都会叫后台的童鞋看下怎么回事, 这时后台的童鞋都会不慌不忙的打开一个骚骚的黑窗口. 一串噼里啪啦的命令输进去, 哐哐 ...

  5. Mysql数据库 day1

    Mysql数据库属于关系型数据库(mysql.oracle.sql server),非关系型数据库有DB2.Redis MySQL执行原理,逻辑分层.更改数据库处理引擎 作者:Stanley 罗昊 [ ...

  6. 关于JQ中ready()方法的几种写法总结

    ——习惯贵在坚持,天才在于积累. 好久没写博客的我,似乎是忘记了当初写博客的初衷是要在博客笔记中提升自己的写作能力和积累自己的知识要点. 废话不多说. ready()方法作用: 在页面加载完成后,立即 ...

  7. PhpStorm 回到上次编辑位置的快捷键

    回到上次编辑位置 Ctrl + Alt + <- (向后) Ctrl + Alt + -> (向前) 这个快捷键有时和电脑桌面快捷键冲突.解决办法: win + D 回到电脑桌面,右键-& ...

  8. linux下pcf8563驱动时钟使用

    环境: HelperA64开发板 Linux3.10内核 时间:2019.01.17 目标:PCF8563实时时钟驱动的使用 问题:因为pcf8563的驱动是linux内核自带的,网上也有很多分析的方 ...

  9. WPF的IsSynchronizedWithCurrentItem属性

    如果两个控件都绑定到同一个源(ObservableCollection)集合视图时,该对象会自动绑定到该视图的 CurrentItem.请注意,CollectionViewSource 对象会自动同步 ...

  10. c# multi-ply download ui

    first neet add an user control "DownloadBar": /* Created by SharpDevelop. User: gwang Date ...