SpringBoot2 使用Spring Session集群
有几种办法:
1.扩展指定server
利用Servlet容器提供的插件功能,自定义HttpSession的创建和管理策略,并通过配置的方式替换掉默认的策略。缺点:耦合Tomcat/Jetty等Servlet容器,不能随意更换容器。
2.利用Filter
利用HttpServletRequestWrapper,实现自己的 getSession()方法,接管创建和管理Session数据的工作。spring-session就是通过这样的思路实现的。
3 利用spring session
Spring Boot中spring session支持方式:
JDBC、MongoDB、Redis、Hazelcast、HashMap
一、引入maven依赖
<properties>
<project.build.sourceEncoding>UTF-</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<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.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependencies>
二、配置application.properties
server.port=8080
spring.redis.host=localhost
spring.redis.port=6379 # spring session使用存储类型
spring.session.store-type=redis
- spirngboot默认就是使用redis方式,如果不想用可以填none。、
三、在启动类中加入@EnableRedisHttpSession 注解

package com.shyroke; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; @EnableCaching
@EnableRedisHttpSession
@SpringBootApplication
public class SpringbootSessionApplication { public static void main(String[] args) {
SpringApplication.run(SpringbootSessionApplication.class, args);
}
}

四、编写控制器

package com.shyroke.controller; import java.util.HashMap;
import java.util.Map; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
@RequestMapping(value = "/")
public class IndexController { @ResponseBody
@RequestMapping(value = "/session")
public Map<String, Object> getSession(HttpServletRequest request) {
request.getSession().setAttribute("username", "admin");
Map<String, Object> map = new HashMap<String, Object>();
map.put("sessionId", request.getSession().getId());
return map;
} @ResponseBody
@RequestMapping(value = "/get")
public String get(HttpServletRequest request) {
String userName = (String) request.getSession().getAttribute("username"); return userName;
} }

五、测试
- 先输入http://localhost:8080/session,在session中设置一个值

- http://localhost:8080/get,获取session中的值

- 复制这个工程,application.properties中的server.port=8081,然后访问“http://localhost:8081/get”,如下获取到了另一个工程中设置的session的值。

SpringBoot2 使用Spring Session集群的更多相关文章
- (十九)SpringBoot之使用Spring Session集群-redis
一.引入maven依赖 <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEnc ...
- Springboot Session集群处理
在集群环境下,常见的基于Session的身份认证就会有一个问题,因为Session是跟着服务器走的,当用户在服务器1登陆成功后,当用户在访问服务器2的时候会因为服务器2没有用户的身份信息而再次跳转到认 ...
- SpringBoot2.0 整合 Redis集群 ,实现消息队列场景
本文源码:GitHub·点这里 || GitEE·点这里 一.Redis集群简介 1.RedisCluster概念 Redis的分布式解决方案,在3.0版本后推出的方案,有效地解决了Redis分布式的 ...
- Springboot2.x集成Redis集群模式
Springboot2.x集成Redis集群模式 说明 Redis集群模式是Redis高可用方案的一种实现方式,通过集群模式可以实现Redis数据多处存储,以及自动的故障转移.如果想了解更多集群模式的 ...
- Spring+quartz集群配置,Spring定时任务集群,quartz定时任务集群
Spring+quartz集群配置,Spring定时任务集群,quartz定时任务集群 >>>>>>>>>>>>>> ...
- Tomcat session集群
author:JevonWei 版权声明:原创作品 环境 tomcatA 172.16.253.108 tomcatB 172.16.253.105 代理服务器 172.16.253.191 Tomc ...
- Spring+quartz集群解决多服务器部署定时器重复执行的问题
一.问题描述 Spring自带的Task虽然能很好使用定时任务,只需要做些简单的配置就可以了.不过如果部署在多台服务器上的时候,这样定时任务会在每台服务器都会执行,造成重复执行. 二.解决方案 Spr ...
- spring boot 集群 + Nginx --- 心得
1.前言 已经掌握了spring cloud 得使用 ,但这是在内部网络做业务 ,现在需要 在外部网络 访问内部网络 服务 ,引入了 服务端负载均衡 Nginx , Nginx 根据预定的策略 ,将请 ...
- spring+redis 集群下的操作
文章就是记录一下工作当中的用到的点,与测试方法以备用,会不断更新. 配置文件spring-redis.xml: <?xml version="1.0" encoding=&q ...
随机推荐
- JS基础-表单元素-新表单元素-js概述
1.表单元素 1.input元素 1.隐藏域和文件选项框 1.隐藏域 <input type="hidden"> 要提交给服务器的数据,但是不想展示给用户看可以放在隐藏 ...
- bundler简介(ruby gem)
簡介 Bundler RubyGem 是包裝.散佈Ruby程式庫的標準方式,相關文件可以參考 RubyGems Guides 的說明,或是 簡介 plugins 中的第二個例子.在使用rails ...
- 2018.11.28 poj3294 Life Forms(后缀数组+双指针)
传送门 后缀数组经典题目. 我们先把所有的字符串都接在一起. 然后求出hththt数组和sasasa数组. 然后对于sasasa数组跑双指针统计答案. 如果双指针包括进去的属于不同字符串的数量达到了题 ...
- 2018.11.08 NOIP模拟 班车(倍增+dfs+bit)
传送门 对于每个点离线处理出向上走2i2^i2i班车到的最上面的点. 然后每个询问(u,v)(u,v)(u,v)先把(u,v)(u,v)(u,v)倍增到刚好走不到lcalcalca的情况(有一个点如果 ...
- tomcat7 安装 windows 服务
tomcat 可以安装成windows 服务,这样 每次启动就不需要启动tomcat了. 具体配置: 1.修改 service.bat 在行首添加 set "JAVA_HOME=E:\jdk ...
- 解决linux系统CentOS下调整home和根分区大小
目标:将VolGroup-lv_home缩小到20G,并将剩余的空间添加给VolGroup-lv_root 1.首先查看磁盘使用情况 [root@localhost ~]# df -h 文件系统 ...
- IntelliJ IDEA 2017版 spring-boot 2.0.5 邮件发送简单实例 (三)
一.搭建SpringBoot项目 详见此文:https://www.cnblogs.com/liuyangfirst/p/8298588.html 注意: 需要添加mail依赖的包,同时还添加了lom ...
- 用IrisSkin2.dll美化你的WinForm
From:http://hi.baidu.com/tr0j4n 在WinForm中,可以方便地给自己的程序添加皮肤,做出各种绚丽的效果,而只需要很简单的几句代码即可搞定,下面来领略下. 前期准备:1. ...
- Android自定义视图一:扩展现有的视图,添加新的XML属性
这个系列是老外写的,干货!翻译出来一起学习.如有不妥,不吝赐教! Android自定义视图一:扩展现有的视图,添加新的XML属性 Android自定义视图二:如何绘制内容 Android自定义视图三: ...
- 20145232 韩文浩 《Java程序设计》第8周学习总结
教材学习内容总结 14 NIO与NIO2 NIO使用频道(channel)来衔接数据节点,对数据区的标记提供了clear(),rewind(),flip(),compact()等高级操作. 想要取得c ...