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添加子













 


 
 
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM