EhCache 集群 配置(RMI方式)

  这里先说明下环境: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>
原文地址:https://www.cnblogs.com/andysd/p/4256008.html