Spring认证中国教育管理中心-Apache Geode的Spring数据教程八
发布于 2 年前 作者 tsu 5166 次浏览 来自 分享

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

5.8.1.快照位置
对于基于缓存的快照服务(即 a CacheSnapshotService),您通常会将包含所有要加载的快照文件而不是单个快照文件的目录传递给它,如API 中的重载load 方法CacheSnapshotService所示。

当然,您可以使用重载load(:File[], :SnapshotFormat, :SnapshotOptions)方法来获取有关将哪些快照文件加载到 Apache Geode 缓存中的具体信息。

但是,Spring Data for Apache Geode 认识到典型的开发人员工作流程可能是从一个环境中提取数据并将其导出到多个快照文件中,将它们全部压缩,然后方便地将 zip 文件移动到另一个环境进行导入。

因此,Spring Data for Apache Geode 允许您在导入时为cache基于 Snapshot 服务指定 jar 或 zip 文件,如下所示:

<gfe-data:snapshot-service id=“cacheBasedSnapshotService” cache-ref=“gemfireCache”>
<gfe-data:snapshot-import location="/path/to/snapshots.zip"/>
</gfe-data:snapshot-service>
Spring Data for Apache Geode 方便地提取提供的 zip 文件并将其视为目录导入(加载)。

5.8.2.快照过滤器
定义多个快照导入和导出的真正威力是通过使用快照过滤器实现的。快照过滤器实现了 Apache Geode 的SnapshotFilter接口,用于过滤区域条目,以便在导入时包含在区域中,并在导出时包含在快照中。

Spring Data for Apache Geode 允许您通过使用filter-ref属性或匿名嵌套 bean 定义在导入和导出时使用快照过滤器,如以下示例所示:

gfe:cache/

<gfe:partitioned-region id=“Admins” persistent=“false”/>
<gfe:partitioned-region id=“Guests” persistent=“false”/>

<bean id=“activeUsersFilter” class="example.gemfire.snapshot.filter.ActiveUsersFilter/>

<gfe-data:snapshot-service id=“adminsSnapshotService” region-ref=“Admins”>
<gfe-data:snapshot-import location="/path/to/import/users.snapshot">
<bean class=“example.gemfire.snapshot.filter.AdminsFilter/>
</gfe-data:snapshot-import>
<gfe-data:snapshot-export location=”/path/to/export/active/admins.snapshot" filter-ref=“activeUsersFilter”/>
</gfe-data:snapshot-service>

<gfe-data:snapshot-service id=“guestsSnapshotService” region-ref=“Guests”>
<gfe-data:snapshot-import location="/path/to/import/users.snapshot">
<bean class=“example.gemfire.snapshot.filter.GuestsFilter/>
</gfe-data:snapshot-import>
<gfe-data:snapshot-export location=”/path/to/export/active/guests.snapshot" filter-ref=“activeUsersFilter”/>
</gfe-data:snapshot-service>
此外,您可以使用ComposableSnapshotFilter该类表达更复杂的快照过滤器。此类实现了 Apache Geode 的SnapshotFilter接口以及Composite软件设计模式。

简而言之,Composite软件设计模式允许您组合多个相同类型的对象,并将聚合视为对象类型的单个实例——一种强大而有用的抽象。

ComposableSnapshotFilter有两个工厂方法,and和or. 它们让您可以分别使用 AND 和 OR 逻辑运算符在逻辑上组合各个快照过滤器。工厂方法采用SnapshotFilters.

以下示例显示了 a 的定义ComposableSnapshotFilter:

<bean id=“activeUsersSinceFilter” class="org.springframework.data.gemfire.snapshot.filter.ComposableSnapshotFilter"
factory-method=“and”>
<constructor-arg index=“0”>
<list>
<bean class=“org.example.app.gemfire.snapshot.filter.ActiveUsersFilter”/>
<bean class="org.example.app.gemfire.snapshot.filter.UsersSinceFilter"
p:since=“2015-01-01”/>
</list>
</constructor-arg>
</bean>
然后,您可以继续使用 将activesUsersSinceFilter与另一个过滤器组合or,如下所示:

<bean id=“covertOrActiveUsersSinceFilter” class="org.springframework.data.gemfire.snapshot.filter.ComposableSnapshotFilter"
factory-method=“or”>
<constructor-arg index=“0”>
<list>
<ref bean=“activeUsersSinceFilter”/>
<bean class=“example.gemfire.snapshot.filter.CovertUsersFilter”/>
</list>
</constructor-arg>
</bean>
Spring认证中国教育管理中心-Apache Geode 的 Spring 数据教程八
5.8.3.快照事件
默认情况下,Spring Data for Apache Geode 在启动时使用 Apache Geode 的快照服务来导入数据,在关闭时使用 Apache Geode 的快照服务来导出数据。但是,您可能希望从 Spring 应用程序中触发定期的、基于事件的快照,用于导入或导出。

为此,Apache Geode 的 Spring Data 定义了两个额外的 Spring 应用程序事件,分别扩展了 Spring 的 ApplicationEvent 导入和导出类:
ImportSnapshotApplicationEvent和ExportSnapshotApplicationEvent.

这两个应用程序事件可以针对整个 Apache Geode 缓存或针对单个 Apache Geode 区域。这些类中的构造函数接受可选的 Region 路径名(例如/Example)以及零个或多个SnapshotMetadata实例。

数组SnapshotMetadata覆盖了由gfe-data:snapshot-importgfe-data:snapshot-export子元素定义的快照元数据,在快照应用程序事件未明确提供的情况下使用SnapshotMetadata。每个单独的SnapshotMetadata实例都可以定义自己的location 和filters属性。

Spring 中定义的所有快照服务 bean 都ApplicationContext接收导入和导出快照应用程序事件。但是,只有匹配的快照服务 bean 才能处理导入和导出事件。

的区域为基础的[Import|Export]SnapshotApplicationEvent匹配,如果所定义的快照服务bean是一个RegionSnapshotService和它的区域参考(如由所确定的region-ref属性)的指定地区的路径名相匹配,如由快照应用程序事件指定。

基于缓存[Import|Export]SnapshotApplicationEvent(即没有区域路径名的快照应用程序事件)触发所有快照服务 bean,包括任何RegionSnapshotServicebean,分别执行导入或导出。

您可以使用 Spring 的 ApplicationEventPublisher 接口从您的应用程序中触发导入和导出快照应用程序事件,如下所示:

@Component
public class ExampleApplicationComponent {

@Autowired
private ApplicationEventPublisher eventPublisher;

@Resource(name = “Example”)
private Region<?, ?> example;

public void someMethod() {

...

File dataSnapshot = new File(System.getProperty("user.dir"), "/path/to/export/data.snapshot");

SnapshotFilter myFilter = ...;

SnapshotMetadata exportSnapshotMetadata =
    new SnapshotMetadata(dataSnapshot, myFilter, null);

ExportSnapshotApplicationEvent exportSnapshotEvent =
    new ExportSnapshotApplicationEvent(this, example.getFullPath(), exportSnapshotMetadata)

eventPublisher.publishEvent(exportSnapshotEvent);

...

}
}
Spring认证中国教育管理中心-Apache Geode 的 Spring 数据教程八
在前面的示例中,只有/ExampleRegion 的 Snapshot Service bean 获取并处理导出事件,将过滤后的“/Example”Region 数据保存到data.snapshot应用程序工作目录子目录中的文件中。

使用 Spring 应用程序事件和消息传递子系统是保持应用程序松散耦合的好方法。您还可以使用 Spring 的调度服务定期触发快照应用程序事件。

5.9.配置函数服务
Spring Data for Apache Geode为实现、注册和执行 Apache Geode 函数提供注释支持。

Spring Data for Apache Geode 还提供 XML 命名空间支持,用于注册 Apache Geode 函数 以进行远程函数执行。

有关 函数执行框架的更多信息,请参阅 Apache Geode 的文档。

Apache Geode 函数被声明为 Spring bean,并且必须实现
org.apache.geode.cache.execute.Function interface 或 extend org.apache.geode.cache.execute.FunctionAdapter。

命名空间使用熟悉的模式来声明函数,如以下示例所示:

gfe:function-service
gfe:function
<bean class=“example.FunctionOne”/>
<ref bean=“function2”/>
</gfe:function>
</gfe:function-service>

<bean id=“function2” class=“example.FunctionTwo”/>
5.10.配置广域网网关
WAN 网关提供了一种跨地理位置同步 Apache Geode 分布式系统的方法。Spring Data for Apache Geode 为配置 WAN 网关提供 XML 命名空间支持,如以下示例所示。

5.10.1.Apache Geode 7.0 中的 WAN 配置
在以下示例中,GatewaySenders被配置用于PARTITION通过将子元素(区域gateway-sender和gateway-sender-ref)的区域。AGatewaySender可以注册EventFilters 和TransportFilters。

以下示例还显示了 的示例配置AsyncEventQueue,它也必须自动连接到区域(未显示):

<gfe:partitioned-region id=“region-with-inner-gateway-sender” >
<gfe:gateway-sender remote-distributed-system-id=“1”>
gfe:event-filter
<bean class=“org.springframework.data.gemfire.example.SomeEventFilter”/>
</gfe:event-filter>
gfe:transport-filter
<bean class=“org.springframework.data.gemfire.example.SomeTransportFilter”/>
</gfe:transport-filter>
</gfe:gateway-sender>
<gfe:gateway-sender-ref bean=“gateway-sender”/>
</gfe:partitioned-region>

<gfe:async-event-queue id=“async-event-queue” batch-size=“10” persistent=“true” disk-store-ref="diskstore"
maximum-queue-memory=“50”>
gfe:async-event-listener
<bean class=“example.AsyncEventListener”/>
</gfe:async-event-listener>
</gfe:async-event-queue>

<gfe:gateway-sender id=“gateway-sender” remote-distributed-system-id=“2”>
gfe:event-filter
<ref bean=“event-filter”/>
<bean class=“org.springframework.data.gemfire.example.SomeEventFilter”/>
</gfe:event-filter>
gfe:transport-filter
<ref bean=“transport-filter”/>
<bean class=“org.springframework.data.gemfire.example.SomeTransportFilter”/>
</gfe:transport-filter>
</gfe:gateway-sender>

<bean id=“event-filter” class=“org.springframework.data.gemfire.example.AnotherEventFilter”/>
<bean id=“transport-filter” class=“org.springframework.data.gemfire.example.AnotherTransportFilter”/>
Spring认证中国教育管理中心-Apache Geode 的 Spring 数据教程八
在a的另一端GatewaySender是一个对应GatewayReceiver的接收网关事件。的GatewayReceiver也可以与配置EventFilters和TransportFilters,如下所示:

<gfe:gateway-receiver id=“gateway-receiver” start-port=“12345” end-port=“23456” bind-address=“192.168.0.1”>
gfe:transport-filter
<bean class=“org.springframework.data.gemfire.example.SomeTransportFilter”/>
</gfe:transport-filter>
</gfe:gateway-receiver>

回到顶部