英文手冊: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
port40404
)這個默認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>
4.2.4使用Gemfire數據連接名空間
其中包括ready-for-events屬性。通知服務器,這種持久的客戶准備接收更新ClientCache.readyForEvents().
除了核心名空間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>