最近使用spring boot开发一个系统,nginx做负载均衡分发请求到多个tomcat,此时访问页面会把请求分发到不同的服务器,session是存在服务器端,如果首次访问被分发到A服务器,那么session就会被存到A服务器,再次访问时负载均衡会分发到B服务器那么第一次访问的session信息就会获取不到之前的session信息,所以需要实现session共享,还好有spring session,使用简单的配置即可实现session共享,下面介绍下:

1. pom.xml中引入jar包

<!-- Spring Boot Redis 依赖  -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency> <dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency> <dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
</dependency>

2. 添加RedisSessionConfig配置类

在项目的目录中,创建一个java文件(名称随意)即可,我这里名称是RedisSessionConfig.java

@EnableRedisHttpSession这个注解非常最重要,加了它之后,会使用spring的一个拦截器来实现Session共享的操作,而配置的这个Bean,则是让Spring根据配置文件中的配置连到Redis。

SpringSession 需要注意的就是redis需要2.8以上版本,然后开启事件通知,在redis配置文件里面加上

notify-keyspace-events Ex  // 打开此配置,其中Ex表示键事件通知里面的key过期事件,每当有过期键被删除时,会发送通知

或是使用如下命令开启开启事件通知:

redis-cli config set notify-keyspace-events Egx

如果你的Redis不是你自己维护的,比如你是使用阿里云的Redis数据库(我就是这种情况),你不能够更改它的配置,那么可以使用下面的java配置文件即可。

package org.spring.springboot.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.data.redis.config.ConfigureRedisAction;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; @Configuration
// maxInactiveIntervalInSeconds 默认是1800秒过期,这里测试修改为60秒
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 1800)
public class RedisSessionConfig { @Bean
public static ConfigureRedisAction configureRedisAction() {
return ConfigureRedisAction.NO_OP;
}
}

3. 配置redis连接

Spring Boot会自动创建一个RedisConnectionFactory将Spring Session连接到端口6379(默认端口)上localhost上的Redis服务器的连接。在生产环境中,您需要确保更新配置以指向Redis服务器

src/main/resources/application.properties

# Redis 配置
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=192.168.0.1
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=1234

上面三个步骤搞定后,即可session共享,有任何问题欢迎留言沟通哦~

了解更多spring session内容可以访问官网:https://docs.spring.io/spring-session/docs/current/reference/html5/guides/boot-redis.html


下面的是我的公众号二维码图片,欢迎关注,欢迎留言,一起学习,一起进步。

Spring boot集成spring session实现session共享的更多相关文章

  1. Spring Boot集成Spring Data Reids和Spring Session实现Session共享

    首先,需要先集成Redis的支持,参考:http://www.cnblogs.com/EasonJim/p/7805665.html Spring Boot集成Spring Data Redis+Sp ...

  2. SpringBoot系列:Spring Boot集成Spring Cache,使用EhCache

    前面的章节,讲解了Spring Boot集成Spring Cache,Spring Cache已经完成了多种Cache的实现,包括EhCache.RedisCache.ConcurrentMapCac ...

  3. SpringBoot系列:Spring Boot集成Spring Cache,使用RedisCache

    前面的章节,讲解了Spring Boot集成Spring Cache,Spring Cache已经完成了多种Cache的实现,包括EhCache.RedisCache.ConcurrentMapCac ...

  4. Spring Boot 集成spring security4

    项目GitHub地址 : https://github.com/FrameReserve/TrainingBoot Spring Boot (三)集成spring security,标记地址: htt ...

  5. Spring Boot 集成 Spring Security 实现权限认证模块

    作者:王帅@CodeSheep   写在前面 关于 Spring Security Web系统的认证和权限模块也算是一个系统的基础设施了,几乎任何的互联网服务都会涉及到这方面的要求.在Java EE领 ...

  6. Spring boot 集成Spring Security

    依赖jar <dependency> <groupId>org.springframework.cloud</groupId> <artifactId> ...

  7. Spring Boot集成Spring Data Reids和Spring Session实现Session共享(多个不同的应用共用一个Redis实例)

    从Redis的Key入手,比如Spring Session在注解@EnableRedisHttpSession上提供了redisNamespace属性,只需要在这里设置不同的值即可,效果应该是这样的: ...

  8. SpringBoot系列:Spring Boot集成Spring Cache

    一.关于Spring Cache 缓存在现在的应用中越来越重要, Spring从3.1开始定义了org.springframework.cache.Cache和org.springframework. ...

  9. Spring Boot 集成 Spring Security

    1.添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

随机推荐

  1. 详解封装微信小程序组件及小程序坑(附带解决方案)

    一.序 上一篇介绍了如何从零开发微信小程序,博客园审核变智障了,每次代码都不算篇幅,好好滴一篇原创,不到3分钟从首页移出来了.这篇介绍一下组件封装和我的踩坑历程. 二.封装微信小程序可复用组件 首先模 ...

  2. flume安装

    1.flume安装 将压缩包减压到当前目录 tar -zxf apache-flume-1.8.0-bin.tar.gz 配置环境变量  编辑当前目录中的  .bashrc  文件(这影响当前用户的环 ...

  3. HTML图片标签路径解析

    img标签中src属性表示的是引用的图片路径,有两种路径类型: 1. 绝对路径    2. 相对路径. 绝对路径:使用图片在硬盘上的绝对位置来访问图片,通常是从根目录开始,向下一个目录一个目录的寻找. ...

  4. PTA第三次作业

    ---恢复内容开始--- 题目 7-1 计算职工工资 1.设计思路 (1)第一步:观察题意了解各个参数与所需函数在题目中的意义: 第二步:设计算法编写函数,让函数的功能实现题目中所需的功能: 第三步: ...

  5. Win10 安装 VMWare中 MAC OS X的安装,VMWare tools的配置与iOS的Helloworld

    iOS的开发必须在MAC OS X系统下进行,这很蛋疼,现在MACBOOK动不动就上千上万大洋,这足够买台配置怪兽了,好吗?然而,我们是可以通过在VMWare中安装MAC OS X进行iOS开发的.对 ...

  6. MySQL InnoDB 备份与恢复七种方式

    有几种方式: 1 mysqldump, 这种方式不仅适用于InnoDB,还适用于其它类型的存储引擎,如MyISAM.备份的时候将数据库备份成SQL(包含drop,create,insert等语句),恢 ...

  7. Apache Storm 核心概念

    前言: Storm读取实时数据流,并传递给处理单元,最终输出处理后的数据. 下图描述了storm的处理数据的主要结构. 元组(Tuple) :       元组是Storm提供的一个轻量级的数据格式, ...

  8. [Swift]LeetCode476. 数字的补数 | Number Complement

    Given a positive integer, output its complement number. The complement strategy is to flip the bits ...

  9. [Swift]LeetCode1030. 距离顺序排列矩阵单元格 | Matrix Cells in Distance Order

    We are given a matrix with R rows and C columns has cells with integer coordinates (r, c), where 0 & ...

  10. Pytorch入门实例:mnist分类训练

    #!/usr/bin/env python # -*- coding: utf-8 -*- __author__ = 'denny' __time__ = '2017-9-9 9:03' import ...