環球微資訊!Spring Session for Apache Geode 教程

2022-12-06 18:21:05 來源:51CTO博客

1. 簡介

Spring 會話提供了用于管理用戶會話信息的 API 和實現。 它還提供與以下各項的透明集成:

HttpSession- 使它能夠被集群化(即復制) 實現高可用性),而無需綁定到特定于應用程序容器的解決方案。HttpSessionRESTAPI - 允許在協議標頭中提供會話 ID 以使用 RESTful API。WebSocket- 提供在接收 WebSocket 消息時保持活動狀態的能力。HttpSessionWebSession- 允許以應用程序容器中立的方式替換Spring WebFlux。WebSession

簡而言之,Spring Session以應用程序容器中立的方式取代了應用程序容器 通過提供更常見和更健壯的會話實現支持。??javax.servlet.http.HttpSession????HttpSession??


(資料圖)

2. 示例和指南(從這里開始)

如果您想從春季課程開始,最好的起點 與我們的示例應用程序一起使用。

Table 1. Sample Application using Spring Boot

描述

指導

帶有Spring Boot和Apache Geode的HttpSession

演示如何使用 Spring 會話在 Spring Boot 應用程序中使用 Apache Geode 進行管理 使用客戶端/服務器拓撲。??HttpSession??

帶有 Spring Boot 和 Apache Geode Guide 的 HttpSession

HttpSession with Spring Boot 和 Apache Geode using Gfsh

演示如何使用 Spring 會話在 Spring Boot 應用程序中使用 Apache Geode 進行管理 使用客戶端/服務器拓撲。此外,還配置和使用Apache Geode的DataSerialization框架。??HttpSession??

HttpSession with Spring Boot and Apache Geode using Gfsh Guide

HttpSession with Spring Boot 和 Apache Geode using Scoped Proxies

演示如何使用 Spring 會話在 Spring Boot 應用程序中使用 Apache Geode 進行管理 使用客戶端/服務器拓撲。該應用程序還使用 Spring 請求和會話范圍的代理 bean。??HttpSession??

HttpSession with Spring Boot and Apache Geode using Scoped Proxies Guide

Table 2. Sample Applications usingSpring’sJava-based configuration

描述

指導

HttpSession with Apache Geode (Client/Server)

演示如何使用 Spring 會話通過客戶端/服務器拓撲來管理 Apache Geode。??HttpSession??

HttpSession with Apache Geode (Client/Server) Guide

HttpSession with Apache Geode (P2P)

演示如何使用春季會話通過 P2P 拓撲與 Apache Geode 進行管理。??HttpSession??

HttpSession with Apache Geode (P2P) Guide

Table 3. Sample Applications usingSpring’sXML-based configuration

描述

指導

HttpSession with Apache Geode (Client/Server)

演示如何使用 Spring 會話通過客戶端/服務器拓撲來管理 Apache Geode。??HttpSession??

HttpSession with Apache Geode (Client/Server) Guide

HttpSession with Apache Geode (P2P)

演示如何使用春季會話通過 P2P 拓撲與 Apache Geode 進行管理。??HttpSession??

HttpSession with Apache Geode (P2P) Guide

3. 會話集成

春季會議提供透明的集成。這意味著開發人員 可以將實現替換為由 Spring 會話支持的實現。??javax.servlet.http.HttpSession????HttpSession??

Spring Session允許插入多個不同的數據存儲提供程序(例如Apache Geode)。 為了管理狀態。??HttpSession??

3.1. 為什么是Spring Session和HttpSession?

我們已經提到春季會議提供了透明的集成,但是有什么好處 我們能擺脫困境嗎???HttpSession??

HttpSession- 使它能夠被集群化(即復制) 實現高可用性),而無需綁定到特定于應用程序容器的解決方案。HttpSessionRESTAPI - 允許在協議標頭中提供會話 ID 以使用 RESTful API。WebSocket- 提供在接收 WebSocket 消息時保持活動狀態的能力。HttpSessionWebSession- 允許以應用程序容器中立的方式替換Spring WebFlux。WebSession

3.2. 使用 Apache Geode 進行 HttpSession Management

當Apache?Geode 與 Spring Session 一起使用時,Web 應用程序的掃描將替換為由 Apache Geode 管理的集群實現 并使用春季會話的 API 方便地訪問。javax.servlet.http.HttpSession

使用 Apache Geode 管理會話狀態的兩種最常見的拓撲包括:

客戶端-服務器點對點 (P2P)

此外,Apache Geode 支持使用WAN 拓撲進行站點到站點復制。 配置和使用Apache Geode的WAN功能的能力獨立于Spring Session, 并且超出了本文檔的范圍。

有關使用 Spring Data for Apache Geode 配置 Apache Geode WAN 功能的更多詳細信息 可以在這里找到。

3.2.1. Apache Geode 客戶端-服務器

客戶端-服務器拓撲可能是 在春季會話中使用Apache Geode作為提供程序時,用戶中最常見的配置選擇,因為 與應用程序相比,Apache Geode 服務器具有明顯不同且獨特的 JVM 堆要求。 使用客戶端-服務器拓撲使應用程序能夠獨立管理(例如復制)應用程序狀態 從其他應用程序進程。

在客戶端-服務器拓撲中,使用 Spring 會話的應用程序將打開 1 個或多個到遠程群集的連接 的 Apache Geode 服務器,這些服務器將管理對 allstate 的訪問。HttpSession

您可以使用以下任一方式配置客戶端-服務器拓撲:

基于 Java 的配置基于 XML 的配置
Apache Geode Client-Server based Configuration

本節介紹如何將 Apache Geode 的客戶端-服務器拓撲配置為管理狀態 使用基于 Java 的配置。??HttpSession??

HttpSession with Apache Geode(客戶端-服務器)提供了一個工作示例,演示了如何 將Spring Session與Apache Geode集成,以使用Java配置進行管理狀態。您可以閱讀 通過下面的基本集成步驟,但建議您按照詳細的“HttpSession”進行操作 與您自己的應用程序集成時使用 Apache Geode(客戶端-服務器)指南。??HttpSession??

Spring Java 配置

添加所需的依賴項和存儲庫聲明后,我們可以創建 Spring 配置。 Spring 配置負責創建一個 Servlet,用 Spring Session 和 Apache Geode 支持的實現替換它。??Filter????HttpSession??

客戶端配置

添加以下 Spring 配置:

@ClientCacheApplication(name = "SpringSessionDataGeodeJavaConfigSampleClient", logLevel = "error",  readTimeout = 15000, retryAttempts = 1, subscriptionEnabled = true) @EnableGemFireHttpSession(poolName = "DEFAULT") public class ClientConfig extends ClientServerIntegrationTestsSupport {  @Bean  ClientCacheConfigurer gemfireServerReadyConfigurer(       @Value("${spring.data.gemfire.cache.server.port:40404}") int cacheServerPort) {    return (beanName, clientCacheFactoryBean) -> waitForServerToStart("localhost", cacheServerPort);  }}

首先,我們通過注釋我們的類來聲明我們的 Web 應用程序是 Apache Geode 緩存客戶端。

跟。此外,我們調整了一些基本的“默認”設置(例如)。??ClientConfig????@ClientCacheApplication????Pool????readTimeout??

??@EnableGemFireHttpSession??創建一個名為“實現”的春豆。過濾器替換為Spring Session提供的實現

并得到Apache Geode的支持。此外,該配置還將創建必要的客戶端(默認情況下,“ClusteredSpringSessions”,即 a)對應于相同的服務器端名稱。所有會話狀態都通過數據訪問操作從客戶端發送到服務器。客戶端使用“默認”。??springSessionRepositoryFilter????javax.servlet.Filter????HttpSession????Region????PROXY????Region????Region????Region????Region????Pool??

然后,我們等待確保Apache Geode服務器已啟動并運行,然后再繼續。這才真正有用

用于自動化(集成)測試目的。

在典型的 Apache Geode 生產部署中,集群可能包含數百或數千個 服務器(又稱數據節點),客戶端連接到 1 個或多個正在運行的 Apache Geode 定位器更為常見 在同一集群中。定位器將有關集群中可用服務器的元數據傳遞給客戶端,單個 服務器負載以及哪些服務器具有感興趣的客戶端數據,這對于直接, 單跳數據訪問和延遲敏感型應用程序。有關客戶端/服務器部署的更多詳細信息,請參閱 Apache Geode 用戶指南。

有關配置 Spring 數據 Geode 的更多信息,請參閱參考指南。

注釋使開發人員能夠配置兩個春季會話的某些方面 和使用以下屬性的開箱即用的 Apache Geode:??@EnableGemFireHttpSession??

??clientRegionShortcut??- 使用ClientRegionShortcut(默認為 )在客戶端上指定 Apache Geode數據管理策略。此屬性僅在配置客戶端時使用。PROXYRegion??indexableSessionAttributes??- 按名稱標識應為查詢目的編制索引的會話屬性。 只有按名稱顯式標識的會話屬性才會被編入索引。??maxInactiveIntervalInSeconds??- 控制HttpSession空閑超時過期時間(默認為30 分鐘)。??poolName??- 用于將客戶端連接到服務器群集的專用 Apache GeoDeused 的名稱。 僅當應用程序是緩存客戶端時才使用此屬性。默認為。PoolgemfirePool??regionName??- 指定用于存儲和管理狀態的 Apache GeoDeused 的名稱 (默認為“集群春季會話”)。RegionHttpSession??serverRegionShortcut??- 使用RegionShortcut(默認為 )在服務器上指定 Apache Geode數據管理策略。此屬性僅在配置服務器時使用, 或者當采用 P2P 拓撲時。PARTITIONRegions

重要的是要記住,如果客戶端是 aor,則 Apache Geode 客戶端名稱必須與同名的服務器匹配。客戶端和服務器名稱 如果用于存儲會話狀態的客戶端是,則不需要匹配。但是,請記住 該會話狀態將不會傳播到服務器,您將失去使用Apache Geode的所有好處 在分布式服務器上存儲和管理分布式復制的會話狀態信息, 復制方式。??Region????Region????Region????PROXY????CACHING_PROXY????Region????Region????LOCAL??

服務器配置

到目前為止,我們只涵蓋了等式的一面。我們還需要一個Apache Geode服務器供我們的緩存客戶端通信 并將會話狀態發送到服務器進行管理。

在此示例中,我們將使用以下 Java 配置來配置和運行 Apache Geode 服務器:

@CacheServerApplication(name = "SpringSessionDataGeodeJavaConfigSampleServer", logLevel = "error") @EnableGemFireHttpSession(maxInactiveIntervalInSeconds = 30) public class GemFireServer {  @SuppressWarnings("resource")  public static void main(String[] args) {    new AnnotationConfigApplicationContext(GemFireServer.class).registerShutdownHook();  }}

首先,我們使用注釋來簡化對等緩存實例的創建

包含與 afor 緩存客戶端連接。??@CacheServerApplication????CacheServer??

(可選)然后,對類進行注釋以創建必要的

服務器端(默認情況下,“ClusteredSpringSessions”)用于存儲狀態。此步驟是

可選,因為會話可以手動創建,也許使用外部方式。使用方便快捷。??GemFireServer????@EnableGemFireHttpSession????Region????HttpSession????Region????@EnableGemFireHttpSession??

Apache Geode 客戶端-服務器基于 XML 的配置

本節介紹如何將 Apache Geode 的客戶端-服務器拓撲配置為管理狀態 使用基于 XML 的配置。??HttpSession??

??使用 XML 的 HttpSession with Apache Geode (Client-Server)??提供了一個工作示例 演示如何使用XML將Spring Session與Apache Geode集成以管理狀態 配置。您可以通讀下面的集成基本步驟,但建議您遵循 在詳細的“HttpSession”中使用 Apache Geode(客戶端-服務器)使用 XML 指南與您自己的應用程序。??HttpSession??

彈簧 XML 配置

添加所需的依賴項和存儲庫聲明后,我們可以創建 Spring 配置。 Spring 配置負責創建一個替換由 Spring Session 和 Apache Geode 支持的實現。??Servlet????Filter????javax.servlet.http.HttpSession??

客戶端配置

添加以下 Spring 配置:

            ${spring.data.gemfire.cache.log-level:error}                    

(可選)首先,我們可以包含 abean 來配置 Apache GeodeusingPivotal GemFire 屬性的某些方面。在這種情況下,我們只是

使用特定于應用程序的系統屬性設置 Apache Geode 的“日志級別”,默認為“警告”如果未指定。??Properties????ClientCache??

我們必須創建一個 Apache Geode 的實例。我們用我們的初始化它。??ClientCache????gemfireProperties??

然后,我們配置 aof 連接以與客戶端/服務器拓撲中的 Apache Geode 服務器通信。

在我們的配置中,我們對超時、連接數等使用合理的設置。此外,我們已經配置為直接連接到服務器(使用嵌套元素)。??Pool????Pool????gfe:server??

最后,注冊 abean 以啟用春季會話功能。??GemFireHttpSessionConfiguration??

在典型的 Apache Geode 生產部署中,集群可能包含數百或數千個 服務器(又稱數據節點),客戶端連接到 1 個或多個正在運行的 Apache Geode 定位器更為常見 在同一集群中。定位器將有關集群中可用服務器的元數據傳遞給客戶端,單個 服務器負載以及哪些服務器具有感興趣的客戶端數據,這對于直接, 單跳數據訪問和延遲敏感型應用程序。有關客戶端/服務器部署的更多詳細信息,請參閱 Apache Geode 用戶指南。

有關為 Apache Geode 配置 Spring 數據的更多信息,請參閱參考指南。

服務器配置

到目前為止,我們只涵蓋了等式的一面。我們還需要一個Apache Geode服務器供我們的緩存客戶端通信 并將會話狀態發送到服務器進行管理。

在此示例中,我們將使用以下 XML 配置來啟動 Apache Geode 服務器:

          SpringSessionDataGeodeSampleXmlServer    ${spring.data.gemfire.cache.log-level:error}              

(可選)首先,我們可以使用Pivotal GemFire 屬性包含 abean 來配置 Apache Geode 對等的某些方面。在這種情況下,我們只是

使用特定于應用程序的系統屬性設置 Apache Geode 的“日志級別”,默認為“警告”如果未指定。??Properties????Cache??

我們必須配置一個Apache Geode對等實例。我們使用 Apache Geode 屬性初始化它。??Cache??

接下來,我們定義一個由緩存客戶端使用的合理配置

用于連接到服務器并發送會話狀態的應用程序。??CacheServer????bind-address????port??

最后,我們啟用我們在客戶端 XML 配置中聲明的相同 Spring 會話功能

通過注冊一個實例,除了我們設置的會話過期超時到30 秒。稍后我們將解釋這意味著什么。??GemFireHttpSessionConfiguration??

Apache Geode 服務器通過以下方式引導:

@Configuration @ImportResource("META-INF/spring/session-server.xml") public class GemFireServer {  public static void main(String[] args) {    new AnnotationConfigApplicationContext(GemFireServer.class).registerShutdownHook();  }}

與其使用 amethod定義一個簡單的 Java 類,不如考慮使用 Spring Boot 來代替。??main??

Theannotation 將此 Java 類指定為 Spring 配置元數據的來源,使用

7.9. 基于注釋的容器配置[Spring 的注釋配置支持]。??@Configuration??

主要,配置來自文件。??META-INF/spring/session-server.xml??

XML Servlet 容器初始化

我們的Spring XML 配置創建了一個名為 Spring bean 的實現接口。底豆負責更換 使用Spring Session和Apache Geode提供的自定義實現。??springSessionRepositoryFilter????javax.servlet.Filter????springSessionRepositoryFilter????javax.servlet.http.HttpSession??

為了讓我們的魔力發揮,我們需要指示 Spring 加載 我們的配置文件。??Filter????session-client.xml??

我們使用以下配置執行此操作:

src/main/webapp/WEB-INF/web.xml

  contextConfigLocation  /WEB-INF/spring/session-client.xml  org.springframework.web.context.ContextLoaderListener

ContextLoaderListener讀取上下文參數值并獲取我們的會話客戶端.xml配置文件。??contextConfigLocation??

最后,我們需要確保我們的 Servlet 容器(即 Tomcat)在每個請求中使用 ourfor。??springSessionRepositoryFilter??

以下代碼片段為我們執行了最后一步:

src/main/webapp/WEB-INF/web.xml

  springSessionRepositoryFilter  org.springframework.web.filter.DelegatingFilterProxy  springSessionRepositoryFilter  /*  REQUEST  ERROR

DemissionatingFilterProxy將按 的名稱查找 bean 并將其轉換為 a。對于每個 HTTP 請求, Theis 調用,它委托給。??springSessionRepositoryFilter????Filter????DelegatingFilterProxy????springSessionRepositoryFilter??

3.2.2. Apache Geode 點對點 (P2P)

一種不太常見的方法是將 Spring 會話應用程序配置為 Apache Geode 集群中的對等成員 使用對等 (P2P)拓撲。 在此配置中,Spring 會話應用程序將是 Apache Geode 中的實際服務器(或數據節點) 群集,而不僅僅是以前的緩存客戶端。

這種方法的一個優點是應用程序接近應用程序的狀態(即其數據), 特別是國家。但是,還有其他有效的方法可以實現類似的目標 依賴于數據的計算,例如使用 Apache Geode 的函數執行?。 Apache Geode的任何其他功能都可以在以下情況下使用。 Apache Geode在Spring Session中擔任提供者。??HttpSession??

P2P 拓撲對于測試目的以及更小、更集中和獨立的應用程序非常有用, 例如在微服務架構中找到的那些,并且肯定會改善應用程序的感知 延遲和吞吐量需求。

您可以使用以下任一方式配置對等 (P2P) 拓撲:

基于 Java 的配置基于 XML 的配置
Apache Geode Peer-to-Peer (P2P) 基于 Java 的配置

本節介紹如何將 Apache Geode 的對等 (P2P) 拓撲配置為管理狀態 使用基于 Java 的配置。??HttpSession??

HttpSession with Apache Geode (P2P)提供了一個工作示例,演示了如何 將Spring Session與Apache Geode集成,以使用Java配置進行管理狀態。您可以閱讀 通過下面的基本集成步驟,但建議您按照詳細的“HttpSession”進行操作 與您自己的應用程序集成時使用 Apache Geode (P2P) 指南。??HttpSession??

Spring Java 配置

添加所需的依賴項和存儲庫聲明后,我們可以創建 Spring 配置。

Spring 配置負責創建一個替換由 Spring Session 和 Apache Geode 支持的實現。??Servlet????Filter????javax.servlet.http.HttpSession??

添加以下 Spring 配置:

@PeerCacheApplication(name = "SpringSessionDataGeodeJavaConfigP2pSample", logLevel = "error") @EnableGemFireHttpSession(maxInactiveIntervalInSeconds = 30) public class Config {}

首先,我們使用注釋來簡化對等緩存實例的創建。??@PeerCacheApplication??

然后,對類進行注釋以創建用于存儲狀態的必要服務器端(默認情況下為“ClusteredSpringSessions”)。??Config????@EnableGemFireHttpSession????Region????HttpSession??

有關為 Apache Geode 配置 Spring 數據的更多信息,請參閱參考指南。

注釋使開發人員能夠配置兩個春季會話的某些方面 和使用以下屬性的開箱即用的 Apache Geode:??@EnableGemFireHttpSession??

??clientRegionShortcut??- 使用ClientRegionShortcut(默認為 )在客戶端上指定 Apache Geode數據管理策略。此屬性僅在配置客戶端時使用。PROXYRegion??indexableSessionAttributes??- 按名稱標識應為查詢目的編制索引的會話屬性。 只有按名稱顯式標識的會話屬性才會被編入索引。??maxInactiveIntervalInSeconds??- 控制HttpSession空閑超時過期時間(默認為30 分鐘)。??poolName??- 用于將客戶端連接到服務器群集的專用 Apache GeoDeused 的名稱。 僅當應用程序是緩存客戶端時才使用此屬性。默認為。PoolgemfirePool??regionName??- 指定用于存儲和管理狀態的 Apache GeoDeused 的名稱 (默認為“集群春季會話”)。RegionHttpSession??serverRegionShortcut??- 使用RegionShortcut(默認為 )在服務器上指定 Apache Geode數據管理策略。此屬性僅在配置服務器時使用, 或者當采用 P2P 拓撲時。PARTITIONRegions
Java Servlet 容器初始化

我們的<<[httpsession-spring-java-configuration-p2p,Spring Java Configuration>>創建了一個名為Spring bean的實現。底比亞 負責替換為由 春季會議和阿帕奇巨地。??springSessionRepositoryFilter????javax.servlet.Filter????springSessionRepositoryFilter????javax.servlet.http.HttpSession??

為了讓我們的魔法發揮,春天需要加載我們的班級。我們還需要確保我們的 Servlet 容器(即 Tomcat)在每個 HTTP 請求中使用 ouron。??Filter????Config????springSessionRepositoryFilter??

幸運的是,Spring Session提供了一個實用程序類,名為使兩者兼而有之。 步驟非常簡單。??AbstractHttpSessionApplicationInitializer??

您可以在下面找到一個示例:

src/main/java/sample/Initializer.java

public class Initializer extends AbstractHttpSessionApplicationInitializer {   public Initializer() {    super(Config.class);   }}

我們類 () 的名稱無關緊要。重要的是我們擴大。??Initializer????AbstractHttpSessionApplicationInitializer??

第一步是擴展。這確保了Spring bean namedis注冊到我們的Servlet容器中,并在每個HTTP請求中使用。??AbstractHttpSessionApplicationInitializer????springSessionRepositoryFilter??

??AbstractHttpSessionApplicationInitializer??還提供了一種機制,可輕松讓彈簧加載

我們的班級。??Config??

Apache Geode Peer-to-Peer (P2P) 基于 XML 的配置

本節介紹如何將 Apache Geode 的對等 (P2P) 拓撲配置為管理狀態 使用基于 XML 的配置。??HttpSession??

使用 XML 的 HttpSession with Apache Geode (P2P)提供了一個工作示例,演示了如何 將Spring Session與Apache Geode集成,以使用XML配置來管理狀態。您可以閱讀 通過下面的基本集成步驟,但建議您按照詳細的“HttpSession”進行操作 與您自己的應用程序集成時使用 XML 指南的 Apache Geode (P2P)。??HttpSession??

彈簧 XML 配置

添加所需的依賴項和存儲庫聲明后,我們可以創建 Spring 配置。

Spring 配置負責創建一個替換由 Spring Session 和 Apache Geode 支持的實現。??Servlet????Filter????javax.servlet.http.HttpSession??

添加以下 Spring 配置:

src/main/webapp/WEB-INF/spring/session.xml

  SpringSessionDataGeodeXmlP2pSample  ${spring.data.gemfire.cache.log-level:error}

(可選)首先,我們可以包含 abean 來配置 Apache Geode 對等的某些方面,使用VMware Tanzu GemFire 屬性。在這種情況下,我們只是

使用特定于應用程序的系統屬性設置 Apache Geode 的“日志級別”,默認為“警告”如果未指定。??Properties????Cache??

我們必須配置一個Apache Geode對等實例。我們使用 Apache Geode 屬性初始化它。??Cache??

最后,我們通過注冊一個實例來啟用 Spring 會話功能。??GemFireHttpSessionConfiguration??

有關為 Apache Geode 配置 Spring 數據的更多信息,請參閱參考指南。

XML Servlet 容器初始化

SpringXML 配置創建了一個名為 Spring bean 的實現。Thebean負責用Spring Session和Apache Geode支持的自定義實現替換the。??springSessionRepositoryFilter????javax.servlet.Filter????springSessionRepositoryFilter????javax.servlet.http.HttpSession??

為了讓我們的魔力發揮,我們需要指示 Spring 加載我們的配置文件。??Filter????session.xml??

我們使用以下配置執行此操作:

src/main/webapp/WEB-INF/web.xml

  contextConfigLocation      /WEB-INF/spring/*.xml        org.springframework.web.context.ContextLoaderListener  

ContextLoaderListener讀取上下文參數值并獲取我們的會話.xml配置文件。??contextConfigLocation??

最后,我們需要確保我們的 Servlet 容器(即 Tomcat)使用 our for 每個 HTTP 請求。??springSessionRepositoryFilter??

以下代碼片段為我們執行了最后一步:

src/main/webapp/WEB-INF/web.xml

  springSessionRepositoryFilter  org.springframework.web.filter.DelegatingFilterProxy  springSessionRepositoryFilter  /*  REQUEST  ERROR

DemissionatingFilterProxy將按 的名稱查找 bean 并將其轉換為 a。對于每個 HTTP 請求 Theis 調用,委托給 the。??springSessionRepositoryFilter????Filter????DelegatingFilterProxy????springSessionRepositoryFilter??

3.3. 使用帶有屬性的 Apache Geode 進行配置管理??HttpSession??

雖然注釋在開始使用春季會話時易于使用且方便 和 Apache Geode 在您的 Spring 啟動應用程序中,當您從一個應用程序遷移時,您很快就會遇到限制 環境到另一個環境,例如,從 DEV 遷移到 QA 再到 PROD 時。??@EnableGemFireHttpSession??

使用注釋屬性,無法從一個更改配置 環境到另一個。因此,Apache Geode 的春季課程介紹了眾所周知的、記錄在案的屬性 對于所有注釋屬性。??@EnableGemFireHttpSession????@EnableGemFireHttpSession??

表 4.注釋屬性的已知、記錄的屬性。??@EnableGemFireHttpSession??

財產

批注屬性

描述

違約

spring.session.data.gemfire.cache.client.pool.name

??EnableGemFireHttpSession.poolName??

客戶端區域存儲/訪問會話狀態使用的專用池的名稱。

寶石火池

spring.session.data.gemfire.cache.client.region.shortcut

??EnableGemFireHttpSession.clientRegionShortcut??

在客戶端-服務器拓撲中設置客戶端區域數據管理策略。

ClientRegionShortcut.PROXY

spring.session.data.gemfire.cache.server.region.shortcut

??EnableGemFireHttpSession.serverRegionShortcut??

在對等 (P2P) 拓撲中設置對等區域數據管理策略。

區域快捷方式.分區

spring.session.data.gemfire.session.attributes.indexable

??EnableGemFireHttpSession.indexableSessionAttributes??

要在會話區域中編制索引的會話屬性的逗號分隔列表。

spring.session.data.gemfire.session.expiration.bean-name

??EnableGemFireHttpSession.sessionExpirationPolicyBeanName??

實現過期策略的 Spring 容器中 Bean 的名稱

spring.session.data.gemfire.session.expiration.max-inactive-interval-seconds

??EnableGemFireHttpSession.maxInactiveIntervalInSeconds??

會話過期超時(以秒為單位)

1800

spring.session.data.gemfire.session.region.name

??EnableGemFireHttpSession.regionName??

用于存儲和訪問會話狀態的客戶端或對等區域的名稱。

集群春季會話

spring.session.data.gemfire.session.serializer.bean-name

??EnableGemFireHttpSession.sessionSerializerBeanName??

實現序列化策略的 Spring 容器中 Bean 的名稱

會話Pdx序列化程序

所有屬性也都記錄在注釋屬性Javadoc中。??@EnableGemFireHttpSession??

因此,在使用Apache Geode時調整Spring Session的配置非常簡單。 作為使用屬性的提供程序,如下所示:

@SpringBootApplication@ClientCacheApplication@EnableGemFireHttpSession(maxInactiveIntervalInSeconds = 900)class MySpringSessionApplication {  // ...}

然后,在:??application.properties??

#application.propertiesspring.session.data.gemfire.cache.client.region.shortcut=CACHING_PROXYspring.session.data.gemfire.session.expiration.max-inactive-internval-seconds=3600

顯式定義的任何屬性都將覆蓋相應的批注屬性。??@EnableGemFireHttpSession??

在上面的例子中,即使注釋屬性 設置為秒或 15 分鐘,相應的屬性屬性 (即) 重寫該值并將過期時間設置為秒或 60 分鐘。??EnableGemFireHttpSession????maxInactiveIntervalInSeconds????900????spring.session.data.gemfire.session.expiration.max-inactive-interval-seconds????3600??

請記住,屬性在運行時會覆蓋批注屬性值。

3.3.1. 屬性的屬性

您甚至可以變得更復雜,并使用其他屬性配置您的屬性,如下所示:

#application.propertiesspring.session.data.gemfire.session.expiration.max-inactive-internval-seconds=${app.geode.region.expiration.timeout:3600}

此外,您可以使用 Spring 配置文件根據環境改變過期超時(或其他屬性)。 或您的應用程序,或您的應用程序要求規定的任何標準。

屬性占位符和嵌套是核心 Spring 框架的一個功能,而不是特定于 Spring 會話 或 Apache Geode 的春季會議。

3.4. 使用帶有配置器的 Apache Geode 進行配置管理??HttpSession??

除了屬性之外,Spring Session for Apache Geode 還允許您調整 春季會議與Apache Geode使用界面。該接口定義了一個 協定包含可重寫的 Eachannotation 屬性的默認方法 以調整配置。??SpringSessionGemFireConfigurer????@EnableGemFireHttpSession??

這在概念上類似于Spring Web MVC的配置器接口 (例如),它調整 Web 應用程序的各個方面 啟動時配置,例如配置異步支持。聲明和實現 ais 的優點是,它使您能夠以編程方式控制配置。這在您需要的情況下很有用 輕松表達確定是否應應用配置的復雜條件邏輯。??SpringSessionGemFireConfigurer????o.s.web.servlet.config.annotation.WebMvcConfigurer????Configurer??

例如,要像之前一樣調整客戶端區域數據管理策略和會話過期超時, 使用以下內容:

@Configurationclass MySpringSessionConfiguration {  @Bean  SpringSessionGemFireConfigurer exampleSpringSessionGemFireConfigurer() {    return new SpringSessionGemFireConfigurer() {      @Override      public ClientRegionShortcut getClientRegionShortcut() {        return ClientRegionShortcut.CACHING_PROXY;      }      @Override      public int getMaxInactiveIntervalInSeconds() {        return 3600;      }    };  }}

當然,這個例子不是很有創意。您肯定可以使用更復雜的邏輯來確定 每個配置屬性的配置。

您可以隨心所欲地復雜,例如通過實現其他屬性的 yourin 使用泉三注解,如下:??Configurer????@Value??

@Configurationclass MySpringSessionConfiguration {  @Bean  @Primary  @Profile("production")  SpringSessionGemFireConfigurer exampleSpringSessionGemFireConfigurer(      @Value("${app.geode.region.data-management-policy:CACHING_PROXY}") ClientRegionShortcut shortcut,      @Value("${app.geode.region.expiration.timeout:3600}") int timeout) {    return new SpringSessionGemFireConfigurer() {      @Override      public ClientRegionShortcut getClientRegionShortcut() {        return shortcut;      }      @Override      public int getMaxInactiveIntervalInSeconds() {        return timeout;      }    };  }}

Spring 引導將自動解析注釋屬性占位符值或 SpEL 表達式。 但是,如果您不使用 Spring Boot,則必須顯式注冊靜態 bean 定義。??@Value????PropertySourcesPlaceholderConfigurer??

但是,您一次只能在 Spring 容器中聲明 1bean,除非 您還在使用 Spring 配置文件或已將 1 個多豆標記為主 通過使用 Spring 的上下文注釋。??SpringSessionGemFireConfigurer????SpringSessionGemFireConfigurer????@Primary??

3.4.1. 配置優先級

優先于任一注釋屬性 或任何知名且有記錄的 Apache Geode 屬性春季會議 (例如) 在 Spring Boot 中定義??SpringSessionGemFireConfigurer????@EnableGemFireHttpSession????spring.session.data.gemfire.session.expiration.max-inactive-interval-seconds????application.properties.??

如果 Web 應用程序采用 1 種以上的配置方法,則將應用以下優先級:

??SpringSessionGemFireConfigurer??“實現的”回調方法記錄的 Apache Geode 屬性的春季會話(請參閱相應的注釋 屬性Javadoc;例如@EnableGemFireHttpSessionspring.session.data.gemfire.session.region.name)??@EnableGemFireHttpSession??批注屬性

Apache Geode 的春季會話小心翼翼地只應用來自 abean 的配置 在 Spring 容器中聲明了您實際實現的方法。??SpringSessionGemFireConfigurer??

在上面的示例中,由于您沒有實現該方法,因此 Apache Geode 區域的名稱 管理狀態不會由配置程序決定。??getRegionName()????HttpSession??

例如,請考慮以下配置:

Apache Geode 配置的春季會話示例

@ClientCacheApplication@EnableGemFireHttpSession(    maxInactiveIntervalInSeconds = 3600,    poolName = "DEFAULT")class MySpringSessionConfiguration {  @Bean  SpringSessionGemFireConfigurer sessionExpirationTimeoutConfigurer() {    return new SpringSessionGemFireConfigurer() {      @Override      public int getMaxInactiveIntervalInSeconds() {        return 300;      }    };  }}

此外,請考慮以下 Spring 引導文件:??application.properties??

彈簧啟動application.properties
spring.session.data.gemfire.session.expiration.max-inactive-interval-seconds = 900spring.session.data.gemfire.session.region.name = Sessions

會話過期超時將為 300 秒或 5 分鐘,覆蓋這兩個屬性 (即)900秒或15分鐘, 以及顯式注釋屬性值 3600 秒或 1 小時。??spring.session.data.gemfire.session.expiration.max-inactive-interval-seconds????@EnableGemFireHttpSession.maxInactiveIntervalInSeconds??

由于“sessionExexpationTimeoutConfigurer”bean不會覆蓋該方法,因此會話區域 名稱將由屬性確定(即),設置為“會話”, 它覆蓋隱式批注屬性的默認值 “集群春季會議”。??getRegionName()????spring.session.data.gemfire.session.region.name????@EnableGemFireHttpSession.regionName??

注釋屬性的值“DEFAULT”將決定池的名稱 在客戶端和服務器之間發送區域操作以管理服務器上的會話狀態時使用,因為兩者都不是 設置了相應的屬性(即 spring.session.data.gemfire.cache.client.pool.name"),也沒有 該方法被“sessionExexpationTimeoutConfigurer”bean覆蓋。??@EnableGemFireHttpSession.poolName????SpringSessionGemFireConfigurer.getPoolName()??

最后,用于管理會話狀態的客戶端區域將具有默認的數據管理策略 注釋屬性的值,該屬性未顯式設置,也沒有顯式設置 是此屬性的相應屬性(即)。 并且,由于該方法未被重寫,因此使用默認值。??PROXY????@EnableGemFireHttpSession.clientRegionShortcut????spring.session.data.gemfire.cache.client.region.shortcut????SpringSessionConfigurer.getClientRegionShortcut()??

3.5. Apache Geode 過期

默認情況下,Apache Geode 配置了區域條目、空閑超時 (TTI) 過期策略,使用 過期超時為 30 分鐘,并使條目無效作為操作。這意味著當用戶的會話保持非活動狀態時 (即空閑)超過 30 分鐘,會話將過期并失效,用戶必須開始新的會話 為了繼續使用該應用程序。

但是,如果您對會話狀態管理和過期以及使用 默認情況下,空閑超時 (TTI) 過期策略不足以滿足您的使用案例 (UC)?

現在,Apache Geode 的春季會話支持特定于應用程序的自定義過期策略。作為應用程序 開發人員,您可以指定自定義規則來管理由 Spring 會話管理的會話的到期時間,該規則由 Apache Geode。

Apache Geode的春季會議提供了新的策略界面。??SessionExpirationPolicy??

會話過期策略界面

@FunctionalInterfaceinterface SessionExpirationPolicy {    // determine timeout for expiration of individual Session    Optional determineExpirationTimeout(Session session);    // define the action taken on expiration    default ExpirationAction getExpirationAction() {        return ExpirationAction.INVALIDATE;    }    enum ExpirationAction {        DESTROY,        INVALIDATE    }}

實現此接口以指定應用程序所需的會話過期策略,然后注冊 作為 Spring 應用程序上下文中的 Bean 的實例。

使用注釋,屬性來配置名稱 Bean 實現自定義應用程序策略和會話過期規則。??@EnableGemFireHttpSession????sessionExpirationPolicyBeanName????SessionExpirationPolicy??

例如:

習慣??SessionExpirationPolicy??

class MySessionExpirationPolicy implements SessionExpirationPolicy {    public Duration determineExpirationTimeout(Session session) {        // return a java.time.Duration specifying the length of time until the Session should expire    }}

然后,在應用程序類中,簡單聲明以下內容:

自定義配置??SessionExpirationPolicy??

@SpringBootApplication@EnableGemFireHttpSession(    maxInactiveIntervalInSeconds = 600,    sessionExpirationPolicyBeanName = "expirationPolicy")class MySpringSessionApplication {    @Bean    SessionExpirationPolicy expirationPolicy() {        return new MySessionExpirationPolicy();    }}

或者,可以使用屬性配置bean的名稱,或者通過在Spring容器中聲明abean并覆蓋該方法來配置bean。??SessionExpirationPolicy????spring.session.data.gemfire.session.expiration.bean-name????SpringSessionGemFireConfigurer????getSessionExpirationPolicyBeanName()??

你只需要實現該方法, 它封裝了規則以確定會話何時到期。會話的過期超時 表示為 anof,它指定會話到期前的時間長度。??determineExpirationTimeout(:Session):Optional????Optional????java.time.Duration??

該方法可以是特定于會話的,并且可能會隨著每次調用而更改。??determineExpirationTimeout??

(可選)您可以實現該方法以指定會話過期時執行的操作。默認情況下, 區域條目(即會話)無效。另一種選擇是在到期時銷毀區域條目, 這將刪除鍵(會話 ID)和值(會話)。無效僅刪除該值。??getAction??

在引擎蓋下,這些被改編成Apache GeodeCustomExpiry?接口的實例。 然后,此 Spring 會話對象將設置為會話區域的自定義條目空閑超時過期策略。??SessionExpirationPolicy????CustomExpiry??

在確定過期時間期間,每次運行過期線程時,都會為區域中的每個條目(即會話)調用該方法,進而運行過期線程 調用我們的方法。 返回的值轉換為秒,并用作從CustomExpiry.getExpiry(..)?返回的過期屬性中的過期?超時方法調用。??CustomExpiry.getExpiry(:Region.Entry):ExpirationAttributes????SessionExpirationPolicy.determineExpirationTimout(:Session):Optional????java.time.Duration??

Apache Geode 的過期線程每秒運行一次,評估區域中的每個條目(即會話) 以確定條目是否已過期。您可以使用屬性控制過期線程數。有關更多詳細信息,請參閱 Apache Geode文檔?。??gemfire.EXPIRY_THREADS??

3.5.1. 過期超時配置

如果您想為您的自定義設置過期超時 注釋,屬性,或者, 對應性質, 那么你的自定義實現也可以實現接口。??SessionExpirationPolicy????@EnableGemFireHttpSession????maxInactiveIntervalInSeconds????spring.session.data.gemfire.session.expiration.max-inactive-interval-seconds????SessionExpirationPolicy????SessionExpirationTimeoutAware??

接口定義為:??SessionExpirationTimeoutAware??

會話過期超時感知接口

interface SessionExpirationTimeoutAware {    void setExpirationTimeout(Duration expirationTimeout);}

當您的自定義實現也實現接口時, 然后,Apache Geode 的 Spring 會話將為您的實現提供來自注釋、屬性或屬性(如果已設置)的值,或者來自 Spring 應用程序上下文中聲明的任何 bean 的值,作為實例。??SessionExpirationPolicy????SessionExpirationTimeoutAware????@EnableGemFireHttpSession????maxInactiveIntervalInSeconds????spring.session.data.gemfire.session.expiration.max-inactive-interval-seconds????SpringSessionGemFireConfigurer????java.time.Duration??

如果使用 1 個以上的配置選項,則按以下順序優先:

??SpringSessionGemFireConfigurer.getMaxInactiveIntervalInSeconds()????spring.session.data.gemfire.session.expiration.max-inactive-interval-seconds??財產??@EnableGemFireHttpSession??注釋,屬性maxInactiveIntervalInSeconds

3.5.2. 修復超時過期問題

為了增加便利性,適用于Apache Geode的Spring Session提供了固定持續時間過期(或“絕對會話超時”,如核心春季會話問題#922中所述)的接口實現。??SessionExpirationPolicy??

在某些情況下,例如出于安全原因,可能需要在修復后使用戶的會話過期 時間長度(例如每小時),無論用戶的會話是否仍處于活動狀態。

Apache Geode 春季會議提供開箱即用的實現 對于這個確切的用例 (UC)。除了處理固定期限到期外,還要謹慎考慮 并應用默認的空閑過期超時。??FixedTimeoutSessionExpirationPolicy??

例如,考慮這樣一種情況:用戶登錄,開始會話,活動 10 分鐘,然后離開 讓會話閑置。如果固定持續時間過期超時設置為 60 分鐘,但空閑過期 超時僅設置為 30 分鐘,并且用戶不返回,則會話應在 40 分鐘后過期 而不是固定持續時間到期時的 60 分鐘。

相反,如果用戶忙了整整 40 分鐘,則保持會話處于活動狀態,從而避免了 30 分鐘 空閑過期超時,然后離開,然后我們的固定持續時間過期超時應該啟動并過期 用戶的會話權限為 60 分鐘,即使用戶的空閑過期超時在 70 分鐘之前不會發生 在(40 分鐘(活動)+ 30 分鐘(空閑)= 70 分鐘)。

嗯,這正是你所做的。??FixedTimeoutSessionExpirationPolicy??

要進行配置,請執行以下操作:??FixedTimeoutSessionExpirationPolicy??

固定持續時間到期配置

@SpringBootApplication@EnableGemFireHttpSession(sessionExpirationPolicyBeanName = "fixedTimeoutExpirationPolicy")class MySpringSessionApplication {    @Bean    SessionExpirationPolicy fixedTimeoutExpirationPolicy() {        return new FixedTimeoutSessionExpirationPolicy(Duration.ofMinutes(60L));    }}

在上面的例子中,thewas在Spring應用程序上下文中聲明為bean。 并使用 60 分鐘的固定持續時間過期超時進行初始化。因此,用戶會話將 在空閑超時(默認為 30 分鐘)或固定超時(配置為 60 分鐘)之后過期, 哪個先發生。??FixedTimeoutSessionExpirationPolicy??

還可以通過使用 Apache Geode的春季會議。這個 BPP 包裝 任何特定于實現的數據存儲 僅評估訪問時的會話過期時間。此方法與基礎數據存儲無關 因此可以與任何春季會話提供程序一起使用。到期確定完全基于 會話屬性和必需指定固定持續時間 過期超時。??FixedDurationExpirationSessionRepositoryBeanPostProcessor????SessionRepository????FixedDurationExpirationSessionRepository????creationTime????java.time.Duration??

不應在嚴格的過期超時情況下使用,例如 當會話必須在固定持續時間過期超時過后立即過期時。此外,與 的,不采取空閑過期 考慮超時。也就是說,在確定過期超時時,它只使用固定持續時間 對于給定會話。??FixedDurationExpirationSessionRepository????FixedTimeoutSessionExpirationPolicy????FixedDurationExpirationSessionRepository??

3.5.3.鏈接??SessionExpirationPolicy??

使用Composite 軟件設計模式,您可以將一組實例視為單個實例,就像在鏈中一樣運行,就像在鏈中一樣 Servlet 過濾器本身。??SessionExpirationPolicy??

復合軟件設計模式是一種功能強大的模式,只需返回 anoffrom 即可獲得支持。 方法。??SessionExpirationPolicy????@FunctionalInterface????Optional????java.time.Duration????determineExpirationTimeout??

這允許每個組合“可選”僅在過期時返回 可以通過此實例確定。或者,此實例可以下注到組合中的下一個實例,或者鏈直到返回非空過期超時,或者最終 不返回過期超時。??SessionExpirationPolicy????Duration????SessionExpirationPolicy??

實際上,此策略在內部使用,在空閑超時將在固定超時之前發生的情況下返回。通過返回 沒有過期超時,Apache Geode 將遵循默認的、配置的條目空閑超時過期策略 在區域管理會話狀態上。??FixedTimeoutSessionExpirationPolicy????Optional.empty()??

這種確切的行為也記錄在org.apache.geode.cache.CustomExpiry.getExpiry(:Region.Entry):ExplumationAttributes方法中。

3.6. Apache Geode 序列化

為了在客戶端和服務器之間傳輸數據,或者在對等節點之間分發和復制數據時 在集群中,數據必須序列化。在這種情況下,有問題的數據是會話的狀態。

每當在客戶端/服務器拓撲中保留或訪問會話時,會話的狀態都會通過網絡發送。 通常,啟用了 Spring 會話的 Spring 引導應用程序將是集群中服務器的客戶端 的 Apache Geode 節點。

在服務器端,會話狀態可能分布在群集中的多個服務器(數據節點)上以進行復制 數據并保證會話狀態的高可用性。使用Apache Geode時,可以對數據進行分區, 或分片,并且可以指定冗余級別。分發數據以進行復制時,還必須 序列化以在群集中的對等節點之間傳輸會話狀態。

開箱即用的Apache Geode支持Java序列化Java 序列化有很多優點, 例如在對象圖中處理循環,或者被任何用 Java 編寫的應用程序普遍支持。 但是,Java 序列化非常冗長,不是最有效的在線格式。

因此,Apache Geode提供了自己的序列化框架來序列化Java類型:

數據序列化PDX 序列化

3.6.1. Apache Geode 序列化背景

如上所述,Apache Geode 提供了 2 個額外的序列化框架:數據序列化和 PDX序列化

數據序列化

數據序列化是一種非常有效的格式(即快速緊湊),與Java序列化相比,開銷很小。

它通過發送支持增量傳播只有實際更改的數據位,而不是發送整個對象,這肯定會減少 除了在持久保存數據時減少 IO 量外,還通過網絡發送的數據量 或溢出到磁盤。

但是,每當數據通過線傳輸或持久化/溢出到 并從磁盤訪問,因為接收端執行反序列化。事實上,無論何時使用增量傳播,都必須在接收端對對象進行反序列化,以便應用“增量”。Apache Geode 適用 通過在實現接口的對象上調用方法進行增量。顯然,你不能 在序列化對象上調用方法。??org.apache.geode.Delta??

PDX

另一方面,PDX代表便攜式數據交換,保留發送數據的形式。 例如,如果客戶端以 PDX 格式將數據發送到服務器,則服務器會將數據保留為 PDX 序列化字節 并將它們存儲在數據訪問操作所針對的緩存中。??Region??

此外,顧名思義,PDX 是“可移植的”,這意味著它同時支持 Java 和本地語言客戶端, 例如 C、C++ 和 C# 客戶端,以便對同一數據集進行互操作。

PDX 甚至允許對序列化字節執行 OQL 查詢,而不會導致對象反序列化 首先為了計算查詢謂詞并執行查詢。這可以實現,因為Apache Geode 維護一個“類型注冊表”,其中包含序列化和存儲在 使用 PDX 的 Apache Geode。

但是,可移植性確實有成本,其開銷略高于數據序列化。盡管如此,PDX還很遠。 比 Java 序列化更高效、更靈活,Java 序列化將類型元數據存儲在對象的序列化字節中 而不是像使用 PDX 時 Apache Geode 那樣在單獨的類型注冊表中。

PDX 不支持增量。從技術上講,PDX 可序列化對象可以通過實現?org.apache.geode.Delta接口,只有“delta”將是 發送,即使在 PDX 的上下文中也是如此。但是,必須反序列化 PDX 序列化對象才能應用增量。 請記住,調用一個方法來應用增量,這首先違背了使用 PDX 的目的。

在開發管理 {data-store-name} 集群中的數據的本機客戶端(例如 C)時,甚至在混合時 具有 Java 客戶機的本機客戶機,通常不會在類路徑上提供任何關聯的 Java 類型 群集中的服務器。使用 PDX,沒有必要在類路徑上提供 Java 類型,并且許多 僅開發和使用本機客戶端來訪問存儲在 {data-store-name} 中的數據的用戶將不會提供任何 Java 類型對應的 C/C/C# 類型。

Apache Geode還支持序列化到PDX的JSON。在這種情況下,Java 類型很可能不會 在服務器類路徑上提供,因為許多不同的語言(例如JavaScript,Python,Ruby)支持JSON可以 與Apache Geode一起使用。

盡管如此,即使使用 PDX,用戶也必須注意不要在群集中的服務器上造成 PDX 序列化對象。 要反序列化。

例如,考慮對序列化為 PDX 的以下 Java 類型的對象的 OQL 查詢...

@Region("People")class Person {  private LocalDate birthDate;  private String name;  public int getAge() {    // no explicit "age" field/property in Person    // age is just implemented in terms of the "birthDate" field  }}

隨后,如果 OQL 查詢在對象上調用方法,例如:??Person??

??SELECT * FROM /People p WHERE p.age >= 21??

然后,這將導致 PDX 序列化對象被反序列化,因為不是字段, 而是一種包含基于另一個字段(即)的計算的方法。同樣,調用 OQL 查詢中的任何方法都會導致反序列化發生 也。??Person????age????Person????Person????birthDate????java.lang.Object????Object.toString()??

Apache Geode 確實提供了讀取序列化配置設置,以便可能在 a 內部調用的任何緩存操作不會導致 PDX 序列化對象被反??序列化??。但是,沒有什么能阻止考慮不周的 OQL 查詢 導致反序列化,因此要小心。??Region.get(key)????Function??

數據序列化+ PDX +Java 序列化

Apache Geode 可以同時支持所有 3 種序列化格式。

例如,應用程序域模型可能包含實現接口的對象, 并且您可能將數據序列化框架與 PDX 結合使用。??java.io.Serialiable??

雖然可以將Java序列化與數據序列化和 PDX 一起使用,但通常更可取 并建議使用 1 個序列化策略。

Java序列化不同,數據序列化和 PDX序列化不處理對象圖周期。

有關Apache Geode序列化機制的更多背景可以在這里找到。

3.6.2. 春季會話的序列化

以前,Apache Geode 的 Spring Session 僅支持 Apache GeodeData Serialization格式。主要 這背后的動機是利用Apache Geode的增量傳播功能,因為會話 狀態可以是任意大的。

但是,從Apache Geode 2.0的春季會話開始,PDX也受支持,現在是新的默認序列化。 選擇。在 Apache Geode 2.0 的春季會話中,默認值更改為 PDX,主要是因為 PDX 是最 用戶廣泛使用和要求的格式。

PDX 無疑是最靈活的格式,以至于您甚至不需要 Apache Geode 的 Spring 會話。 或其對集群中服務器的類路徑的任何傳遞依賴關系,以便將 Spring 會話與 Apache Geode。事實上,使用 PDX,您甚至不需要將應用程序域對象類型存儲在 服務器類路徑上的 (HTTP) 會話。

本質上,當使用PDX序列化時,Apache Geode不需要存在關聯的Java類型。 在服務器的類路徑上。只要群集中的服務器上不發生反序列化,您就是安全的。

注釋引入了用戶的新屬性可用于配置在 Spring 容器中聲明和注冊的 bean 的名稱,實現所需的 序列化策略。Spring會話使用序列化策略進行Apache Geode序列化 會話狀態。??@EnableGemFireHttpSession????sessionSerializerBeanName??

開箱即用的 Apache Geode 春季會話提供了 2 種序列化策略:1 種用于 PDX,1 種用于數據序列化。它會自動在 Spring 容器中注冊兩個序列化策略 bean。 但是,這些策略中只有 1 種在運行時實際使用 PDX!

在實現數據序列化和 PDX 的 Spring 容器中注冊的 2 個 bean 分別被命名為 and。默認情況下,屬性 設置為,就像用戶注釋他/她的 Spring 啟動一樣,啟用 Spring 會話的應用程序 配置類,具有:??SessionDataSerializer????SessionPdxSerializer????sessionSerializerBeanName????SessionPdxSerializer??

@SpringBootApplication@EnableGemFireHttpSession(sessionSerializerBeanName = "SessionPdxSerializer")class MySpringSessionApplication {  }

通過將屬性設置為將序列化策略更改為數據序列化是一件簡單的事情,如下所示:??sessionSerializerBeanName????SessionDataSerializer??

@SpringBootApplication@EnableGemFireHttpSession(sessionSerializerBeanName = "SessionDataSerializer")class MySpringSessionApplication {  }

由于這兩個值非常常見,因此 Apache Geode 的 Spring Session 為 class:and 中的每個值提供了常量。因此,您可以顯式配置 PDX, 如下:??GemFireHttpSessionConfiguration????GemFireHttpSessionConfiguration.SESSION_PDX_SERIALIZER_BEAN_NAME????GemFireHttpSessionConfiguration.SESSION_DATA_SERIALIZER_BEAN_NAME??

import org.springframework.session.data.geode.config.annotation.web.http.GemFireHttpSessionConfiguration;@SpringBootApplication@EnableGemFireHttpSession(sessionSerializerBeanName = GemFireHttpSessionConfiguration.SESSION_PDX_SERIALIZER_BEAN_NAME)class MySpringSessionApplication {  }

使用 1 個屬性和 2 個現成的 Bean 定義,您可以指定所需的序列化框架 與您的 Spring Boot 一起使用,由 Apache Geode 支持的啟用 Spring 會話的應用程序。

3.6.3. Apache Geode 序列化框架的春季會議

為了抽象出Apache Geode的數據序列化和PDX序列化框架的細節,?Apache Geode的春季會議提供了自己的序列化框架(外觀),包裝了Apache Geode的 序列化框架。

序列化 API 存在于包下。主要 此 API 中的接口是。??org.springframework.session.data.gemfire.serialization????org.springframework.session.data.gemfire.serialization.SessionSerializer??

接口定義為:

春季會話接口??SessionSerializer??

interface SessionSerializer {  void serialize(T session, OUT out);  T deserialize(IN in);  boolean canSerialize(Class type);  boolean canSerialize(Object obj) {    // calls Object.getClass() in a null-safe way and then calls and returns canSerialize(:Class)  }}

基本上,該接口允許您序列化和反序列化 Springobject。??Session??

這些類型的 theandtype 參數和相應的方法參數提供對對象的引用 負責寫入字節流或從字節流中讀取。實際的 參數將特定于類型,具體取決于配置的基礎 Apache Geode 序列化策略。??IN????OUT????Session????Session??

例如,當使用Apache Geode的PDX序列化框架時,and將分別是and的實例。當Apache Geode的數據序列化框架被配置后,thenand將分別是and的實例。??IN????OUT????org.apache.geode.pdx.PdxReader????org.apache.geode.pdx.PdxWriter????IN????OUT????java.io.DataInput????java.io.DataOutput??

這些參數由框架自動提供給實現,并作為 前面提到的,是基于配置的基礎 Apache Geode 序列化策略。??SessionSerializer??

從本質上講,即使Apache Geode的春季會議提供了一個圍繞Apache Geode的門面。 序列化框架,底層的Apache Geode仍然期望這些序列化框架之一 用于將數據序列化到 Apache Geode 或從 Apache Geode 序列化。

那么???,SessionSerializer??接口到底有什么用呢?

實際上,它允許用戶自定義會話狀態的哪些方面實際被序列化和存儲 在阿帕奇·喬德。應用程序開發人員可以提供自己的自定義、特定于應用程序的實現,在 Spring 容器中將其注冊為 bean,然后將其配置為由 Spring 會話使用 讓 Apache Geode 序列化會話狀態,如下所示:??SessionSerializer??

@EnableGemFireHttpSession(sessionSerializerBeanName = "MyCustomSessionSerializer")class MySpringSessionDataGemFireApplication {  @Bean("MyCustomSessionSerializer")  SessionSerializer myCustomSessionSerializer() {    // ...  }}
Implementing a SessionSerializer

Apache Geode 的春季會話在用戶想要實現適合 Apache Geode 序列化框架之一的自定義時提供幫助。??SessionSerializer??

如果用戶只是實現接口 直接沒有從 Apache Geode 提供的抽象基類的春季會話之一擴展,相關 到 Apache Geode 的序列化框架中的 1 個,然后 Apache Geode 的 Spring 會話將包裝用戶的 自定義實現 在實例中并將其注冊到 Apache Geode 作為 a。??org.springframework.session.data.gemfire.serialization.SessionSerializer????SessionSerializer????org.springframework.session.data.gemfire.serialization.pdx.support.PdxSerializerSessionSerializerAdapter????org.apache.geode.pdx.PdxSerializer??

Apache Geode的春季會話小心翼翼地不要踩到用戶的任何現有實現 可能已經通過其他方式在Apache Geode注冊。事實上,有幾個不同的,提供 Apache Geode接口的實現存在:??PdxSerializer????org.apache.geode.pdx.PdxSerializer??

Apache Geode 本身提供了org.apache.geode.pdx.ReflectionBasedAutoSerializer。Spring Data for Apache Geode (SDG) 提供了org.springframework.data.gemfire.mapping.MappingPdxSerializer, 用于 SD存儲庫抽象和 SDG 的擴展,以處理將 PDX 序列化類型映射到 在應用程序存儲庫接口中定義的應用程序域對象類型。

這是通過獲取緩存上當前注冊的任何實例并對其進行組合來實現的 包裝用戶的自定義應用程序實現并在 Apache Geode 緩存上重新注冊此“復合”。“復合”實現由Spring Session為Apache Geode的類提供 當實體作為 PDX 存儲在 Apache Geode 中時。??PdxSerializer????PdxSerializerSessionSerializerAdapter????SessionSerializer????PdxSerializer????PdxSerializer????org.springframework.session.data.gemfire.pdx.support.ComposablePdxSerializer??

如果當前沒有其他適配器在 Apache Geode 緩存中注冊,則適配器 只是注冊。??PdxSerializer??

當然,您可以通過執行以下操作之一來強制使用與自定義實現一起使用的底層 Apache Geode 序列化策略:??SessionSerializer??

自定義實現可以實現Apache Geode的接口,或者為了方便起見,為Apache Geode的類擴展Spring Session。 Apache Geode的春季會議將向Apache Geode注冊customas a。SessionSerializerorg.apache.geode.pdx.PdxSerializerorg.springframework.session.data.gemfire.serialization.pdx.AbstractPdxSerializableSessionSerializerSessionSerializerPdxSerializer自定義實現可以擴展 Apache Geode 的類,或者為了方便起見,擴展 Apache Geode 類的 Spring 會話 Apache Geode的春季會議將向Apache Geode注冊customas a。SessionSerializerorg.apache.geode.DataSerializerorg.springframework.session.data.gemfire.serialization.data.AbstractDataSerializableSessionSerializerSessionSerializerDataSerializer最后,用戶可以像以前一樣創建自定義實現,而無需指定哪個 要使用的 Apache Geode 序列化框架,因為自定義實現未實現 任何 Apache Geode 序列化接口或從任何 Spring Session 擴展 for Apache Geode 提供的 抽象基類,并且仍然在 Apache Geode 中將其注冊為 a,方法是聲明一個 在類型的 Spring 容器中為 Apache Geode bean 提供額外的春季會話,例如...SessionSerializerSessionSeriaizerDataSerializerorg.springframework.session.data.gemfire.serialization.data.support.DataSerializerSessionSerializerAdapter

強制將自定義 SessionSerializer 注冊為 Apache Geode 中的數據序列化程序

@EnableGemFireHttpSession(sessionSerializerBeanName = "customSessionSerializer")class Application {  @Bean  DataSerializerSessionSerializerAdapter dataSerializerSessionSerializer() {    return new DataSerializerSessionSerializerAdapter();  }  @Bean  SessionSerializer customSessionSerializer() {    // ...  }}

僅僅通過春天容器中注冊為豆子的存在, 任何中性自定義實現都將被視為 ain Apache Geode 并注冊。??DataSerializerSessionSerializerAdapter????SessionSerializer????DataSerializer??

對數據序列化的其他支持

如果您正在配置和引導Apache Geode服務器,請隨時跳過本節 在集群中使用 Spring (引導),因為通常,以下信息將不適用。答案是肯定的 這完全取決于您聲明的依賴項和 Spring 配置。但是,如果您使用Gfsh啟動集群中的服務器,請務必繼續閱讀。

背景

使用 Apache Geode 的DataSerialization框架時,尤其是在序列化 (HTTP) 會話時從客戶端 狀態,您必須注意在集群中配置 Apache Geode 服務器 具有適當的依賴項。在利用增量時尤其如此,如前面部分所述 關于數據序列化

使用數據序列化框架作為序列化策略以序列化(HTTP) 會話狀態時 您的 Web 應用程序客戶端到服務器,則必須使用春季會話正確配置服務器 用于表示 (HTTP) 會話及其內容的 Apache Geode 類類型。這意味著包括 服務器類路徑上的 Spring JAR。

此外,使用DataSerialization可能還需要包含包含應用程序域的 JAR。 Web 應用程序使用并作為會話屬性值放入 (HTTP) 會話中的類, 特別是如果:

您的類型實現接口。org.apache.geode.DataSerializable您的類型實現接口。org.apache.geode.Delta您已注冊標識和序列化類型的 a。org.apache.geode.DataSerializer您的類型實現接口。java.io.Serializable

當然,必須確保放入 (HTTP) 會話中的應用程序域對象類型在某些會話中是可序列化的 形式或其他形式。但是,您并不嚴格要求使用數據序列化,也不一定 在以下情況下,需要在服務器類路徑上具有應用程序域對象類型:

您的類型實現接口。org.apache.geode.pdx.PdxSerializable或者,您已注冊正確標識和序列化 您的應用程序域對象類型。org.apache.geode.pdx.PdxSerializer

Apache Geode 在確定要使用的序列化策略時將應用以下優先級順序 序列化對象圖:

首先,對象和/或任何已注冊標識要序列化的對象。DataSerializableDataSerializers然后對象和/或任何已注冊標識要序列化的對象。PdxSerializablePdxSerializer最后,所有類型。java.io.Serializable

這也意味著,如果特定的應用程序域對象類型(例如)實現, 但是,已向Apache Geode注冊了(自定義)標識相同的應用程序 域對象類型(即),則 Apache Geode 將使用 PDX 來序列化“A”而不是 Java序列化, 在這種情況下。??A????java.io.Serializable????PdxSerializer????A??

這特別有用,因為您可以使用DataSerialization來序列化 (HTTP) 會話對象,利用 增量和數據序列化的所有強大功能,但隨后使用 PDX 序列化應用程序域對象 類型,這大大簡化了所涉及的配置和/或工作量。

現在我們已經大致了解了為什么存在這種支持,您如何啟用它?

配置

首先,創建一個 Apache Geode,如下所示:??cache.xml??

阿帕奇大地勘測配置??cache.xml??

            org.springframework.session.data.gemfire.serialization.data.support.DataSerializableSessionSerializerInitializer      

然后,使用*Gfsh*啟動服務器:

使用 Gfsh 啟動服務器

gfsh> start server --name=InitializedServer --cache-xml-file=/path/to/cache.xml --classpath=...

使用適當的依賴項配置Apache Geode服務器是棘手的部分, 但一般來說,以下內容應該有效:??classpath??

類路徑配置

gfsh> set variable --name=REPO_HOME --value=${USER_HOME}/.m2/repositorygfsh> start server ... --classpath=\${REPO_HOME}/org/springframework/spring-core/{spring-version}/spring-core-{spring-version}.jar\:${REPO_HOME}/org/springframework/spring-aop/{spring-version}/spring-aop-{spring-version}.jar\:${REPO_HOME}/org/springframework/spring-beans/{spring-version}/spring-beans-{spring-version}.jar\:${REPO_HOME}/org/springframework/spring-context/{spring-version}/spring-context-{spring-version}.jar\:${REPO_HOME}/org/springframework/spring-context-support/{spring-version}/spring-context-support-{spring-version}.jar\:${REPO_HOME}/org/springframework/spring-expression/{spring-version}/spring-expression-{spring-version}.jar\:${REPO_HOME}/org/springframework/spring-jcl/{spring-version}/spring-jcl-{spring-version}.jar\:${REPO_HOME}/org/springframework/spring-tx/{spring-version}/spring-tx-{spring-version}.jar\:${REPO_HOME}/org/springframework/data/spring-data-commons/{spring-data-commons-version}/spring-data-commons-{spring-data-commons-version}.jar\:${REPO_HOME}/org/springframework/data/spring-data-geode/{spring-data-geode-version}/spring-data-geode-{spring-data-geode-version}.jar\:${REPO_HOME}/org/springframework/session/spring-session-core/{spring-session-core-version}/spring-session-core-{spring-session-core-version}.jar\:${REPO_HOME}/org/springframework/session/spring-session-data-geode/{spring-session-data-geode-version}/spring-session-data-geode-{spring-session-data-geode-version}.jar\:${REPO_HOME}/org/slf4j/slf4j-api/1.7.25/slf4j-api-1.7.25.jar

請記住,您可能還需要將應用程序域對象 JAR 文件添加到服務器類路徑中。

若要全面了解其工作原理,請參閱示例。

自定義更改檢測

默認情況下,每當修改會話(例如更新到當前時間)時,會話 被 Apache Geode (SSDG) 的春季會議認為是骯臟的。在使用Apache Geode數據序列化框架時,利用Apache Geode的增量傳播功能也非常有用和有價值。??lastAccessedTime??

使用數據序列化時,SSDG 還使用增量傳播僅發送對會話狀態的更改 客戶端和服務器。這包括可能已添加、刪除或更新的任何會話屬性。

默認情況下,每當調用時,會話屬性都被視為“臟” 并將在客戶端和服務器之間的增量中發送。即使應用程序域對象也是如此 尚未更改。??Session.setAttribute(name, value)??

通常,除非對象已更改,否則永遠沒有理由調用。然而 如果發生這種情況,并且您的對象相對較大(具有復雜的對象層次結構),那么您可能需要 請考慮以下任一情況:??Session.setAttribute(..)??

在應用程序域對象中實現增量接口 模型雖然有用,但非常具有侵入性,或者...提供 SSDG 戰略界面的自定義實現。org.springframework.session.data.gemfire.support.IsDirtyPredicate

開箱即用,SSDG 提供了 5 種策略接口實現:??IsDirtyPredicate??

表 5.實現??IsDirtyPredicate??

描述

違約

??IsDirtyPredicate.ALWAYS_DIRTY??

新會話屬性值始終被視為臟。

??IsDirtyPredicate.NEVER_DIRTY??

新的會話屬性值永遠不會被視為臟。

??DeltaAwareDirtyPredicate??

當舊值和新值時,新會話屬性值被視為臟 如果新值的類型未實現或新值的方法返回true,則不同。??Delta????Delta.hasDelta()??

是的

??EqualsDirtyPredicate??

如果舊值不等于,則新會話屬性值被視為臟 確定方法的新值。??Object.equals(:Object)??

??IdentityEqualsPredicate??

新會話屬性值被視為臟值,如果舊值與 使用標識等于運算符的新值(即)。??oldValue != newValue??

如上表所示,這是 SSDG 使用的默認實現。 自動考慮實現的應用程序域對象 Apache Geodeinterface。但是,即使您的應用程序也能正常工作 域對象不實現接口。SSDG 會將您的應用程序域對象視為臟對象 任何時候,只要提供新值與舊值不同, 或者新值沒有實現接口。??DeltaAwareDirtyPredicate????DeltaAwareDirtyPredicate????Delta????DeltaAwareDirtyPredicate????Delta????Session.setAttribute(name, newValue)????Delta??

您可以更改 SSDG 的臟實現、確定策略,只需在 Spring 容器中聲明一個 bean 即可 接口類型:??IsDirtyPredicate??

覆蓋 SSDG 的默認策略??IsDirtyPredicate??

@EnableGemFireHttpSessionclass ApplicationConfiguration {  @Bean  IsDirtyPredicate equalsDirtyPredicate() {    return EqualsDirtyPredicate.INSTANCE;  }}
組成

該接口還提供和方法 在組合中組合 2 個或多個實現,以便組織復雜的邏輯和規則 用于確定應用程序域對象是否臟。??IsDirtyPredicate????andThen(:IsDirtyPredicate)????orThen(:IsDirtyPredicate)????IsDirtyPredicate??

例如,您可以使用 OR 運算符組合兩者:??EqualsDirtyPredicate????DeltaAwareDirtyPredicate??

組合使用邏輯 OR 運算符??EqualsDirtyPredicate????DeltaAwareDirtyPredicate??

@EnableGemFireHttpSessionclass ApplicationConfiguration {  @Bean  IsDirtyPredicate equalsOrThenDeltaDirtyPredicate() {    return EqualsDirtyPredicate.INSTANCE      .orThen(DeltaAwareDirtyPredicate.INSTANCE);  }}

您甚至可以實現自己的自定義基于特定應用程序域對象類型:??IsDirtyPredicates??

特定于應用程序域對象類型的實現??IsDirtyPredicate??

class CustomerDirtyPredicate implements IsDirtyPredicate {  public boolean isDirty(Object oldCustomer, Object newCustomer) {      if (newCustomer instanceof Customer) {        // custom logic to determine if a new Customer is dirty      }      return true;  }}class AccountDirtyPredicate implements IsDirtyPredicate {  public boolean isDirty(Object oldAccount, Object newAccount) {      if (newAccount instanceof Account) {        // custom logic to determine if a new Account is dirty      }      return true;  }}

然后與 and 組合回退的默認謂詞,如下所示:??CustomerDirtyPredicate????AccountDirtyPredicate??

組合和配置特定于類型??IsDirtyPredicates??

@EnableGemFireHttpSessionclass ApplicationConfiguration {  @Bean  IsDirtyPredicate typeSpecificDirtyPredicate() {    return new CustomerDirtyPredicate()      .andThen(new AccountDirtyPredicate())      .andThen(IsDirtyPredicate.ALWAYS_DIRTY);  }}

組合和可能性是無窮無盡的。

實施自定義策略時要小心。如果您錯誤地確定 應用程序域對象在實際臟時并不臟,那么它將不會在會話增量中發送 從客戶端到服務器。??IsDirtyPredicate??

更改會話表示形式

在內部,Apache Geode 的 Spring Session 維護 (HTTP) 會話和會話的 2 種表示形式 屬性。每個表示形式都基于是否支持Apache Geode“增量”。

由于前面討論過的原因,Apache Geode增量傳播僅在使用數據序列化時由 Apache Geode 的春季會話啟用。

實際上,該策略是:

如果配置了 Apache Geode數據序列化,則支持增量并使用 theand表示。DeltaCapableGemFireSessionDeltaCapableGemFireSessionAttributes如果配置了 Apache Geode PDX序列化,則將禁用增量傳播并使用與表示。GemFireSessionGemFireSessionAttributes

可以覆蓋Spring Session為Apache Geode和用戶使用的這些內部表示。 以提供自己的會話相關類型。唯一嚴格的要求是會話實現 必須實現核心 Spring 會話接口。??org.springframework.session.Session??

例如,假設您要定義自己的會話實現。

首先,定義類型。也許您的自定義類型甚至封裝和處理會話 屬性,而無需定義單獨的類型。??Session????Session??

用戶定義的會話接口實現

class MySession implements org.springframework.session.Session {  // ...}

然后,您需要擴展類 并重寫該方法以創建自定義實現類的實例。??org.springframework.session.data.gemfire.GemFireOperationsSessionRepository????createSession()????Session??

自定義會話存儲庫實現創建和返回自定義會話類型的實例

class MySessionRepository extends GemFireOperationsSessionRepository {  @Override  public Session createSession() {    return new MySession();  }}

如果您提供自己的自定義實現,并且 Apache Geode PDX序列化是 配置,然后你完成了。??SessionSerializer??

但是,如果您配置了 Apache Geode數據序列化,則必須另外提供自定義 接口的實現,要么讓它直接擴展Apache Geode的類,要么擴展Apache Geode的類的Spring Session。 并覆蓋該方法。??SessionSerializer????org.apache.geode.DataSerializer????org.springframework.session.data.gemfire.serialization.data.AbstractDataSerializableSessionSerializer????getSupportedClasses():Class[]??

例如:

自定義會話序列化程序用于自定義會話類型

class MySessionSerializer extends AbstractDataSerializableSessionSerializer {  @Override  public Class[] getSupportedClasses() {    return new Class[] { MySession.class };  }}

不幸的是,無法返回通用的 Spring 會話接口類型。如果可以,那么我們可以避免顯式需要覆蓋方法 自定義實現。但是,Apache Geode的數據序列化框架只能匹配 在確切的類類型上,因為它錯誤地在內部存儲并按名稱引用類類型,然后 要求用戶重寫和實現該方法。??getSupportedClasses()????org.springframework.session.Session????getSupportedClasses()????DataSerializer????getSupportedClasses()??

3.7. HTTPSESSION集成的工作原理

幸運的是,bothand(用于 獲取 an) 是接口。這意味著我們可以為每個 API 提供自己的實現。??javax.servlet.http.HttpSession????javax.servlet.http.HttpServletRequest????HttpSession??

本節介紹春季會話如何提供透明的集成。 目的是讓用戶了解引擎蓋下發生的事情。此功能已實現 并集成,因此您無需自己實現此邏輯。??javax.servlet.http.HttpSession??

首先,我們創建一個 customReturna 的自定義實現。它看起來像下面這樣:??javax.servlet.http.HttpServletRequest????javax.servlet.http.HttpSession??

public class SessionRepositoryRequestWrapper extends HttpServletRequestWrapper {  public 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 ...}

任何返回 anis 的方法都被重寫。所有其他方法都由原始實現實現,并簡單地委托給原始實現。??javax.servlet.http.HttpSession????javax.servlet.http.HttpServletRequestWrapper????javax.servlet.http.HttpServletRequest??

我們使用 Servletcall 替換實現。偽代碼可以在下面找到:??javax.servlet.http.HttpServletRequest????Filter????SessionRepositoryFilter??

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);  }  // ...}

通過將自定義實現傳遞到我們確保 在我們自定義實現之后調用的任何內容。??javax.servlet.http.HttpServletRequest????FilterChain????Filter????javax.servlet.http.HttpSession??

這突出了為什么春季會議必須放在任何事情之前很重要 與交互。??SessionRepositoryFilter????javax.servlet.http.HttpSession??

3.8. HttpSessionListener

春季會議通過翻譯和聲明支持。??HttpSessionListener????SessionCreatedEvent????SessionDestroyedEvent????HttpSessionEvent????SessionEventHttpSessionListenerAdapter??

要使用此支持,您需要:

確保您的實現支持并配置為觸發和“會話已銷毀事件”。SessionRepositorySessionCreatedEvent配置為春豆。SessionEventHttpSessionListenerAdapter將每個注入HttpSessionListenerSessionEventHttpSessionListenerAdapter

如果您使用的是HttpSession with Apache Geode 中記錄的配置支持, 然后,您需要做的就是注冊每個豆子。??HttpSessionListener??

例如,假設您想要支持 Spring 安全性的并發控制并需要使用, 然后你可以簡單地添加一個豆子。??HttpSessionEventPublisher????HttpSessionEventPublisher??

3.9. 會話

A是簡化的鍵/值對,支持過期。??Session????Map??

3.10. 會話存儲庫

Ais 負責創建、持久化和訪問實例和狀態。??SessionRepository????Session??

如果可能,開發人員不應直接與 aor a 交互。相反,開發人員 應該更喜歡通過和集成間接地與之交互。??SessionRepository????Session????SessionRepository????Session????javax.servlet.http.HttpSession????WebSocket????WebSession??

3.11. 查找按索引名稱會話存儲庫

Spring Session 使用 ais 的最基本 API。該 API 有意非常簡單 以便輕松提供具有基本功能的其他實現。??Session????SessionRepository??

某些實現可能會選擇實現。 例如,Spring Session的Apache Geode支持實現。??SessionRepository????FindByIndexNameSessionRepository????FindByIndexNameSessionRepository??

添加單個方法來查找特定用戶的所有會話。 這是通過確保使用用戶名填充具有名稱的會話屬性來完成的。開發人員有責任確保填充屬性,因為 Spring 會話不知道正在使用的身份驗證機制。??FindByIndexNameSessionRepository????FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME??

某些實現將提供鉤子來自動索引其他會話屬性。 例如,許多實現將自動確保當前 Spring 安全性用戶名的索引為 索引名稱。??FindByIndexNameSessionRepository????FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME??

3.12. 啟用彈簧 httpSession

注釋可以添加到任何類中,以在名為“springSessionRepositoryFilter”的Spring容器中公開Bean。??@EnableSpringHttpSession????@Configuration????SessionRepositoryFilter??

為了利用注釋,必須提供單豆。??SessionRepository??

3.13. 啟用寶石火 httpSession

可以將 theannotation 添加到任何類中來代替 theannotation,以在 Spring 容器中公開一個名為 bean, “springSessionRepositoryFilter”,并將 Apache Geode 定位為管理狀態的提供者。??@EnableGemFireHttpSession????@Configuration????@EnableSpringHttpSession????SessionRepositoryFilter????javax.servlet.http.HttpSession??

使用注釋時,其他配置是開箱即用的導入的,這些配置也 提供了名為 Apache Geode 的接口的特定實現。??@EnableGemFireHttpSession????SessionRepository????GemFireOperationsSessionRepository??

3.14. 寶石火操作會話存儲庫

??GemFireOperationsSessionRepository??是使用春季會話實現的實現 對于Apache Geode"s_。??SessionRepository????GemFireOperationsSessionRepository??

在 Web 環境中,此存儲庫與 一起使用。??SessionRepositoryFilter??

此實現支持,并貫穿始終。??SessionCreatedEvents????SessionDeletedEvents????SessionDestroyedEvents????SessionEventHttpSessionListenerAdapter??

3.14.1. 在 Apache Geode 中使用索引

雖然關于正確定義對Apache Geode性能產生積極影響的索引的最佳實踐 超出了本文檔的范圍,重要的是要認識到 Apache Geode 的春季會話創建 并使用索引有效地查詢和查找會話。

開箱即用的 Apache Geode 春季會話在主體名稱上創建 1 個哈希類型索引。有兩個 用于查找主體名稱的不同內置策略。第一個策略是會話的價值 具有名稱的屬性將被索引到相同的 索引名稱。??FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME??

例如:

String indexName = FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME;session.setAttribute(indexName, username);Map idToSessions =  this.sessionRepository.findByIndexNameAndIndexValue(indexName, username);

3.14.2. 將索引與 Apache Geode 和 Spring 安全性結合使用

或者,Apache Geode 的春季會議將映射 Spring Security 的當前 指數。??Authentication#getName()????FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME??

例如,如果您使用的是 Spring 安全性,則可以使用以下方法查找當前用戶的會話:

SecurityContext securityContext = SecurityContextHolder.getContext();Authentication authentication = securityContext.getAuthentication();String indexName = FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME;Map idToSessions =  this.sessionRepository.findByIndexNameAndIndexValue(indexName, authentication.getName());

3.14.3. 將自定義索引與 Apache Geode 結合使用

這使開發人員能夠以編程方式使用查詢和查找所有會話 有效地使用給定的主體名稱。??GemFireOperationsSessionRepository??

此外,Apache Geode 的春季會議將在實施會話的 映射類型屬性(即在任何任意會話屬性上),當開發人員識別 1 個或多個時 應由 Apache Geode 編制索引的命名會話屬性。??attributes??

索引的會話屬性可以使用注釋上的屬性指定。開發人員將此注釋添加到他們的 Spring 應用程序類中,當他/她希望啟用 Spring 會話的支持時,由 Apache Geode。??indexableSessionAttributes????@EnableGemFireHttpSession????@Configuration????HttpSession??

String indexName = "name1";session.setAttribute(indexName, indexValue);Map idToSessions =  this.sessionRepository.findByIndexNameAndIndexValue(indexName, indexValue);

只有在注釋的屬性中標識的會話屬性名稱才會定義索引。不會為所有其他會話屬性編制索引。??@EnableGemFireHttpSession????indexableSessionAttributes??

但是,有一個問題。存儲在可索引會話屬性中的任何值都必須實現接口。如果這些對象值沒有實現,則 Apache Geode 為具有持久會話數據的區域定義索引時,或者在嘗試時,將在啟動時引發錯誤 在運行時進行,以為可索引會話屬性分配一個不是的值,并且會話被保存 到阿帕奇·喬德。??java.lang.Comparable????Comparable????Comparable??

任何未編制索引的會話屬性都可以存儲非值。??Comparable??

要了解有關 Apache Geode 基于范圍的索引的更多信息,請參閱在地圖字段上創建索引。

要了解有關 Apache Geode 索引的更多信息,請參閱使用索引。

4. 春季會議社區

我們很高興將您視為我們社區的一部分。 請在下面找到更多信息。

4.1. 支持

您可以通過在StackOverflow 上使用標簽spring-session提問來獲得幫助。 同樣,我們鼓勵通過回答StackOverflow上的問題來幫助他人。

4.2. 源代碼

源代碼可以在GitHub上找到,網址為https://github.com/spring-projects/spring-session-data-geode

4.3. 問題跟蹤

我們在 GitHub 問題中跟蹤問題https://github.com/spring-projects/spring-session-data-geode/issues

4.4. 貢獻

我們感謝拉取請求。

4.5. 許可

Apache Geode 的春季會議和 Pivotal GemFire 的春季會議是開源軟件 在Apache 2.0 許可證下發布。

5. 最低要求

春季課程的最低要求是:

爪哇 8+如果您在 Servlet 容器中運行(不是必需的),Servlet 2.5+如果您使用的是其他 Spring 庫(不是必需的),則所需的最低版本是 Spring Framework 5.0.x。??@EnableGemFireHttpSession??需要 Apache Geode 2.0.x 的 Spring Data 和 Pivotal GemFire 2.0.x 的 Spring Data。??@EnableGemFireHttpSession??需要 Apache Geode 1.2.x 或 Pivotal GemFire 9.1.x。

春季會議的核心只有必需的依賴關系。??spring-jcl??

標簽: 應用程序 會話狀態 應用程序域

上一篇:全球今亮點!如何使用JPA實現Spring 授權服務器的核心服務
下一篇:Spring for GraphQL入門