分布式本地缓存 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>
相关推荐
果汁华1 小时前
java学习连续打卡30天(1)
java
武子康2 小时前
Java-171 Neo4j 备份与恢复 + 预热与执行计划实战
java·开发语言·数据库·性能优化·系统架构·nosql·neo4j
m0_639817152 小时前
基于springboot火锅店管理系统【带源码和文档】
java·spring boot·后端
angushine2 小时前
SpringCloud Gateway缓存body参数引发的问题
spring cloud·缓存·gateway
会编程的林俊杰3 小时前
SpringBoot项目启动时的依赖处理
java·spring boot·后端
李慕婉学姐3 小时前
【开题答辩过程】以《Javaweb的火花流浪动物救助系统设计与实现》为例,不会开题答辩的可以进来看看
vue.js·spring boot·mysql
q***72873 小时前
SpringBoot中整合RabbitMQ(测试+部署上线 最完整)
spring boot·rabbitmq·java-rabbitmq
一叶飘零_sweeeet3 小时前
深度拆解汽车制造系统设计:用 Java + 设计模式打造高扩展性品牌 - 车型动态生成架构
java·设计模式·工厂设计模式
王家羽翼-王羽3 小时前
nacos 3.1.0 运行主类报错 com.alibaba.cloud.nacos.logging.NacosLoggingAppRunListener
java
刘一说4 小时前
深入理解 Spring Boot 单元测试:从基础到最佳实践
spring boot·后端·单元测试