Ehcache学习(4)

6.    分布式缓存集群环境配置

6.1.   集群配置方式

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 代码

  <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" over flowToDisk= "true"

diskSpoolBufferSizeMB = "30" maxElementsOnDisk = "10000000"

diskPersistent = "false" diskExpiryThreadIntervalSecon ds= "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 代码

<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 );

}

}

}

猜你喜欢

转载自ligf06.iteye.com/blog/1710887