天天看點

Spring認證中國教育管理中心-Apache Geode 的 Spring 資料教程一

  1. 文檔結構

以下章節解釋了 Spring Data 為 Apache Geode 提供的核心功能:

Bootstrapping Apache Geode with the Spring Container描述了為配置、初始化和通路 Apache Geode 緩存、區域和相關分布式系統元件提供的配置支援。

使用 Apache Geode API解釋了 Apache Geode API 與 Spring 中可用的各種資料通路功能之間的內建,例如基于模闆的資料通路、異常轉換、事務管理和緩存。

使用 Apache Geode 序列化描述了對 Apache Geode 的托管對象序列化和反序列化的增強。

POJO 映射描述了使用 Spring Data 存儲在 Apache Geode 中的 POJO 的持久性映射。

Spring Data for Apache Geode Repositories描述了如何通過使用基本的 CRUD 和簡單的查詢操作來建立和使用 Spring Data Repositories 來通路存儲在 Apache Geode 中的資料。

函數執行的注釋支援描述了如何通過使用注釋來執行資料所在的分布式計算來建立和使用 Apache Geode 函數。

連續查詢 (CQ)描述了如何使用 Apache Geode 的連續查詢 (CQ) 功能來處理基于興趣的事件流,該興趣使用 Apache Geode 的 OQL(對象查詢語言)定義和注冊。

在 Apache Geode中引導 Spring ApplicationContext描述了如何ApplicationContext 使用Gfsh.

示例應用程式描述了随發行版提供的示例,以說明 Spring Data for Apache Geode 中可用的各種功能。

  1. 使用 Spring 容器引導 Apache Geode

Spring Data for Apache Geode 使用 Spring IoC 容器提供了 Apache Geode In-Memory Data Grid (IMDG) 的完整配置和初始化。該架構包括幾個類來幫助簡化 Apache Geode 元件的配置,包括:緩存、區域、索引、磁盤存儲、函數、WAN 網關、持久性備份和其他幾個分布式系統元件,以最少的工作支援各種應用程式用例.

本節假設您基本熟悉 Apache Geode。有關更多資訊,請參閱 Apache Geode 産品文檔。

5.1.使用 Spring 而不是 Apache Geode 的優勢cache.xml

Spring Data for Apache Geode 的 XML 命名空間支援 Apache Geode In-Memory Data Grid (IMDG) 的完整配置。XML 命名空間是在 Spring 上下文中配置 Apache Geode 以在 Spring 容器内正确管理 Apache Geode 生命周期的兩種方法之一。在 Spring 上下文中配置 Apache Geode 的另一種方法是使用基于注解的配置。

雖然cache.xml由于遺留原因仍然支援 Apache Geode 的本機,但鼓勵使用 XML 配置的 Apache Geode 應用程式開發人員在 Spring XML 中做所有事情,以利用 Spring 提供的許多美妙的東西,例如子產品化 XML 配置、屬性占位符和覆寫、SpEL(Spring 表達式語言)和環境配置檔案。在 XML 命名空間的背後,Spring Data for Apache Geode 廣泛使用 Spring 的FactoryBean模式來簡化 Apache Geode 元件的建立、配置和初始化。

阿帕奇的Geode提供了幾個回調接口,如CacheListener,CacheLoader和CacheWriter,這讓開發人員添加自定義事件處理程式。使用 Spring 的 IoC 容器,您可以将這些回調配置為普通的 Spring bean,并将它們注入到 Apache Geode 元件中。這是對 native 的重大改進cache.xml,它提供了相對有限的配置選項,并且需要回調來實作 Apache Geode 的Declarable接口(請參閱Wiring DeclarableComponents以了解如何仍然可以Declarables 在 Spring 的容器中使用)。

此外,諸如 Spring Tool Suite (STS) 之類的 IDE 為 Spring XML 命名空間提供了出色的支援,包括代碼完成、彈出注釋和實時驗證。

5.2.使用核心命名空間

為了簡化配置,Spring Data for Apache Geode 提供了一個專用的 XML 命名空間來配置核心 Apache Geode 元件。可以使用 Spring 的标準定義直接配置 bean 。但是,所有 bean 屬性都通過 XML 名稱空間公開,是以使用原始 bean 定義幾乎沒有好處。

有關 Spring 中基于 XML Schema 的配置的更多資訊,請參閱Spring Framework 參考文檔中的 附錄。

Spring Data Repository 支援使用單獨的 XML 命名空間。有關如何為 Apache Geode Repositories 配置 Spring Data 的更多資訊,請參閱Spring Data for Apache Geode Repositories。

要使用 Spring Data for Apache Geode XML 命名空間,請在 Spring XML 配置中繼資料中聲明它,如以下示例所示:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:gfe="https://www.springframework.org/schema/geode" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
https://www.springframework.org/schema/geode https://www.springframework.org/schema/geode/spring-geode.xsd            

">

<gfe:cache ...>

pring Data for Apache Geode XML 命名空間字首。任何名稱都可以使用,但在本參考文檔gfe中使用了所有名稱。

XML 命名空間字首映射到 URI。

XML 命名空間 URI 位置。請注意,即使該位置指向外部位址(确實存在且有效),Spring 也會在本地解析模式,因為它包含在 Spring Data for Apache Geode 庫中。

使用帶有gfe字首的 XML 命名空間的示例聲明。

您可以将預設命名空間從 更改beans為gfe。這對于主要由 Apache Geode 元件組成的 XML 配置很有用,因為它避免了聲明字首。為此,請交換前面顯示的命名空間字首聲明,如以下示例所示:

<beans xmlns="https://www.springframework.org/schema/geode"

xmlns:beans="http://www.springframework.org/schema/beans" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
https://www.springframework.org/schema/geode https://www.springframework.org/schema/geode/spring-geode.xsd           

<beans:bean id ... >

此 XML 文檔的預設命名空間聲明指向 Spring Data for Apache Geode XML 命名空間。

beansSpring 原始 bean 定義的命名空間字首聲明。

使用beans命名空間的Bean 聲明。注意字首。

使用gfe命名空間的Bean 聲明。請注意缺少字首,因為gfe它是預設命名空間。

Spring認證中國教育管理中心-Apache Geode 的 Spring 資料教程一

5.3.使用資料通路命名空間

除了核心 XML 命名空間 ( gfe) 之外,Spring Data for Apache Geode 還提供了資料通路 XML 命名空間 ( gfe-data),主要用于簡化 Apache Geode 用戶端應用程式的開發。此命名空間目前包含對 Apache Geode Repositories和 Function execution 的支援,以及提供連接配接到 Apache Geode 叢集的便捷方式的标簽。

5.3.1.連接配接到 Apache Geode 的簡單方法

對于許多應用程式,使用預設值與 Apache Geode 資料網格的基本連接配接就足夠了。Spring Data for Apache Geode 的标簽提供了一種通路資料的簡單方法。資料源建立一個ClientCache 和連接配接Pool。此外,它會查詢所有現有根區域的叢集伺服器,并為每個區域建立一個(空)用戶端區域代理。

<gfe-data:datasource>

</gfe-data:datasource>

該标簽在文法上類似于<gfe:pool>。它可以配置一個或多個嵌套locator 或server元素以連接配接到現有資料網格。此外,支援可用于配置池的所有屬性。此配置為連接配接到 Locator 的叢集成員上定義的每個 Region 自動建立用戶端 Region bean,是以它們可以被 Spring Data 映射注釋 ( GemfireTemplate)無縫引用并自動裝配到應用程式類中。

當然,您可以顯式配置用戶端區域。例如,如果要将資料緩存在本地記憶體中,如下例所示:

<gfe:client-region id="Example" shortcut="CACHING_PROXY"/>