ehcache 叢集使用 rmi方式 有圖有真想

來源:互聯網
上載者:User

標籤:叢集   ehcache   

ehcache 有幾種方式叢集,rmi,jgroup還有jms;這裡講一下ehcache的使用

ehcache 使用rmi方式複製緩衝是可取的,原因如下:

1、rmi是java 預設的遠程機制

2、可以調優tcp選項

3、Elements因為要存到磁碟,所以肯定是早已序列化。所以不需要藉助xml格式化什麼的

4、通過配置可以通過防火牆

rmi方式叢集互動圖:

Ehcache的rmi方式是一種點對點的協議,因此它會產生很多區域網路的內部通訊,當然Ehcache會通過一種非同步批處複製理機制類解決

如果要配置ehcache 需要配置一下元素

PeerProvider

CacheManagerPeerListener

配置Provider,這裡有兩種方式:自動探索、手動設定

自動方式:自動探索方式使用tcp廣播來建立和包含一個廣播組,它的特徵是最小配置和對成員組的自動添加和管理。沒有那個伺服器是有優先順序的。對等點每一秒中向廣播組發送心跳,如果一個對等點在五秒鐘內沒發送過來,則此對等點將會被刪除,如果有新的,則會被加入叢集

cacheManagerPeerProviderFactory 的properties有以下配置:

peerDiscovery=automatic

multicastGroupAddress=230.0.0.1
multicastGroupPort=40001
timeToLive=0-255 
hostName=hostname

   peerDiscovery 方式:atutomatic 為自動 ;mulicastGroupAddress 廣播組地址:230.0.0.1;mulicastGroupPort 廣播組連接埠:40001;timeToLive是指搜尋範圍:0是同一台伺服器,1是同一個子網,32是指同一網站,64是指同一塊地區,128是同一塊大陸,還有個256,我就不說了;hostName:主機名稱或者ip,用來接受或者發送資訊的介面

在我這次實驗中具體如下:

 

<cacheManagerPeerProviderFactoryclass="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.1,    multicastGroupPort=4446, timeToLive=32,hostName=192.168.1.101" />

當然還有一種方式就是手動設定,貼上例子,但不作敘述了

server1

<cacheManagerPeerProviderFactoryclass="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"properties="peerDiscovery=manual,rmiUrls=//server2:40001/sampleCache11|//server2:40001/sampleCache12"/>
server2

<cacheManagerPeerProviderFactoryclass="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"properties="peerDiscovery=manual,rmiUrls=//server1:40001/sampleCache11|//server1:40001/sampleCache12"/>

將方式配好之後需要配置listener才會有用,接下來講講:Listener

Listener是用來監聽從叢集發送過來的資訊

Listenner有兩個屬性:class和propertis

class 一個完整的工廠類名

properties 都好分割的對facotory有用的屬性

此次實驗具體配置如下:

<cacheManagerPeerListenerFactoryclass="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"properties="hostName=192.168.1.101, port=40001,socketTimeoutMillis=2000" />
hostName指的是本機,這裡注意如果使用的localhost,則只會對本機有效,請使用子網內的ip地址或者主機名稱,port連接埠 40001,socketTimeoutMillis是指socket子模組的逾時時間,預設是2000ms,注意port兩台主機可以相同可以不同。最好相同,個人建議

然後配置緩衝的複製 Replicators:

本機配置如下:

<cache name="myCache" maxEntriesLocalHeap="10" eternal="false"timeToIdleSeconds="10000" timeToLiveSeconds="10000" overflowToDisk="false"><cacheEventListenerFactoryclass="net.sf.ehcache.distribution.RMICacheReplicatorFactory"properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy=false, replicateRemovals=true,asynchronousReplicationIntervalMillis=1000" /> <bootstrapCacheLoaderFactory                  class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"/> </cache>
name為cache制定名字,maxEntriesLocalHeap:記憶體中可駐留最大Element數量,timeToLiveSeconds 生存周期 10000s;overflowToDisk:當記憶體不足,是否啟用磁碟:這裡為false;給myCache價格監聽,然後是非同步方式,在put,update,copy,remove操作是否複製,然後同步時間1s,bootstrapCacheLoaderFactory 工廠是指啟動是指一啟動就同步資料

以下是完整的配置

<?xml version="1.0" encoding="UTF-8"?><ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"monitoring="autodetect" dynamicConfig="true"><diskStore path="D:/ehcache/diskStore" /><cacheManagerPeerProviderFactoryclass="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.1,    multicastGroupPort=4446, timeToLive=32,hostName=192.168.1.101" /><cacheManagerPeerListenerFactoryclass="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"properties="hostName=192.168.1.101, port=40001,socketTimeoutMillis=2000" /><cache name="myCache" maxEntriesLocalHeap="10" eternal="false"timeToIdleSeconds="10000" timeToLiveSeconds="10000" overflowToDisk="false"><cacheEventListenerFactoryclass="net.sf.ehcache.distribution.RMICacheReplicatorFactory"properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy=false, replicateRemovals=true,asynchronousReplicationIntervalMillis=1000" /> <bootstrapCacheLoaderFactory                  class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"/> </cache></ehcache>

在server2 也就是 192.168.1.116 在hostName配置成此地址,就行了

下面是測試代碼

package com.ehcache;import java.io.IOException;import java.io.InputStream;import net.sf.ehcache.Cache;import net.sf.ehcache.CacheException;import net.sf.ehcache.CacheManager;import net.sf.ehcache.Element;public class Test2 {   public static void main(String[] args) throws InterruptedException {     InputStream is=null;CacheManager manager=null;try {is = Test2.class.getResourceAsStream("/ehcache.xml");   manager = CacheManager.newInstance(is);} catch (CacheException e1) {try {if(is!=null){is.close();is=null;}} catch (IOException e) {e.printStackTrace();}e1.printStackTrace();}          Cache cache = manager.getCache("myCache");           Element element = new Element("client3" + System.currentTimeMillis(), "client3");         cache.put(element);         int i=0;       while (true)         {         Element element2 = new Element("client-3-"+i,i);            cache.put(element2);           Thread.sleep(3000);             System.out.println("\n");             for (Object key : cache.getKeys())             {                 System.out.println(key + ":" + cache.get(key).getObjectValue());             }             i++;       }      }}


server2 的測試代碼將 element那裡改成client-2-即可

然後貼上一張:


client3 同步到client2的資料了

另外附敘述ehcache的淘汰緩衝的演算法:

LRU是Least Recently Used 近期最少使用演算法;

FIFO 以一種隊列方式誰先進誰先出

LFUleast frequently used即最不經常使用頁置換演算法

這是工程檔案的:點擊開啟連結


備忘:作者水平有限,並且這些配置只在區域網路的實踐過,如果有錯誤,請指出。

前面也寫了並且實驗了區域網路內的,負載平衡,tomcat叢集,session回話的分離並且雙機熱備,希望以後有機會讓來親自架構一下大規模的叢集。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.