這里先說明下環境:JDK1.6、ehcache-core-2.1.0.jar、Tomcat6、Spring3.0.2。使用的是RMI方式配置集群的,這里先吐槽下遇到的情況,在搜相關知識的時候發現到處都是同一篇文章,被抄來抄去,最后沒辦法只好用英文在google上搜索才找到了大量有用的文章以及遇到類似的帖子,哎,繼續寫我的配置。(出自博客園)
RMI的介紹不多說,因為我也不是非常理解,不敢妄加說明,還怕誤導別人。
我的集群環境是兩台硬件服務器分別為A服務器和B服務器,每台服務器上有兩個tomcat,A服務器上的是Tomcat1和Tomcat2,B服務器上的是Tomcat3和Tomcat4,大家一定要注意環境,因為有的環境不同,配置就不同,RMI集群有兩種方式配置:自動成員發現和手動成員發現。我采用的是自動成員發現(本人不會考慮手動成員發現,局限性太大,不利於項目的統一部署,原因是:因為每次都要修改配置文件)。項目是通過Spring配置文件來加載ehcache.xml的,不再說明,這里只說ehcache.xml配置(配置中的中文注釋是在編寫這篇文章的時候添加的,因為這個配置文件中存在中文注釋讓我的項目啟動不起來):
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="false" monitoring="autodetect"> <diskStore path="java.io.tmpdir" /> <!--緩存成員發現工廠,管理cacheManager對象 --> <cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory" properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.1, multicastGroupPort=4446, multicastPacketTimeToLive=32" /> <!--針對cacheManager事件的監聽,這里只介紹properties中的hostName、port屬性, 這里我忽略了hostName的配置,查看他們的源碼發現如果不填寫hostName, 他們就會通過JDK中的InterAddress.getLocalHost().getHostAddress()獲取本機的ip地址, 所以在這里我沒有填寫hostName的配置,方便部署到多台硬件服務器上。 但是如果一台已經服務器上有多個網卡,這里一定要指定hostName的IP,原因參考InterAddress源碼。 post這里我指定的時40001,如果這里不填寫port配置,ehcache就會通過ServerSocket的getLocalPort獲取一個本機沒有被占用的端口 --> <cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory" properties="port=40001" /> <!--默認緩存配置 --> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" /> <!--自定義緩存配置1 --> <cache name="cache1" maxElementsInMemory="10000" maxElementsOnDisk="10000" eternal="false" overflowToDisk="false" diskSpoolBufferSizeMB="20" timeToIdleSeconds="7200" timeToLiveSeconds="7200" diskPersistent="false" memoryStoreEvictionPolicy="LFU"> <!--監聽緩存事件,緩存移除、修改的時候同步其他服務器(Tomcat)的緩存,時間限制,具體屬性不在這里說明 --> <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy=false, replicateRemovals=true " /> <!--服務器(Tomcat)啟動就同步其他服務器(Tomcat)中的緩存,時間限制,具體屬性不再這里說明 --> <bootstrapCacheLoaderFactory class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" /> </cache> <!--自定義緩存配置2 --> <cache name="cache2" maxElementsInMemory="10000" maxElementsOnDisk="10000" eternal="false" overflowToDisk="false" diskSpoolBufferSizeMB="20" timeToIdleSeconds="7200" timeToLiveSeconds="7200" diskPersistent="false" memoryStoreEvictionPolicy="LFU"> <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy=false, replicateRemovals=true " /> <bootstrapCacheLoaderFactory class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" /> </cache> </ehcache>