分布式本地缓存 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 小时前
在 Spring Boot 中使用 Kafka 并保证顺序性(Topic 分区为 100)的完整案例
spring boot·kafka
無欲無为2 小时前
Spring Boot 整合 RabbitMQ 详细指南:从入门到实战
spring boot·rabbitmq·java-rabbitmq
.鸣8 小时前
set和map
java·学习
ha_lydms9 小时前
5、Spark函数_s/t
java·大数据·python·spark·数据处理·maxcompute·spark 函数
黄河滴滴9 小时前
java系统变卡变慢的原因是什么?从oom的角度分析
java·开发语言
侠客行03179 小时前
Mybatis二级缓存实现详解
java·mybatis·源码阅读
老华带你飞10 小时前
农产品销售管理|基于java + vue农产品销售管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
潇洒畅想10 小时前
分布式锁极端场景解决方案总结
分布式
Edward1111111111 小时前
tomcat_servlet
java·servlet·tomcat
短剑重铸之日11 小时前
SpringBoot声明式事务的源码解析
java·后端·spring·springboot