关于java:Spring认证中国教育管理中心Apache-Geode-的-Spring-数据教程四

10次阅读

共计 8409 个字符,预计需要花费 22 分钟才能阅读完成。

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

5.5.4. 压缩
Apache Geode Regions 也可能被压缩,以缩小 JVM 内存耗费和压力,从而可能防止全局 GC。当您为区域启用压缩时,存储在该区域内存中的所有值都将被压缩,而键和索引放弃未压缩状态。新值在放入 Region 时被压缩,所有值在从 Region 读回时主动解压缩。当保留到磁盘或通过线路发送到其余对等成员或客户端时,值不会被压缩。

以下示例显示了启用压缩的区域:

<beans>
<gfe:replicated-region id=”exampleReplicateRegionWithCompression”>

<gfe:compressor>
  <bean class="org.apache.geode.compression.SnappyCompressor"/>
</gfe:compressor>

</gfe:replicated-region>
</beans>
无关区域压缩的更多信息,请参阅 Apache Geode 的文档。

5.5.5. 堆外
Apache Geode Regions 也能够配置为将 Region 值存储在堆外内存中,这是 JVM 内存的一部分,不受垃圾收集 (GC) 的影响。通过防止低廉的 GC 周期,您的应用程序能够将更多工夫花在重要的事件上,例如解决申请。

应用堆外内存就像申明要应用的内存量而后启用您的区域应用堆外内存一样简略,如以下配置所示:

<util:properties id=”gemfireProperties”>

<prop key="off-heap-memory-size">200G</prop>

</util:properties>

<gfe:cache properties-ref=”gemfireProperties”/>

<gfe:partitioned-region id=”ExampleOffHeapRegion” off-heap=”true”/>
您能够通过应用 <gfe:cache> 元素设置以下 Apache Geode 配置属性来管制堆外内存治理的其余方面:s

<gfe:cache critical-off-heap-percentage=”90″ eviction-off-heap-percentage”80″/>
Apache GeodeResourceManager 将应用这两个阈值 (
critical-off-heap-percentage & eviction-off-heap-percentage) 来更无效地治理堆外内存,其形式与 JVM 在治理堆内存时所做的大致相同。Apache GeodeResourceManager 将通过驱赶旧数据来避免缓存耗费过多的堆外内存。如果堆外管理器无奈跟上,则 ResourceManager 回绝向缓存增加内容,直到堆外内存管理器开释足够数量的内存。

无关治理堆和堆外内存的更多信息,请参阅 Apache Geode 的文档。

具体来说,请浏览“治理堆外内存”局部。

5.5.6. 次区域
Spring Data for Apache Geode 也反对 Sub-Regions,容许 Region 以层级关系排列。

例如,Apache Geode 容许一个 /Customer/Address 区域和一个不同的 /Employee/Address 区域。此外,一个子区域可能有本人的子区域和配置。子区域不会从其父区域继承属性。区域类型能够混合和匹配受 Apache Geode 束缚。Sub-Region 天然地被申明为 Region 的子元素。Sub-Region 的 name 属性是简略的名称。后面的示例可能配置如下:

<beans>
<gfe:replicated-region name=”Customer”>

<gfe:replicated-region name="Address"/>

</gfe:replicated-region>

<gfe:replicated-region name=”Employee”>

<gfe:replicated-region name="Address"/>

</gfe:replicated-region>
</beans>
请留神,该 Monospaced ([id])属性不容许用于子区域。子区域是应用 bean 名称创立的(在本例中别离为 /Customer/Address 和 /Employee/Address)。因而,它们可能会 GemfireTemplate 通过应用区域的残缺路径名注入到其余须要它们的应用程序 bean 中,例如。还应在 OQL 查问字符串中应用区域的残缺路径名。

5.5.7. 区域模板
Spring Data for Apache Geode 也反对 Region 模板。

此性能容许开发人员一次性定义公共 Region 配置和属性,并在 Spring 中申明的多个 Region bean 定义中重用该配置 ApplicationContext。

Spring Data for Apache Geode 在其命名空间中蕴含五个 Region 模板标签:

Spring 认证中国教育管理中心 -Apache Geode 的 Spring 数据教程四
除了标签之外,具体 <gfe:-region> 元素(连同形象 <gfe:-region-template> 元素)还有一个 template 属性,用于定义区域模板,区域从该模板继承其配置。区域模板甚至能够从其余区域模板继承。

以下示例显示了一种可能的配置:

<beans>
<gfe:async-event-queue id=”AEQ” persistent=”false” parallel=”false” dispatcher-threads=”4″>

<gfe:async-event-listener>
  <bean class="example.AeqListener"/>
</gfe:async-event-listener>

</gfe:async-event-queue>

<gfe:region-template id=”BaseRegionTemplate” initial-capacity=”51″ load-factor=”0.85″ persistent=”false” statistics=”true”

  key-constraint="java.lang.Long" value-constraint="java.lang.String">
<gfe:cache-listener>
  <bean class="example.CacheListenerOne"/>
  <bean class="example.CacheListenerTwo"/>
</gfe:cache-listener>
<gfe:entry-ttl timeout="600" action="DESTROY"/>
<gfe:entry-tti timeout="300 action="INVLIDATE"/>

</gfe:region-template>

<gfe:region-template id=”ExtendedRegionTemplate” template=”BaseRegionTemplate” load-factor=”0.55″>

<gfe:cache-loader>
  <bean class="example.CacheLoader"/>
</gfe:cache-loader>
<gfe:cache-writer>
  <bean class="example.CacheWriter"/>
</gfe:cache-writer>
<gfe:async-event-queue-ref bean="AEQ"/>

</gfe:region-template>

<gfe:partitioned-region-template id=”PartitionRegionTemplate” template=”ExtendedRegionTemplate”

  copies="1" load-factor="0.70" local-max-memory="1024" total-max-memory="16384" value-constraint="java.lang.Object">
<gfe:partition-resolver>
  <bean class="example.PartitionResolver"/>
</gfe:partition-resolver>
<gfe:eviction type="ENTRY_COUNT" threshold="8192000" action="OVERFLOW_TO_DISK"/>

</gfe:partitioned-region-template>

<gfe:partitioned-region id=”TemplateBasedPartitionRegion” template=”PartitionRegionTemplate”

  copies="2" local-max-memory="8192" persistent="true" total-buckets="91"/>

</beans>
Spring 认证中国教育管理中心 -Apache Geode 的 Spring 数据教程四
区域模板也实用于子区域。请留神,“
TemplateBasedPartitionRegion”扩大了“PartitionRegionTemplate”,后者扩大了“ExtendedRegionTemplate”,后者扩大了“BaseRegionTemplate”。在后续继承的 Region bean 定义中定义的属性和子元素会笼罩父元素中的内容。

模板的工作原理
Spring Data for Apache Geode 在 ApplicationContext 解析 Spring 配置元数据时会利用 Region 模板,因而必须依照继承的程序申明 Region 模板。换句话说,父模板必须在子模板之前定义。这样做可确保利用正确的配置,尤其是在笼罩元素属性或子元素时。

同样重要的是要记住 Region 类型只能从其余相似类型的 Region 继承。例如,a 不可能 <gfe:replicated-region> 从 a 继承 <
gfe:partitioned-region-template>。

区域模板是单继承的。

对于区域、子区域和查找的注意事项
此前,的根本属性之一 replicated-region,partitioned-region,local-region,和 client-region 在秋季的数据元素的 Apache 的 Geode XML 命名空间是第一次尝试创立一个区域之前执行查找。这是在 Region 曾经存在的状况下实现的,如果该 Region 是在导入的 Apache Geode 本机 cache.xml 配置文件中定义的,则会呈现这种状况。因而,首先执行查找以防止任何谬误。这是设计使然,可能会发生变化。

此行为已更改,当初默认行为是先创立区域。如果 Region 曾经存在,则创立逻辑会疾速失败并抛出适当的异样。然而,与 CREATE TABLE IF NOT EXISTS …DDL 语法十分类似,Apache Geode <gfe:*-region>XML 命名空间元素的 Spring Data 当初蕴含一个 ignore-if-exists 属性,该属性通过在尝试创立区域之前首先执行按名称标识的现有区域的查找来复原旧行为。如果通过名称找到现有 Region 并将 ignore-if-exists 其设置为 true,则疏忽 Spring 配置中定义的 Region bean 定义。

Spring 团队强烈建议仅将 replicated-region、partitioned-region、local-region 和 client-regionXML 命名空间元素严格用于定义新区域。当这些元素定义的 Regions 曾经存在并且 Region 元素首先执行查找时可能呈现的一个问题是,如果您在应用程序配置中为驱赶、到期、订阅等定义了不同的 Region 语义和行为,那么 Region 定义可能不匹配,并可能体现出与应用程序要求的行为相同的行为。更蹩脚的是,您可能心愿将 Region 定义为分布式 Region(例如,PARTITION),而实际上现有的 Region 定义仅是本地的。

举荐做法 - 仅应用 replicated-region,partitioned-region,local-region,和 client-region XML 命名空间的元素来定义新的地区。

思考以下本机 Apache Geodecache.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=”Customers” refid=”REPLICATE”>

<region name="Accounts" refid="REPLICATE">
  <region name="Orders" refid="REPLICATE">
    <region name="Items" refid="REPLICATE"/>
  </region>
</region>

</region>

</cache>
此外,请思考您可能曾经定义了一个应用程序 DAO,如下所示:

public class CustomerAccountDao extends GemDaoSupport {

@Resource(name = "Customers/Accounts")
private Region customersAccounts;

...

}
在这里,咱们 Customers/Accounts 在应用程序 DAO 中注入了对 Region 的援用。因而,开发人员在 Spring XML 配置元数据中为局部或全副这些 Region 定义 bean 的状况并不少见,如下所示:

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

   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
https://www.springframework.org/schema/geode https://www.springframework.org/schema/geode/spring-geode.xsd

“>

<gfe:cache cache-xml-location=”classpath:cache.xml”/>

<gfe:lookup-region name=”Customers/Accounts”/>
<gfe:lookup-region name=”Customers/Accounts/Orders”/>

</beans>
该 Customers/Accounts 和 Customers/Accounts/Orders 地区作为在 Spring 容器如豆类援用 Customers/Accounts 和 Customers/Accounts/Orders 别离。应用 lookup-region 元素和相应语法(后面形容过)的益处在于,它容许您间接援用子区域,而无需为父区域(Customers 在本例中为)定义一个 bean。

思考以下谬误示例,它更改了配置元数据语法以应用嵌套格局:

<gfe:lookup-region name=”Customers”>
<gfe:lookup-region name=”Accounts”>

<gfe:lookup-region name="Orders"/>

</gfe:lookup-region>
</gfe:lookup-region>
当初思考另一个不好的例子,它应用顶级 replicated-region 元素和 ignore-if-exists 属性集来首先执行查找:

<gfe:replicated-region name=”Customers” persistent=”true” ignore-if-exists=”true”>
<gfe:replicated-region name=”Accounts” persistent=”true” ignore-if-exists=”true”>

<gfe:replicated-region name="Orders" persistent="true" ignore-if-exists="true"/>

</gfe:replicated-region>
</gfe:replicated-region>
Spring 中定义的 Region beansApplicationContext 包含以下内容:{“Customers”, “/Customers/Accounts”, “
/Customers/Accounts/Orders” }. 这意味着后面示例中显示的依赖注入援用(即 @Resource(name = “Customers/Accounts”))当初已损坏,因为 Customers/Accounts 实际上没有定义具备 name 的 bean。因而,您不应像后面两个示例中所示配置区域。

Apache Geode 能够灵便地援用带有或不带有前导斜杠的父区域和子区域。例如,父项能够援用为 /Customersor Customers,子项能够援用为 /Customers/Accounts or Customers/Accounts。然而,Apache Geode 的 Spring Data 在以区域命名 bean 时十分具体。它始终应用正斜杠 (/) 来示意子区域(例如,/Customers/Accounts)。

因而,您应该应用 lookup-region 后面显示的非嵌套语法或应用前导正斜杠 (/) 定义间接援用,如下所示:

<gfe:lookup-region name=”/Customers/Accounts”/>
<gfe:lookup-region name=”/Customers/Accounts/Orders”/>
后面的示例,其中嵌套 replicated-region 元素用于援用子区域,显示了后面所述的问题。客户、账户和订单区域和子区域是否长久?它们不是长久的,因为区域是在本地 Apache Geodecache.xml 配置文件中定义的,REPLICATE 并且在缓存 bean 初始化之前就存在(一旦 <gfe:cache> 元素被解决)。

5.5.8. 数据驱赶(溢出)
基于各种限度,每个区域都能够有一个驱赶策略,用于从内存中驱赶数据。目前,在 Apache Geode 中,驱赶实用于最近起码应用的条目(也称为 LRU)。被驱赶的条目要么被毁坏,要么被分页到磁盘(称为“溢出到磁盘”)。

Spring Data for Apache Geode 通过应用嵌套 eviction 元素反对分区区域、复制区域和客户端、本地区域的所有驱赶策略(条目计数、内存和堆应用)。

例如,要配置一个 PARTITION Region 在内存大小超过 512 MB 时溢出到磁盘,您能够指定以下配置:

<gfe:partitioned-region id=”examplePartitionRegionWithEviction”>
<gfe:eviction type=”MEMORY_SIZE” threshold=”512″ action=”OVERFLOW_TO_DISK”/>
</gfe:partitioned-region>
正本不能应用 local destroy 驱赶,因为这会使它们有效。无关更多信息,请参阅 Apache Geode 文档。

在为溢出配置 Region 时,您应该通过 disk-store 元素配置存储以取得最大效率。

无关驱赶政策的具体阐明,请参阅无关驱赶的 Apache Geode 文档。

正文完
 0