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共享的更多相关文章

  1. SpringBoot安全篇Ⅵ --- 整合Spring Security

    知识储备: 关于SpringSecurity的详细学习可以查看SpringSecurity的官方文档. Spring Security概览 应用程序的两个主要区域是"认证"和&qu ...

  2. 使用Spring Session和Redis解决分布式Session跨域共享问题

    http://blog.csdn.net/xlgen157387/article/details/57406162 使用Spring Session和Redis解决分布式Session跨域共享问题

  3. 170222、使用Spring Session和Redis解决分布式Session跨域共享问题

    使用Spring Session和Redis解决分布式Session跨域共享问题 原创 2017-02-27 徐刘根 Java后端技术 前言 对于分布式使用Nginx+Tomcat实现负载均衡,最常用 ...

  4. Spring Session解决Session共享

    1. 分布式Session共享   在分布式集群部署环境下,使用Session存储用户信息,往往出现Session不能共享问题.   例如:服务集群部署后,分为服务A和服务B,当用户登录时负载到服务A ...

  5. Spring Boot 使用 Redis 共享 Session 代码示例

    参考资料 博客:spring boot + redis 实现session共享 1. 新建 Maven 工程 我新建 spring-boot-session-redis maven 工程 2. 引入 ...

  6. 【转】centos安装memcached+php多服务器共享+session多机共享问题

    参考博文: centos安装memcached 源码安装 Yum安装Memcache Memcached内存分配优化及使用问题 <转>php+memcached 实现session共享 P ...

  7. Apache shiro集群实现 (六)分布式集群系统下的高可用session解决方案---Session共享

    Apache shiro集群实现 (一) shiro入门介绍 Apache shiro集群实现 (二) shiro 的INI配置 Apache shiro集群实现 (三)shiro身份认证(Shiro ...

  8. 修改记录-优化后(springboot+shiro+session+redis+ngnix共享)

    1.普通用户实现redis共享session 1.配置 #cache指定缓存类型 spring.cache.type=REDIS #data-redis spring.redis.database=1 ...

  9. 【转载】spring mvc 使用session

    http://home.51.com/gaoyangboy/diary/item/10036382.html Spring2.5 访问 Session 属性的四种策略 Posted on 2008-1 ...

随机推荐

  1. SqlServer批量压缩数据库日志-多数据库批量作业,批量备份还原

    原文:SqlServer批量压缩数据库日志-多数据库批量作业,批量备份还原 --作业定时压缩脚本 多库批量操作 DECLARE @DatabaseName NVARCHAR(50) DECLARE @ ...

  2. PowerShell将Windows store应用程序安装为开发者模式

    原文: PowerShell将Windows store应用程序安装为开发者模式 在本地部署Windows 商店应用程序时,我们会遇到Add-AppDevPackage.ps1脚本,这个脚本和所在安装 ...

  3. Linux C/C++编程手册查阅方法

    Linux Programmer's Manual & User Commands https://www.kernel.org/doc/man-pages/ 搜索框输入epoll调用搜索引擎 ...

  4. 麻省理工的 Picture 语言:代码瘦身的秘诀

    直击现场 如今,机器学习算法已经进入了主流的计算机,而麻省理工学院正在研究一款让每日的编程变得更加简单的技术. MIT 研究者将在六月发布一款新的叫做 Picture 的编程语言,当计算机在视频或者图 ...

  5. FMX中实现PostMessage的方法

    首先,做为一个从Windows下转过来的开发人员,许多时候,我们喜欢用PostMessage来触发一些异步执行的代码,但遗憾的是,FMX做为EMB的跨平台库的基础,只提供了SendMessage方法, ...

  6. return view 详解 MVC

    1.return View(); 返回值 类型:System.Web.Mvc.ViewResult将视图呈现给响应的 View() 结果. 注释 View() 类的此方法重载将返回一个具有空 View ...

  7. Spring Boot配置篇(基于Spring Boot 2.0系列)

    1:概述 SpringBoot支持外部化配置,配置文件格式如下所示: properties files yaml files environment variables command-line ar ...

  8. 通往Google之路:***

    *** & BBR 安装 系统支持:CentOS 6+, Debian 7+, Ubuntu 12+ 内存要求:≥128M --- 前提 满足以上要求的VPS服务器一台 安装基础命令工具:yu ...

  9. 系列教程 之 Android开发之旅

    工作室持续推出Android开发系列教程与案例,供广大朋友分享交流技术经验,帮助喜欢Android的朋友们学习进步: 1. Android开发之旅(1) 之 Android 开发环境搭建 代码之间工作 ...

  10. ES6_09_Generator函数

    Generator函数: 概念: 1.ES6提供的解决异步编程的方案之一 2.Generator函数是一个状态机,内部封装了不同状态的数据, 3.用来生成遍历器对象 4.可暂停函数(惰性求值), yi ...