EhCache配置集群--记录

EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认CacheProvider。Ehcache是一种广泛使用的开源Java分布式缓存。主要面向通用缓存,Java EE和轻量级容器。它具有内存和磁盘存储,缓存加载器,缓存扩展,缓存异常处理程序,一个gzip缓存servlet过滤器,支持REST和SOAP api等特点。

EhCache也是shiro常用的缓存框架,小项目可以选用EhCache集群的方式,集群分为三种方式:手动配置,自动发现、JGroups模式,本文主要记录前两种方式;值得注意的是,EhCache集群不是完全同步的(同步需要时间),所以如果利用nginx做集群的话,最好使用 ip_hash 等策略,使用默认的轮询策略可能导致登录后马上退出的问题。

 一、手动配置

<!-- 配置同步ip和端口,发送数据 -->
<cacheManagerPeerProviderFactory 
    class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory" 
    properties="hostName=127.0.0.1,
                port=40002,
                socketTimeoutMillis=2000,
                peerDiscovery=manual, 
                rmiUrls=//127.0.0.1:40002/demo|//127.0.0.1:40002/test"/>

<!-- 配置监听ip和商品,接收数据 -->
<cacheManagerPeerListenerFactory
    class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
    properties="hostName=127.0.0.1,
                port=40001,
                socketTimeoutMillis=2000" />

二、自动发现

<?xml version="1.0" encoding="UTF-8"?>
<ehcache name="itriman" updateCheck="false">

    <!-- 磁盘缓存位置 -->
    <diskStore path="java.io.tmpdir"/>
    
    <!-- maxEntriesLocalHeap:堆内存中最大缓存对象数,0没有限制 -->
    <!-- maxElementsInMemory: 在内存中缓存的element的最大数目。-->
    <!-- eternal:elements是否永久有效,如果为true,timeouts将被忽略,element将永不过期 -->
    <!-- timeToIdleSeconds:失效前的空闲秒数,当eternal为false时,这个属性才有效,0为不限制 -->
    <!-- timeToLiveSeconds:失效前的存活秒数,创建时间到失效时间的间隔为存活时间,当eternal为false时,这个属性才有效,0为不限制 -->
    <!-- overflowToDisk: 如果内存中数据超过内存限制,是否要缓存到磁盘上 -->
    <!-- statistics:是否收集统计信息。如果需要监控缓存使用情况,应该打开这个选项。默认为关闭(统计会影响性能)。设置statistics="true"开启统计 -->
    
    <!-- 默认缓存 -->
    <defaultCache
            maxEntriesLocalHeap="1000"
            eternal="false"
            timeToIdleSeconds="3600"
            timeToLiveSeconds="3600"
            overflowToDisk="false">
    </defaultCache>
    
    <!-- 系统会话缓存 -->
    <cache name="shiro-activeSessionCache"
           maxEntriesLocalHeap="10000"
           overflowToDisk="false"
           eternal="false"
           diskPersistent="false"
           timeToLiveSeconds="0"
           timeToIdleSeconds="0"
           statistics="false">
        <!--
        这个工厂支持以下属性:
           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。
        你可以使用ehcache的默认行为从而减少配置的工作量,默认的行为是以异步的方式复制每件事。
        -->
        <cacheEventListenerFactory
             class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
             properties="replicateAsynchronously=true,
             	replicatePuts=true,
                replicatePutsViaCopy=true,
             	replicateUpdates=true,
             	replicateUpdatesViaCopy=true,
             	replicateRemovals=true"/> 
        <bootstrapCacheLoaderFactory 
 			class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"/>
    </cache>
    
    <!-- 多播集群配置 -->
 	<cacheManagerPeerProviderFactory
 		class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
 		properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.1,
 			multicastGroupPort=4446, timeToLive=0" />
	
	<!-- 多播集群监听配置,同一服务器端口不能相同 -->
 	<cacheManagerPeerListenerFactory
 		class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
 		properties="port=40001,socketTimeoutMillis=30000" />
    
</ehcache>
	

猜你喜欢

转载自blog.csdn.net/hjl0722/article/details/111504321