`ehcache有几种方式实现集群:rmi、jgroup、jms,这里讲一下ehcache使用rmi的实现方式。 主要有三个配置: 1、cacheManagerPeerProviderFactory配置: <cacheManagerPeerProviderFactory class= “net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory” properties= “peerDiscovery=automatic, multicastGroupAddress=230.0.0.1, multicastGroupPort=4446, timeToLive=1”/> timeToLive的值指的是数据包可以传递的域或是范围。约定如下: 0是限制在同一个服务器 1是限制在同一个子网(默认) 32是限制在同一个网站 64是限制在同一个region 128是限制在同一个大洲 255是不限制
2、cacheManagerPeerListenerFactory配置: <cacheManagerPeerListenerFactory class= “net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory” properties= “hostName=192.168.1.101, port=40001, socketTimeoutMillis=2000”/> hostName说明:为本机IP,不指定时hostname将使用InetAddress.getLocalHost().getHostAddress()来得到。当在同一台机器上有多个CacheManager的时候,应该用localhost来配置。 3、cacheEventListenerFactory配置: <cacheEventListenerFactory class= “net.sf.ehcache.distribution.RMICacheReplicatorFactory” properties= “replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true, replicateUpdatesViaCopy=true, replicateRemovals=true” /> 可以使用默认配置: <cacheEventListenerFactory class=”net.sf.ehcache.distribution.RMICacheReplicatorFactory”/> 参数说明: replicateAsynchronously=true | false – 复制方式是异步的(指定为true时)还是同步的(指定为false时)。默认是true。 replicatePuts=true | false – 当一个新元素增加到缓存中的时候是否要复制到其他的peers。默认是true。 replicateUpdates=true | false – 当一个已经在缓存中存在的元素被覆盖时是否要进行复制。默认是true。 replicateUpdatesViaCopy=true | false – 当一个元素被拷贝到其他的cache中时是否进行复制(指定为true时为复制)。默认是true。 replicateRemovals= true | false – 当元素移除的时候是否进行复制。默认是true。 附上实例:
<?xml version=“1.0” encoding= “UTF-8”?>
<ehcache xmlns:xsi= “http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation=“http://ehcache.org/ehcache.xsd” updateCheck= “false” monitoring =“autodetect” dynamicConfig=“true” > <diskStore path =“java.io.tmpdir/ehcache” />
<cacheManagerPeerProviderFactory
class= “net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory”
properties= “peerDiscovery=automatic, multicastGroupAddress=230.0.0.1, multicastGroupPort=4446, timeToLive=1”/>
<cacheManagerPeerListenerFactory
class= “net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory”
properties= “port=40001, socketTimeoutMillis=2000”/>
<cache name =“sessionCache”
maxElementsInMemory= “10000”
eternal= “false”
overflowToDisk= “true”
timeToIdleSeconds= “3600”
memoryStoreEvictionPolicy= “LFU”>
<cacheEventListenerFactory
class= “net.sf.ehcache.distribution.RMICacheReplicatorFactory”
properties= “replicateAsynchronously=true,
replicatePuts=true,
replicateUpdates=true,
replicateUpdatesViaCopy=true,
replicateRemovals=true” />
</cache >
</ehcache>
转载至: http://www.yyjjssnn.cn/articles/715.html © www.yyjjssnn.cn`