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

本文介绍了如何在Spring应用中集成Apache Geode,包括集群定义、客户端区域配置、函数执行、连续查询、集群配置、GatewayReceiver和GatewaySender的配置。重点讲解了如何启用相关注解和配置类以实现数据处理和高可用性功能。

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

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

6.20.16。从集群定义的区域配置客户端区域

或者,您可以使用 定义从集群中已定义的区域中定义客户端 [*PROXY] 区域@
EnableClusterDefinedRegions,如下所示:

@SpringBootApplication
@ClientCacheApplication
@EnableClusterDefinedRegions
@EnableGemfireRepositories
public class ClientApplication {

  public static void main(String[] args) {
    SpringApplication.run(ClientApplication.class, args);
  }

  ...
}

6.20.17。配置功能

Apache Geode Functions 在分布式计算场景中很有用,在这种场景中,需要数据的潜在昂贵计算可以跨集群中的节点并行执行。在这种情况下,将逻辑带到数据所在(存储)的位置比请求和获取要由计算处理的数据更有效。

使用@EnableGemfireFunctions和@GemfireFunction注释来启用作为 POJO 方法实现的 Apache Geode Functions 定义,如下所示:

@PeerCacheApplication
@EnableGemfireFunctions
class ServerApplication {

  public static void main(String[] args) {
    SpringApplication.run(ServerApplication.class, args);
  }

  @GemfireFunction
  Integer computeLoyaltyPoints(Customer customer) {
    ...
  }
}

使用@
EnableGemfireFunctionExecutions与函数调用注解1一起:@OnMember,@OnMembers, @OnRegion,@OnServer和@OnServers。

@ClientCacheApplication
@EnableGemfireFunctionExecutions(basePackageClasses = CustomerRewardsFunction.class)
class ClientApplication {

  public static void main(String[] args) {
    SpringApplication.run(ClientApplication.class, args);
  }
}

@OnRegion("Customers")
interface CustomerRewardsFunctions {

  Integer computeLoyaltyPoints(Customer customer);

}

请参阅@
EnableGemfireFunctionsJavadoc。

@target(值= TYPE)
  @Retention(值= RUNTIME)
  @Inherited 
 @Documented 
 @Import(值= GemfireFunctionBeanPostProcessorRegistrar.class)
公共@interface EnableGemfireFunctions

启用 GemFire 注释函数实现。使容器发现任何用 {code}@GemfireFunction{code} 注释的 bean,将它们包装在 中PojoFunctionWrapper,并将它们注册到缓存中。

请参阅@GemfireFunctionJavadoc。

@Retention ( value = RUNTIME )
  @Target ( value = METHOD )
公共@interface GemfireFunction

用于将具体方法声明为 GemFire 函数实现

请参阅@
EnableGemfireFunctionExecutionsJavadoc。

@Target ( value = TYPE )
  @Retention ( value = RUNTIME )
  @Documented 
 @Inherited 
 @Import ( value = FunctionExecutionBeanDefinitionRegistrar.class )
公共@interface EnableGemfireFunctionExecutions

为注释为 GemFire 函数执行(函数调用)的接口启用类路径扫描。这些包括用 {code} @OnRegion、@OnServer、@OnServers、@OnMember、@OnMembers{code} 之一注释的接口

请参阅@OnMemberJavadoc、 @OnMembersJavadoc、 @OnRegionJavadoc、 @OnServerJavadoc和@OnServersJavadoc。


@Retention ( value = RUNTIME )
  @Target ( value = TYPE )
公共@interface OnMember

将接口声明为 GemFire OnMember 函数执行的注释

@Retention ( value = RUNTIME )
  @Target ( value = TYPE )
公共@interface OnMembers

将接口声明为 GemFire OnMembers 函数执行的注释

@Retention ( value = RUNTIME )
  @Target ( value = TYPE )
公共@interface OnRegion

将接口声明为 GemFire OnRegion 函数执行的注释

@Retention ( value = RUNTIME )
  @Target ( value = TYPE )
公共@interface OnServer

将接口声明为 GemFire OnServer 函数执行的注释

@Retention ( value = RUNTIME )
  @Target ( value = TYPE )
公共@interface OnServers

将接口声明为 GemFire OnServers 函数执行的注释

6.20.18。配置连续查询

实时事件流处理正成为数据密集型应用程序越来越重要的任务,主要是为了及时响应用户请求。Apache Geode Continuous Query (CQ) 将帮助您轻松完成这项相当复杂的任务。

通过@EnableContinuousQueries使用相关联的事件处理程序注释您的应用程序类并定义您的CQ 来启用 CQ ,如下所示:

@ClientCacheApplication
@EnableContinuousQueries
class ClientApplication {

  public static void main(String[] args) {
    SpringApplication.run(ClientApplication.class, args);
  }
}

然后,通过使用 注释关联的处理程序方法来定义您的 CQ @ContinousQuery,如下所示:

@Service
class CustomerService {

  @ContinuousQuery(name = "CustomerQuery", query = "SELECT * FROM /Customers c WHERE ...")
  public void process(CqEvent event) {
    ...
  }
}

每当发生更改Customer数据以匹配连续 OQL 查询 (CQ) 中的谓词的事件时,process都会调用该方法。

Apache Geode CQ 只是一个客户端特性。

请参阅@
EnableContinuousQueriesJavadoc。

@Target ( value = TYPE )
  @Retention ( value = RUNTIME )
  @Inherited 
 @Documented 
 @Import ( value = ContinuousQueryConfiguration.class )
公共@interface EnableContinuousQueries

该EnableContinuousQueries注释标记了一个带有 Spring@Configuration注释的应用程序配置类,以启用 Pivotal GemFire / Apache Geode 连续查询 (CQ) 功能。

请参阅@ContinuousQueryJavadoc。

@Target ( value = METHOD )
  @Retention ( value = RUNTIME )
  @Inherited 
 @Documented 
public @interface ContinuousQuery

在ContinuousQuery处理所有 CQ 事件和错误的 POJO 方法上定义 GemFire/Geode 连续查询 (CQ)的注释。

6.20.19。配置集群配置

当使用 Apache Geode 作为 Apache GeodeClientCache应用程序开发 Spring Data 应用程序时,在开发期间配置服务器以匹配客户端/服务器拓扑中的客户端很有用。事实上,Apache Geode 期望当您Region在客户端上有一个“/Example”代理时,服务器中存在一个匹配Region的名称(即“Example”)。

您可以使用Gfsh创建您的应用程序需要的每个区域和索引,或者,您可以在运行时简单地推送在使用 Apache Geode 开发 Spring Data 应用程序时已经表达的配置元数据。

这就像注释您的主应用程序类一样简单@
EnableClusterConfiguration(..):

使用 @
EnableClusterConfiguration

@ClientCacheApplication
@EnableClusterConfiguration(useHttp = true)
class ClientApplication {
  ...
}

大多数时候,当使用客户端/服务器拓扑时,特别是在生产环境中,集群的服务器将使用Gfsh启动。在这种情况下,习惯上使用 HTTP(S) 将配置元数据(例如区域和索引定义)发送到集群。当使用 HTTP 时,配置元数据被发送到集群中的 Manager 并一致地分布在集群中的服务器节点上。

为了使用,@
EnableClusterConfiguration您必须org.springframework:spring-web在 Spring 应用程序类路径中声明依赖项。

请参阅@
EnableClusterConfigurationJavadoc。

@Target ( value = TYPE )
  @Retention ( value = RUNTIME )
  @Inherited 
 @Documented 
 @Import ( value = ClusterConfigurationConfiguration.class )
公共@interface EnableClusterConfiguration


EnableClusterConfiguration注释使 Spring [Boot] 中定义的 Apache Geode / Pivotal GemFire 模式对象定义、ClientCache使用 Spring 配置的Apache Geode / Pivotal GemFire应用程序能够推送到 Apache Geode / Pivotal GemFire 集群,类似于模式命令(例如`create region `) 在 Gfsh 中由 Apache Geode / Pivotal GemFire 管理器处理。

6.20.20。配置GatewayReceivers

不同 Apache Geode 集群之间的数据复制是一种越来越重要的容错和高可用性 (HA) 机制。Apache Geode WAN 复制是一种机制,允许一个 Apache Geode 集群以可靠且容错的方式将其数据复制到另一个 Apache Geode 集群。

Apache Geode WAN 复制需要配置两个组件:

  • GatewayReceiver- 从远程 Apache Geode 集群的GatewaySender.
  • GatewaySender- 将数据发送到远程 Apache Geode 集群的GatewayReceiver.

要启用 a GatewayReceiver,需要对应用程序类进行@EnableGatewayReceiver如下注释:

@CacheServerApplication
@EnableGatewayReceiver(manualStart = false, startPort = 10000, endPort = 11000, maximumTimeBetweenPings = 1000,
    socketBufferSize = 16384, bindAddress = "localhost",transportFilters = {"transportBean1", "transportBean2"},
    hostnameForSenders = "hostnameLocalhost"){
      ...
      ...
    }
}
class MySpringApplication { .. }

Apache GeodeGatewayReceiver只是一个服务器端特性,只能在一个CacheServer 或对等Cache节点上配置。

6.20.21。配置GatewaySenders

要启用GatewaySender,应用程序类需要使用@EnableGatewaySenders 和进行注释,@EnableGatewaySender如下所示:

@CacheServerApplication
@EnableGatewaySenders(gatewaySenders = {
		@EnableGatewaySender(name = "GatewaySender", manualStart = true,
			remoteDistributedSystemId = 2, diskSynchronous = true, batchConflationEnabled = true,
			parallel = true, persistent = false,diskStoreReference = "someDiskStore",
			orderPolicy = OrderPolicyType.PARTITION, alertThreshold = 1234, batchSize = 100,
			eventFilters = "SomeEventFilter", batchTimeInterval = 2000, dispatcherThreads = 22,
			maximumQueueMemory = 400,socketBufferSize = 16384,
			socketReadTimeout = 4000, regions = { "Region1"}),
		@EnableGatewaySender(name = "GatewaySender2", manualStart = true,
			remoteDistributedSystemId = 2, diskSynchronous = true, batchConflationEnabled = true,
			parallel = true, persistent = false, diskStoreReference = "someDiskStore",
			orderPolicy = OrderPolicyType.PARTITION, alertThreshold = 1234, batchSize = 100,
			eventFilters = "SomeEventFilter", batchTimeInterval = 2000, dispatcherThreads = 22,
			maximumQueueMemory = 400, socketBufferSize = 16384,socketReadTimeout = 4000,
			regions = { "Region2" })
}){
class MySpringApplication { .. }
}

Apache GeodeGatewaySender只是一个服务器端特性,只能在一个CacheServer 或一个对等Cache节点上配置。

在上面的例子中,应用程序配置了 2 个区域,Region1和Region2. 此外,GatewaySenders将配置两个为两个区域提供服务。GatewaySender1将被配置为复制 Region1’s data and `GatewaySender2将被配置为复制`Region2 的数据。

正如演示的那样,GatewaySender可以在每个EnableGatewaySender注释上配置每个属性。

还可以使用更通用的“默认”属性方法,其中所有属性都在EnableGatewaySenders注释上配置。这样,可以在父注释上设置一组通用的默认值,然后根据需要在子注释上覆盖,如下所示:

@CacheServerApplication
@EnableGatewaySenders(gatewaySenders = {
		@EnableGatewaySender(name = "GatewaySender", transportFilters = "transportBean1", regions = "Region2"),
		@EnableGatewaySender(name = "GatewaySender2")},
		manualStart = true, remoteDistributedSystemId = 2,
		diskSynchronous = false, batchConflationEnabled = true, parallel = true, persistent = true,
		diskStoreReference = "someDiskStore", orderPolicy = OrderPolicyType.PARTITION, alertThreshold = 1234, batchSize = 1002,
		eventFilters = "SomeEventFilter", batchTimeInterval = 2000, dispatcherThreads = 22, maximumQueueMemory = 400,
		socketBufferSize = 16384, socketReadTimeout = 4000, regions = { "Region1", "Region2" },
		transportFilters = { "transportBean2", "transportBean1" })
class MySpringApplication { .. }

当regions属性为空或未填充时,GatewaySender(s) 将自动附加到Region应用程序中的每个配置。

 

**发布通知: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 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

「已注销」

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

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

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

打赏作者

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

抵扣说明:

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

余额充值