EHCache分布式緩衝叢集環境配置_設計模式

來源:互聯網
上載者:User
EHCache分布式緩衝叢集環境配置

ehcache提供三種網路連接策略來實現叢集,rmi,jgroup還有jms。同時ehcache可以可以實現多播的方式實現叢集,也可以手動指定叢集主機序列實現叢集。

 

Ehcache支援的分布式緩衝支援有三種RMI,JGroups,JMS,這裡介紹下MRI和JGrpups兩種方式,Ehcache使用版本為1.5.0,關於ehcache的其他資訊請參考http://ehcache.sourceforge.net/EhcacheUserGuide.html,

關於jgroups的資訊請參考http://www.jgroups.org/manual/html_single/index.html。

環境為兩台機器 server1 ip:192.168.2.154,server2 ip:192.168.2.23 1. RMI方式:

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

a. 配置PeerProvider:

Xml代碼

<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。

b. 配置CacheManagerPeerListener:

Xml代碼

<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="::" />
 
<defaultCache 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"/>
</defaultCache>
 
<cache name="velcroCache" 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>
<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>
<cache name="resourceCache" 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>
</ehcache>
EHCache分布式緩衝叢集環境配置

ehcache提供三種網路連接策略來實現叢集,rmi,jgroup還有jms。同時ehcache可以可以實現多播的方式實現叢集,也可以手動指定叢集主機序列實現叢集。

 

Ehcache支援的分布式緩衝支援有三種RMI,JGroups,JMS,這裡介紹下MRI和JGrpups兩種方式,Ehcache使用版本為1.5.0,關於ehcache的其他資訊請參考http://ehcache.sourceforge.net/EhcacheUserGuide.html,

關於jgroups的資訊請參考http://www.jgroups.org/manual/html_single/index.html。

環境為兩台機器 server1 ip:192.168.2.154,server2 ip:192.168.2.23 1. RMI方式:

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

a. 配置PeerProvider:

Xml代碼

<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。

b. 配置CacheManagerPeerListener:

Xml代碼

<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="::" />
 
<defaultCache 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"/>
</defaultCache>
 
<cache name="velcroCache" 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>
<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>
<cache name="resourceCache" 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>
</ehcache>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.