分布式下的session问题
生活随笔
收集整理的這篇文章主要介紹了
分布式下的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: 30mspringmvc使用的話,還是使用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);}// ... }總結
以上是生活随笔為你收集整理的分布式下的session问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 二分法求方程的根_快速求解方程的根——二
- 下一篇: poi excel文档生成与读取