本机测试 ehcache分布式缓存问题!求高手赐教~。。

穆大叔 2015-05-31 09:46:38
现在要做测试ehcache的分布式缓存, 在我本机用两个tomcat项目, server1工程里put一个对象 在server2工程中取不到!
server1 的ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd">
<diskStore path="java.io.tmpdir/ehcache" />

<!--调用ehcache2的RMI-->
<cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=manual,rmiUrls=//localhost:40002/userIdcache"/>

<!--RMI监听40001端口-->
<cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=localhost,port=40001,socketTimeoutMillis=2000"/>

<defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU">
</defaultCache>

<cache name="userIdcache"
maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="1800" timeToLiveSeconds="0"
overflowToDisk="false" diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000" diskPersistent="true"
diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU">

<!--监听配置-->
<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy= false, replicateRemovals= true " />
</cache>

</ehcache>


server2的ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd">
<diskStore path="java.io.tmpdir/ehcache" />

<!--调用ehcache1的RMI-->
<cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=manual,rmiUrls=//localhost:40001/userIdcache"/>

<!--RMI监听40002端口-->
<cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=localhost,port=40002,socketTimeoutMillis=2000"/>

<defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU">
</defaultCache>

<cache name="userIdcache"
maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="1800" timeToLiveSeconds="0"
overflowToDisk="false" diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000" diskPersistent="true"
diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU">

<!--监听配置-->
<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy= false, replicateRemovals= true " />
</cache>

</ehcache>



下边是java代码


public class CacheFactory {
private CacheManager manager;
private static CacheFactory factory= null;
private CacheFactory(){
manager = new CacheManager("src/ehcache.xml");
}
public static CacheFactory getInstance(){
if(factory==null){
factory = new CacheFactory();
}
return factory;
}
public Cache getCache(){
return manager.getCache("userIdcache");
}
public static void main(String[] args) {
System.out.println(CacheFactory.getInstance().getCache());
}
}



server1 工程的测试代码

public class TestCache {
public static void main(String[] args) {
Cache cache = CacheFactory.getInstance().getCache();
//这里给cache里边放一个key1
Element element = new Element("key1", "value1");
cache.put(element);
System.out.println("server1: put cache :"+element.getValue());
}
}



server2 工程的测试代码


public class TestCache {
public static void main(String[] args) {
Cache cache = CacheFactory.getInstance().getCache();
//这里Element为空 获取不到key1
Element element = cache.get("key1");
System.out.println("server2:"+element.getValue());

}
}



在 server1工程的存了key1 的值,在server2 工程取不到 就是上边获取不到!!!!!怎么解决 ? 是不是本机测试不了分布式啊? 求大神赐教~~~~~~~~


...全文
112 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧