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

本文档详细介绍了Spring Data for Apache Geode如何配置不同类型区域,包括客户端、集群定义、分区、复制区域等。此外,还讨论了配置驱逐、过期和压缩策略,以有效管理内存和数据。Apache Geode支持LRU驱逐和TTL/TTI过期策略,同时允许通过压缩减少内存消耗。

原标题:Spring认证中国教育管理中心-Apache Geode 的 Spring 数据教程十二(Spring中国教育管理中心)

6.12.1.配置特定于类型的区域

Apache Geode 支持多种不同类型的 Regions。每种类型对应于 Region 的DataPolicy,它确切地决定了 Region 中的数据将如何管理(即分布式、复制等)。

其他配置设置(例如区域的scope)也会影响数据的管理方式。有关 更多详细信息,请参阅Apache Geode 用户指南中的“存储和分发选项”。

当您使用通用@Region映射注释对应用程序域对象类型进行注释时,Spring Data for Apache Geode 决定要创建哪种类型的区域。SDG 的默认策略在确定要创建的 Region 类型时会考虑缓存类型。

例如,如果您ClientCache使用@ClientCacheApplication注解将应用程序声明为 a ,则 SDGPROXY Region默认会创建一个客户端。或者,如果您Cache使用 the@PeerCacheApplication或@
CacheServerApplicationannotations将应用程序声明为对等体,SDGPARTITION Region默认会创建一个服务器。

当然,您始终可以在必要时覆盖默认值。为了覆盖 Spring Data for Apache Geode 应用的默认值,引入了四个新的 Region 映射注释:

  • @ClientRegion
  • @LocalRegion
  • @PartitionRegion
  • @ReplicateRegion

该@ClientRegion映射的注释是针对客户端应用程序。上面列出的所有其他 Region 映射注释只能在具有嵌入式 peer 的服务器应用程序中使用Cache。

客户端应用程序有时需要创建和使用仅限本地的 Region,也许是为了聚合来自其他 Region 的数据,以便在本地分析数据并代表用户执行应用程序执行的某些功能。在这种情况下,除非其他应用程序需要访问结果,否则不需要将数据分发回服务器。该区域甚至可能是临时的并在使用后被丢弃,这可以通过区域本身的空闲超时 (TTI) 和生存时间 (TTL) 过期策略来实现。(有关过期策略的更多信息,请参阅“配置过期”。)

区域级空闲超时 (TTI) 和生存时间 (TTL) 过期策略独立于并不同于入门级 TTI 和 TTL 过期策略。

在任何情况下,如果您想创建一个仅限本地的客户端 Region,其中数据不会分发回服务器上具有相同名称的相应 Region,您可以声明@ClientRegion映射注释并将shortcut属性设置为
ClientRegionShortcut.LOCAL,如如下:

ClientCache具有仅限本地的客户端区域的Spring应用程序

@ClientRegion(shortcut = ClientRegionShortcut.LOCAL)
class ClientLocalEntityType { .. }

所有特定于 Region 类型的注释都提供了额外的属性,这些属性在 Region 类型之间是通用的,并且仅特定于该类型的 Region。比如注解中的collocatedWith和redundantCopies属性PartitionRegion,PARTITION只适用于服务端,Regions。

可以在此处找到有关 Apache Geode Region 类型的更多详细信息 。

6.12.2.配置的集群定义区域

除了@
EnableEntityDefinedRegions注解之外,Spring Data for Apache Geode 还提供了反向注解 @EnableClusterDefinedRegions. 与其根据应用程序用例 (UC) 和需求(最常见和合乎逻辑的方法)定义和驱动的实体类来创建您的区域,您还可以根据ClientCache应用程序所在集群中已定义的区域来声明您的区域将连接。

这允许您使用服务器集群作为数据定义的主要来源来集中配置,并确保集群的所有客户端应用程序具有一致的配置。这在快速扩展同一客户端应用程序的大量实例以处理云管理环境中增加的负载时特别有用。

这个想法是,用户使用 Apache Geode 的Gfsh CLI shell 工具定义区域,而不是驱动数据字典的客户端应用程序。这有一个额外的好处,当额外的对等点被添加到集群时,它们也将拥有并共享相同的配置,因为它被 Apache Geode 的集群配置服务记住 。

**发布通知:Spring Boot for Apache Geode & Pivotal GemFire 1.1.0.RC2 已发布!** 2023年10月,Spring 团队发布了 **Spring Boot for Apache Geode & Pivotal GemFire 1.1.0.RC2(Release Candidate 2)** —— 这是专为集成 **Apache Geode** 和 **Pivotal GemFire** 分布式内存数据网格(IMDG)而设计的 Spring Boot 扩展模块的候选版本。该版本进一步增强了与 Spring Boot 的自动配置能力,简化了在微服务架构中使用高性能缓存和实时数据处理的能力。 > 📢 官方公告地址:[https://spring.io/blog/2023/10/18/spring-boot-for-apache-geode-1-1-0-rc2-released](https://spring.io/blog/2023/10/18/spring-boot-for-apache-geode-1-1-0-rc2-released) > ✅ 基于 Spring Boot 2.4.0-M2 构建 > 🔗 版本仓库:[https://repo.spring.io/release](https://repo.spring.io/release) --- ### 🌟 什么是 Spring Boot for Apache Geode / Pivotal GemFire? 这是一个官方支持的 Spring 模块(`spring-boot-starter-data-gemfire` 或 `spring-boot-starter-data-geode`),用于: - 自动配置 Apache Geode 或 Pivotal GemFire 客户端/服务器 - 提供基于注解的缓存抽象(`@EnableGemFireCaching`) - 集成 Spring Data Repositories 到 Geode/GemFire 区域(Region) - 支持声明式事务管理 - 实现分布式会话存储、高频读写场景下的低延迟数据访问 适用于: ✅ 实时金融交易系统 ✅ 游戏排行榜缓存 ✅ 大规模 IoT 数据聚合 ✅ 微服务间共享状态 --- ## 🚀 1.1.0.RC2 主要更新内容 ### 1. **增强的自动配置(Auto-Configuration Improvements)** 现在可根据类路径自动检测并配置客户端连接模式(Client/Server 或 Peer-to-Peer)。 ```yaml # application.yml spring: data: gemfire: pool: subscription-enabled: true locators: - host: locator1.example.com port: 10334 ``` 无需手动编写 `ClientCacheFactory` 或 `PeerCacheFactory`,Spring Boot 自动完成初始化。 --- ### 2. **支持 Spring Boot Actuator 集成** 新增健康指示器(Health Indicator)和指标暴露(Metrics via Micrometer): ```yaml management: endpoint: health: show-details: always endpoints: web: exposure: include: health,info,metrics,gemfire ``` 访问 `/actuator/gemfire` 可查看当前节点的角色、区域统计、连接状态等信息。 --- ### 3. **改进的缓存抽象支持** 使用标准 Spring 缓存注解操作 Geode/GemFire 区域。 ```java @Service public class UserService { @Cacheable(value = "Users", key = "#id") public User findById(Long id) { return slowQueryDatabase(id); } @CachePut(value = "Users", key = "#user.id") public User update(User user) { return saveToDatabase(user); } @CacheEvict(value = "Users", key = "#id") public void deleteById(Long id) { removeFromDatabase(id); } } ``` 底层自动映射到名为 `Users` 的 Geode Region。 --- ### 4. **Spring Data Repository 支持(实验性)** 可通过接口方式定义对 Region 的 CRUD 操作: ```java @Repository public interface UserRepository extends CrudRepository<User, Long> { List<User> findByLastName(String lastName); Optional<User> findByEmail(String email); } ``` 配合实体映射: ```java @Region("Users") @Serializable public class User implements Serializable { @Id private Long id; private String firstName; private String lastName; private String email; // getter/setter... } ``` > ✅ 自动创建索引以优化查询性能。 --- ### 5. **支持安全认证与加密通信(Security Enhancements)** 可配置 SSL 加密客户端与服务器之间的通信,并启用身份验证。 ```yaml spring: data: gemfire: security: username: admin password: secret ssl: enabled: true keystore: classpath:client.keystore.jks truststore: classpath:client.truststore.jks passphrase: changeit ``` 适用于生产环境中保护敏感数据传输。 --- ### 6. **DevTools 热重载支持提升** 开发阶段修改 `cache.xml` 或应用逻辑后,可通过 DevTools 触发快速重启,避免完整 JVM 重启带来的延迟。 --- ## 🧪 如何引入 Spring Boot for Apache Geode 1.1.0.RC2? ### Maven ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-geode</artifactId> <version>1.1.0.RC2</version> </dependency> <!-- 可选:启用监控 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> <repositories> <repository> <id>spring-milestones</id> <url>https://repo.spring.io/release</url> </repository> </repositories> ``` ### Gradle ```groovy dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-geode:1.1.0.RC2' implementation 'org.springframework.boot:spring-boot-starter-actuator' } repositories { maven { url 'https://repo.spring.io/release' } } ``` --- ## ⚠️ 注意事项 | 功能 | 状态 | 建议 | |------|------|------| | 对 Pivotal GemFire 的兼容性 | ✅ 支持 9.x+ | 推荐企业级部署 | | 对 Apache Geode 的支持 | ✅ 支持 1.13+ | 开源首选 | | Spring Data Repository 查询能力 | ⚠️ 实验性 | 不适合复杂 JOIN 查询 | | 分布式事务(XA)支持 | ✅ 支持 | 需额外配置 TransactionManager | --- ## 📚 学习资源推荐 - 📘 官方文档:[https://docs.spring.io/spring-boot-data-geode](https://docs.spring.io/spring-boot-data-geode) - 🧪 示例工程:[https://github.com/spring-projects/spring-boot-data-geode-examples](https://github.com/spring-projects/spring-boot-data-geode-examples) - 🎥 视频课程:“Building Real-Time Applications with Spring Boot & Apache Geode” – Spring Tips ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

「已注销」

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

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

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

打赏作者

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

抵扣说明:

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

余额充值