在Tomcat集群中使用ehcache缓存复制

时间:2016-03-06 22:11:05

标签: java spring java-ee caching ehcache

我有3个不同的tomcat实例的以下ehcache配置。

我的假设是,在每个ehcache中,我们应该将所有其他节点定义为提供者,并将此当前节点定义为侦听器。

我们应该在每个ehcache.xml中定义所有缓存吗?如果是,我们是否应该将这些名称添加到RMI地址? 我定义了这样的ehcaches。两个缓存的复制工作正常,但对于其他两个缓存,它不起作用(activityCache和classificationCache)。当然它应该依赖于它们的实现,但是在这个时候,我想确保ehcache配置是正确的。

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="ehcache.xsd">

 <cache name="activityCache"
           maxElementsInMemory="30"
           maxElementsOnDisk="100000"
           eternal="true"
           overflowToDisk="true"
           diskPersistent="true"
           diskSpoolBufferSizeMB="20"
           timeToIdleSeconds="50"
           timeToLiveSeconds="5"
           memoryStoreEvictionPolicy="LFU">

            <bootstrapCacheLoaderFactory class = "net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" properties = "bootstrapAsynchronously=false, maximumChunkSizeBytes=5000000"/>

            <cacheEventListenerFactory class = "net.sf.ehcache.distribution.RMICacheReplicatorFactory" properties="replicateAsynchronously=true, asynchronousReplicationIntervalMillis=100"/>

           </cache>

         <cache name="featureCache"
           maxElementsInMemory="30"
           maxElementsOnDisk="100000"
           eternal="true"
           overflowToDisk="true"
           diskPersistent="true"
           diskSpoolBufferSizeMB="20"
           timeToIdleSeconds="50"
           timeToLiveSeconds="5"
           memoryStoreEvictionPolicy="LFU">            

            <bootstrapCacheLoaderFactory class = "net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" properties = "bootstrapAsynchronously=false, maximumChunkSizeBytes=5000000"/>

            <cacheEventListenerFactory class = "net.sf.ehcache.distribution.RMICacheReplicatorFactory" properties="replicateAsynchronously=true, asynchronousReplicationIntervalMillis=100"/>

           </cache> 

        <cache name="classificationCache"
           maxElementsInMemory="30"
           maxElementsOnDisk="100000"
           eternal="true"
           overflowToDisk="true"
           diskPersistent="true"
           diskSpoolBufferSizeMB="20"
           timeToIdleSeconds="50"
           timeToLiveSeconds="5"
           memoryStoreEvictionPolicy="LFU">    

            <bootstrapCacheLoaderFactory class = "net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" properties = "bootstrapAsynchronously=false, maximumChunkSizeBytes=5000000"/>

            <cacheEventListenerFactory class = "net.sf.ehcache.distribution.RMICacheReplicatorFactory" properties="replicateAsynchronously=true, asynchronousReplicationIntervalMillis=100"/>

           </cache>

    <cache name="userGroupCache"
           maxElementsInMemory="30"
           maxElementsOnDisk="100000"
           eternal="true"
           overflowToDisk="true"
           diskPersistent="true"
           diskSpoolBufferSizeMB="20"
           timeToIdleSeconds="50"
           timeToLiveSeconds="5"
           memoryStoreEvictionPolicy="LFU">

            <bootstrapCacheLoaderFactory class = "net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" properties = "bootstrapAsynchronously=false, maximumChunkSizeBytes=5000000"/>

            <cacheEventListenerFactory class = "net.sf.ehcache.distribution.RMICacheReplicatorFactory" properties="replicateAsynchronously=true, asynchronousReplicationIntervalMillis=100"/>

           </cache>                

 <cacheManagerPeerProviderFactory class = "net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory" properties = "peerDiscovery=manual, rmiUrls=//localhost:40002/activityCache|//localhost:40002/userGroupCache|//localhost:40002/featureCache|//localhost:40002/classificationCache//localhost:40003/activityCache|//localhost:40003/userGroupCache|//localhost:40003/featureCache|//localhost:40003/classificationCache"/>

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

</ehcache>

以下是缓存和cahchSevices的定义:

     <bean id="featureCache" parent="abstractOptionalCache">
        <property name="maxElementsInMemory" value="${cache.feature.maxMemoryElements}"/>
        <property name="maxElementsOnDisk" value="${cache.feature.maxDiskElements}"/> 
        <property name="overflowToDisk" value="${cache.feature.useDisk}"/>
      </bean>

      <bean id="featureCacheService" parent="abstractService" class="com.service.business.cache.impl.ehcache.EHcacheFeatureCacheService">
        <property name="cache"  ref="featureCache"/>
        <property name="enabled" value="${cache.feature.enable}"/>
      </bean> 

      <bean id="activityCache" parent="abstractMandatoryCache">
        <property name="cacheManager" ref="cacheManager"/>
        <property name="maxElementsInMemory" value="${cache.activity.maxMemoryElements}"/>
      </bean>

       <bean id="activityCacheService" parent="abstractService" class="com.service.business.cache.impl.ehcache.EHcacheActivityCacheService">
        <property name="cache"  ref="activityCache"/>
      </bean> 

      <bean id="classificationCache" parent="abstractOptionalCache">
        <property name="cacheManager" ref="cacheManager"/>
        <property name="maxElementsInMemory" value="${cache.classification.maxMemoryElements}"/>
        <property name="maxElementsOnDisk" value="${cache.classification.maxDiskElements}"/> 
        <property name="overflowToDisk" value="${cache.classification.useDisk}"/>
      </bean>

      <bean id="classificationCacheService" parent="abstractService" class="com.service.business.cache.impl.ehcache.EHcacheClassificationCacheService">
        <property name="cache"     ref="classificationCache" />
        <property name="enabled" value="${cache.classification.enable}"/>
      </bean>

        <bean id="userGroupCache" parent="abstractOptionalCache">
    <property name="cacheManager" ref="cacheManager"/>
    <property name="maxElementsInMemory" value="${cache.userGroup.maxMemoryElements}"/>
    <property name="maxElementsOnDisk" value="${cache.userGroup.maxDiskElements}"/> 
    <property name="overflowToDisk" value="${cache.userGroup.useDisk}"/>
  </bean>

  <bean id="userGroupCacheService" parent="abstractService" class="com.service.business.cache.impl.ehcache.EHcacheUserGroupCacheService">
    <property name="cache"     ref="userGroupCache" />
    <property name="enabled" value="${cache.userGroup.enable}"/>
  </bean> 

我会根据您的要求提供更多信息。

提前谢谢。

1 个答案:

答案 0 :(得分:2)

你没事,但在

错了
rmiUrls=//localhost:40002/activityCache|//localhost:40002/userGroupCache|//localhost:40002/featureCache|//localhost:40002/classificationCache//localhost:40003/activityCache|//localhost:40003/userGroupCache|//localhost:40003/featureCache|//localhost:40003/classificationCache

您在分类和活动缓存之间错过了 |

//localhost:40002/classificationCache//localhost:40003/activityCache

这就是这些缓存都不起作用的原因!