原题目: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.xsdhttp://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsdhttps://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文档理解更多信息英寸