A、在ehcache.xml中配置PeerDiscovery成员发现对象
Server1配置,配置本地hostName、port是400001,分别监听192.168.8.32:400002的mobileCache和192.168.5.231:400003的mobileCache。注意这里的mobileCache是缓存的名称,分别对应着server2、server3的cache的配置。
[code]
[code]<?xmlversion="1.0"encoding="gbk"?>
<ehcachexmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="ehcache.xsd">
<diskStorepath="java.io.tmpdir"/>
<!--
集群多台服务器中的缓存,这里是要同步一些服务器的缓存
server1hostName:192.168.8.9port:400001cacheName:mobileCache
server2hostName:192.168.8.32port:400002cacheName:mobileCache
server3hostName:192.168.8.231port:400003cacheName:mobileCache
注意:每台要同步缓存的服务器的RMI通信socket端口都不一样,在配置的时候注意设置
-->
<!--server1的cacheManagerPeerProviderFactory配置-->
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="hostName=localhost,
port=400001,
socketTimeoutMillis=2000,
peerDiscovery=manual,
rmiUrls=//192.168.8.32:400002/mobileCache|//192.168.5.231:400003/mobileCache"
/>
</ehcache>
[/code]
[/code]
以上注意cacheManagerPeerProviderFactory元素出现的位置在diskStore下
同样在你的另外2台服务器上增加配置
Server2,配置本地host,port为400002,分别同步192.168.8.9:400001的mobileCache和192.168.5.231:400003的mobileCache
[code]
[code]<!--server2的cacheManagerPeerProviderFactory配置-->
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="hostName=localhost,
port=400002,
socketTimeoutMillis=2000,
peerDiscovery=manual,
rmiUrls=//192.168.8.9:400001/mobileCache|//192.168.5.231:400003/mobileCache"
/>
[/code]
[/code]
Server3,配置本地host,port为400003,分别同步192.168.8.9:400001的mobileCache缓存和192.168.8.32:400002的mobileCache缓存
[code]
[code]<!--server3的cacheManagerPeerProviderFactory配置-->
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="hostName=localhost,
port=400003,
socketTimeoutMillis=2000,
peerDiscovery=manual,
rmiUrls=//192.168.8.9:400001/mobileCache|//192.168.8.32:400002/mobileCache"
/>
[/code]
[/code]
这样就在三台不同的服务器上配置了手动查找cache的PeerProvider成员发现的配置了。值得注意的是你在配置rmiUrls的时候要特别注意url不能重复出现,并且端口、地址都是对的。
如果指定,hostname将使用InetAddress.getLocalHost().getHostAddress()来得到。
警告:不要将localhost配置为本地地址127.0.0.1,因为它在网络中不可见将会导致不能从远程服务器接收信息从而不能复制。在同一台机器上有多个CacheManager的时候,你应该只用localhost来配置。
B、下面配置缓存和缓存同步监听,需要在每台服务器中的ehcache.xml文件中增加cache配置和cacheEventListenerFactory、cacheLoaderFactory的配置
[code]
[code]<defaultCachemaxElementsInMemory="10000"eternal="false"timeToIdleSeconds="30"timeToLiveSeconds="30"overflowToDisk="false"/>
<!--
配置自定义缓存
maxElementsInMemory:缓存中允许创建的最大对象数
eternal:缓存中对象是否为永久的,如果是,超时设置将被忽略,对象从不过期。
timeToIdleSeconds:缓存数据空闲的最大时间,也就是说如果有一个缓存有多久没有被访问就会被销毁,如果该值是0就意味着元素可以停顿无穷长的时间。
timeToLiveSeconds:缓存数据存活的时间,缓存对象最大的的存活时间,超过这个时间就会被销毁,这只能在元素不是永久驻留时有效,如果该值是0就意味着元素可以停顿无穷长的时间。
overflowToDisk:内存不足时,是否启用磁盘缓存。
memoryStoreEvictionPolicy:缓存满了之后的淘汰算法。
每一个小时更新一次缓存(1小时过期)
-->
<cachename="mobileCache"
maxElementsInMemory="10000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="1800"
timeToLiveSeconds="3600"
memoryStoreEvictionPolicy="LFU">
<!--
RMI缓存分布同步查找class使用net.sf.ehcache.distribution.RMICacheReplicatorFactory
这个工厂支持以下属性:
replicatePuts=true|false–当一个新元素增加到缓存中的时候是否要复制到其他的peers。默认是true。
replicateUpdates=true|false–当一个已经在缓存中存在的元素被覆盖时是否要进行复制。默认是true。
replicateRemovals=true|false–当元素移除的时候是否进行复制。默认是true。
replicateAsynchronously=true|false–复制方式是异步的指定为true时,还是同步的,指定为false时。默认是true。
replicatePutsViaCopy=true|false–当一个新增元素被拷贝到其他的cache中时是否进行复制指定为true时为复制,默认是true。
replicateUpdatesViaCopy=true|false–当一个元素被拷贝到其他的cache中时是否进行复制指定为true时为复制,默认是true。
asynchronousReplicationIntervalMillis=1000
-->
<!--监听RMI同步缓存对象配置注册相应的的缓存监听类,用于处理缓存事件,如put,remove,update,和expire-->
<cacheEventListenerFactory
class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=true,
replicatePuts=true,
replicateUpdates=true,
replicateUpdatesViaCopy=false,
replicateRemovals=true"/>
<!--用于在初始化缓存,以及自动设置-->
<bootstrapCacheLoaderFactoryclass="net.sf.ehcache.bootstrap.BootstrapCacheLoaderFactory"/>
</cache>
[/code]
[/code]
C、这样就完成了3台服务器的配置,下面给出server1的完整的ehcache.xml的配置
[code]
[code]<?xmlversion="1.0"encoding="gbk"?>
<ehcachexmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="ehcache.xsd">
<diskStorepath="java.io.tmpdir"/>
<!--
集群多台服务器中的缓存,这里是要同步一些服务器的缓存
server1hostName:192.168.8.9port:400001cacheName:mobileCache
server2hostName:192.168.8.32port:400002cacheName:mobileCache
server3hostName:192.168.8.231port:400003cacheName:mobileCache
注意每台要同步缓存的服务器的RMI通信socket端口都不一样,在配置的时候注意设置
-->
<!--server1的cacheManagerPeerProviderFactory配置-->
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="hostName=localhost,
port=400001,
socketTimeoutMillis=2000,
peerDiscovery=manual,
rmiUrls=//192.168.8.32:400002/mobileCache|//192.168.5.231:400003/mobileCache"
/>
<defaultCachemaxElementsInMemory="10000"eternal="false"timeToIdleSeconds="30"timeToLiveSeconds="30"overflowToDisk="false"/>
<!--
配置自定义缓存
maxElementsInMemory:缓存中允许创建的最大对象数
eternal:缓存中对象是否为永久的,如果是,超时设置将被忽略,对象从不过期。
timeToIdleSeconds:缓存数据空闲的最大时间,也就是说如果有一个缓存有多久没有被访问就会被销毁,
如果该值是0就意味着元素可以停顿无穷长的时间。
timeToLiveSeconds:缓存数据存活的时间,缓存对象最大的的存活时间,超过这个时间就会被销毁,
这只能在元素不是永久驻留时有效,如果该值是0就意味着元素可以停顿无穷长的时间。
overflowToDisk:内存不足时,是否启用磁盘缓存。
memoryStoreEvictionPolicy:缓存满了之后的淘汰算法。
每一个小时更新一次缓存(1小时过期)
-->
<cachename="mobileCache"
maxElementsInMemory="10000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="1800"
timeToLiveSeconds="3600"
memoryStoreEvictionPolicy="LFU">
<!--
RMI缓存分布同步查找class使用net.sf.ehcache.distribution.RMICacheReplicatorFactory
这个工厂支持以下属性:
replicatePuts=true|false–当一个新元素增加到缓存中的时候是否要复制到其他的peers。默认是true。
replicateUpdates=true|false–当一个已经在缓存中存在的元素被覆盖时是否要进行复制。默认是true。
replicateRemovals=true|false–当元素移除的时候是否进行复制。默认是true。
replicateAsynchronously=true|false–复制方式是异步的指定为true时,还是同步的,指定为false时。默认是true。
replicatePutsViaCopy=true|false–当一个新增元素被拷贝到其他的cache中时是否进行复制指定为true时为复制,默认是true。
replicateUpdatesViaCopy=true|false–当一个元素被拷贝到其他的cache中时是否进行复制指定为true时为复制,默认是true。
asynchronousReplicationIntervalMillis=1000
-->
<!--监听RMI同步缓存对象配置注册相应的的缓存监听类,用于处理缓存事件,如put,remove,update,和expire-->
<cacheEventListenerFactory
class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=true,
replicatePuts=true,
replicateUpdates=true,
replicateUpdatesViaCopy=false,
replicateRemovals=true"/>
<!--用于在初始化缓存,以及自动设置-->
<bootstrapCacheLoaderFactoryclass="net.sf.ehcache.bootstrap.BootstrapCacheLoaderFactory"/>
</cache>
</ehcache>
[/code]
[/code]
2、自动发现
自动发现配置和手动查找的方式有一点不同,其他的地方都基本是一样的。同样在ehcache.xml中增加配置,配置如下
[code]
[code]<!--
搜索某个网段上的缓存
timeToLive
0是限制在同一个服务器
1是限制在同一个子网
32是限制在同一个网站
64是限制在同一个region
128是限制在同一个大洲
255是不限制
-->
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=automatic,multicastGroupAddress=192.168.0.1,
multicastGroupPort=400004,timeToLive=32"
/>
[/code]
[/code]
其他的配置和手动查找方式的配置是一样的,这里就不再赘述了。关于ehcache的其他缓存配置方式这里将不再介绍,大家可以自己去研究。可以参考:
官方文档:http://www.ehcache.org/documentation/user-guide/cache-topologies#using-a-cache-server
ibmdeveloperworks文档:http://www.ibm.com/developerworks/cn/java/j-lo-ehcache/index.html
JGroups集群模式
EhCache从1.5.版本开始增加了JGroups的分布式集群模式。与RMI方式相比较,JGroups提供了一个非常灵活的协议栈、可靠的单播和多播消息传输,主要的缺点是配置复杂以及一些协议栈对第三方包的依赖。
JGroups也提供了基于TCP的单播(Unicast)和基于UDP的多播(Multicast),对应RMI的手工配置和自动发现。使用单播方式需要指定其它节点的主机地址和端口,下面是两个节点,并使用了单播方式的配置:
[code]<?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>
[/code]
[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理