EHCache–分布式缓存集群环境配置


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:

<cacheManagerPeerProviderFactory 
	class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory" 
  	properties="peerDiscovery=manual,
		    rmiUrls=//192.168.2.23:40001/userCache|//192.168.2.23:40001/resourceCache" />



b. 配置CacheManagerPeerListener:

<cacheManagerPeerListenerFactory 
	class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory" 
	properties="hostName=192.168.2.154, 
		    port=40001,
		    socketTimeoutMillis=2000" />

配置中server1监听本机40001端口。

c. 在每一个cache中添加cacheEventListener,例子如下:

<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,用于在初始化缓存,以及自动设置。
 
参考另外一篇学习笔记http://wozailongyou.javaeye.com/blog/230252,也有集群的说明



2. JGroups方式:



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

a. 配置PeerProvider,使用tcp的方式,例子如下:
<cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.jgroups.JGroupsCacheManagerPeerProviderFactory"
        properties="connect=TCP(start_port=7800):
        TCPPING(<strong>initial_hosts</strong>=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:

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



3,其他ehcache集群参考资料

EHCache 是一个纯java的,在Hibernate2.1充当可插入的在进程中的缓存。 
它具有以下特点:最小的依赖性,全面的文特性:快速,简单,丰富的文档和测试用例。 
EHCache从1.2版本开始支持集群。(支持分布式,同步缓存)

EHCache的使用很简单:可以去http://www.blogjava.net/zyl/archive/2007/02/28/101208.html看看。
下面主要说下EHCache的集群:
分布式同步缓存要让这边的cache知道对方的cache,叫做Peer Discovery(成员发现) 

EHCache实现成员发现的方式有两种: 

第一,自动查找 
自动的发现方式用 TCP 广播机制来确定和维持一个广播组。它只需要一个简单的配置可以自动的在组中添加和移除成员。在集群中也不需要什么优化服务器的知识,这是默认推荐的。 
成员每秒向群组发送一个“心跳”。如果一个成员 5 秒种都没有发出信号它将被群组移除。如果一个新的成员发送了一个“心跳”它将被添加进群组。 
任何一个用这个配置安装了复制功能的 cache 都将被其他的成员发现并标识为可用状态。 
要设置自动的成员发现,需要指定ehcache配置文件中cacheManagerPeerProviderFactory元素的 properties属性,就像下面这样:  
peerDiscovery=automatic multicastGroupAddress=multicast address | multicast host name 
multicastGroupPort=port timeToLive=0-255 (timeToLive属性详见常见问题部分的描述)
 
示例 :
假设你在集群中有两台服务器。你希望同步 sampleCache1 和 sampleCache2。每台独立的服务器都要有这样的配置: 
配置 server1和 server2
<cacheManagerPeerProviderFactory 
	class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory" 
	properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.1, 
		multicastGroupPort=4446, timeToLive=32"/>

第二,手动查找 
进行手动成员配置要知道每个监听器的 IP 地址和端口。成员不能在运行时动态地添加和移除。在技术上很难使用广播的情况下就可以手动成员发现,例如在集群的服务器之间有一个不能传送广播报文的路由器。你也可以用手动成员发现进行单向的数据复制,只让server2 知道 server1而 server1 不知道 server2。 
配置手动成员发现,需要指定 ehcache 配置文件中 cacheManagerPeerProviderFactory 的properties属性,像下面这样: 
peerDiscovery=manual rmiUrls=//server:port/cacheName, ... rmiUrls 配置的是服务器 cache peers 的列表。注意不要重复配置。 


示例 :
假设你在集群中有两台服务器。你要同步 sampleCache1 和 sampleCache2。下面是每个 
服务器需要的配置: 
配置 server1
<cacheManagerPeerProviderFactory 
	class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory" 
	properties="peerDiscovery=manual, 
		rmiUrls=//server2:40001/sampleCache11|//server2:40001/sampleCache12"/>

配置 server2
<cacheManagerPeerProviderFactory 
	class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory" 
	properties="peerDiscovery=manual, 
		rmiUrls=//server1:40001/sampleCache11|//server1:40001/sampleCache12"/>


最终完整的配置:
<?xml version="1.0" encoding="UTF-8"?>
<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=//10.129.0.203:40000/UserCache |//10.129.0.203:40000/ReturnCache"/>
	<cacheManagerPeerListenerFactory 
		class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory" 
		properties="hostName=10.129.0.202,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>


属性解释: 
必须属性: 
        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,用于在初始化缓存,以及自动设置。
 

集群的作用有两方面吧,第一,负载分流。第二,安全性。
       如果一个web应用的访问量不是很多的话,集群和非集群的效果相差不了多少,甚至有的时候集群会消耗更多的资源,导致垃圾回收频繁,内存占用率高等的现象。当然,如果访问量大,那当然是集群的效果会好很多。 
         那在访问量小的时候是不是就不用做集群呢,答案是否定的。集群的另外一个好处就是数据和状态的安全性,EHCache会自动同步集群中服务器的缓存,当一台服务器挂掉后,另外的服务器可以马上接上来,防止用户状态数据丢失,所以,对安全性的保障是十分有效的。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值