将ehcache从一台服务器复制到另一台服务器

时间:2016-03-16 14:13:37

标签: java ehcache

我有一个带ehcache的tomcat服务器。我有一个带servlet的第二个tomcat。当第二个servlet被引入时,它应该从#1获取ehcache并将所有数据放入其缓存中。

ehcache中是否存在“启动复制”的内置机制?或者我如何获取序列化的ehcache数据,然后将它们反序列化为ehcache。我明白我可以逐个读取所有键,然后所有的值然后序列化,但也许有更好的方法?

谢谢

2 个答案:

答案 0 :(得分:0)

没有开箱即用的支持你所要求的。

替代方案是:

请注意,两者之间的主要区别在于第二个选项在两个Ehcache实例之间的数据一致性方面没有提供任何保证。

免责声明:我在Ehcache上为Terracotta工作

答案 1 :(得分:0)

这是我使用内置rmi复制解决方案的解决方案:

主配置:

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

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

<cache name="masterCache"
       maxEntriesLocalHeap="10000"
       eternal="false"
       timeToIdleSeconds="300"
       timeToLiveSeconds="600"
       memoryStoreEvictionPolicy="LFU"
       transactionalMode="off">
    <cacheEventListenerFactory
            class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
            properties="replicateAsynchronously=true, replicatePuts=false, replicateUpdates=false,
    replicateUpdatesViaCopy=false, replicateRemovals=false "/>
    <persistence strategy="none"/>
</cache>

在这里,我打开了从属服务器的RMI监听器,并为缓存启用了RMI,但没有任何复制,因为我只需要启动奴隶的数据,这一切都是

奴隶配置:

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

<cacheManagerPeerProviderFactory
        class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
        properties="peerDiscovery=manual,
rmiUrls=//localhost:40001/masterCache"/>


<cache name="masterCache"
       maxEntriesLocalHeap="10000"
       eternal="false"
       timeToIdleSeconds="300"
       timeToLiveSeconds="600"
       memoryStoreEvictionPolicy="LFU"
       transactionalMode="off">
    <bootstrapCacheLoaderFactory class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"
                                 properties="bootstrapAsynchronously=false"/>
    <persistence strategy="none"/>
</cache>

我将RMI连接到主节点并打开缓存的引导程序。