SpringBoot2.x 整合Spring-Session实现Session共享
SpringBoot2.x 整合Spring-Session实现Session共享
1.前言
发展至今,已经很少还存在单服务的应用架构,不说都使用分布式架构部署, 至少也是多点高可用服务。在多个服务器的情况下,Seession共享就是必须面对的问题了。
解决Session共享问题,大多数人的思路都是比较清晰的, 将需要共享的数据存在某个公共的服务中,如缓存。很多人都采用的Redis,手动将Session存在Redis,需要使用时,再从Redsi中读取数据。毫无疑问,这种方案是可行的,只是在手动操作的工作量确实不少。
LZ在这里采用的Spring-Session来实现。它使用代理过滤器,将Session操作拦截,自动将数据同步到Redis中,以及自动从Redis读取数据。从此,操作分布式的Session就像操作单服务的Session一样,可以为所欲为了。
2.实践
2.1 创建工程
使用idea创建SpringBoot工程, 添加组件Web、Spring Session和Redis。 我这里idea是2019版本,SpringBoot是2.1.6。

pom.xml文件
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2.2 配置Redis
spring:
redis:
port: 6379
password: xofcO46Fy
host: 10.17.153.104
server:
port: 9090
2.3 测试
代码实现
package com.xiaoqiang.sessionshare.web;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
/**
* SessionShareController <br>
* 〈session共享控制器〉
*
* @author XiaoQiang
* @create 2019-7-6
* @since 1.0.0
*/
@RestController
@RequestMapping(value = "/session")
public class SessionShareController {
@Value("${server.port}")
Integer port;
@GetMapping(value = "/set")
public String set(HttpSession session){
session.setAttribute("user","wangwq8");
return String.valueOf(port);
}
@GetMapping(value = "get")
public String get(HttpSession session){
return "用户:"+session.getAttribute("user")+",端口:"+port;
}
}
maven package打包发布到服务器服务器,过程略。
分别使用9090 9091端口启动项目。
nohup java -jar sessionshare-0.0.1-SNAPSHOT.jar --server.port=9090 &
nohup java -jar sessionshare-0.0.1-SNAPSHOT.jar --server.port=9091 &
先访问http://10.17.158.136:9090/session/set,在9090这个服务的session保存用户变量;

然后再访问http://10.17.158.136:9091/session/get,从session中获取得到用户信息。

从上面样例,可以看出session已经实现了共享,只是测试过程是需要手动切换服务。为了更好地模式真实项目环境,为此,我们配置Nginx,来进行测试。
2.4 配置Nginx
在Nginx安装目录conf下,编辑nginx.conf,
upstream tomcatServer {
server 10.17.158.136:9092 weight=1;
server 10.17.158.136:9091 weight=2;
}
server {
listen 9000;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://tomcatServer;
proxy_redirect default;
#root html;
#index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
在这里我们只需要配置简单的负载均衡,端口是9000。所有localhost:9000都会按一定策略(这里是按权重分发,配置weight=1一样,随机分发的;nginx默认是轮询策略)分发到上游服务upstream配置的服务上。
配置完成后,启动Nginx;
/apps/test/software/nginx/nginx-1.6.2/sbin/nginx
首先访问http://10.17.158.136:9000/session/set,向seesion中保存数据,从下图中可知9090端口的服务处理了该请求。

然后在访问/get请求,是从9091端口的服务获取得到的用户信息,至此,测试完成。

3.总结
本文主要是Spring Session的简单使用,从上面可以看出,除了引入了Spring Session的jar, 其他方面,不管是代码还是配置,都与之没有什么关联,就相当于在操作最常用的HttpSession,在实际项目中用起来也是相当方便。
样例已上传github,地址:https://github.com/lanxuan826/sample-library/tree/master/sessionshare,有兴趣可下载测试。
另外,此文是从松哥博客中得到启示,在此推荐:https://blog.csdn.net/u012702547/article/list/2?
,还推荐一篇关于Spring Session原理的博客:https://blog.csdn.net/u010648555/article/details/79491988
SpringBoot2.x 整合Spring-Session实现Session共享的更多相关文章
- SpringBoot安全篇Ⅵ --- 整合Spring Security
知识储备: 关于SpringSecurity的详细学习可以查看SpringSecurity的官方文档. Spring Security概览 应用程序的两个主要区域是"认证"和&qu ...
- 使用Spring Session和Redis解决分布式Session跨域共享问题
http://blog.csdn.net/xlgen157387/article/details/57406162 使用Spring Session和Redis解决分布式Session跨域共享问题
- 170222、使用Spring Session和Redis解决分布式Session跨域共享问题
使用Spring Session和Redis解决分布式Session跨域共享问题 原创 2017-02-27 徐刘根 Java后端技术 前言 对于分布式使用Nginx+Tomcat实现负载均衡,最常用 ...
- Spring Session解决Session共享
1. 分布式Session共享 在分布式集群部署环境下,使用Session存储用户信息,往往出现Session不能共享问题. 例如:服务集群部署后,分为服务A和服务B,当用户登录时负载到服务A ...
- Spring Boot 使用 Redis 共享 Session 代码示例
参考资料 博客:spring boot + redis 实现session共享 1. 新建 Maven 工程 我新建 spring-boot-session-redis maven 工程 2. 引入 ...
- 【转】centos安装memcached+php多服务器共享+session多机共享问题
参考博文: centos安装memcached 源码安装 Yum安装Memcache Memcached内存分配优化及使用问题 <转>php+memcached 实现session共享 P ...
- Apache shiro集群实现 (六)分布式集群系统下的高可用session解决方案---Session共享
Apache shiro集群实现 (一) shiro入门介绍 Apache shiro集群实现 (二) shiro 的INI配置 Apache shiro集群实现 (三)shiro身份认证(Shiro ...
- 修改记录-优化后(springboot+shiro+session+redis+ngnix共享)
1.普通用户实现redis共享session 1.配置 #cache指定缓存类型 spring.cache.type=REDIS #data-redis spring.redis.database=1 ...
- 【转载】spring mvc 使用session
http://home.51.com/gaoyangboy/diary/item/10036382.html Spring2.5 访问 Session 属性的四种策略 Posted on 2008-1 ...
随机推荐
- nginx 负载均衡,多站点共享Session
原文:nginx 负载均衡,多站点共享Session nginx 负载均衡,多站点共享Session 多站点共享Session常见的作法有: 使用.net自动的状态服务(Asp.net State S ...
- 避免用户重复点击按钮(使用Enable:=False,消息繁忙时会有堵塞的问题,只能改用Sleep)
// 现象描述:// 用户点击按钮后程序开始繁忙工作,这时候用户不知道是否成功,就继续点几次// 采用Enalbe = false ... = true的方式发现还会触发点击,分析原因如下 ...
- Delphi berlin ShowMessage的改进与使用
这个版本的delphi对ShowMessage进行了重新实现,更好的适应移动跨平台,即在移动平台下是异步执行的,而在Windows及os X下是同步执行的,如果自己控制是否异步显示对话框,也可以通过T ...
- 使用 acl_cpp 的 HttpServlet 类及服务器框架编写WEB服务器程序(系列文章)
在 <用C++实现类似于JAVA HttpServlet 的编程接口 > 文章中讲了如何用 HttpServlet 等相关类编写 CGI 程序,于是有网友提出了 CGI 程序低效性,不错, ...
- jmeter报告分析工具
一直以来做性能测试都是用jmeter和LR,当然还有一些自己写测试脚本,LR不用说,分析结果那个组件杠杠的!但是jmeter毕竟是开源的,所以分析查看结果不像LR那样自带图形神马的,虽然可以自己写脚本 ...
- C语言实现贪吃蛇
日期:2018.9.11 用时:150min 项目:贪吃蛇(C语言--数组 结构体实现) 开发工具:vs2013 关键知识:数组,结构体,图形库,键位操作 源代码: #include<std ...
- PATA 1009. Product of Polynomials (25)
1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- python函数之enumerate()
enumrate 语法: # enumerate(sequence, [start=]) 参数:# sequence -- 一个序列.迭代器或其他支持迭代对象.# start -- 下标起始位置. 使 ...
- hive 常用的 join 操作 实例
test_a 表 id value 1 java 2 python 3 c++ test_b 表 id value 1 java 2 go 3 php 4 c++ 1. join 计算的是笛卡尔积,不 ...
- Python基础-组织列表
1.字符串的几个常用方法 name = ''adb lovelace '' name.title() 首字母大写 name.upper() 全部大写 name.lower() 全部小写 2.组织列表操 ...