ehcache集群配置
2011-06-30 17:24
204 查看
这篇我将探讨ehcache的集群配置,并写两个分布在不同逻辑机器上的程序去测试是不是达到了集群效果。我的目标就是得到踏踏实实的配置成功的感觉。
一.集群配置方式
ehcache提供三种网络连接策略来实现集群,rmi,jgroup还有jms。这里只说rmi方式。同时ehcache可以可以实现多播的方式实现集群。也可以手动指定集群主机序列实现集群,本例应用手动指定。
这里说点题外话,本来看着分发包中的原来的例子配置是一件不怎么难的事情,应该很容易就能实现。但是一开始,我是在我的linux主机上和我的主操作系统 windows上实现集群配置。结果反过来弄过去,都没有成功。然后在网上找一些别人的配置经验,竟然都是配置片段,没有完整的实例文件。结果配置半天没成功。但我怀疑是我的linux系统有些地方可能没有配置好,于是先不管他。有开启了我的另一个windows主机。然后把程序部署上去,竟然一次试验成功。高兴的同时,我得发句话“不要把代码片段称作实例,这很不负责任”。同时还存在一个问题,在linux下没有部署成功的原因有待查明。
具体说明:配置cacheManagerPeerListenerFactory是配宿主主机配置监听程序,来发现其他主机发来的同步请求
配置cacheManagerPeerProviderFactory是指定除自身之外的网络群体中其他提供同步的主机列表,用“|”分开不同的主机。
下面的例子的测试过程是:主机B缓存开启,并从名为UserCache的缓存中循环抓取键值为“key1”的元素,直到取到,才退出循环。主机A缓存启动,并在名为UserCache的缓存中放入键值为“key1”的元素。显然,如果主机B取到的元素,那么就证明同步成功,也就是集群成功。
所以在测试过程中先启动主机B的测试程序,在启动主机A的测试程序。
下面具体说配置文件以及测试程序:
1. 主机A的配置文件以及测试源代码
config/ehcache_cluster.xml
XML/HTML代码
<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=//192.168.1.254:40000/UserCache" />
<cacheManagerPeerListenerFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=192.168.1.126,port=40000,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>
tutorial/UsingCacheCluster
Java代码
package tutorial;
import java.net.URL;
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_cluster.xml");
CacheManager manager = new CacheManager(url);
//取得Cache
Cache cache = manager.getCache("UserCache");
Element element = new Element("key1", "value1");
cache.put(element);
Element element1 = cache.get("key1");
System.out.println(element1.getValue());
}
}
2. 主机B上的配置文件以及测试代码
config/ehcache_cluster.xml
XML/HTML代码
<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=//192.168.1.126:40000/UserCache" />
<cacheManagerPeerListenerFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=192.168.1.254,port=40000, 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>
tutorial/UsingCacheCluster
Java代码
package tutorial;
import java.net.URL;
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_cluster.xml");
CacheManager manager = new CacheManager(url);
//取得Cache
Cache cache = manager.getCache("UserCache");
while(true) {
Element e = cache.get("key1");
if(e != null) {
System.out.println(e.getValue());
break;
}
Thread.sleep(1000);
}
}
}
一.集群配置方式
ehcache提供三种网络连接策略来实现集群,rmi,jgroup还有jms。这里只说rmi方式。同时ehcache可以可以实现多播的方式实现集群。也可以手动指定集群主机序列实现集群,本例应用手动指定。
这里说点题外话,本来看着分发包中的原来的例子配置是一件不怎么难的事情,应该很容易就能实现。但是一开始,我是在我的linux主机上和我的主操作系统 windows上实现集群配置。结果反过来弄过去,都没有成功。然后在网上找一些别人的配置经验,竟然都是配置片段,没有完整的实例文件。结果配置半天没成功。但我怀疑是我的linux系统有些地方可能没有配置好,于是先不管他。有开启了我的另一个windows主机。然后把程序部署上去,竟然一次试验成功。高兴的同时,我得发句话“不要把代码片段称作实例,这很不负责任”。同时还存在一个问题,在linux下没有部署成功的原因有待查明。
具体说明:配置cacheManagerPeerListenerFactory是配宿主主机配置监听程序,来发现其他主机发来的同步请求
配置cacheManagerPeerProviderFactory是指定除自身之外的网络群体中其他提供同步的主机列表,用“|”分开不同的主机。
下面的例子的测试过程是:主机B缓存开启,并从名为UserCache的缓存中循环抓取键值为“key1”的元素,直到取到,才退出循环。主机A缓存启动,并在名为UserCache的缓存中放入键值为“key1”的元素。显然,如果主机B取到的元素,那么就证明同步成功,也就是集群成功。
所以在测试过程中先启动主机B的测试程序,在启动主机A的测试程序。
下面具体说配置文件以及测试程序:
1. 主机A的配置文件以及测试源代码
config/ehcache_cluster.xml
XML/HTML代码
<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=//192.168.1.254:40000/UserCache" />
<cacheManagerPeerListenerFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=192.168.1.126,port=40000,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>
tutorial/UsingCacheCluster
Java代码
package tutorial;
import java.net.URL;
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_cluster.xml");
CacheManager manager = new CacheManager(url);
//取得Cache
Cache cache = manager.getCache("UserCache");
Element element = new Element("key1", "value1");
cache.put(element);
Element element1 = cache.get("key1");
System.out.println(element1.getValue());
}
}
2. 主机B上的配置文件以及测试代码
config/ehcache_cluster.xml
XML/HTML代码
<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=//192.168.1.126:40000/UserCache" />
<cacheManagerPeerListenerFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=192.168.1.254,port=40000, 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>
tutorial/UsingCacheCluster
Java代码
package tutorial;
import java.net.URL;
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_cluster.xml");
CacheManager manager = new CacheManager(url);
//取得Cache
Cache cache = manager.getCache("UserCache");
while(true) {
Element e = cache.get("key1");
if(e != null) {
System.out.println(e.getValue());
break;
}
Thread.sleep(1000);
}
}
}
相关文章推荐
- EhCache 集群 配置(RMI方式)
- 【集群】EHCache分布式缓存集群环境配置
- ehcache jgroups 集群配置
- EHCache分布式缓存集群环境配置
- ehcache集群配置
- EHCache分布式缓存集群环境配置
- HibernateEhcacheJgroups - 基于Annotation的集群缓存配置
- ehcache之RMI集群配置
- EHCache入门系列(四)–分布式缓存集群环境配置
- HibernateEhcacheJgroups - 基于Annotation的集群缓存配置
- EHCache入门系列(四)–分布式缓存集群环境配置
- EHCache分布式缓存集群环境配置
- Ehcache与terracotta集群配置
- EHCache分布式缓存集群环境配置
- ehcache集群缓存配置说明
- EHCache分布式缓存集群环境配置
- ehcache集群详解二(集群的最小配置)
- ehCache集群JGroups方式配置
- Ehcache集群环境配置
- EHCache分布式缓存集群环境配置