【问题标题】:Ehcache not replicating between two tomcat on the same machineEhcache 不在同一台机器上的两个 tomcat 之间复制
【发布时间】:2016-06-23 14:31:45
【问题描述】:

我正在尝试在同一台机器上的两个 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"/>

感谢您的回答!

【问题讨论】:

  • 两个tomcat在不同服务器时是否有效?
  • 不,但我不确定网络管理(防火墙、端口等)。这就是为什么我之前在同一台机器上测试,而不是在多播模式下。

标签: java hibernate tomcat caching ehcache


【解决方案1】:

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" />

【讨论】:

    猜你喜欢
    • 2023-03-05
    • 2018-09-25
    • 2016-05-18
    • 1970-01-01
    • 2011-10-11
    • 1970-01-01
    • 1970-01-01
    • 2011-06-08
    • 2018-01-17
    相关资源
    最近更新 更多