EHCache入门系列(四)–分布式缓存集群环境配置
2015-07-09 22:47
615 查看
ehcache提供三种网络连接策略来实现集群,rmi,jgroup还有jms。同时ehcache可以可以实现多播的方式实现集群,也可以手动指定集群主机序列实现集群。
Ehcache支持的分布式缓存支持有三种RMI,JGroups,JMS,这里介绍下MRI和JGrpups两种方式,Ehcache使用版本为1.5.0,关于ehcache的其他信息请参考http://ehcache.sourceforge.net/EhcacheUserGuide.html,
关于jgroups的信息请参考http://www.jgroups.org/manual/html_single/index.html。
环境为两台机器server1ip:192.168.2.154,server2ip:192.168.2.23
1.RMI方式:
rmi的方式配置要点(下面均是server1上的配置,server2上的只需要把ip兑换即可)
a.配置PeerProvider:
Xml代码
配置中通过手动方式同步sever2中的userCache和resourceCache。
b.配置CacheManagerPeerListener:
Xml代码
配置中server1监听本机40001端口。
c.在每一个cache中添加cacheEventListener,例子如下:
Xml代码
属性解释:
必须属性:
name:设置缓存的名称,用于标志缓存,惟一
maxElementsInMemory:在内存中最大的对象数量
maxElementsOnDisk:在DiskStore中的最大对象数量,如为0,则没有限制
eternal:设置元素是否永久的,如果为永久,则timeout忽略
overflowToDisk:是否当memory中的数量达到限制后,保存到Disk
可选的属性:
timeToIdleSeconds:设置元素过期前的空闲时间
timeToLiveSeconds:设置元素过期前的活动时间
diskPersistent:是否diskstore在虚拟机启动时持久化。默认为false
diskExpiryThreadIntervalSeconds:运行disk终结线程的时间,默认为120秒
memoryStoreEvictionPolicy:策略关于Eviction
缓存子元素:
cacheEventListenerFactory:注册相应的的缓存监听类,用于处理缓存事件,如put,remove,update,和expire
bootstrapCacheLoaderFactory:指定相应的BootstrapCacheLoader,用于在初始化缓存,以及自动设置。
参考另外一篇学习笔记http://wozailongyou.javaeye.com/blog/230252,也有集群的说明
2.JGroups方式:
ehcache1.5.0之后版本支持的一种方式,配置起来比较简单,要点:
a.配置PeerProvider,使用tcp的方式,例子如下:
Xml代码
b.为每个cache添加cacheEventListener:
Xml代码
JGroup方式配置的两个server上的配置文件一样,若有多个server,在initial_hosts中将serverip加上即可。
一个完整的ehcache.xml文件:
Xml代码
Ehcache支持的分布式缓存支持有三种RMI,JGroups,JMS,这里介绍下MRI和JGrpups两种方式,Ehcache使用版本为1.5.0,关于ehcache的其他信息请参考
关于jgroups的信息请参考http://www.jgroups.org/manual/html_single/index.html。
环境为两台机器server1ip:192.168.2.154,server2ip:192.168.2.23
1.RMI方式:
rmi的方式配置要点(下面均是server1上的配置,server2上的只需要把ip兑换即可)
a.配置PeerProvider:
Xml代码
<cacheManagerPeerProviderFactoryclass="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代码
<cacheManagerPeerListenerFactoryclass="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=192.168.2.154,port=40001,socketTimeoutMillis=2000"/>
配置中server1监听本机40001端口。
c.在每一个cache中添加cacheEventListener,例子如下:
Xml代码
<cachename="userCache"maxElementsInMemory="10000"eternal="true"overflowToDisk="true"timeToIdleSeconds="0"timeToLiveSeconds="0"diskPersistent="false"diskExpiryThreadIntervalSeconds="120">
<cacheEventListenerFactoryclass="net.sf.ehcache.distribution.RMICacheReplicatorFactory"properties="replicateAsynchronously=true,replicatePuts=true,replicateUpdates=true,replicateUpdatesViaCopy=false,replicateRemovals=true"/>
</cache>
属性解释:
必须属性:
name:设置缓存的名称,用于标志缓存,惟一
maxElementsInMemory:在内存中最大的对象数量
maxElementsOnDisk:在DiskStore中的最大对象数量,如为0,则没有限制
eternal:设置元素是否永久的,如果为永久,则timeout忽略
overflowToDisk:是否当memory中的数量达到限制后,保存到Disk
可选的属性:
timeToIdleSeconds:设置元素过期前的空闲时间
timeToLiveSeconds:设置元素过期前的活动时间
diskPersistent:是否diskstore在虚拟机启动时持久化。默认为false
diskExpiryThreadIntervalSeconds:运行disk终结线程的时间,默认为120秒
memoryStoreEvictionPolicy:策略关于Eviction
缓存子元素:
cacheEventListenerFactory:注册相应的的缓存监听类,用于处理缓存事件,如put,remove,update,和expire
bootstrapCacheLoaderFactory:指定相应的BootstrapCacheLoader,用于在初始化缓存,以及自动设置。
参考另外一篇学习笔记
2.JGroups方式:
ehcache1.5.0之后版本支持的一种方式,配置起来比较简单,要点:
a.配置PeerProvider,使用tcp的方式,例子如下:
Xml代码
<cacheManagerPeerProviderFactoryclass="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代码
<cachename="userCache"maxElementsInMemory="10000"eternal="true"
overflowToDisk="true"timeToIdleSeconds="0"timeToLiveSeconds="0"
diskPersistent="false"diskExpiryThreadIntervalSeconds="120">
<cacheEventListenerFactoryclass="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
properties="replicateAsynchronously=true,replicatePuts=true,
replicateUpdates=true,replicateUpdatesViaCopy=false,replicateRemovals=true"/>
</cache>
JGroup方式配置的两个server上的配置文件一样,若有多个server,在initial_hosts中将serverip加上即可。
一个完整的ehcache.xml文件:
Xml代码
<?xmlversion="1.0"encoding="UTF-8"?>
<ehcachexmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.sf.net/ehcache.xsd">
<diskStorepath="java.io.tmpdir"/>
<cacheManagerPeerProviderFactoryclass="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="::"/>
<defaultCachemaxElementsInMemory="10000"eternal="true"
overflowToDisk="true"timeToIdleSeconds="0"timeToLiveSeconds="0"
diskPersistent="false"diskExpiryThreadIntervalSeconds="120">
<cacheEventListenerFactoryclass="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
properties="replicateAsynchronously=true,replicatePuts=true,
replicateUpdates=true,replicateUpdatesViaCopy=false,replicateRemovals=true"/>
</defaultCache>
<cachename="velcroCache"maxElementsInMemory="10000"eternal="true"
overflowToDisk="true"timeToIdleSeconds="0"timeToLiveSeconds="0"
diskPersistent="false"diskExpiryThreadIntervalSeconds="120">
<cacheEventListenerFactoryclass="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
properties="replicateAsynchronously=true,replicatePuts=true,
replicateUpdates=true,replicateUpdatesViaCopy=false,replicateRemovals=true"/>
</cache>
<cachename="userCache"maxElementsInMemory="10000"eternal="true"
overflowToDisk="true"timeToIdleSeconds="0"timeToLiveSeconds="0"
diskPersistent="false"diskExpiryThreadIntervalSeconds="120">
<cacheEventListenerFactoryclass="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
properties="replicateAsynchronously=true,replicatePuts=true,
replicateUpdates=true,replicateUpdatesViaCopy=false,replicateRemovals=true"/>
</cache>
<cachename="resourceCache"maxElementsInMemory="10000"
eternal="true"overflowToDisk="true"timeToIdleSeconds="0"
timeToLiveSeconds="0"diskPersistent="false"
diskExpiryThreadIntervalSeconds="120">
<cacheEventListenerFactoryclass="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
properties="replicateAsynchronously=true,replicatePuts=true,
replicateUpdates=true,replicateUpdatesViaCopy=false,replicateRemovals=true"/>
</cache>
</ehcache>
相关文章推荐
- 奔跑中的2015:Linux运维学习八大禁忌
- Socket模拟客户端与服务器
- poj 2192 Zipper
- SpringMVC
- EHCache入门系列(三)–在Hibernate 中运用EHCache
- 我们的java基础到底有多差 一个视频引发的感想
- 我们的java基础到底有多差 一个视频引发的感想
- 南阳oj 题目113 字符串替换
- UIView复习day1
- 【再认真理解】1003. Emergency (25) Dijastra最优路径算法修改版求解
- leetcode 101 Symmetric Tree
- NFA到DFA的转换
- 计算机视觉领域的一些牛人博客,超有实力的研究机构等的网站链接
- leetcode 101 Symmetric Tree
- JS中的数据类型(见《Jquery实战<附录>》)
- Two Sum (LeetCode)
- jsp如何获取servlet的值
- java反射调用String[] wrong number of arguments异常解决
- 注解和XML的使用选择
- Android 高通平台Camera录制--MPEG4Writer.cpp 简单跟读