一台电脑两个tomcat 模仿集群session共享,ehcache 里面的内容无法同步进入
第一个XML
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<diskStore path="D:\cache" />
<defaultCache maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="120" diskSpoolBufferSizeMB="30"
maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120">
</defaultCache>
<cache name="test1" maxElementsInMemory="100" maxElementsOnDisk="0"
eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="0">
<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=true,replicatePuts=true, replicateUpdates=true, replicateUpdatesViaCopy=false, replicateRemovals=true "/>
</cache>
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=manual,
rmiUrls=//localhost:8090/test1" />
<cacheManagerPeerListenerFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=localhost, port=8091,
socketTimeoutMillis=2000"/>
</ehcache>
第二个:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<diskStore path="D:\cache" />
<defaultCache maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="120" diskSpoolBufferSizeMB="30"
maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120">
</defaultCache>
<cache name="test1" maxElementsInMemory="100" maxElementsOnDisk="0"
eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="0">
<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=true,replicatePuts=true, replicateUpdates=true, replicateUpdatesViaCopy=false, replicateRemovals=true "/>
</cache>
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=manual,
rmiUrls=//localhost:8091/test1" />
<cacheManagerPeerListenerFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=localhost, port=8090,
socketTimeoutMillis=2000"/>
</ehcache>
测试的额
CacheManager manager = new CacheManager("E:\\workspace\\jeesite-yjy\\src\\main\\resources\\cache\\ehcache-rmi.xml");
//get Cache
Cache cache = manager.getCache("test1");
Thread.sleep(10000);
Element element = new Element("key","test");
cache.put(element);
System.out.println("Initial:\n"//+url.toString()
+"\n"+manager.getName()
+"\n"+cache.getName()
+" 's size = "+cache.getSize()
+"\n"+element.toString());
CacheManager manager = new CacheManager("E:\\workspace\\jeesite-yjy2\\src\\main\\resources\\cache\\ehcache-rmi.xml");
//get Cache
Cache cache = manager.getCache("test1");
Thread.sleep(1000);
while(true){
System.out.println("搜索中...");
System.out.println("当前资源数:" + cache.getSize());
Element element = cache.get("key");
if (element != null) {
System.out.println(element.getValue());
break;
}
Thread.sleep(1000);
}
跪求大神
ehcache有几种方式实现集群:rmi、jgroup、jms,这里讲一下ehcache使用rmi的实现方式。
主要有三个配置:
1、cacheManagerPeerProviderFactory配置:
class= “net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory”
properties= “peerDiscovery=automatic, multicastGroupAddress=230.0.0.1, multicastGroupPort=4446, timeToLive=1”/>
timeToLive的值指的是数据包可以传递的域或是范围。约定如下:
0是限制在同一个服务器
1是限制在同一个子网(默认)
32是限制在同一个网站
64是限制在同一个region
128是限制在同一个大洲
255是不限制
2、cacheManagerPeerListenerFactory配置:
class= “net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory”
properties= “hostName=192.168.1.101, port=40001, socketTimeoutMillis=2000”/>
hostName说明:为本机IP,不指定时hostname将使用InetAddress.getLocalHost().getHostAddress()来得到。当在同一台机器上有多个CacheManager的时候,应该用localhost来配置。
3、cacheEventListenerFactory配置:
class= “net.sf.ehcache.distribution.RMICacheReplicatorFactory”
properties= “replicateAsynchronously=true,
replicatePuts=true,
replicateUpdates=true,
replicateUpdatesViaCopy=true,
replicateRemovals=true” />
可以使用默认配置:
参数说明:
replicateAsynchronously=true | false – 复制方式是异步的(指定为true时)还是同步的(指定为false时)。默认是true。
replicatePuts=true | false – 当一个新元素增加到缓存中的时候是否要复制到其他的peers。默认是true。
replicateUpdates=true | false – 当一个已经在缓存中存在的元素被覆盖时是否要进行复制。默认是true。
replicateUpdatesViaCopy=true | false – 当一个元素被拷贝到其他的cache中时是否进行复制(指定为true时为复制)。默认是true。
replicateRemovals= true | false – 当元素移除的时候是否进行复制。默认是true。
附上实例:
<?xml version=“1.0” encoding= “UTF-8”?>
xsi:noNamespaceSchemaLocation=“http://ehcache.org/ehcache.xsd”
updateCheck= “false” monitoring =“autodetect” dynamicConfig=“true” >
<cacheManagerPeerProviderFactory
class= “net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory”
properties= “peerDiscovery=automatic, multicastGroupAddress=230.0.0.1, multicastGroupPort=4446, timeToLive=1”/>
<cacheManagerPeerListenerFactory
class= “net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory”
properties= “port=40001, socketTimeoutMillis=2000”/>
<cache name =“sessionCache”
maxElementsInMemory= “10000”
eternal= “false”
overflowToDisk= “true”
timeToIdleSeconds= “3600”
memoryStoreEvictionPolicy= “LFU”>
<cacheEventListenerFactory
class= “net.sf.ehcache.distribution.RMICacheReplicatorFactory”
properties= “replicateAsynchronously=true,
replicatePuts=true,
replicateUpdates=true,
replicateUpdatesViaCopy=true,
replicateRemovals=true” />
</cache >
转载至: http://www.yyjjssnn.cn/articles/715.html © www.yyjjssnn.cn