分布式下的session问题

分布式下的session

    • 在分布式项目下
    • 项目中使用

在分布式项目下

session是一种会话状态,是储存在服务器上的,可以区分每个用户。浏览器会保存着对应的信息。浏览器保存的叫cookie。用户访问,浏览器会带上cookie让服务器识别。

如果是单体项目,session是在一个服务器下,所有的用户信息都保存在一个服务器,好做区分。

但是如果在分布式情况下,一个大的系统有很多模块,比如电商项目,有登录权限模块,商品模块,商品检索模块… 每个模块都是在不同的服务器上部署的,如果用户进行登录,session只在一个服务器上保存,其它服务器并不能同步。

痛点:解决session共享的问题。

这里就可以把session存储到一个公用的地方,各个模块都能取到的地方。如数据库, NoSql redis… 等。

而spring session框架正好能解决这个问题,更加简化我们的操作。

项目中使用

可以照着官网来。

pom.xml

        <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><!-- https://docs.spring.io/spring-session/docs/2.2.2.RELEASE/reference/html5/guides/boot-redis.html --><!-- 使用redis管理session --><dependency><groupId>org.springframework.session</groupId><artifactId>spring-session-data-redis</artifactId></dependency>

配置类

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.DefaultCookieSerializer;/*** @author echo lovely* @date 2021/9/26 17:35* @description session 配置*/@EnableRedisHttpSession
@Configuration
public class HttpSessionConfig {// cookie 存储规则 确保在*.domain.com 同一个域名下及子域名@Beanpublic CookieSerializer cookieSerializer() {DefaultCookieSerializer serializer = new DefaultCookieSerializer();serializer.setCookieName("JSESSIONID");// 设置子域共享sessionserializer.setDomainName("yourdomain.com");return serializer;}// RedisSerializer@Beanpublic RedisSerializer<Object> springSessionDefaultRedisSerializer() {// 使用json方式序列化.. 保存用户的session信息return new GenericJackson2JsonRedisSerializer();}}

yaml配置, 配置session以redis存储

spring:redis:host: 192.168.56.10port: 6379session:store-type: redistimeout: 30m

springmvc使用的话,还是使用HttpSession, 设置值就行了。
这个httpSession会经过过滤器,过滤将请求和响应用装饰器模式包装。

原理

public class SessionRepositoryRequestWrapper extends HttpServletRequestWrapper {// 原生requestpublic SessionRepositoryRequestWrapper(HttpServletRequest original) {super(original);}public HttpSession getSession() {return getSession(true);}public HttpSession getSession(boolean createNew) {// create an HttpSession implementation from Spring Session}// ... other methods delegate to the original HttpServletRequest ...
}

经过过滤器后被包装。

public class SessionRepositoryFilter implements Filter {public doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {HttpServletRequest httpRequest = (HttpServletRequest) request;SessionRepositoryRequestWrapper customRequest =new SessionRepositoryRequestWrapper(httpRequest);chain.doFilter(customRequest, response, chain);}// ...
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/420309.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

c++ primer 笔记 (三)

标准库类型string 和 vector &#xff0c;分别定义了大小可变的字符串和集合。 bitset&#xff0c;提供了一个抽象方法来操作位的集合。提供更方便的处理位的方式&#xff08;相对于整型值上的位操作符&#xff09;vector用于保存一组指定类型的对象。&#xff1a;&#xff1a;…

前端学习(1334):mongodb增2

const mongoose require(mongoose); mongoose.connect(mongodb://localhost/playground, { useUnifiedTopology: true }).then(() > console.log(数据库连接成功)).catch(err > console.log(err, 数据库连接失败))//创建集合规则 const courseSchema new mongoose.Sche…

vue登录如何存储cookie_vue保持用户登录状态(各种token存储方式)

而作为前端,存储这些值同样有多种方式,你可以存在Cookie、LocalStorage、SessionStorage或者Vuex状态管理器中,当然他们的作用也不同 怎么设置Cookie HttpResponse来响应对象的cookie,设置好对应的视图和路由,只要通过浏览器访问该路由,浏览器就会自动获取到cookie值并存…

关于fetch api这点事

fetchfetch api文档地址模拟登录demofetch api fetch api 是浏览器的异步可跨域请求。基于XMLHttpRequest, 也就是对原生Ajax请求的包装&#xff0c;以回调的形式展开。 使用方法&#xff1a; fetch(http://example.com/movies.json).then(function(response) {return respo…

ChemBioDraw 制作DMT屏保

&#xff5b;关于DMT&#xff5d;二甲基色胺&#xff08;DMT&#xff09; dimethyltryptamine 第一类精神药品&#xff0c;色胺类致幻剂&#xff0c;药性强。不仅存在于植物中&#xff0c;还以痕量见于人体中&#xff0c; 由色胺-N-转甲基酶催化产生&#xff0c;但具体功能不明…

前端学习(1335):mongoDB导入数据

mongoimport --d playground --c users --file ./user.json 运行结果

力扣刷题01

简单版1. 两数求和2. 回文数3. 将整数倒转4. 罗马转整数1. 两数求和 我用的暴力求和。 package top.bitqian.easy.two_sum;import java.util.Arrays; import java.util.LinkedHashMap; import java.util.Map;/*** 给定一个整数数组 nums 和一个整数目标值 target&#xff0c;…

属性被分为八大类不包括_家庭软装八大类有哪些 软装八大类风格有什么特点...

软装就是西方基本装修的硬装完工之后&#xff0c;放置的一些家具以及装饰用品&#xff0c;那么在家装中软装八大类有哪些呢?下面就和郑州装修网小编一起来了解下吧。软装八大类有哪些软装八大类指的是&#xff1a;家具、灯具、布艺、花艺、地毯、工艺品、墙饰以及床品。 1、家…

前端学习(1336):从数据库查询文档

const mongoose require(mongoose); mongoose.connect(mongodb://localhost/playground, { useUnifiedTopology: true }).then(() > console.log(数据库连接成功)).catch(err > console.log(err, 数据库连接失败))//创建集合规则 const courseSchema new mongoose.Sche…

前端学习(1337):mongoDB文档查询

const mongoose require(mongoose); mongoose.connect(mongodb://localhost/playground, { useUnifiedTopology: true }).then(() > console.log(数据库连接成功)).catch(err > console.log(err, 数据库连接失败))//创建集合规则 const courseSchema new mongoose.Sche…

声速的测量的实验原理和应用_创想智控:光学三角测量系统的测量原理与应用...

测量原理是三角测量&#xff1a;将激光线投影到表面上&#xff0c;并从与照相机的投影方向不同的方向进行观察。表面距离的变化会导致相机上成像线的偏移。这意味着传感器正在获取表面轮廓。光学三角测量是一种易于使用的方法&#xff0c;用于测量到物体的距离而不接触它们&…

PHP 5.4 on CentOS/RHEL 7.0, 6.5 and 5.10 via Yum

PHP 5.4.36 has been released on PHP.net on 18th December 2014, and is also available for CentOS/RHEL 5.10 and 6.5 at Webtatic via Yum. Update 2013-07-21 – A new package “php54w-mysqlnd” has been added as an alternative to “php54w-mysql”. This will ins…

仓库移动_移动式RFID仓库管理解决方案,智能仓库未来应用的会更广泛

在当下的仓储物流行业中&#xff0c;尤其在中国&#xff0c;信息化的应用不断的在行业内深化其作用。因为在新的产业环境下和信息技术的背景下&#xff0c;对企业提出了新的要求。客户要实现更加独特的体验&#xff0c;相关企业需要提高仓储作业效率&#xff0c;提高数据的准确…

Github作为maven私服仓库用

上传jar包到GitHub仓库流程打包一个项目本地使用流程 打包一个项目 distribute_id 是自定义的仓库id git_repo_path是本地git仓库的路径&#xff0c;也是存放编译的路径 使用下面的命令编译并打包到本地仓库 mvn deploy -DaltDeploymentRepository${distribute_id}::default:…

前端学习(1338):mongoDB删除文档

const mongoose require(mongoose); mongoose.connect(mongodb://localhost/playground, { useUnifiedTopology: true }).then(() > console.log(数据库连接成功)).catch(err > console.log(err, 数据库连接失败))//创建集合规则 const courseSchema new mongoose.Sche…

MS SQL SERVER数据库简单回顾

MS SQL SERVER数据库 1.创建数据库create database javateam;2.使用数据库use javateam;3.创建表create table 表名 (字段名 字段类型 主键 字段增长&#xff08;从X开始 , 每次加X个&#xff09;,字段名 字段类型,字段名 字段类型);create table peopleNone(pid…

docker绑定端口主机访问curl: (56) Recv failure: Connection reset by peer

这个问题超简单… 我用的nuxt&#xff0c;项目丢到了node容器里面。然后主机访问不了挂载的端口。 容器内是能curl通的。 然后&#xff0c;经过排查&#xff0c;nuxt只指定了127.0.0.1, host必须是0.0.0.0才能暴露。 https://www.nuxtjs.cn/faq/host-port

fl如何保存再次打开_「Excel技巧」Excel2016如何将自己设计的图表存为模板反复使用?...

现在的Excel系统内置的默认图表类型很丰富&#xff0c;各种各样&#xff0c;但好像也还是满足不了大家的需求。很多人会在插入默认图表后&#xff0c;在默认图表的基础上根据自己的需求进行修改。好不容易修改好的图表&#xff0c;能不能把它保存为模板&#xff0c;用来以后调用…

前端学习(1339):mongodb更新数据文档

const mongoose require(mongoose); mongoose.connect(mongodb://localhost/playground, { useUnifiedTopology: true }).then(() > console.log(数据库连接成功)).catch(err > console.log(err, 数据库连接失败))//创建集合规则 const courseSchema new mongoose.Sche…

[REGEX] 匹配任意字符(包括换行符)

[\s|\S]*转载于:https://www.cnblogs.com/bushe/p/4229522.html