本文介绍了Spring Data Geode Region具有本地范围,无法监听远程Region事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Spring Data for Apache Geode用于使用远程Apache Geode服务器的Spring Boot应用程序.要设置本地区域,我使用的是@Configuration类,例如:

I am using Spring Data for Apache Geode for a Spring Boot app that uses a remote Apache Geode server. To set up local Regions I am using @Configuration class like:

@Configuration
public class GeodeConfig {

  public static Region<String, String> myRegion;

  @Bean("setupMyRegion")
  public Region<String, String> setupMyRegion(GemFireCache cache) {

    Region<String, String> r = cache.getRegion("myRegion");
    myRegion = r;

    return r;
  }
}

这使我能够在Region中放置和获取数据.

This enables me to put and get data with the Region.

当我对订阅密钥感兴趣时,将使用scope = LOCAL设置订阅,如下所示:

When I register interest in subscribing to a key, the subscription is setup with scope=LOCAL like this:

org.apach.geode.inter.cache.LocalRegion 4439 logKeys: org.apache.geode.internal.cache.LocalRegion[path='/myRegion';scope=LOCAL';dataPolicy=NORMAL; concurrencyChecksEnabled] refreshEntriesFromServerKeys count=1 policy=KEYS_VALUES

,我想我想将其设置为CACHING_PROXY,以便稍后在订阅Region中的事件时,本地订阅将看到服务器的Region事件.

and I guess that I want to set it up as a CACHING_PROXY so that later, on subscribing to events in the Region, the local subscription will see the server Region events.

我已经为@ClientCacheApplication设置了subscriptionEnabled=TRUE,我正在尝试设置

I've set subscriptionEnabled=TRUE for the @ClientCacheApplication and I am trying to set

clientRegionShortcut = ClientRegionShortcut.CACHING_PROXY 

我不知道该在哪里做.我已经尝试过@EnableClusterDefinedRegions批注,但是Region仍设置为本地.

I don't know where to do this. I've tried the @EnableClusterDefinedRegions annotation but the Region is still being set to local.

如果我尝试从GemFireCache创建Region Factory,则会给我一个错误,提示对于类型GemFireCache,方法createClientRegionFactory(ClientRegionShortcut)是未定义".

If I try to create a Region Factory from the GemFireCache it gives me an error that the method createClientRegionFactory(ClientRegionShortcut) is "undefined" for the type GemFireCache.

谢谢

推荐答案

通常,不好的做法是直接使用Apache Geode API在 Spring 上下文中配置bean.例如...

Generally, it is bad practice to use the Apache Geode API directly to configure beans in a Spring context. For example...

  @Bean("MyRegion")
  public Region<String, Object> myRegion(GemFireCache cache) {
    return cache.getRegion("myRegion");
  }

或使用:

  @Bean("MyRegion")
  public Region<String, Object> myRegion(ClientCache clientCache) {
    return clientCache.createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY)
      .create("MyRegion");
  }

不建议同时使用上述两种方法!

Both approaches above are not recommended!

Apache Geode的Spring数据(SDG)包含特定的 Spring FactoryBeans 做同样的事情.例如,SDG ClientRegionFactoryBean .

Spring Data for Apache Geode (SDG) contains specific Spring FactoryBeans to do the same thing. For instance, the SDG ClientRegionFactoryBean.

您将在 Spring 应用程序配置中使用ClientRegionFactoryBean,如下所示:

You would use the ClientRegionFactoryBean in your Spring application configuration as follows:

@Bean("MyRegion")
ClientRegionFactoryBean<String, Object> myRegion(ClientCache clientCache) {

  ClientRegionFactoryBean<String, Object> myRegion = 
    new ClientRegionFactoryBean<>();

  myRegion.setCache(clientCache);
  myRegion.setShortcut(ClientRegionShortcut.CACHING_PROXY);

  return myRegion;
}

SDG FactoryBeans用于不同类型的Geode对象,例如[客户端] RegionsIndexesDiskStoresGatewayReceiversGatewaySenders等.

There are SDG FactoryBeans for different types of Geode objects, such a [client] Regions, Indexes, DiskStores, GatewayReceivers, GatewaySenders, etc.

这些FactoryBeans对于确保Geode对象(例如Region(s))的生命周期"正确同步到 Spring 容器的生命周期至关重要.通过放弃使用SDG的FactoryBeans,您可以有效地忽略 Spring 容器和自定义(框架)FactoryBeans提供的生命周期协调,因此,当直接在 Spring 配置中使用Geode的API.这是SDG首先存在的主要原因之一.

These FactoryBeans are essential in ensuring the the "lifecycle" of the Geode objects (e.g. Region(s)) are properly synced to the lifecycle of the Spring container. By forgoing the use of SDG's FactoryBeans you are effectively ignoring the lifecycle coordination provided by the Spring container and custom (framework) FactoryBeans, and are therefore responsible for the lifecycle of the Geode objects themselves when using Geode's API direct in Spring config. This is one of the primary reasons SDG's exists in the first place.

客户端" Region必须是*PROXY(例如PROXYCACHING_PROXY),以便启用订阅并从该区域的服务器接收有趣的事件.这是因为Geode群集(服务器)在客户端连接时为客户端维护一个订阅队列,其中包含客户端表示有兴趣接收的事件.

A "client" Region must be a *PROXY (e.g. PROXY or CACHING_PROXY) in order to enable subscriptions and receive interesting events from servers for that Region. This is because the Geode cluster (servers) maintain a subscription queue for clients when they connect, which contain events that the client has expressed interests in receiving.

对于*PROXY客户端Regions,这还意味着客户端Region必须具有相同名称的相应服务器端对等缓存Region.

For *PROXY client Regions, this also means that the client Region must have a corresponding server-side, peer cache Region by the same name.

通过使用SDG的基于注释的配置模型,尤其是@EnableEntityDefinedRegions@EnableCachingDefinedRegions,您可以总体上简化区域创建,尤其是客户端区域创建.

You can simplify Region creation in general, and client Region creation in particular, by using SDG's Annotation-based configuration model, and specifically, @EnableEntityDefinedRegions or @EnableCachingDefinedRegions.

例如:

@ClientCacheApplication(subscriptionsEnabled = true)
@EnableEntityDefinedRegions(basePackageClasses = SomeEntity.class, 
  clientRegionShortcut = ClientRegionShortcut.CACHING_PROXY)
class GeodeConfiguration {
  ...
}

当然,这假设您已定义了应用程序域模型实体类,例如:

Of course, this assumes you have defined application domain model entity classes, such as:

package example.app.model;

import ...;

@Region("MyRegion")
class SomeEntity {

  @Id
  private Long id;

  ...
}

ClientRegionFactoryBean将被相应键入:ClientRegionFactoryBean<Long, SomeEntity>.

请参阅基于SDG注释的配置模型文档以了解更多详细信息,尤其是涵盖区域定义的注释配置.

See the SDG Annotation-based configuration model documentation for more details, and specifically Annotation configuration covering Region definitions.

@EnableClusterDefinedRegions 注释为客户端连接到的所有现有服务器端(群集)Regions定义了客户端Regions.

例如,如果您在 Gfsh 中启动集群,如下所示:

For example, if you start a cluster in Gfsh as follows:

gfsh>start locator --name=MyLocator ...

gfsh>start server --name=MyServer ...

gfsh>create region --name=Example --type=PARTITION

gfsh>create region --name=AnotherExample --type=REPLICATE

然后使用以下Geode配置将 Spring Boot ,Apache Geode ClientCache应用程序连接到此集群:

And then connect a Spring Boot, Apache Geode ClientCache application to this cluster by using the following Geode configuration:

@ClientCacheApplication
@EnableClusterDefinedRegions(clientRegionShortcut = 
  ClientRegionShortcut.CACHING_PROXY)
class GeodeConfiguration {
  ...
}

然后,在 Spring Boot 中,Apache Geode ClientCache应用程序将具有2个 Spring 配置的客户端Region Bean,其名称分别为类型"Example"和"AnotherExample" c1>.

Then the Spring Boot, Apache Geode ClientCache application would have 2 Spring configured client Region beans with names "Example" and "AnotherExample" both of type, CACHING_PROXY.

如果集群中目前没有服务器端区域,则@EnableClusterDefinedRegions不会定义任何客户端区域.

If there are NO server-side Regions presently in the cluster, then the @EnableClusterDefinedRegions would not define any client-side Regions.

无论如何,我希望这是有道理的并且对您有所帮助!

Anyway, I hope this makes sense and helps!

干杯!

这篇关于Spring Data Geode Region具有本地范围,无法监听远程Region事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 01:29