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. oracle查询指定月份数据

    SELECT * FROM [表名]       where  to_number(to_char([表中日期字段],'mm')) = [要查找的月份]

  2. c++——this指针

    实验1:若类成员函数的形参 和 类的属性,名字相同,通过this指针来解决. 实验2:类的成员函数可通过const修饰,请问const修饰的是谁 #include <iostream> u ...

  3. 移动端适配之sprite雪碧图背景定位

    移动端适配一般我会使用rem进行适配,大致操作就是按照一定尺寸设计稿进行制作,最后将所有px值转换为rem,但是一些手机背景精灵图(cssSprite)就会出现一些误差(1px左右),如果公司要求不高 ...

  4. 添加Image Stream(转)参考 开源容器云OpenShift

    Image Stream是一组镜像的集合,可以在一个Image Stream中定义一些名称及标签,并定义这些名字及标签指向的具体镜像. 使用Image Stream的目的是方便地将一组相关联的镜像进行 ...

  5. json和jsonp的问题

    本文转载自:http://www.cnblogs.com/dowinning/archive/2012/04/19/json-jsonp-jquery.html 前言: 说到AJAX就会不可避免的面临 ...

  6. highchart本地化导出图片

    因为项目执行在内容,并且本身自带的功能是想highcharts  server写文件然后再下载的,所以 highchart本地化导出图片 就非常须要. 第一步改动export.js 里的URl 在在e ...

  7. 开源 免费 java CMS - FreeCMS1.9 全文检索

    项目地址:http://code.google.com/p/freecms/ 全文检索 从FreeCMS 1.7開始支持 仅仅有创建过索引的对象才干被lucene类标签查询到. 信息类数据会在信息更新 ...

  8. OO第一次总结作业

    第一次OO博客作业 前言 面向对象课程已经经过了4周的时间.前三次作业全部是关于多项式求导的相关内容,内容由易到难,同时我也开始逐渐深入感受学习面向对象的各项特征,逐渐将自己的编程风格从C向真正的面向 ...

  9. 纯SVG实现的Loading动画,拿走不谢

    转载自:https://blog.csdn.net/wo_shi_ma_nong/article/details/88833828 话不多说,直接上代码. ( 到这里看效果: http://www.v ...

  10. docker的简单使用----适用于新手

    Docker 1.简介 Docker是一个开源的应用容器引擎 将软件编译成一个镜像:然后在镜像里各种软件做好配置,将镜像发布出去,其他的使用这就可以直接使用这个镜像.运行中的这个镜像叫做容器,容器启动 ...