Opening
ehcache提供三种网络连接策略来实现集群,rmi,jgroup还有jms。同时ehcache可以可以实现多播的方式实现集群,也可以手动指定集群主机序列实现集群。Ehcache支持的分布式缓存支持有三种RMI,JGroups,JMS,这里介绍下MRI和JGrpups两种方式,Ehcache使用版本为1.5.0. 环境为两台机器 server1 ip:192.168.2.154,server2 ip:192.168.2.23
RMI mode
RMI Mode configuration Essentials (below are the configuration on the Server1, server2 only need to convert the IP)
a. 配置PeerProvider:Xml代码
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"properties="peerDiscovery=manual,rmiUrls=//192.168.2.23:40001/userCache|//192.168.2.23:40001/resourceCache" />
配置中通过手动方式同步sever2中的userCache和resourceCache。b. 配置CacheManagerPeerListener:Xml代码
class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"properties="hostName=192.168.2.154, port=40001,socketTimeoutMillis=2000" />
配置中server1监听本机40001端口。c. 在每一个cache中添加cacheEventListener,例子如下:Xml代码
<cache name = "Usercache" maxelementsinmemory = "10000" eternal = "true" overflowtodisk = "true" timetoidleseconds = "0" timetoliveseconds = "0" diskpersistent= "false" diskexpirythreadintervalseconds = "+" > <cacheeventlistenerfactory class = " Net.sf.ehcache.distribution.RMICacheReplicatorFactory " properties =< Span class= "Hljs-value" "Replicateasynchronously=true, Replicateputs=true, Replicateupdates=true, Replicateupdatesviacopy= false, Replicateremovals= true "/>
属性解释:必须属性: name:设置缓存的名称,用于标志缓存,惟一 maxElementsInMemory:在内存中最大的对象数量 maxElementsOnDisk:在DiskStore中的最大对象数量,如为0,则没有限制 eternal:设置元素是否永久的,如果为永久,则timeout忽略 overflowToDisk:是否当memory中的数量达到限制后,保存到Disk可选的属性: timeToIdleSeconds:设置元素过期前的空闲时间 timeToLiveSeconds:设置元素过期前的活动时间 diskPersistent:是否disk store在虚拟机启动时持久化。默认为false diskExpiryThreadIntervalSeconds:运行disk终结线程的时间,默认为120秒 memoryStoreEvictionPolicy:策略关于Eviction缓存子元素: cacheEventListenerFactory:注册相应的的缓存监听类,用于处理缓存事件,如put,remove,update,和expirebootstrapCacheLoaderFactory:指定相应的BootstrapCacheLoader,用于在初始化缓存,以及自动设置。
JGroups Way
a. 配置PeerProvider,使用tcp的方式,例子如下:Xml代码
<cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.jgroups.JGroupsCacheManagerPeerProviderFactory"properties="connect=TCP(start_port=7800):TCPPING(initial_hosts=192.168.2.154[7800],192.168.2.23[7800];port_range=10;timeout=3000;num_initial_members=3;up_thread=true;down_thread=true):VERIFY_SUSPECT(timeout=1500;down_thread=false;up_thread=false):pbcast.NAKACK(down_thread=true;up_thread=true;gc_lag=100;retransmit_timeout=3000):pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;shun=false;print_local_addr=false;down_thread=true;up_thread=true)"propertySeparator="::" />
b.为每个cache添加cacheEventListener:Xml代码
<cache name = "Usercache" maxelementsinmemory = "10000" eternal = "true" overflowtodisk = "true" timetoidleseconds = "0" timetoliveseconds = "0" diskpersistent = diskexpirythreadintervalseconds = "+" > <cacheeventlistenerfactory class=" Net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory "Properties=" Replicateasynchronously=true, Replicateputs=true,replicateupdates=true, Replicateupdatesviacopy=false, Replicateremovals=true "/></Cache>
JGroup方式配置的两个server上的配置文件一样,若有多个server,在initial_hosts中将server ip加上即可。一个完整的ehcache.xml文件:Xml代码
<?xml version= "1.0" encoding= "UTF-8"?><ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi: noNamespaceSchemaLocation="Http://ehcache.sf.net/ehcache.xsd"><diskstore path="Java.io.tmpdir" /><cachemanagerpeerproviderfactory class = " Net.sf.ehcache.distribution.jgroups.JGroupsCacheManagerPeerProviderFactory " Properties = "connect=tcp (start_port=7800): Tcpping (initial_hosts=192.168.2.154[ 7800],192.168.2.23[7800];p ort_range=10;timeout=3000;num_initial_members=3;up_thread=true;down_thread=true): Verify_suspect (timeout=1500;down_thread=false;up_thread=false):p bcast. Nakack (down_thread=true;up_thread=true;gc_lag=100;retransmit_timeout=3000):p bcast. GMS (join_timeout=5000;join_retry_timeout=2000;shun=false;print_local_addr=false;down_thread=true;up_thread= true) " propertyseparator =":: "/> <defaultcache Maxelementsinmemory = "10000" eternal = "true" overflowtodisk = "true" timetoidleseconds = "0" timetoliveseconds = "0" diskpersistent = diskexpirythreadintervalseconds = "+" > <cacheeventlistenerfactory class=" Net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory "Properties=" Replicateasynchronously=true, Replicateputs=true,replicateupdates=true, Replicateupdatesviacopy=false, Replicateremovals=true "/></defaultcache><cache name = "Velcrocache" maxelementsinmemory = "10000" eternal = "true" overflowtodisk = "true" timetoidleseconds = "0" timetoliveseconds = "0" diskpersistent = diskexpirythreadintervalseconds = "+" > <cacheeventlistenerfactory class=" Net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory "Properties=" Replicateasynchronously=true, Replicateputs=true,replicateupdates=true, Replicateupdatesviacopy=false, Replicateremovals=true "/></Cache><cache name = "Usercache" maxelementsinmemory = "10000" eternal = "true" overflowtodisk = "true" timetoidleseconds = "0" timetoliveseconds = "0" diskpersistent = diskexpirythreadintervalseconds = "+" > <cacheeventlistenerfactory class=" Net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory "Properties=" Replicateasynchronously=true, Replicateputs=true,replicateupdates=true, Replicateupdatesviacopy=false, Replicateremovals=true "/></Cache><cache name = "Resourcecache" maxelementsinmemory =" 10000 " eternal =" true " Span class= "Hljs-attribute" >overflowtodisk = "true" timetoidleseconds = "0" timetoliveseconds = "0" diskpersistent = diskexpirythreadintervalseconds = "+" > <cacheeventlistenerfactory class=" Net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory "Properties=" Replicateasynchronously=true, Replicateputs=true,replicateupdates=true, Replicateupdatesviacopy=false, Replicateremovals=true "/></Cache></ehcache>
End
The distributed cache support for Ehcache can be accomplished in either of these ways.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Ehcache Distributed Cache implementation