EHCache分佈式緩存集羣環境配置 EHCache分佈式緩存集羣環境配置

EHCache分佈式緩存集羣環境配置

ehcache提供三種網絡鏈接策略來實現集羣,rmi,jgroup還有jms。同時ehcache能夠能夠實現多播的方式實現集羣,也能夠手動指定集羣主機序列實現集羣。html

 

Ehcache支持的分佈式緩存支持有三種RMI,JGroups,JMS,這裏介紹下MRI和JGrpups兩種方式,Ehcache使用版本爲1.5.0,關於ehcache的其餘信息請參考http://ehcache.sourceforge.net/EhcacheUserGuide.htmljava

關於jgroups的信息請參考http://www.jgroups.org/manual/html_single/index.html。bootstrap

環境爲兩臺機器 server1 ip:192.168.2.154,server2 ip:192.168.2.23緩存

1. RMI方式:

rmi的方式配置要點(下面均是server1上的配置,server2上的只須要把ip兌換便可)網絡

a. 配置PeerProvider:app

Xml代碼socket

<cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=manual,rmiUrls=//192.168.2.23:40001/userCache|//192.168.2.23:40001/resourceCache" />
 

配置中經過手動方式同步sever2中的userCache和resourceCache。tcp

b. 配置CacheManagerPeerListener:分佈式

Xml代碼ide

<cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=192.168.2.154, port=40001,socketTimeoutMillis=2000" />
 

配置中server1監聽本機40001端口。

c. 在每個cache中添加cacheEventListener,例子以下:

Xml代碼

<cache name="userCache" maxElementsInMemory="10000" eternal="true" overflowToDisk="true" timeToIdleSeconds="0" timeToLiveSeconds="0"diskPersistent="false" diskExpiryThreadIntervalSeconds="120">
<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy= false, replicateRemovals= true " />
</cache>

屬性解釋:

必須屬性:

        name:設置緩存的名稱,用於標誌緩存,唯一

        maxElementsInMemory:在內存中最大的對象數量

        maxElementsOnDisk:在DiskStore中的最大對象數量,如爲0,則沒有限制

        eternal:設置元素是否永久的,若是爲永久,則timeout忽略

        overflowToDisk:是否當memory中的數量達到限制後,保存到Disk

可選的屬性:

        timeToIdleSeconds:設置元素過時前的空閒時間

        timeToLiveSeconds:設置元素過時前的活動時間

        diskPersistent:是否disk store在虛擬機啓動時持久化。默認爲false

   diskExpiryThreadIntervalSeconds:運行disk終結線程的時間,默認爲120秒

        memoryStoreEvictionPolicy:策略關於Eviction

緩存子元素:

    cacheEventListenerFactory:註冊相應的的緩存監聽類,用於處理緩存事件,如put,remove,update,和expire

    bootstrapCacheLoaderFactory:指定相應的BootstrapCacheLoader,用於在初始化緩存,以及自動設置。

 

 

參考另一篇學習筆記http://wozailongyou.javaeye.com/blog/230252,也有集羣的說明

2. JGroups方式:

ehcache 1.5.0以後版本支持的一種方式,配置起來比較簡單,要點:

a. 配置PeerProvider,使用tcp的方式,例子以下:

Xml代碼

<cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.jgroups.JGroupsCacheManagerPeerProviderFactory"
properties="connect=TCP(start_port=7800):
TCPPING(initial_hosts=192.168.2.154[7800],192.168.2.23[7800];port_range=10;timeout=3000;
num_initial_members=3;up_thread=true;down_thread=true):
VERIFY_SUSPECT(timeout=1500;down_thread=false;up_thread=false):
pbcast.NAKACK(down_thread=true;up_thread=true;gc_lag=100;retransmit_timeout=3000):
pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;shun=false;
print_local_addr=false;down_thread=true;up_thread=true)"
propertySeparator="::" />

 

b.爲每一個cache添加cacheEventListener:

Xml代碼

<cache name="userCache" maxElementsInMemory="10000" eternal="true"
overflowToDisk="true" timeToIdleSeconds="0" timeToLiveSeconds="0"
diskPersistent="false" diskExpiryThreadIntervalSeconds="120">
<cacheEventListenerFactory class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
properties="replicateAsynchronously=true, replicatePuts=true,
replicateUpdates=true, replicateUpdatesViaCopy=false, replicateRemovals=true"/>
</cache>

JGroup方式配置的兩個server上的配置文件同樣,如有多個server,在initial_hosts中將server ip加上便可。

一個完整的ehcache.xml文件:

Xml代碼

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.sf.net/ehcache.xsd">
<diskStore path="java.io.tmpdir" />
 
<cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.jgroups.JGroupsCacheManagerPeerProviderFactory"
properties="connect=TCP(start_port=7800):
TCPPING(initial_hosts=192.168.2.154[7800],192.168.2.23[7800];port_range=10;timeout=3000;
num_initial_members=3;up_thread=true;down_thread=true):
VERIFY_SUSPECT(timeout=1500;down_thread=false;up_thread=false):
pbcast.NAKACK(down_thread=true;up_thread=true;gc_lag=100;retransmit_timeout=3000):
pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;shun=false;
print_local_addr=false;down_thread=true;up_thread=true)"
propertySeparator="::" />
相關文章
相關標籤/搜索