Ehcache 分布式缓存

自从Ehcache 到了1.2+的版本,就支持分布式缓存了。我们考虑到Spring + Hibernate的结构 ,ehcache的对这几个框架的支持较好,就一直采用这个缓存方案。
地址:http://ehcache.sourceforge.net/

先介绍没有分布式缓存需求的配置:
1.先下载ehcache的jar包。download
最新版本 11 June 2007: ehcache-1.3 released。
解压后,有几个文件:
ehcache-1.3.0.jar:需要将它放置到WEB-INF/lib下
ehcache-1.3.0-remote-debugger.jar:不要发布到你的工程中,是用来调试和监控你的cache状况的
ehcache-1.3.0-sources.jar:源代码
ehcache.xml :重要的配置文件,需要复制到classpath下 。
2.ehcach.xml配置文件主要参数的解释,其实文件里有详细的英文注释


//DiskStore 配置,cache文件的存放目录 ,主要的值有
*user.home - 用户主目录
* user.dir - 用户当前的工作目录
* java.io.tmpdir - Default temp file path默认的temp文件目录

//强制默认的cache配置

<defaultCache 
            
maxElementsInMemory="10000"
            
eternal="false"
            
timeToIdleSeconds="120"
            
timeToLiveSeconds="120"
            
overflowToDisk="true"
            
diskSpoolBufferSizeMB="30"
            
maxElementsOnDisk="10000000"
            
diskPersistent="false"
            
diskExpiryThreadIntervalSeconds="120"
            
memoryStoreEvictionPolicy="LRU"
            
/>

必须属性:
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,和expire
bootstrapCacheLoaderFactory:指定相应的BootstrapCacheLoader,用于在初始化缓存,以及自动设置。

3.分布式缓存配置方法 是基于RMI方式的
在cache.xml(ehcache的配置文件)中加入

<cacheManagerPeerProviderFactory
        
class="net.sf.ehcache.distribution
.RMICacheManagerPeerProviderFactory
"
        
properties="peerDiscovery=automatic, 
multicastGroupAddress=230.0.0.1, multicastGroupPort=4446
"/> 
<cacheManagerPeerListenerFactory
        
class="net.sf.ehcache.distribution.
RMICacheManagerPeerListenerFactory
"/>

//自动查找局域网中分布式的peer 。也可以手工指定节点的地址的,如:
peerDiscovery=manual,rmiUrls=//server1:40000/sampleCache1

另外在每个cache属性中加入

<cacheEventListenerFactory 
class="net.sf.ehcache.distribution.
RMICacheReplicatorFactory
"/>

例如:

1. <cache name="a"

2.      maxElementsInMemory="10000"

3.      eternal="true"

4.      overflowToDisk="true">

5.      <cacheEventListenerFactory

6. class="net.sf.ehcache.distribution.

7. RMICacheReplicatorFactory"/>

8. </cache>

 

 

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。

环境为两台机器 server1 ip:192.168.2.154,server2 ip:192.168.2.23

1. RMI方式:

rmi的方式配置要点(下面均是server1上的配置,server2上的只需要把ip兑换即可)

a. 配置PeerProvider

Xml代码

 

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

 

<cacheManagerPeerListenerFactory 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="120">
<cacheEventListenerFactory class="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:是否disk store在虚拟机启动时持久化。默认为false

   diskExpiryThreadIntervalSeconds:运行disk终结线程的时间,默认为120

       memoryStoreEvictionPolicy:策略关于Eviction

缓存子元素:

    cacheEventListenerFactory:注册相应的的缓存监听类,用于处理缓存事件,如put,remove,update,expire

    bootstrapCacheLoaderFactory:指定相应的BootstrapCacheLoader,用于在初始化缓存,以及自动设置。

 

 

2. JGroups方式:

ehcache 1.5.0之后版本支持的一种方式,配置起来比较简单,要点:

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="false" diskExpiryThreadIntervalSeconds="120">
<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代码

 

<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="::" />
 
<defaultCache maxElementsInMemory="10000" eternal="true"
overflowToDisk="true" timeToIdleSeconds="0" timeToLiveSeconds="0"
diskPersistent="false" diskExpiryThreadIntervalSeconds="120">
<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="false" diskExpiryThreadIntervalSeconds="120">
<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="false" diskExpiryThreadIntervalSeconds="120">
<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" overflowToDisk="true" timeToIdleSeconds="0"
timeToLiveSeconds="0" diskPersistent="false"
diskExpiryThreadIntervalSeconds="120">
<cacheEventListenerFactory class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
properties="replicateAsynchronously=true, replicatePuts=true,
replicateUpdates=true, replicateUpdatesViaCopy=false, replicateRemovals=true"/>
</cache>
</ehcache>

ehcache分布式缓存不是很理想,用memcached做一级缓存会更加地高效和易用。

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值