Spring认证中国教育管理中心-Apache Geode 的 Spring 数据教程一

本文档介绍了如何使用 Spring 容器引导和配置 Apache Geode,强调了使用 Spring 配置的优势,如模块化、属性占位符支持和回调注入。内容涵盖核心和数据访问命名空间的使用,以及使用 `<datasource>` 标签简化连接到 Apache Geode 的方法。

4. 文档结构

以下章节解释了 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 中可用的各种功能。

5. 使用 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 。但是,所有 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 
">

  <bean id ... >

  <gfe:cache ...> 

</beans>

pring Data for Apache Geode XML 命名空间前缀。任何名称都可以使用,但在本参考文档gfe中使用了所有名称。

XML 命名空间前缀映射到 URI。

XML 命名空间 URI 位置。请注意,即使该位置指向外部地址(确实存在且有效),Spring 也会在本地解析模式,因为它包含在 Spring Data for Apache Geode 库中。

使用带有gfe前缀的 XML 命名空间的示例声明。

您可以将默认命名空间从 更改beans为gfe。这对于主要由 Apache Geode 组件组成的 XML 配置很有用,因为它避免了声明前缀。为此,请交换前面显示的命名空间前缀声明,如以下示例所示:

<?xml version="1.0" encoding="UTF-8"?>
<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 ... > 

  <cache ...> 

</beans>

此 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 的支持,以及<datasource>提供连接到 Apache Geode 集群的便捷方式的标签。

5.3.1.连接到 Apache Geode 的简单方法

对于许多应用程序,使用默认值与 Apache Geode 数据网格的基本连接就足够了。Spring Data for Apache Geode 的<datasource>标签提供了一种访问数据的简单方法。数据源创建一个ClientCache 和连接Pool。此外,它会查询所有现有根区域的集群服务器,并为每个区域创建一个(空)客户端区域代理。

<gfe-data:datasource>
  <locator host="remotehost" port="1234"/>
</gfe-data:datasource>

该<datasource>标签在语法上类似于<gfe:pool>。它可以配置一个或多个嵌套locator 或server元素以连接到现有数据网格。此外,支持可用于配置池的所有属性。此配置为连接到 Locator 的集群成员上定义的每个 Region 自动创建客户端 Region bean,因此它们可以被 Spring Data 映射注释 ( GemfireTemplate)无缝引用并自动装配到应用程序类中。

当然,您可以显式配置客户端区域。例如,如果要将数据缓存在本地内存中,如下例所示:

<gfe-data:datasource>
  <locator host="remotehost" port="1234"/>
</gfe-data:datasource>

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

 

**Spring Session for Apache Geode & Pivotal GemFire** 是 Spring Session 项目中的个模块,旨在将用户会话(HTTP Session)数据存储到 **Apache Geode** 或其商业版本 **Pivotal GemFire** 中,以实现分布式环境下的会话共享与高可用。它特别适用于运行在云平台(如 Cloud Foundry)上的 Spring 应用,支持横向扩展、故障恢复和跨实例会话致性。 > ✅ 官方模块名称:`spring-session-data-geode` > 🔗 项目地址:[https://github.com/spring-projects/spring-session/tree/main/spring-session-data-geode](https://github.com/spring-projects/spring-session) > 📦 所属生态:Spring Session > Spring Data > Spring Boot --- ### 🎯 核心目标 | 目标 | 说明 | |------|------| | 🔁 **会话集群化** | 将 HTTP Session 存储在 Geode/GemFire 分布式缓存中,而非本地内存 | | 💥 **高可用性** | 即使某个应用实例宕机,用户会话仍可被其他节点访问 | | ⚖️ **负载均衡友好** | 支持无粘性会话(Session Stickiness)的负载策略 | | 🛡️ **安全性增强** | 集成 Spring Security,支持认证信息持久化 | | 🧩 **透明集成** | 对开发者透明,无需修改业务代码即可启用 | --- ### 🏗️ 架构概览 ```text +------------------+ +------------------+ | App Instance 1 |<----->| | +------------------+ | | Apache Geode / +------------------+ Pivotal GemFire <--> [Management UI, gfsh] | App Instance 2 |<----->| (Distributed | +------------------+ | Cache Cluster) | | | +------------------+ | | | App Instance N |<----->| | +------------------+ +------------------+ ↑ 所有实例共享同份会话数据 ``` - 每个 Spring Boot 应用通过 `spring-session-data-geode` 连接到 Geode 集群。 - 用户登录后,`HttpSession` 被序列化并写入 Geode Region(如 `SESSIONS`)。 - 后续请求可由任意实例处理,自动从缓存读取会话。 --- ### 💡 快速使用示例(基于 Spring Boot) #### 1. 添加依赖(Maven) ```xml <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-geode</artifactId> <version>2.7.0</version> <!-- 匹配 Spring Boot 版本 --> </dependency> <!-- 如果使用 Pivotal GemFire --> <dependency> <groupId>com.vmware.gemfire</groupId> <artifactId>gemfire-core</artifactId> <version>9.15.0</version> </dependency> ``` #### 2. 启用 Spring Session + Geode ```java @SpringBootApplication @EnableGemFireHttpSession // 关键注解:启用 Geode 作为 Session 存储 public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } } ``` #### 3. 配置连接信息(application.yml) ```yaml spring: data: gemfire: pool: subscription-enabled: true locators: - host: localhost port: 10334 session: timeout: 30m store-type: GEODE ``` #### 4. 自动创建 Region(默认为 `SESSIONS`) Spring Session 会在 Geode 中自动创建名为 `SESSIONS` 的 Region 来存储会话数据: ```java @Region("SESSIONS") public class SessionEntry implements Serializable { ... } ``` 你也可以自定义 Region 名称: ```java @EnableGemFireHttpSession(regionName = "MySessions") ``` --- ### 🔧 核心特性详解 | 特性 | 描述 | |------|------| | ✅ **自动配置** | 与 Spring Boot 自动装配无缝集成 | | ✅ **事件驱动更新** | 当 `HttpSession.setAttribute()` 被调用时,异步同步到 Geode | | ✅ **过期自动清理** | 支持 TTL 和 Idle Timeout,Geode 自动清除过期会话 | | ✅ **与 Spring Security 集成** | 支持并发会话控制、会话固定攻击防护 | | ✅ **支持客户端/服务器拓扑** | 可连接远程 Geode Server 或嵌入本地 Peer | | ✅ **JSON 序列化支持** | 可选使用 `jackson-databind` 序列化复杂对象 | --- ### ⚙️ 高级配置选项 #### 自定义序列化方式(使用 Jackson) ```java @Configuration @EnableGemFireHttpSession public class SessionConfig { @Bean public ObjectMapper objectMapper() { return new ObjectMapper() .registerModule(new Jdk8Module()) .registerModule(new JavaTimeModule()); } @Bean public SessionSerializer<Object> springSessionDefaultRedisSerializer() { return new GenericJackson2JsonRedisSerializer(objectMapper()); } } ``` > 注意:虽然类名含 "Redis",但在 Geode 模块中也适用(历史原因命名)。 #### 使用 Pool 连接 Geode Server ```java @Bean(name = "DEFAULT_CLIENT_POOL") public ClientPoolFactoryBean clientPool() { ClientPoolFactoryBean pool = new ClientPoolFactoryBean(); pool.setLocators(Collections.singletonList(new HostPort("localhost", 10334))); pool.setSubscriptionEnabled(true); return pool; } ``` --- ### ✅ 优势总结 | 优点 | 说明 | |------|------| | 🚀 **高性能读写** | Geode 基于内存数据网格,延迟极低 | | 🔁 **强致性模型** | 支持复制型(REPLICATE)或分区型(PARTITION)Region | | 📈 **水平扩展能力** | 可动态添加 Geode 节点提升容量 | | 🧩 **与 Cloud Foundry 深度集成** | Pivotal Web Services(PWS)原生支持 | | 🔄 **事件监听机制** | 支持 `SessionCreatedEvent`, `SessionDestroyedEvent` 等 | --- ### ⚠️ 局限性与挑战 | 问题 | 说明 | |------|------| | ❌ **Geode 学习曲线陡峭** | 需掌握 gfsh、region、pool、locator 等概念 | | ❌ **运维复杂度高** | 需维护独立的缓存集群 | | ❌ **序列化兼容性要求高** | 所有存入 Session 的对象必须可序列化 | | ❌ **调试困难** | 分布式环境下难以追踪会话状态变化 | | ❌ **已被 Redis/MongoDB 替代趋势明显** | 更多团队选择更简单的方案 | --- ### 🔁 替代方案对比 | 存储方案 | 优势 | 劣势 | |--------|------|-------| | **Spring Session with Redis** | 成熟、简单、社区广泛支持 | 单点风险(需 Sentinel/Cluster) | | **Spring Session with JDBC** | 易备份、事务致 | 性能较低 | | **Spring Session with MongoDB** | 文档结构灵活 | 内存占用较高 | | **Spring Session with Hazelcast** | 内嵌集群、零配置启动 | 不适合超大规模部署 | | **Apache Geode / GemFire** | 高性能、企业级 SLA、金融级可靠性 | 复杂、资源消耗大、学习成本高 | --- ### ✅ 适用场景建议 | 场景 | 是否推荐 | |------|----------| | 金融、电信等对稳定性要求极高系统 | ✅ 强烈推荐 | | 使用 Pivotal Platform / PCF 的企业 | ✅ 推荐 | | 中小型互联网项目 | ❌ 不推荐(过于重量级) | | 已有 Geode 基础设施的企业 | ✅ 推荐复用 | | 快速原型开发 | ❌ 推荐使用 Redis 或 Map-based 实现 | --- ### ✅ 总结:Spring Session for Apache Geode & GemFire 的定位 | 项目 | 结论 | |------|------| | **技术价值** | 提供了种企业级、高可靠的分布式会话解决方案 | | **当前状态** | 维护良好,但使用范围逐渐缩小 | | **核心优势** | 与 Pivotal 生态深度整合,适合大型组织 | | **新项目建议** | 若已有 Geode 技术栈则继续使用;否则优先考虑 Redis 或 Spring Session + R2DBC/JDBC | | **学习意义** | 理解分布式缓存与会话管理结合的经典范例 | > 📣 **句话总结:它是“企业级会话管理”的重型武器,适合需要极致稳定性和扩展性的关键业务系统,但在轻量化时代需权衡成本与收益。** ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

「已注销」

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值