java 缓存 集群_EhCache 分布式缓存/缓存集群

本文介绍了Java集群中Ehcache缓存同步的配置方法。首先在ehcache.xml中配置PeerDiscovery成员发现对象,分别对三台服务器进行手动查找cache的PeerProvider成员发现配置;接着配置缓存和缓存同步监听,在每台服务器的ehcache.xml文件中增加相关配置;最后给出了server1完整的ehcache.xml配置。

A、 在ehcache.xml中配置PeerDiscovery成员发现对象

Server1配置,配置本地hostName、port是400001,分别监听192.168.8.32:400002的mobileCache和192.168.5.231:400003 的mobileCache。注意这里的mobileCache是缓存的名称,分别对应着server2、server3的cache的配置。

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"

/>

以上注意cacheManagerPeerProviderFactory元素出现的位置在diskStore下

同样在你的另外2台服务器上增加配置

Server2,配置本地host,port为400002,分别同步192.168.8.9:400001的mobileCache和192.168.5.231:400003的mobileCache

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"

/>

Server3,配置本地host,port为400003,分别同步192.168.8.9:400001的mobileCache缓存和192.168.8.32:400002的mobileCache缓存

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"

/>

这样就在三台不同的服务器上配置了手动查找cache的PeerProvider成员发现的配置了。 值得注意的是你在配置rmiUrls的时候要特别注意url不能重复出现,并且端口、地址都是对的。

如果指定,hostname将使用InetAddress.getLocalHost().getHostAddress()来得到。

警告:不要将localhost配置为本地地址127.0.0.1,因为它在网络中不可见将会导致不能从远程服务器接收信息从而不能复制。在同一台机器上有多个CacheManager的时候,你应该只用localhost来配置。

B、 下面配置缓存和缓存同步监听,需要在每台服务器中的ehcache.xml文件中增加cache配置和cacheEventListenerFactory、cacheLoaderFactory的配置

maxElementsInMemory="10000"

eternal="false"

overflowToDisk="true"

timeToIdleSeconds="1800"

timeToLiveSeconds="3600"

memoryStoreEvictionPolicy="LFU">

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

properties="replicateAsynchronously=true,

replicatePuts=true,

replicateUpdates=true,

replicateUpdatesViaCopy=false,

replicateRemovals=true "/>

C、 这样就完成了3台服务器的配置,下面给出server1的完整的ehcache.xml的配置

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"

/>

maxElementsInMemory="10000"

eternal="false"

overflowToDisk="true"

timeToIdleSeconds="1800"

timeToLiveSeconds="3600"

memoryStoreEvictionPolicy="LFU">

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

properties="replicateAsynchronously=true,

replicatePuts=true,

replicateUpdates=true,

replicateUpdatesViaCopy=false,

replicateRemovals=true "/>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值