Ehcache 不在同一台计算机上的两个 tomcat 之间复制



我正在尝试在同一台机器上的两个tomcat之间设置缓存复制。缓存工作正常,但复制不工作。

对于我的测试,我在服务器 1 上调用 count 方法。然后我在服务器 2 上添加一个实体,并在服务器 2 上调用 count 方法。最后,我在服务器 1 上再次调用 count 方法:缓存被命中,找不到我添加的实体。

Tomcat版本:6,Java版本:

1.6,Ehcache版本:1.3,操作系统:linux

启动时记录(两个服务器相同):

DEBUG net.sf.ehcache.store.MemoryStore - Initialized net.sf.ehcache.store.LruMemoryStore for myCacheSample
DEBUG net.sf.ehcache.store.LruMemoryStore - myCacheSample Cache: Using SpoolingLinkedHashMap implementation
DEBUG net.sf.ehcache.Cache - Initialised cache: myCacheSample
DEBUG net.sf.ehcache.distribution.RMICacheManagerPeerListener - Adding myCacheSample to RMI listener
DEBUG net.sf.ehcache.distribution.RMICacheManagerPeerListener - 0 RMICachePeers bound in registry for RMI listener

代码添加:

public void persist(MyEntity myEntity) {
    getEntityManager().persist(myEntity);
}

带缓存设置的代码计数:

public int count(String criteriaStr) {
    Criteria criteria = ((HibernateEntityManager) getEntityManager()).getSession().createCriteria(MyEntity.class);
    criteria.setCacheable(true).setCacheRegion("myCacheSample");
    criteria.add(Restrictions.eq("criteriaStr", criteriaStr));
    return (Integer) criteria.setProjection(Projections.rowCount()).uniqueResult();
}

代码删除:

public void remove(MyEntity myEntity) {
    getEntityManager().remove(myEntity);
    getEntityManager().flush();
}

服务器 1 ehcache.xml(对于服务器 2:端口 40001 和 40002 切换):

<cacheManagerPeerProviderFactory
    class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
    properties="peerDiscovery=manual,
    rmiUrls=//localhost:40002/myCacheSample"/>
<cacheManagerPeerListenerFactory
    class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
    properties="
    hostName=localhost,
    port=40001,
    socketTimeoutMillis=2000
"/>
<defaultCache
    diskExpiryThreadIntervalSeconds="120"
    diskPersistent="false"
    eternal="false"
    maxElementsInMemory="1000"
    memoryStoreEvictionPolicy="LRU"
    overflowToDisk="true"
    timeToIdleSeconds="120"
    timeToLiveSeconds="120">
<cacheEventListenerFactory
    class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
    properties="
        asynchronousReplicationIntervalMillis=1000,
        replicateAsynchronously=true,
        replicatePuts=false,
        replicateRemovals=true,
        replicateUpdates=true,
        replicateUpdatesViaCopy=false
    "/>
<cache
    eternal="false"
    maxElementsInMemory="1000"
    timeToLiveSeconds="300"
    timeToIdleSeconds="300"
    name="myCacheSample"
    overflowToDisk="false"/>

谢谢你的回答!

3 个 tomcat 实例的 RMI 复制工作配置 希望它对您有用。

    Server 1
<cacheManagerPeerProviderFactory
    class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
    properties="peerDiscovery=manual, rmiUrls=//localhost:40002/apicache|//localhost:40003/apicache" />
<cacheManagerPeerListenerFactory
    class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
    properties="hostName=192.168.1.152, port=40001, socketTimeoutMillis=2000" />
Server 2        
<cacheManagerPeerProviderFactory
    class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
    properties="peerDiscovery=manual, rmiUrls=//localhost:40001/apicache|//localhost:40003/apicache" />
<cacheManagerPeerListenerFactory
    class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
    properties="hostName=192.168.1.152, port=40002, socketTimeoutMillis=2000" />
Server 3
<cacheManagerPeerProviderFactory
    class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
    properties="peerDiscovery=manual, rmiUrls=//localhost:40001/apicache|//localhost:40002/apicache" />
<cacheManagerPeerListenerFactory
    class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
    properties="hostName=192.168.1.152, port=40003, socketTimeoutMillis=2000" />

最新更新