Spring Data GemFire学习

英文手册:http://docs.spring.io/spring-data/gemfire/docs/1.5.2.RELEASE/reference/html/(Spring Data GemFire Reference Guide)

 

新版本不用配置Gemfire自带的cache.xml直接用引用名空间的方式,配置Spring配置文件。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlxsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:gfe="http://www.springframework.org/schema/gemfire"
  xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/gemfire http://www.springframework.org/schema/gemfire/spring-gemfire.xsd"> 

    <bean id ... >

    <gfe:cache ...> 

</beans>

注解说明:
<gfe:cache/>创建一个默认的cache:首先Spring会注册一个CacheFactoryBean来创建一个实现了Cache接口的gemfireCache对象。
这个Cache可以是一个既存的也可以是个已经在服务器上存在连接上去的

<gfe:cache id="cache-with-xml" cache-xml-location="classpath:cache.xml"/>
也可以指定用Gemfire本地的配置文件配置。

     <gfe:cache properties-ref="props"/>

     <util:properties id="props" location="file:/vfabric/gemfire/gemfire.properties"/>
</beans>
也可以这样外化配置,通过引用的properties文件配置
这些配置只有在创建的时候才会应用,如果JVM中已经有Cache了,则会忽略

高级Cache配置
<gfe:cache
        copy-on-read="true"
        critical-heap-percentage="70"
        eviction-heap-percentage="60"
        lock-lease="120"
        lock-timeout="60"
        pdx-serializer="myPdxSerializer"
        pdx-disk-store="diskStore"
        pdx-ignore-unread-fields="true"
        pdx-persistent="true"
        pdx-read-serialized="false"
        message-sync-interval="1"
        search-timeout="300"
        close="false"
        lazy-init="true">

     <gfe:transaction-listener ref="myTransactionListener"/>

     <gfe:transaction-writer> 
        <bean class="org.springframework.data.gemfire.example.TransactionListener"/>
      </gfe:transaction-writer>

     <gfe:dynamic-region-factory/> 允许Gemfire动态region factory
     <gfe:jndi-binding jndi-name="myDataSource" type="ManagedDataSource"/> 声明jdni接口绑定外部数据源
</gfe:cache>
更多的配置信息可以看Gemfire官网  http://gemfire.docs.pivotal.io/index.html
close属性表明cache是否和Spring application context 一起关闭
lazy-init是不是等到用的时候才init
TransactionListener 中引用的bean必须实现TransactionListener接口
use-bean-factory-locator属性代表用Spring内部类型BeanFactoryLocator来让定义在cache.xml中的locator注册成一个 Spring beans
在某些情况下,比如UT和IT时,设置use-bean-factory-locator为false,来防止异常,这个异常也会在用Maven build 脚本启动test的时候。测试人员要fork给每个testFork一个新的JVM。
 (in maven, set<forkmode>always</forkmode>

允许PDX序列化
PDX是Gemfire增强系列化框架。
设置pdx-serializer属性就启用了PDX序列化Gemfire提供了一个PDX的实现com.gemstone.gemfire.pdx.ReflectionBasedAutoSerializer(基于反射的自动序列化)具体参考Working with GemFire Serialization


配置一个Gemfire Cache Server
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:gfe="http://www.springframework.org/schema/gemfire"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/gemfire http://www.springframework.org/schema/gemfire/spring-gemfire.xsd
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

  <gfe:cache />

  <!-- Advanced example depicting various cache server configuration options -->
  <gfe:cache-server id="advanced-config" auto-startup="true"
       bind-address="localhost" port="${gfe.port.6}" host-name-for-clients="localhost"
       load-poll-interval="2000" max-connections="22" max-threads="16"
       max-message-count="1000" 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>

CacheServer会在container完全初始化完成后启动,这允许潜在的regions,listeners,writers或者实例定义在server开始接受连接时声明已经完全初始化并注册


配置Gemfire Client Cache
和Cache用法和配置一样,也是org.springframework.data.gemfire.client.ClientCacheFactoryBean提供的(里面有Pool)
<beans>
    <gfe:client-cache />
</beans>
和Cache不同的是Client cache通过Pool连接remote cache server。默认创建一个Pool(localhost port 40404)这个默认pool被client region用,除非client region设置使用另外的pool
client pool可以配置连接为单独的entity或者整个cache连接server
<beans>
  <gfe:client-cache id="simple" pool-name="my-pool"/>

  <gfe:pool id="my-pool" subscription-enabled="true">
     <gfe:locator host="${locatorHost}" port="${locatorPort}"/>
 </gfe:pool>
</beans>
其中包括ready-for-events属性。通知服务器,这种持久的客户准备接收更新ClientCache.readyForEvents().


4.2.4使用Gemfire数据连接名空间
除了核心名空间gfe,Spring Data Gemfire提供gfe-data名空间主要为了简化Gemfire客户端程序的部署。
他包括<datasource>标签来方便的连接data grid
<gfe-data:datasource>
   <locator host="somehost" port="1234"/>
</gfe-data:datasource>
datasource创建client cache 和 connection pool 他将查询所有存在的root regions中的server的数量并且为每个server创建client region的代理
这个标签的语法和<gfe:pool>很像。他可能和一个或多个locator或server tags配置在一起来连接一个存在的data grid,此外所有配置pool的属性都是支持的。
这个配置会为每个连接到locator定义在member上的region自动创建ClientRegion Bean。所以可以无缝的使用Spring Data mapping 注解,GemfireTemplate,织入程序中
<gfe-data:datasource>
   <locator host="somehost" port="1234"/>
</gfe-data:datasource>

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

4.2.5配置Gemfire Region
region是用来存储cache中检索的数据。Region是一个继承了java.util.Map,可以使用键值对访问的接口。
相当于关系型数据库中的table

Gemfire实现了以下几个类型的region
1、Replicated-数据在每个cache member中存在一个备份,这提供了很高的读性能,但是写比较耗时
2、Partioned-数据分别储存在多个cache member中的bucket中,这提供了高的读写性能,并且适用于非常大的数据集(对于单点来说很大的数据集)
3、Local-数据只存储在本地节点
4、Client-从技术上说,client region是一个扮演者代理到远程服务器上的replicated或partitioned region的本地region,
他将持有数据,在本地创建和查询,他可以是空的。本地的更新会同步到远程cache server,并且Client region可以接受事件来保持和远端同步

使用外部配置Region
<gfe:lookup-region id="region-bean" name="Orders"/>
用lookup-region来关联在cache.xml中配置好的Region(Orders是既存的Region名)
<!-- lookup for a region called 'Orders' -->
<gfe:lookup-region id="Orders"/>
如果没有配置name 就会用id属性,如果Region不存在,就会抛出初始化异常
<gfe:cache id="cache"/>
<gfe:lookup-region id="region-bean" name="Orders" cache-ref="cache"/>
用cache-ref来关联某个cache

lookup-region提供了一个简单的方法检索存在的,预配置好的region。不暴露Region语义和设置基础设施

自动Region查找
Spring Data Gemfire1.5以后可以自动查找定义在cache.xml中的Region,并且使用<gfe:cache>中的‘cache-xml-location’来引入

<?xml version="1.0"?>
<!DOCTYPE cache PUBLIC  "-//GemStone Systems, Inc.//GemFire Declarative Caching 7.0//EN"
	"http://www.gemstone.com/dtd/cache7_0.dtd">
<cache>
	<region name="Parent" refid="REPLICATE">
		<region name="Child" refid="REPLICATE"/>
	</region>
</cache>
<gfe:cache cache-xml-location="cache.xml"/>用这句话来映入cache.xml
用<gfe:lookup-region id="Parent"/>来关联某个region到Spring上下文
<gfe:auto-region-lookup/>自动创建关联到Region的Beans

Spring Data Gemfire使用BeanPostProcessor接口来发送Cache处理,将Region加到Spring上下文
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;

  ...
}
也可以用XML定义:
<bean class="example.ApplicationDao" depends-on="gemfireCache"/>
用<gfe:auto-region-lookup>来加载cache.xml中的Region

配置Region
Spring Data Gemfire 提供综合的支持来通过以下元素来配置各种类型的Region
  • Local Region <local-region>

  • Replicated Region <replicated-region>

  • Partitioned Region <partitioned-region>

  • Client Region <client-region>

     
 一般的Region属性
cache-ref  Cache名。。。。表省略

Cache Listeners
Cache Listeners 和一个Region一起注册来处理region事件,例如创建条目、更新、删除。Cache Listener是一个实现了CacheListener接口的类。
一个Region可能有许多个listener,用cache-listener对象来添加到*-region对象中。下面的例子中有两个'CacheListener'
<gfe:replicated-region id="region-with-listeners">
    <gfe:cache-listener>
        <!-- nested cache listener reference -->
        <ref bean="c-listener"/>
        <!-- nested cache listener declaration -->
        <bean class="some.pkg.AnotherSimpleCacheListener"/>
    </gfe:cache-listener>

    <bean id="c-listener" class="some.pkg.SimpleCacheListener"/>
</gfe:replicated-region>

Cache Loaders和Cache Writers
和cache-listener相似,名空间提供了cache-loader和cache-writer元素来注册各自组建到Region中
CacheLoader调用一个Cache允许从外部数据源添加条目。CacheWriter在条目被穿件或者更新,需要同步外部数据源时被调用

Subregions
在1.2.0后,Spring Data Genfire添加了对Sub region的支持。允许region被组织在一个层次关系中
例如:Gemfire允许/Customer/Address region和另一个/Employee/Address region。并且一个subregion有自己的subregions和他自己的配置。
一个subregion不会从parent region中继承属性。Region的类型可能是混合的匹配除Gemfire约束,一个subregion 会自然地声明为region的子元素,subregion的名字一般是简单名。
<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])属性在subregion中是不被允许的。subregion使用名字
/Customer/Address region和/Employee/Address region创建的,所以别的bean,例如GemfireTemplatez在调用他们的时候需要使用全名,在使用OQL时也要使用全路径名

Region Templates
Spring Data Gemfire1.5版本后提供Region Templates。这一特性允许开发者来一次性定义通用的Region设置和属性并在Spring上下文中声明Region时多次使用。

Region Template有5个标签
表略。。。。

<gfe:*-region>元素和<gfe:*-region-template>元素有template属性来定义从Region配置中继承的Region Template.
甚至,Region template可以从其他的Region Template中继承。
下面有个例子
<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" cloning-enabled="true"
    concurrency-checks-enabled="false" disk-synchronous="false"
    ignore-jta="true" initial-capacity="51" key-constraint="java.lang.Long"
    load-factor="0.85" persistent="false" statistics="true"
    value-constraint="java.lang.String">
  <gfe:cache-listener>
    <bean class="example.CacheListenerOne"/>
    <bean class="example.CacheListenerTwo"/>
  </gfe:cache-listener>
  <gfe:entry-ttl timeout="300" action="INVALIDATE"/>
  <gfe:entry-tti timeout="600" action="DESTROY"/>
</gfe:region-template>

<gfe:region-template id="ExtendedRegionTemplate" template="BaseRegionTemplate"
    index-update-type="asynchronous" cloning-enabled="false"
    concurrency-checks-enabled="true" key-constraint="java.lang.Integer"
    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:membership-attributes required-roles="readWriteNode" loss-action="limited-access" resumption-action="none"/>
  <gfe:async-event-queue-ref bean="AEQ"/>
</gfe:region-template>

<gfe:partitioned-region-template id="PartitionRegionTemplate" template="ExtendedRegionTemplate"
    copies="1" local-max-memory="1024" total-max-memory="16384" recovery-delay="60000"
    startup-recovery-delay="15000" enable-async-conflation="false"
    enable-subscription-conflation="true" load-factor="0.70"
    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" total-buckets="91" disk-synchronous="true"
    enable-async-conflation="true" ignore-jta="false" key-constraint="java.util.Date"
    persistent="true">
  <gfe:cache-writer>
    <bean class="example.CacheWriter"/>
  </gfe:cache-writer>
  <gfe:membership-attributes required-roles="admin,root" loss-action="no-access" resumption-action="reinitialize"/>
  <gfe:partition-listener>
    <bean class="example.PartitionListener"/>
  </gfe:partition-listener>
  <gfe:subscription type="ALL"/>
</gfe:partitioned-region>
Region Template 甚至可以为Subregion中作,注意到TemplateBasedPartitionRegion 继承了PartitionRegionTemplate,PartitionRegionTemplate又继承了ExtendedRegionTemplate,它继承了BaseRegionTemplate


Under the hood(在底层)
Spring Data Gemfire 适用于从Spring上下文配置信息解析后的Region Template。因此必须按继承顺序定义。父template在子之前。这确保了配置被使用,尤其是在存在重载的情况下

有关Region、Subregion和Lookup的警告
在Spring Data Gemfire1.4之前,在Spring Data Gemfire中的XML名空间中对应基于数据策略的Region类型的高级底层属性replicated-region, partitioned-region, local-region 和 client-region元素在创建Region前会先做查找。
如果Region在cache.xml中已经定义个Region,则上面的标签会先lookup如果Region已经存在就结束。
Spring团队强烈建议replicated-region, partitioned-region, local-region and client-region严格的用来定义新的region。如果不使用这些标签在创建Region前做lookup会出现一个问题,如果开发者假设定义一个复制的新Region,但是同名的Region已经存在,这样就会导致各种语义的问题。
但是,因为使用了高级Region元素,会查找。当我们想将依赖的Region注入到程序中撕可能会出现问题。

见下方实例,首先有一个本地Gemfire配置文件Cache.xml中已经定义个Region,则上面的标签会先lookup如果Region已经存在就结束。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE cache PUBLIC "-//GemStone Systems, Inc.//GemFire Declarative Caching 7.0//EN"
"http://www.gemstone.com/dtd/cache7_0.dtd">
<cache>
<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的GemfireRegion注入到我们的DAO中。把这些bean 定义到Spring XML配置文件中也不鲜见。示例如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:gfe="http://www.springframework.org/schema/gemfire"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/gemfire http://www.springframework.org/schema/gemfire/spring-gemfire.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的Gemfire Region被当做Bean引入Spring上下文。在Spring上下文中名字叫"Customers/Accounts" and "Customers/Accounts/Orders"。这样用lookup-region元素和相应的语法的好处是可以直接引用subregion而不用为父Region定义不必要的Bean(例如 Customer)

然而,如果开发者改变配置语法,用嵌套形式,如下:
<gfe:lookup-region name="Customers">
<gfe:lookup-region name="Accounts">
<gfe:lookup-region name="Orders"/>
</gfe:lookup-region>
</gfe:lookup-region>
或者开发者错误的使用了高级replicated-region元素,会先进行lookup,如下:
<gfe:replicated-region name="Customers" persistent="true">
<gfe:replicated-region name="Accounts" persistent="true">
<gfe:replicated-region name="Orders" persistent="true"/>
</gfe:replicated-region>
</gfe:replicated-region>
这样的话,Spring上下文中包含的bean如下:{ "Customers", "/Customers/Accounts", "/Customers/Accounts/Orders" }这就会导致依赖注入 (i.e. @Resource(name = "Customers/Accounts"))现在是不可用的,因为没有找到叫"Customers/Accounts"的bean

Gemfire可以灵活的引用父Region和子Region。父Region可以使用 "/Customers" or "Customers"名字引用。子Region可以使用"/Customers/Accounts" or just "Customers/Accounts"引用。然而,Spring Data GemFire必须在最前面使用斜杠来表示子Region (e.g. "/Customers/Accounts")
因此,推荐用户使用嵌套式的lookup-region语法或者定义以斜杠开头的直接引用
<gfe:lookup-region name="/Customers/Accounts"/>
<gfe:lookup-region name="/Customers/Accounts/Orders"/>
上方例子中嵌套的replicated-region元素来创建的 Customers, Accounts 和 Orders Regions/Subregions Region还不是持久化的。当在cache.xml定义了Region或者曾经创建了<gfe:cache>bean。这些被创建的Bean会在cache初始化的时候存在。如果使用了高级元素如replicated-region,会先进行lookup,查找是否已经存在cache.xml中定义而产生的Region。

数据持久化
Region 可以被配置成持久化的。如果配置了持久化,Gemfire会确保Region里所有数据会被写到磁盘,当下次创建Region的时候会被恢复。这允许数据在设配或者进程失败重启的时候可以恢复。
来允许持久化,需要设置persistent属性为TRUE
<gfe:partitioned-region id="persitent-partition" persistent="true"/>

持久化可以被配置使用data-policy属性,配置如下:
<gfe:partitioned-region id="persitent-partition" data-policy="PERSISTENT_PARTITION"/>
数据策略必须和region type 匹配,Region必须允许persistent属性。如果persistent属性设置是FALSE,但是持久化数据策略又被配置了就会抛出初始化异常。
持久化Region可以使用disk-store元素来配置存储的路径。也可以设置同步或者异步写入磁盘
<gfe:partitioned-region id="persitent-partition" persistent="true" disk-store-ref="myDiskStore" disk-synchronous="true"/>

Subscription Interest Policy
Gemfire 允许配置一个订阅器来控制P2P事件处理(peer to peer event handling)。Spring Data Gemfire提供<gfe:subscription/>来配置replicated 和 partitioned Region interest策略到ALL或者CACHE_CONTENT上
<gfe:partitioned-region id="subscription-partition">
<gfe:subscription type="CACHE_CONTENT"/>
</gfe:partitioned-region>

数据驱逐(Eviction)和溢出
基于不同的容器,每个Region可以有驱逐策略来从内存中驱逐数据。目前,在Gemfire中,驱逐适用于最近最少使用策略(LRU)。被驱逐的条目回被销毁或者复制到磁盘(溢出)。
Spring data Gemfire通过嵌套的eviction元素为partitioned-region and replicated-region 和client-region提供了所有的驱逐策略(条目计数,内存和堆的使用)。
例如:配置一个分区(partition Region)大于512M的时候溢出到磁盘:
<gfe:partitioned-region id="overflow-partition">
<gfe:eviction type="MEMORY_SIZE" threshold="512" action="OVERFLOW_TO_DISK"/>
</gfe:partitioned-region>
Replicate Region不能使用local destroy 驱逐。这样会让他们都无效。
当配置Region溢出的时候,建议使用disk-store元素来配置存储

数据到期

Gemfire允许控制数据在cache中存在的周期。和通过内容用量来驱逐数据相反,他是通过通过实践来驱动驱逐。一旦一个条目过期,就在内存中访问到。
Gemfire提供如下过期方式:
1、Time to live(TTL)-按秒计,设置存在的最大时间.create 和 put操作会将计数器设置为0.当创建后conter会被重置
2、Idle timeout-空闲超时设定
这些设置适用于region本身或者是region中的条目。Spring Data Gemfire提供了<region-ttl>, <region-tti>, <entry-ttl> 和 <entry-tti>region子元素来进行配置

本地Region
Spring Data Gemfire 提供专用的local-region元素来创建local regions。本地Region是一个独立的Region,不和其他任何分布式系统成员分享数据。支持所有一般的Region配置
<gfe:local-region id="myLocalRegion" />
这样一个本地region就建立了(当他之前不存在)。region的名字和bean id一样(myLocalRegion)并且bean假定存在genfireCache的Gemfire缓存上

Replicated Region
最普遍的region类型就是replicated region或者是replica。他表示每个member都有replicated region,在本地存储region所有条目的拷贝。对replicated region的任何的更新会分发到所有的拷贝。
Spring Data GemFire提供一个replicated-region元素
<gfe:replicated-region id="simpleReplica" />

Partitioned Region 分布式Region
分布式的Region是将数据分散在每个server上,每个节点上存储一个数据的子集

当使用分布式Region,程序就表现成一个region的逻辑视图。就像region中所有的数据都包含在一个map中。当读写这个map的时候会透明的路由到每个存有记录的节点。Gemfire用哈希码将domain分成每个buckets。每个bucket被分配到一个特定的部分,不过可能在任何时候被重定位到其他的部分来提高集群对资源的利用。
分布式的Region使用partitioned-region元素来创建。他的配置与replicated-region相似再加上冗余拷贝数,最大内存总量,buckets的数量,分布式解析器等特殊属性。下方是有两个冗余拷贝的配置示例:
<!-- bean definition named 'distributed-partition' backed by a region named 'redundant' with 2 copies
and a nested resolver declaration -->
<gfe:partitioned-region id="distributed-partition" copies="2" total-buckets="4" name="redundant">
<gfe:partition-resolver>
<bean class="some.pkg.SimplePartitionResolver"/>
</gfe:partition-resolver>
</gfe:partitioned-region>
详细配置表略

Client Region 客户端Region
Gemfire支持多种部署拓扑来管理和分布数据。这个话题已经超了文档的范围,然而快速回顾一下,他可以简略的分为:P2P,client-server,广域网高速缓存(或者是WAN),在最后两个场景中,声明客户端region来连接一个缓存服务器是很常见的。Spring Data GemFire提供专门的支持例如client-region、pool elements来配置客户端Region。正如名字所表示的,前者定义一个客户端region,而后者定义了连接池来给各个不同的客户端region使用。
下方是典型的客户端region配置。
<!-- client region using the default client-cache pool -->
<gfe:client-region id="simple">
<gfe:cache-listener ref="c-listener"/>
</gfe:client-region>

<!-- region using its own dedicated pool -->
<gfe:client-region id="complex" pool-name="gemfire-pool">
<gfe:cache-listener ref="c-listener"/>
</gfe:client-region>

<bean id="c-listener" class="some.pkg.SimpleCacheListener"/>

<!-- pool declaration -->
<gfe:pool id="gemfire-pool" subscription-enabled="true">
<gfe:locator host="someHost" port="40403"/>
</gfe:pool>

和其他Region类型一样,client-region支持CacheListener和一个单独的CacheLoader或者是CacheWriter。他也需要连接池来连接服务器。每个客户端可以独立拥有一个连接池也可以共享同一个。

在上面的例子中,连接池使用一个locator配置的。locator是一个单独的进程来发现分布式系统中的缓存服务器,并且推荐用于生产系统。也可以使用server元素(<server/>)来配置,让pool直接连接到一个或多个缓存服务器。

想要看client特别是pool的完整配置选项,可以参见Spring Data GenFire schema

Client Interests 客户端的好处
为了减小网络传输,每个客户端可以定义自己的配置('interest')来连接Gemfire获得需要的数据。在Spring Data GemFire中,interest可以使用键(key-base)和正则表达式为每个客户端配置。
<gfe:client-region id="complex" pool-name="gemfire-pool">
<gfe:key-interest durable="true" result-policy="KEYS">
<bean id="key" class="java.lang.String">
<constructor-arg value="someKey" />
</bean>
</gfe:key-interest>
<gfe:regex-interest pattern=".*" receive-values="false"/>
</gfe:client-region>
一个特殊的key ALL_KEYS表示为所有keys注册了interest(和表达式.*相同)。receive-value属性表示创建和更新事件时是否接收值。如果是,值被接收了;如果不是,只有无效事件被接收。详细请参见GemFire文档。

JSON支持
Gemfire7.0 支持OQL查询来支持缓存JSON文档。这些在内部通过PDXInstance存储的数据通过JSONFormatter来相互转化。Spring Data Gemfire提供<gfe-data:json-region-autoproxy/>标签来允许Spring AOP对象通知(advice)适合的region操作,有效的封装JSONFormatter,允许程序直接使用JSON字符串。另外,Java对象写入JSON配置的region将会自动使用ObjectMapper转化为JSON,读其中的数据也会返回JSON字符窜。

默认情况下,<gfe-data:json-region-autoproxy/>将会在所有的Region上做转换。为了将此功能应用到选定的region,提供了一个逗号分隔的id列表,通过region-refs属性配置。其他的属性包括pretty-print(默认是false)和convert-returned-collections。默认的region操作getAll()和values()返回的结果时将会转化这些结果。这是通过在本地内存创建一个并行结构完成的。这对大的集合将会产生显著的开销。所以设置flag为FALSE来静止这些操作的自动转化。
NOTE:某些region的操作,尤其那些使用Gemfire专有的Region.Entry比如entries(boolean),entrySet(boolean) 和 getEntry() 这些方法不是AOP的通知。比如entrySet()方法返回Set<java.util.Map.Entry<?,?>>这些方法是不受AOP配置的影响的。
<gfe-data:json-region-autoproxy pretty-print="true" region-refs="myJsonRegion" convert-returned-collections="true"/>

通过将Spring模板声明成Spring bean,这种特性也同样适用于无缝的GemfireTemplate操作。目前,本地QueryService操作不支持。

4.2.6 Create an Index 创建索引
GemFire允许创建索引来提高查询性能。Spring Data GemFire允许通过index元素声明索引。
<gfe:index id="myIndex" expression="someField" from="/someRegion" />
在创建索引之前,Spring Data GemFire将会验证是否已经存在相同名字的索引。如果存在了,他将比较索引的属性,如果不一致将删除旧索引创建新的索引代替。如果一致,Spring Data GemFire将简单的返回该索引(如果不存在就会创建一个)。为了防止索引的更新,即使在属性不匹配的情况下也要设置override属性为false。
注意,索引的声明不是绑定到region而是在顶层元素上(比如gfe:cache)。这允许
将任意数量的索引声明在任意的region上,无论这些索引是刚创建的还是已经存在的。这是对GemFire cache.xml的一种改进。在默认情况下索引依赖默认的cache声明,不过因此可以自定义他,或者使用pool

4.2.7Configuring a Disk Store 配置磁盘存储
在1.2.0版本,Spring Data GemFire支持通过顶层元素disk-store元素配置磁盘存储。
在1.2.0以前disk-store是*-region的子元素。如果想升级为现在的配置方法,可以将原来的配置移到主元素,声明一个id使用region的disk-store-ref属性。同样disk-synchronous属性现是region级别的属性。
<gfe:disk-store id="diskStore1" queue-size="50" auto-compact="true"
max-oplog-size="10" time-interval="9999">
<gfe:disk-dir location="/gemfire/store1/" max-size="20"/>
<gfe:disk-dir location="/gemfire/store2/" max-size="20"/>
</gfe:disk-store>
region使用磁盘存储来实现文件持久化备份、驱逐记录的溢出存储和WAN网关的持续备份。注意,多个组件可能共用一个磁盘存储。而且一个磁盘存储可能被定义多个路径。请到GemFire文档中查看更多的配置方法。

4.2.8.Configuring GemFire's Function Service 配置GemFire的远程方法服务
在1.3.0,Spring Data GemFire提供注解来实现和注册方法。Spring Data Gemfire 也提供名空间来为执行远程方法注册GemFire方法。请查看GemFire文档查看更多关于执行方法框架的信息。方法被声明为Spring beans并且要实现com.gemstone.gemfire.cache.execute.Function接口或者继承com.gemstone.gemfire.cache.execute.FunctionAdapter。名空间使用相似的模式来声明方法。
<gfe:function-service>
<gfe:function>
<bean class="com.company.example.Function1"/>
<ref bean="function2"/>
</gfe:function>
</gfe:function-service>

<bean id="function2" class="com.company.example.Function2"/>

4.2.9.Configuring WAN Gateways 配置WAN网关
广域网网关提供一种同步分散在不同地区的GemFire分布式系统的方法。在1.2.0版本的Spring Data GemFire中提供名空间来支持配置广域网网关。

WAN Configuration in GemFire 7.0
GemFire7.0 提供新的API来配置WAN。GemFire6.0中的API也继续支持,但是推荐使用新的AIP来配置。Spring Data GemFire的名空间中两者都支持。
在下面的例子中GatewaySender添加子













 


 
 
 
 
 

posted on 2016-09-17 21:17  多看多学  阅读(2101)  评论(1编辑  收藏  举报

导航