欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

redis中session會話共享的三種方案

 更新時間:2025年08月05日 11:44:09   作者:morris131  
本文探討了分布式系統(tǒng)中Session共享的三種解決方案,包括粘性會話、Session復(fù)制以及基于Redis的集中存儲,具有一定的參考價值,感興趣的可以了解一下

在分布式系統(tǒng)架構(gòu)中,用戶請求可能被負(fù)載均衡器分發(fā)到不同的服務(wù)器節(jié)點。如果用戶的第一次請求落在服務(wù)器A并創(chuàng)建了Session,而第二次請求被路由到服務(wù)器B,服務(wù)器B無法識別該用戶的Session狀態(tài),導(dǎo)致用戶需要重新登錄,這顯然是災(zāi)難性的用戶體驗。

三種解決方案

粘性會話(Sticky Sessions)

例如在Nginx的負(fù)載均衡策略中,通過IP哈希等策略將同一個ip的用戶請求固定到同一服務(wù)器中,這樣session自然也沒有失效。

缺點:單點故障風(fēng)險高(服務(wù)器宕機導(dǎo)致Session丟失);擴容時Rehash引發(fā)路由混亂。

Session復(fù)制

例如在Tomcat集群中實現(xiàn)Session復(fù)制,需通過修改配置文件使不同節(jié)點間自動同步會話數(shù)據(jù)。集群內(nèi)所有服務(wù)器實時同步Session數(shù)據(jù)。

缺點:同步開銷隨服務(wù)器數(shù)量指數(shù)級增長,引發(fā)網(wǎng)絡(luò)風(fēng)暴和內(nèi)存浪費。

redis統(tǒng)一存儲

SpringBoot整合Spring Session,通過redis存儲方式實現(xiàn)session共享。

通過集中存儲Session(如Redis),實現(xiàn):

  • 無狀態(tài)擴展:新增服務(wù)器無需同步Session,直接訪問中央存儲。
  • 高可用性:即使單服務(wù)器宕機,會話數(shù)據(jù)仍可從Redis恢復(fù),用戶無感知。
  • 數(shù)據(jù)一致性:所有服務(wù)器讀寫同一份Session數(shù)據(jù),避免狀態(tài)沖突

Spring Session + Redis集成

添加依賴

在pom.xml中引入關(guān)鍵依賴:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session-data-redis</artifactId>
</dependency>

配置Redis連接

在application.properties中加上Redis的配置:

spring:
  data:
    redis:
      host: localhost
      port: 6379

redis配置類

需要注入一個名為springSessionDefaultRedisSerializer的序列化對象,用于在redis中寫入對象時進行序列化,不然session中存入對象會拋出異常。

package com.morris.redis.demo.session;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;

@Configuration
public class RedisConfig {

    @Bean
    public GenericJackson2JsonRedisSerializer springSessionDefaultRedisSerializer() {
        // 需要注入一個名為springSessionDefaultRedisSerializer的序列化對象
        // 不然session中存入對象會拋出異常
        return new GenericJackson2JsonRedisSerializer();
    }
}

不需要顯示的通過注解@EnableRedisHttpSession來開啟session共享。

使用Session

package com.morris.redis.demo.session;

import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;

@RestController
public class AuthController {

    @PostMapping("/login")
    public String login(HttpSession session, @RequestBody User user) {
        // 驗證用戶憑證...
        session.setAttribute("currentUser", user);
        return "登錄成功,SessionID:" + session.getId();
    }

    @GetMapping("/profile")
    @ResponseBody
    public User profile(HttpSession session) {
        // 任意服務(wù)節(jié)點都能獲取到相同Session
        return (User) session.getAttribute("currentUser");
    }
}

session共享驗證

調(diào)用登錄接口:

$ curl --location --request POST 'http://172.23.208.1:8080/login' --header 'Content-Type: application/json' --data-raw '{"name": "morris"}' -v
Note: Unnecessary use of -X or --request, POST is already inferred.
*   Trying 172.23.208.1:8080...
* TCP_NODELAY set
* Connected to 172.23.208.1 (172.23.208.1) port 8080 (#0)
> POST /login HTTP/1.1
> Host: 172.23.208.1:8080
> User-Agent: curl/7.68.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 18
>
* upload completely sent off: 18 out of 18 bytes
* Mark bundle as not supporting multiuse
< HTTP/1.1 200
< Set-Cookie: SESSION=ZTE0Yjc5NjItODFiZS00ZGYwLWE0NDktYTBjNmQ4ZjUxYmYy; Path=/; HttpOnly; SameSite=Lax
< Content-Type: text/plain;charset=UTF-8
< Content-Length: 63
< Date: Tue, 24 Jun 2025 03:23:52 GMT
<
* Connection #0 to host 172.23.208.1 left intact
登錄成功,SessionID:e14b7962-81be-4df0-a449-a0c6d8f51bf2

可以看到返回的響應(yīng)頭中帶有cookie,后續(xù)請求需要帶上這個cookie去請求接口才能識別出用戶。

查詢用戶信息:

$ curl --location --request GET 'http://172.23.208.1:8080/profile' --cookie 'SESSION=ZTE0Yjc5NjItODFiZS00ZGYwLWE0NDktYTBjNmQ4ZjUxYmYy'
{"name":"morris"}

可以修改端口再啟動一個服務(wù),換個服務(wù)查詢用戶信息:

$ curl --location 'http://172.23.208.1:8082/profile' --cookie 'SESSION=ZTE0Yjc5NjItODFiZS00ZGYwLWE0NDktYTBjNmQ4ZjUxYmYy'
{"name":"morris"}

高級配置

自定義Cookie配置(支持跨域)

@Bean
public CookieSerializer cookieSerializer() {
    DefaultCookieSerializer serializer = new DefaultCookieSerializer();
    serializer.setCookieName("JSESSIONID");
    serializer.setDomainNamePattern("example.com");
    serializer.setCookiePath("/");
    return serializer;
}

Spring Session核心原理

SessionAutoConfiguration

這就是為什么不需要使用注解@EnableRedisHttpSession來開啟session共享。

SessionAutoConfiguration類中會引入RedisSessionConfiguration。

@Configuration(proxyBeanMethods = false)
@ConditionalOnMissingBean(SessionRepository.class)
@Import({ RedisSessionConfiguration.class, JdbcSessionConfiguration.class, HazelcastSessionConfiguration.class,
    MongoSessionConfiguration.class })
static class ServletSessionRepositoryConfiguration {

}

RedisSessionConfiguration類中會引入RedisHttpSessionConfiguration:

@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(prefix = "spring.session.redis", name = "repository-type", havingValue = "default", matchIfMissing = true)
@Import(RedisHttpSessionConfiguration.class)
static class DefaultRedisSessionConfiguration {

而注解@EnableRedisHttpSession引入的配置類也是RedisSessionConfiguration:

@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@Target({ java.lang.annotation.ElementType.TYPE })
@Documented
@Import(SpringHttpSessionConfiguration.class)
public @interface EnableSpringHttpSession {

}

SessionRepositoryFilter

自定義過濾器SessionRepositoryFilter攔截所有請求,透明地替換了Servlet容器原生的HttpSession實現(xiàn)。

將請求包裝為SessionRepositoryRequestWrapper:

protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
    throws ServletException, IOException {
  request.setAttribute(SESSION_REPOSITORY_ATTR, this.sessionRepository);

  SessionRepositoryRequestWrapper wrappedRequest = new SessionRepositoryRequestWrapper(request, response);
  SessionRepositoryResponseWrapper wrappedResponse = new SessionRepositoryResponseWrapper(wrappedRequest,
      response);

  try {
    filterChain.doFilter(wrappedRequest, wrappedResponse);
  }
  finally {
    wrappedRequest.commitSession();
  }
}

HttpServletRequestWrapper

HttpServletRequestWrapper中重寫getSession()方法實現(xiàn)session會話替換。

public HttpSessionWrapper getSession(boolean create) {
	HttpSessionWrapper currentSession = getCurrentSession();
	if (currentSession != null) {
		return currentSession;
	}
	S requestedSession = getRequestedSession();
	if (requestedSession != null) {
		if (getAttribute(INVALID_SESSION_ID_ATTR) == null) {
			requestedSession.setLastAccessedTime(Instant.now());
			this.requestedSessionIdValid = true;
			currentSession = new HttpSessionWrapper(requestedSession, getServletContext());
			currentSession.markNotNew();
			setCurrentSession(currentSession);
			return currentSession;
		}
	}
	else {
		// This is an invalid session id. No need to ask again if
		// request.getSession is invoked for the duration of this request
		if (SESSION_LOGGER.isDebugEnabled()) {
			SESSION_LOGGER.debug(
					"No session found by id: Caching result for getSession(false) for this HttpServletRequest.");
		}
		setAttribute(INVALID_SESSION_ID_ATTR, "true");
	}
	if (!create) {
		return null;
	}
	if (SessionRepositoryFilter.this.httpSessionIdResolver instanceof CookieHttpSessionIdResolver
			&& this.response.isCommitted()) {
		throw new IllegalStateException("Cannot create a session after the response has been committed");
	}
	if (SESSION_LOGGER.isDebugEnabled()) {
		SESSION_LOGGER.debug(
				"A new session was created. To help you troubleshoot where the session was created we provided a StackTrace (this is not an error). You can prevent this from appearing by disabling DEBUG logging for "
						+ SESSION_LOGGER_NAME,
				new RuntimeException("For debugging purposes only (not an error)"));
	}
	S session = SessionRepositoryFilter.this.sessionRepository.createSession();
	session.setLastAccessedTime(Instant.now());
	currentSession = new HttpSessionWrapper(session, getServletContext());
	setCurrentSession(currentSession);
	return currentSession;
}

RedisSessionRepository

RedisSessionRepository負(fù)責(zé)創(chuàng)建RedisSession。

public RedisSession createSession() {
	MapSession cached = new MapSession(this.sessionIdGenerator);
	cached.setMaxInactiveInterval(this.defaultMaxInactiveInterval);
	RedisSession session = new RedisSession(cached, true);
	session.flushIfRequired();
	return session;
}

RedisSession

session保存時使用的是sessionRedisOperations,其實就是RedisTemplate,這個RedisTemplate是spring session自己創(chuàng)建的,而不是使用的項目中的。

private void save() {
			saveChangeSessionId();
			saveDelta();
			if (this.isNew) {
				this.isNew = false;
			}
		}

private void saveDelta() {
  if (this.delta.isEmpty()) {
    return;
  }
  String key = getSessionKey(getId());
  RedisSessionRepository.this.sessionRedisOperations.opsForHash().putAll(key, new HashMap<>(this.delta));
  RedisSessionRepository.this.sessionRedisOperations.expireAt(key,
      Instant.ofEpochMilli(getLastAccessedTime().toEpochMilli())
        .plusSeconds(getMaxInactiveInterval().getSeconds()));
  this.delta.clear();
}

到此這篇關(guān)于redis中session會話共享的三種方案的文章就介紹到這了,更多相關(guān)redis session會話共享內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • Redis集群(cluster模式)搭建過程

    Redis集群(cluster模式)搭建過程

    文章介紹了Redis集群的概念、使用原因和搭建方法,Redis集群通過分區(qū)實現(xiàn)數(shù)據(jù)水平擴容,提供了一定的可用性,文章詳細(xì)闡述了集群的連接方式,解釋了如何分配節(jié)點,并提供了詳細(xì)的集群搭建步驟,包括創(chuàng)建節(jié)點、清空數(shù)據(jù)、修改配置、啟動節(jié)點、配置集群等
    2024-10-10
  • Redis查看KEY的數(shù)據(jù)類型的方法和步驟

    Redis查看KEY的數(shù)據(jù)類型的方法和步驟

    在Redis中,可以使用 TYPE 命令來查看指定key的數(shù)據(jù)類型,該命令會返回存儲在指定key中的值的數(shù)據(jù)類型,本文給大家介紹了具體的使用方法和步驟,感興趣的朋友可以參考下
    2024-04-04
  • 關(guān)于redis可視化工具讀取數(shù)據(jù)亂碼問題

    關(guān)于redis可視化工具讀取數(shù)據(jù)亂碼問題

    大家來聊一聊在日常操作redis時用的是什么工具,redis提供的一些命令你都了解了嗎,今天通過本文給大家介紹redis可視化工具讀取數(shù)據(jù)亂碼問題,感興趣的朋友跟隨小編一起看看吧
    2021-07-07
  • redission分布式鎖防止重復(fù)初始化問題

    redission分布式鎖防止重復(fù)初始化問題

    這篇文章主要介紹了redission分布式鎖防止重復(fù)初始化問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • 在K8s上部署Redis集群的方法步驟

    在K8s上部署Redis集群的方法步驟

    這篇文章主要介紹了在K8s上部署Redis集群的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • redis持久化的介紹

    redis持久化的介紹

    今天小編就為大家分享一篇關(guān)于redis持久化的介紹,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • Redis獲取某個大key值的腳本實例

    Redis獲取某個大key值的腳本實例

    這篇文章主要給大家分享介紹了關(guān)于Redis獲取某個大key值的一個腳本實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-04-04
  • 詳解如何清理Redis內(nèi)存碎片

    詳解如何清理Redis內(nèi)存碎片

    操作系統(tǒng)的剩余空間總量足夠,但申請一塊N字節(jié)連續(xù)地址的空間時,剩余內(nèi)存空間中沒有大小為N字節(jié)的連續(xù)空間,那么這些剩余內(nèi)存空間中,小于N字節(jié)的連續(xù)內(nèi)存空間就是內(nèi)存碎片,本文詳細(xì)介紹了如何清理Redis內(nèi)存碎片,需要的朋友可以參考一下
    2023-04-04
  • 排查Redis大key的方法總結(jié)

    排查Redis大key的方法總結(jié)

    這篇文章主要介紹了排查Redis大key的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-08-08
  • antd為Tree組件標(biāo)題附加操作按鈕功能

    antd為Tree組件標(biāo)題附加操作按鈕功能

    這篇文章主要介紹了antd為Tree組件標(biāo)題附加操作按鈕功能,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-08-08

最新評論