分布式本地缓存 ehcache 缓存同步复制

使用spring cache的方式

bash 复制代码
spring:
  #ehcache 配置
  cache:
    # 指定缓存类型 ehcache 本地缓存 redis 缓存
    type: ehcache
    ehcache:
      config: classpath:ehcache.xml
    redis:
      # 指定存活时间(ms)
      time-to-live: 86400000
      # 指定前缀
      use-key-prefix: true
      # 是否缓存空值,可以防止缓存穿透
      cache-null-values: true

缓存是在本地内存中的,当部署多台实例缓存无法共享缓存,导致登录状态异常等问题。

ehcache 分为2.x和3.x版本,差异比较大,我这使用的是2.10.9.2 版本;

比较常见的同步方式

(1)RMI

rmi 是ehcache本身支持的,不需要引入其他jar包;

(2)JGroups 需要引入其他jar包,配置好后部署会比较方便

bash 复制代码
<dependency>
<groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache-jgroupsreplication</artifactId>
            <version>1.7</version>
                    </dependency>
<dependency>
            <groupId>org.jgroups</groupId>
            <artifactId>jgroups</artifactId>
            <version>3.6.17.Final</version>
        </dependency>

(3)Terrocotta (4)JMS

我这里用的是RMI的同步方式

系统需要部署两份,机器ip分别为10.59.0.1 和 10.59.0.2

修改项目中ehcache.xml 文件配置

<# red>231

标识当前服务是一个分片,以及需要向哪里发送数据,哪些缓存池需要同步

部署10.59.0.2 机器的时候需要将 rmiUrls 改为 0.1:40000

bash 复制代码
<cacheManagerPeerProviderFactory     class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory" properties="peerDiscovery=manual,rmiUrls=//10.59.0.2:40000/localTempSwap
            |//10.59.0.2:40000/temp_cache"/>
# cacheManagerPeerProviderFactory 标识服务是集群中的一份子,配置数据写入目的地,
#peerDiscovery=manual 手动发现其他服务;
#rmiUrls=//10.59.0.2:40001/localTempSwap|//10.59.0.2:40001/temp_cache    数据变更时候像这个端口的cache池写数据,多个用| 分隔;ip 写上其他机器的ip, 端口对应cacheManagerPeerListenerFactory 标签中的监听端口port;

当前服务ip 端口信息

部署10.59.0.2 机器的时候需要将 hostName 改为 0.2

bash 复制代码
<cacheManagerPeerListenerFactory
            class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
            properties="hostName=10.59.0.1,port=40000,remoteObjectPort=50000"/>
#            位于ehcache标签内
#hostName  本机ip, port监听端口, remoteObjectPort发送数据时候所使用端口

还需要在每个cache 中配置哪些动作需要触发同步

bash 复制代码
<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
                                   properties="replicateAsynchronously=false, replicatePuts=true, replicateUpdates=true,
                 replicateUpdatesViaCopy=true, replicateRemovals=true "/>
#配置在每一个需要同步的<cache>标签中
#replicateUpdatesViaCopy:
        是否将对象变更复制到所有节点,还是只是发送一个失效信息,让对方该缓存失效,当对方需要该缓存时重新计算载入。
        默认为true。鉴于对象复制的消耗挺大的,又有锁的问题,而且对方也未必需要该对象,所以此属性建议设为false。
        如果业务上真的需要设为true时,就可考虑使用Terracotta了。
        
        #replicatePuts:
        增加对象时是否同步,默认为true,如果replicateUpdatesViaCopy为false,选择了失效算法,所以replicatePuts 要设为false。
        
        #replicateUpdates:
        修改加对象时是否同步,默认为true
        
        #replicateRemovals:
        删除加对象时是否同步,默认为true`

附带一个完整的xml 配置

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">

    <!-- 磁盘缓存位置 -->
    <diskStore path="java.io.tmpdir"/>

    <cacheManagerPeerProviderFactory
            class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
            properties="peerDiscovery=manual,rmiUrls=//10.59.0.2:40000/demo"/>
    <cacheManagerPeerListenerFactory
            class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
            properties="hostName=10.59.0.1,port=40000,remoteObjectPort=50000"/>

    <!-- 默认缓存 -->
    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            maxElementsOnDisk="10000000"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
        <persistence strategy="localTempSwap"/>
        <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
                                   properties="replicateAsynchronously=false, replicatePuts=true, replicateUpdates=true,
                 replicateUpdatesViaCopy=true, replicateRemovals=true "/>
    </defaultCache>

</ehcache>
相关推荐
Lionel_SSL3 小时前
《深入理解Java虚拟机》第三章读书笔记:垃圾回收机制与内存管理
java·开发语言·jvm
记得开心一点嘛3 小时前
手搓Springboot
java·spring boot·spring
爱睡觉的圈圈3 小时前
分布式IP代理集群架构与智能调度系统
分布式·tcp/ip·架构
老华带你飞3 小时前
租房平台|租房管理平台小程序系统|基于java的租房系统 设计与实现(源码+数据库+文档)
java·数据库·小程序·vue·论文·毕设·租房系统管理平台
独行soc3 小时前
2025年渗透测试面试题总结-66(题目+回答)
java·网络·python·安全·web安全·adb·渗透测试
脑子慢且灵4 小时前
[JavaWeb]模拟一个简易的Tomcat服务(Servlet注解)
java·后端·servlet·tomcat·intellij-idea·web
华仔啊5 小时前
SpringBoot 中 6 种数据脱敏方案,第 5 种太强了,支持深度递归!
java·后端
APItesterCris5 小时前
构建分布式京东商品数据采集系统:基于 API 的微服务实现方案
分布式·微服务·架构
异常驯兽师6 小时前
Spring 中处理 HTTP 请求参数注解全解析
java·spring·http
不吃饭的猪6 小时前
kafka启动小脚本
分布式·kafka