Spring缓存抽象 - 分布式环境

 mongcheng 发布于 2023-01-29 14:27

我想在我的分布式Web应用程序中使用spring cache abstraction.

我的Web应用程序由一个Web应用程序组成,该应用程序在带有负载均衡器的3个不同的tomcats上运行.

现在,我的问题是当另一个tomcat执行更新时,@Evict如何在所有tomcats中缓存?

春天是否支持这种事情?

谢谢!

1 个回答
  • 如果你告诉Spring使用它是EHCache,那么EHCache支持跨不同物理服务器的多个缓存实例的复制.我在使用多播发现的RMI Replicated Caching方面取得了一些成功.从一个缓存中逐出将自动复制到其他缓存中 - 同样在添加到缓存时也是如此.

    就Spring配置而言,您需要设置各种配置元素和bean:

    <cache:annotation-driven />
    
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager" ref="cacheManager" />
    </bean>
    
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation"  value="classpath:/ehcache.xml"/>
    </bean> 
    

    其余配置在ehcache.xml文件中完成.复制缓存的示例ehcache.xml可能如下所示:

    <cache name="example" 
            maxElementsInMemory="1000"
            eternal="false" 
            overflowToDisk="false" 
            timeToIdleSeconds="0"
            timeToLiveSeconds="600">
            <cacheEventListenerFactory 
                class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/>
            <bootstrapCacheLoaderFactory
                class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"
                properties="bootstrapAsynchronously=true, maximumChunkSizeBytes=5000000"/>
    </cache>
    

    然后,您需要将复制设置添加到ehcache.xml可能如下所示:

    <cacheManagerPeerProviderFactory
            class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
            properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.2,
                        multicastGroupPort=4455, timeToLive=1" />
    
    <cacheManagerPeerListenerFactory
            class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
            properties="hostName=localhost, port=40001, socketTimeoutMillis=2000" />
    

    如文档中所述,还有其他方法可以在EHCache中配置复制,但上面描述的RMI方法相对简单并且对我来说效果很好.

    2023-01-29 14:30 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有