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

发布于:2023-03-12 ⋅ 阅读:(91) ⋅ 点赞:(0)

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

5.4.2.配置 Apache Geode CacheServer
Spring Data for Apache Geode 包括对配置CacheServer 的专用支持 ,允许通过 Spring 容器进行完整配置,如以下示例所示:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"

   xmlns:context="http://www.springframework.org/schema/context"
   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
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
https://www.springframework.org/schema/geode https://www.springframework.org/schema/geode/spring-geode.xsd

">

<gfe:cache/>

<!-- Example depicting serveral Apache Geode CacheServer configuration options -->
<gfe:cache-server id="advanced-config" auto-startup="true"

   bind-address="localhost" host-name-for-clients="localhost" port="${gemfire.cache.server.port}"
   load-poll-interval="2000" max-connections="22" max-message-count="1000" max-threads="16"
   max-time-between-pings="30000" groups="test-server">

<gfe:subscription-config eviction-type="ENTRY" capacity="1000" disk-store="file://${java.io.tmpdir}"/>

</gfe:cache-server>

<context:property-placeholder location="classpath:cache-server.properties"/>

</beans>
Spring认证中国教育管理中心-Apache Geode 的 Spring 数据教程三
前面的配置显示了cache-server元素和许多可用选项。

这个配置不是对端口进行硬编码,而是使用 Spring 的 上下文 命名空间来声明一个property-placeholder. 一个 属性占位符 读取一个或多个属性文件,然后在运行时值替换属性的占位符。这样做可以让管理员更改值而无需接触主应用程序配置。Spring 还提供 SpEL 和环境抽象, 以支持从主代码库中将特定于环境的属性外部化,从而简化跨多台机器的部署。

为避免初始化问题,CacheServerSpring Data for Apache Geode 的启动会在Spring 容器完全初始化后启动。这样做可以让以声明方式定义的潜在区域、侦听器、编写器或实例化器在服务器开始接受连接之前完全初始化和注册。在以编程方式配置这些元素时请记住这一点,因为服务器可能在您的组件之前启动,因此不会被立即连接的客户端看到。

5.4.3.配置 Apache Geode ClientCache
除了定义 Apache Geode peer 之外Cache,Spring Data for Apache Geode 还支持ClientCache 在 Spring 容器中定义 Apache Geode 。甲ClientCache定义是在配置和使用了Apache的Geode对等类似高速缓存 ,并由支持
org.springframework.data.gemfire.client.ClientCacheFactoryBean。

使用默认配置的 Apache Geode 缓存客户端的最简单定义如下:

<beans>
<gfe:client-cache/>
</beans>
client-cache支持许多与Cache元素相同的选项。但是,与成熟的对等Cache成员不同,缓存客户端通过池连接到远程缓存服务器。默认情况下,会创建一个 Pool 以连接到运行localhost并侦听端口的服务器40404。默认池由所有客户端区域使用,除非该区域配置为使用特定池。

池可以用pool元素定义。此客户端池可用于通过一个或多个定位器为单个实体或整个缓存直接配置到服务器的连接。

例如,要自定义 使用的默认 Pool client-cache,开发人员需要定义一个 Pool 并将其连接到缓存定义,如下例所示:

<beans>
<gfe:client-cache id="myCache" pool-name="myPool"/>

<gfe:pool id="myPool" subscription-enabled="true">

<gfe:locator host="${gemfire.locator.host}" port="${gemfire.locator.port}"/>

</gfe:pool>
</beans>
该<client-cache>元素也有一个ready-for-events属性。如果该属性设置为true,则客户端缓存初始化包括对 的调用
ClientCache.readyForEvents()。

客户端区域更详细地介绍了客户端配置。

Apache Geode 的 DEFAULT Pool 和 Spring Data for Apache Geode Pool Definitions
如果 Apache GeodeClientCache是本地的,则不需要池定义。例如,您可以定义以下内容:

<gfe:client-cache/>

<gfe:client-region id="Example" shortcut="LOCAL"/>
在这种情况下,“示例”区域是LOCAL并且没有数据在客户端和服务器之间分布。因此,不需要池。这适用于任何客户端的、仅限本地的区域,如 Apache Geode 定义的 ClientRegionShortcut (所有LOCAL_*快捷方式)。

但是,如果客户端区域是服务器端区域的(缓存)代理,则需要一个池。在这种情况下,有多种方法可以定义和使用池。

当 a ClientCache、一个 Pool 和一个基于代理的 Region 都被定义但没有明确标识时,Spring Data for Apache Geode 会自动解析引用,如以下示例所示:

<gfe:client-cache/>

<gfe:pool>
<gfe:locator host="${geode.locator.host}" port="${geode.locator.port}"/>
</gfe:pool>

<gfe:client-region id="Example" shortcut="PROXY"/>
在前面的示例中,ClientCache标识为gemfireCache,池标识为,gemfirePool客户区域标识为“示例”。但是,从ClientCache初始化 Apache Geode 的DEFAULT池gemfirePool,并且客户端区域gemfirePool在客户端和服务器之间分发数据时使用。

基本上,Apache Geode 的 Spring Data 将上述配置解析为以下内容:

<gfe:client-cache id="gemfireCache" pool-name="gemfirePool"/>

<gfe:pool id="gemfirePool">
<gfe:locator host="${geode.locator.host}" port="${geode.locator.port}"/>
</gfe:pool>

<gfe:client-region id="Example" cache-ref="gemfireCache" pool-name="gemfirePool" shortcut="PROXY"/>
Apache Geode 仍然会创建一个名为DEFAULT. Spring Data for Apache Geode 导致DEFAULT池从gemfirePool. 在定义多个池并且客户端区域使用单独的池或根本不声明池的情况下,这样做很有用。

考虑以下:

<gfe:client-cache pool-name="locatorPool"/>

<gfe:pool id="locatorPool">
<gfe:locator host="${geode.locator.host}" port="${geode.locator.port}"/>
</gfe:pool>

<gfe:pool id="serverPool">
<gfe:server host="${geode.server.host}" port="${geode.server.port}"/>
</gfe:pool>

<gfe:client-region id="Example" pool-name="serverPool" shortcut="PROXY"/>

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

<gfe:client-region id="YetAnotherExample" shortcut="LOCAL"/>
在此设置中,Apache Geodeclient-cache DEFAULT池从 初始化locatorPool,如pool-name属性所指定。Apache Geode-defined 没有 Spring Data gemfirePool,因为两个池都被明确标识(命名) - locatorPool和serverPool。

“示例”区域明确引用并专门使用serverPool. 该AnotherExample区域使用 Apache Geode 的DEFAULT池,它也是locatorPool根据客户端缓存 bean 定义的pool-name属性配置的。

最后,该YetAnotherExample区域不使用池,因为它是LOCAL.

该AnotherExample地区将首先查找名为池豆gemfirePool,但这需要一个匿名池bean的定义(即<gfe:pool/>)或游泳池豆明确指定gemfirePool (例如<gfe:pool id="gemfirePool"/>)。

如果我们将locatorPoolto的名称更改为gemfirePool或将 Pool bean 定义设为匿名,则其效果与前面的配置相同。

5.5.配置区域
需要一个 Region 来存储和检索缓存中的数据。
org.apache.geode.cache.Region是一个扩展接口java.util.Map并使用熟悉的键值语义实现基本数据访问。该Region接口连接到需要它的应用程序类中,因此实际的 Region 类型与编程模型分离。通常,每个 Region 与一个域对象相关联,类似于关系数据库中的表。

Apache Geode 实现了以下类型的区域:

REPLICATE - 在定义区域的集群中的所有缓存成员之间复制数据。这提供了非常高的读取性能,但写入需要更长的时间来执行复制。
PARTITION - 数据在定义区域的集群中的许多缓存成员之间被划分为存储桶(分片)。这提供了很高的读写性能,适用于对于单个节点来说太大的大数据集。
LOCAL - 数据仅存在于本地节点上。
客户端- 从技术上讲,客户端区域是一个本地区域,它充当集群中缓存服务器上托管的复制或分区区域的代理。它可能保存在本地创建或获取的数据。或者,它可以为空。本地更新同步到缓存服务器。此外,客户端区域可以订阅事件以保持最新(同步)来自访问同一服务器区域的远程进程的更改。
有关各种区域类型及其功能以及配置选项的更多信息,请参阅 Apache Geode 关于区域类型的文档 。

5.5.1.使用外部配置的 Region
要引用已在 Apache Geode 本机cache.xml文件中配置的区域,请使用该lookup-region元素。只需使用name属性声明目标区域名称。例如,要ordersRegion为名为 的现有区域声明标识为 的 bean 定义Orders,您可以使用以下 bean 定义:

<gfe:lookup-region id="ordersRegion" name="Orders"/>
如果name未指定,beanid将用作区域的名称。上面的例子变成:

<!-- lookup for a Region called 'Orders' -->
<gfe:lookup-region id="Orders"/>
如果 Region 不存在,则会抛出初始化异常。要配置新区域,请继续下面的相应部分。

在前面的示例中,由于没有明确定义缓存名称,因此使用了默认命名约定 ( gemfireCache)。或者,可以使用以下cache-ref属性引用缓存 bean :

<gfe:cache id="myCache"/>
<gfe:lookup-region id="ordersRegion" name="Orders" cache-ref="myCache"/>
lookup-region 允许您检索现有的、预先配置的区域,而无需暴露区域语义或设置基础设施。

5.5.2.自动区域查找
auto-region-lookup当您在元素上使用该属性时,允许您将 Apache Geode 本机cache.xml文件中定义的所有区域导入Spring 。
ApplicationContextcache-xml-location<gfe:cache>

例如,考虑以下cache.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<cache xmlns="https://geode.apache.org/schema/cache"

   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://geode.apache.org/schema/cache https://geode.apache.org/schema/cache/cache-1.0.xsd"
   version="1.0">

<region name="Parent" refid="REPLICATE">

<region name="Child" refid="REPLICATE"/>

</region>

</cache>
您可以cache.xml按如下方式导入上述文件:

<gfe:cache cache-xml-location="cache.xml"/>
然后,您可以使用<gfe:lookup-region>元素(例如,<gfe:lookup-region id="Parent"/>)将特定 Region 引用为 Spring 容器中的 bean,或者您可以选择cache.xml 使用以下命令导入其中定义的所有 Region :

<gfe:auto-region-lookup/>
Spring Data for Apache Geode 自动为定义的所有 Apache Geode 区域创建 bean,这些区域cache.xml尚未使用显式<gfe:lookup-region>bean 声明显式添加到 Spring 容器中。

重要的是要意识到 Spring Data for Apache Geode 使用 Spring BeanPostProcessor 在创建和初始化缓存后对缓存进行后处理,以确定在 Apache Geode 中定义的 Regions 以作为 bean 添加到 Spring 中ApplicationContext。

您可以像注入 Spring 中定义的任何其他 bean 一样注入这些“自动查找”区域ApplicationContext,但有一个例外:您可能需要定义depends-on与 'gemfireCache' bean的关联,如下所示:

package example;

import ...

@Repository("appDao")
@DependsOn("gemfireCache")
public class ApplicationDao extends DaoSupport {

@Resource(name = "Parent")
private Region<?, ?> parent;

@Resource(name = "/Parent/Child")
private Region<?, ?> child;

...

}
前面的示例仅在您使用 Spring 的component-scan功能时适用。

如果您使用 Spring XML 配置声明组件,那么您将执行以下操作:

<bean class="example.ApplicationDao" depends-on="gemfireCache"/>
这样做可确保 Apache Geode 缓存和cache.xml在使用<gfe:auto-region-lookup>元素时在任何具有自动连接引用的组件之前创建Apache Geode 缓存和所有区域。

5.5.3.配置区域
Spring Data for Apache Geode 通过以下元素为配置任何类型的 Region 提供全面支持:

本地区域: <local-region>
分区区域: <partitioned-region>
复制区域: <replicated-region>
客户地区: <client-region>
有关区域类型的全面描述,请参阅 Apache Geode 文档 。

公共区域属性
下表列出了可用于所有区域类型的属性:

Spring认证中国教育管理中心-Apache Geode 的 Spring 数据教程三
Spring认证中国教育管理中心-Apache Geode 的 Spring 数据教程三
Spring认证中国教育管理中心-Apache Geode 的 Spring 数据教程三
CacheListener实例
CacheListener实例注册到一个 Region 来处理 Region 事件,例如条目何时被创建、更新、销毁等。ACacheListener可以是实现该CacheListener接口的任何 bean 。一个区域可能有多个侦听器,用cache-listener嵌套在包含*-region元素中的 元素声明。

以下示例声明了两个CacheListener’s. 第一个引用命名的顶级 Spring bean。第二个是匿名内部 bean 定义。

<bean id="myListener" class="org.example.app.geode.cache.SimpleCacheListener"/>

<gfe:replicated-region id="regionWithListeners">
<gfe:cache-listener>

<!-- nested CacheListener bean reference -->
<ref bean="myListener"/>
<!-- nested CacheListener bean definition -->
<bean class="org.example.app.geode.cache.AnotherSimpleCacheListener"/>

</gfe:cache-listener>
</gfe:replicated-region>
以下示例使用具有属性的cache-listener元素的替代形式ref。这样做允许在定义单个CacheListener.

注意:XML 命名空间只允许一个cache-listener元素,因此必须使用前面示例中显示的样式或以下示例中的样式。

<beans>
<gfe:replicated-region id="exampleReplicateRegionWithCacheListener">

<gfe:cache-listener ref="myListener"/>

</gfe:replicated-region>

<bean id="myListener" class="example.CacheListener"/>
</beans>
ref在cache-listener元素中 使用和嵌套声明是非法的。这两个选项是互斥的,在同一元素中使用两者会导致异常。

Bean 引用约定

该cache-listener元素是 Apache Geode 提供回调接口以调用自定义代码以响应缓存或区域事件的任何地方的 XML 命名空间中使用的常见模式示例。当您使用 Spring 的 IoC 容器时,实现是一个标准的 Spring bean。为了简化配置,模式允许cache-listener元素出现一次,但是,如果允许多个实例,它可以包含任意组合的嵌套 bean 引用和内部 bean 定义。约定是使用单数形式(即cache-listenervs cache-listeners),反映最常见的场景实际上是单个实例。我们已经在高级缓存 配置示例中看到了这种模式的示例。
CacheLoaders 和 CacheWriters

与 类似cache-listener,XML 命名空间提供cache-loader和cache-writer元素来为区域注册这些 Apache Geode 组件。

CacheLoader在缓存未命中时调用A以允许从外部数据源(例如数据库)加载条目。CacheWriter在创建或更新条目之前调用A ,以允许将条目同步到外部数据源。主要的区别是阿帕奇的Geode支持,最多的单个实例CacheLoader 和CacheWriter每个区域。但是,可以使用任一声明样式。

以下示例声明了一个包含 aCacheLoader和 a 的区域CacheWriter:

<beans>
<gfe:replicated-region id="exampleReplicateRegionWithCacheLoaderAndCacheWriter">

<gfe:cache-loader ref="myLoader"/>
<gfe:cache-writer>
  <bean class="example.CacheWriter"/>
</gfe:cache-writer>

</gfe:replicated-region>

<bean id="myLoader" class="example.CacheLoader">

<property name="dataSource" ref="mySqlDataSource"/>

</bean>

<!-- DataSource bean definition -->
</beans>
见CacheLoader 和CacheWriter Apache的的Geode文档了解更多信息英寸

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到