当前位置: 首页 > 工具软件 > Ecache > 使用案例 >

ecache配置rmi

戚翰飞
2023-12-01
ecache的集群支持多种方式,这里通过RMI实现。

测试方式:

本机,两个不同的进程(java两个不同的工程)


工程1的ehcache.xml



<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd">
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=manual,
rmiUrls=//localhost:40000/UserCache" />

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

<defaultCache maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000"
diskPersistent="false" diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<cacheEventListenerFactory
class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" />
</defaultCache>

<cache name="UserCache" maxElementsInMemory="1000" eternal="false"
timeToIdleSeconds="100000" timeToLiveSeconds="100000"
overflowToDisk="false">
<cacheEventListenerFactory
class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" />
</cache>
</ehcache>



rmiUrls=//localhost:40000/UserCache (ip+port+缓存名称)
表示本服务缓存数据改变的时候,通知其他服务也更新,可配置多个
properties="hostName=localhost,port=40001,socketTimeoutMillis=120000"
表示本服务监听的一个监听端口,当其他服务的缓存数据改变的时候,就通过其通知服务同步更新数据
工程1的java代码:


package tutorial;

import java.net.URL;
import java.util.List;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

public class UsingCacheCluster {

public static void main(String[] args) throws Exception {
URL url = UsingCacheCluster.class.getClassLoader().getResource(
"config/ehcache.xml");
CacheManager manager = new CacheManager(url);
//取得Cache
Cache cache = manager.getCache("UserCache");
int i=0;
while(true) {
//System.out.println("write:"+t);
Element element = new Element("Write"+i, "Write"+i++);
cache.put(element);
Thread.sleep(4500);
List list = cache.getKeys();
for (String str : list) {
System.out.print(str+" || ");
}
System.out.println();
}

//Element element1 = cache.get("key1");
//System.out.println(element1.getValue());
}

}



工程2的ehcache.xml配置
只需将工程1的ehcache.xml配置文件中,上面的列举说明的两个属性的端口调换


工程2的java代码:

package tutorial;

import java.net.URL;
import java.util.List;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

public class UsingCacheCluster {

public static void main(String[] args) throws Exception {
URL url = UsingCacheCluster.class.getClassLoader().getResource(
"config/ehcache.xml");
CacheManager manager = new CacheManager(url);
//取得Cache
Cache cache = manager.getCache("UserCache");

String t="";
int k = 100;
while(true) {
Thread.sleep(4500);
cache.put(new Element("read"+k,"read"+k++));
List<String> list = cache.getKeys();
for (String str : list) {
System.out.print(str+" || ");
}
System.out.println();
}
}

}
 类似资料: