ehcache缓存同步配置

在本地搭建了两个Tomcat实例模拟服务器环境,通过Ehcache配置实现缓存同步。详细步骤包括:配置ehcache.xml,设置缓存同步,结合Spring AOP整合,启动两个Tomcat。经验证,通过Tomcat1访问并添加缓存数据后,Tomcat2能成功获取到同步的缓存,达到了缓存同步的目标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

准备环境

tomcat1 端口8080

tomcat2 端口8081

两个tomcat都安装在本机上,模拟两个单独的服务器

目标:两台服务器缓存达到同步

tomcat1 ehcache.xml文件配置

<?xml version="1.0" encoding="UTF-8"?>

<ehcache>

	<diskStore path="D:\cache" />

	<defaultCache maxElementsInMemory="10000" eternal="false"
		timeToIdleSeconds="120" timeToLiveSeconds="120" diskSpoolBufferSizeMB="30"
		maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120">
	</defaultCache>
	<cache name="test1" maxElementsInMemory="100" maxElementsOnDisk="0"
		eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="0">
		<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
           properties="replicateAsynchronously=true,replicatePuts=true, replicateUpdates=true, replicateUpdatesViaCopy=false, replicateRemovals=true "/>
	</cache>

	<cacheManagerPeerProviderFactory
		class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
		properties="peerDiscovery=manual,
        rmiUrls=//localhost:8091/test1" />  #test1为上面配置的  也就是要同步的缓存  这里可以配置多  
        
<cacheManagerPeerListenerFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=localhost, port=8090,  #端口不能和tomcat重复 此端口为本服务器端口 必须未被占用
socketTimeoutMillis=2000"/>

</ehcache>
tomcat2的缓存配置文件 同上 因为本机是模拟两台服务器 为了不让端口冲突 需要将上面的 port=8090 改成其他未使用端口  我用的是8091

缓存同步的配置 完成了

然后在结合spring aop 整合到一块

启动两个tomcat

首先在tomcat1通过浏览器访问一次,第一次访问没有缓存数据,所以加入缓存数据

然后在通过tomcat2访问刚才加入缓存的key,发现缓存数据存在 也就是同步了!

更多交流(QQ512567300)!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值