准备环境
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)!