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. C# Encoding.GetEncoding 编码列表

    原文:C# Encoding.GetEncoding 编码列表 代码页 名称 显示名称 37 IBM037 IBM EBCDIC(美国 - 加拿大) 437 IBM437 OEM 美国 500 IBM ...

  2. 如何使用C#创建Windows Webcam应用

    原文:如何使用C#创建Windows Webcam应用 最近想用C#写一个camera的应用.搜索了Google和StackOverflow,发现大部分的sample用了WIA或者DirectShow ...

  3. Sysinternals套件2016年11月更新发布,诸多工具被更新

    Sysinternals 实用程序可帮助您管理.解决和诊断 Windows 系统和应用程序,在 Sysinternals 的 2016 年 11 月更新中,微软增强 在 sysmon 对注册表和文件事 ...

  4. delphi 实现微信开发(1) (使用kbmmw web server)

    原文地址:delphi 实现微信开发(1)作者:红鱼儿 大体思路: 1.用户向服务号发消息,(这里可以是个菜单项,也可以是一个关键词,如:注册会员.) 2.kbmmw web server收到消息,生 ...

  5. 解决C/C++程序执行一闪而过的方法(使用getchar,或者cin.get,不推荐system(“pause”))

    简述 在VS编写控制台程序的时候,包括使用其他IDE(Visual C++)编写C/C++程序,经常会看到程序的执行结果一闪而过,要解决这个问题,可以在代码的最后加上system(“pause”).g ...

  6. 设计模式——(Abstract Factory)抽象工厂“改正为简单工厂”

    设计面向对象软件比较困难,而设计可复用的面向对象软件就更加困难.你必须设计相关类,并设计类的接口和继承之间的关系.设计必须可以解决当前问题,同时必须对将来可能发生的问题和需求也有足够的针对性.掌握面向 ...

  7. C语言实现常用数据结构——栈

    #include<stdio.h> #include<stdlib.h> //用链表实现栈 typedef struct Node { int data; struct Nod ...

  8. java多线程之wait和notify

    多线程中的通信是非常重要的概念,线程直接实现通信就可以并发完成很多复杂工作. java在Object类中就设计了wait()和notify()两个方法,以解决这个问题. 1.释义: wait()方法将 ...

  9. 浅入深出Vue:数据渲染

    今天来正式开始 vue的学习,首当其冲的当然是数据的渲染.毕竟数据就是拿来看的,看看如果使用 vue来展示数据. 为什么渲染 俗话说 "人靠衣装马靠鞍", 那咱们的代码就是得靠 U ...

  10. 记录一次关于Cookie、Json中文乱码的解决方法

    今天工作上遇到一个问题,需要把一个对象集合List<Model>存入一个Cookie,按照原来都封装方法存入都ok,但是到取值都时候中文会变成乱码. 首先,我们可以确认Json和Cooki ...