java - Spring缓存抽象——分布式环境

标签 java spring tomcat caching ehcache

我想在我的分布式 Web 应用程序中使用 spring 缓存抽象。

我的 Web 应用程序在 3 个不同的 tomcat 上运行,并带有负载均衡器。

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

spring支持这种东西吗?

谢谢!

最佳答案

如果您告诉 Spring 使用的是 EHCache,那么 EHCache 支持 replication跨不同物理服务器的多个缓存实例。我在 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 方法相对简单,对我来说效果很好。

关于java - Spring缓存抽象——分布式环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21554658/

相关文章:

java - 注册-Spring Boot-MongoDb-主键替换新数据

tomcat - 设置 Tomcat 9.0.19 时未正确定义 CATALINA_HOME 环境变量

java - 401 在 Tomcat 中未经授权

java - 无法转换 java.lang.Boolean 类型的对象

java - org.springframework.expression.spel.SpelEvaluationException

java - 删除重复项后对 alpha 进行排序的方法 java(集合)

java - Spring MVC 3.2.8 : FtpInboundFileSynchronizer

tomcat - 在 Plesk 11.5 上安装并启用 Tomcat 7

java - 菜单操作项的 setVisibility

Java jface 从相对位置调用浏览器