`
jetway
  • 浏览: 480258 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

EHCache分布式缓存集群环境配置

 
阅读更多

ehcache提供三种网络连接策略来实现集群,rmi,jgroup还有jms。同时ehcache可以可以实现多播的方式实现集群,也可以手动指定集群主机序列实现集群。

 

Ehcache支持的分布式缓存支持有三种RMI,JGroups,JMS,这里介绍下MRI和JGrpups两种方式,Ehcache使用版本为1.5.0,关于ehcache的其他信息请参考http://ehcache.sourceforge.net/EhcacheUserGuide.html

关于jgroups的信息请参考http://www.jgroups.org/manual/html_single/index.html。

环境为两台机器 server1 ip:192.168.2.154,server2 ip:192.168.2.23

1. RMI方式:

rmi的方式配置要点(下面均是server1上的配置,server2上的只需要把ip兑换即可)

a. 配置PeerProvider:

Xml代码

<cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"

 

properties="peerDiscovery=manual,rmiUrls=//192.168.2.23:40001/userCache|//192.168.2.23:40001/resourceCache" />

 

 

配置中通过手动方式同步sever2中的userCache和resourceCache。

b. 配置CacheManagerPeerListener:

Xml代码

<cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"

 

properties="hostName=192.168.2.154, port=40001,socketTimeoutMillis=2000" />

 

 

配置中server1监听本机40001端口。

c. 在每一个cache中添加cacheEventListener,例子如下:

Xml代码

<cache name="userCache" maxElementsInMemory="10000" eternal="true" overflowToDisk="true" timeToIdleSeconds="0" timeToLiveSeconds="0"diskPersistent="false" diskExpiryThreadIntervalSeconds="120">

 

<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy= false, replicateRemovals= true " />

 

</cache>

 

 

属性解释:

必须属性:

        name:设置缓存的名称,用于标志缓存,惟一

        maxElementsInMemory:在内存中最大的对象数量

        maxElementsOnDisk:在DiskStore中的最大对象数量,如为0,则没有限制

        eternal:设置元素是否永久的,如果为永久,则timeout忽略

        overflowToDisk:是否当memory中的数量达到限制后,保存到Disk

可选的属性:

        timeToIdleSeconds:设置元素过期前的空闲时间

        timeToLiveSeconds:设置元素过期前的活动时间

        diskPersistent:是否disk store在虚拟机启动时持久化。默认为false

   diskExpiryThreadIntervalSeconds:运行disk终结线程的时间,默认为120秒

        memoryStoreEvictionPolicy:策略关于Eviction

缓存子元素:

    cacheEventListenerFactory:注册相应的的缓存监听类,用于处理缓存事件,如put,remove,update,和expire

    bootstrapCacheLoaderFactory:指定相应的BootstrapCacheLoader,用于在初始化缓存,以及自动设置。

 

 

参考另外一篇学习笔记http://wozailongyou.javaeye.com/blog/230252,也有集群的说明

2. JGroups方式:

ehcache 1.5.0之后版本支持的一种方式,配置起来比较简单,要点:

a. 配置PeerProvider,使用tcp的方式,例子如下:

Xml代码

<cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.jgroups.JGroupsCacheManagerPeerProviderFactory"

 

properties="connect=TCP(start_port=7800):

 

TCPPING(initial_hosts=192.168.2.154[7800],192.168.2.23[7800];port_range=10;timeout=3000;

 

num_initial_members=3;up_thread=true;down_thread=true):

 

VERIFY_SUSPECT(timeout=1500;down_thread=false;up_thread=false):

 

pbcast.NAKACK(down_thread=true;up_thread=true;gc_lag=100;retransmit_timeout=3000):

 

pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;shun=false;

 

print_local_addr=false;down_thread=true;up_thread=true)"

 

propertySeparator="::" />

 

 

b.为每个cache添加cacheEventListener:

Xml代码

<cache name="userCache" maxElementsInMemory="10000" eternal="true"

 

overflowToDisk="true" timeToIdleSeconds="0" timeToLiveSeconds="0"

 

diskPersistent="false" diskExpiryThreadIntervalSeconds="120">

 

<cacheEventListenerFactory class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"

 

properties="replicateAsynchronously=true, replicatePuts=true,

 

replicateUpdates=true, replicateUpdatesViaCopy=false, replicateRemovals=true"/>

 

</cache>

 

JGroup方式配置的两个server上的配置文件一样,若有多个server,在initial_hosts中将server ip加上即可。

一个完整的ehcache.xml文件:

Xml代码

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

 

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

 

xsi:noNamespaceSchemaLocation="http://ehcache.sf.net/ehcache.xsd">

 

<diskStore path="java.io.tmpdir" />

 

 

 

<cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.jgroups.JGroupsCacheManagerPeerProviderFactory"

 

properties="connect=TCP(start_port=7800):

 

TCPPING(initial_hosts=192.168.2.154[7800],192.168.2.23[7800];port_range=10;timeout=3000;

 

num_initial_members=3;up_thread=true;down_thread=true):

 

VERIFY_SUSPECT(timeout=1500;down_thread=false;up_thread=false):

 

pbcast.NAKACK(down_thread=true;up_thread=true;gc_lag=100;retransmit_timeout=3000):

 

pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;shun=false;

 

print_local_addr=false;down_thread=true;up_thread=true)"

 

propertySeparator="::" />

 

 

 

<defaultCache maxElementsInMemory="10000" eternal="true"

 

overflowToDisk="true" timeToIdleSeconds="0" timeToLiveSeconds="0"

 

diskPersistent="false" diskExpiryThreadIntervalSeconds="120">

 

<cacheEventListenerFactory class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"

 

properties="replicateAsynchronously=true, replicatePuts=true,

 

replicateUpdates=true, replicateUpdatesViaCopy=false, replicateRemovals=true"/>

 

</defaultCache>

 

 

 

<cache name="velcroCache" maxElementsInMemory="10000" eternal="true"

 

overflowToDisk="true" timeToIdleSeconds="0" timeToLiveSeconds="0"

 

diskPersistent="false" diskExpiryThreadIntervalSeconds="120">

 

<cacheEventListenerFactory class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"

 

properties="replicateAsynchronously=true, replicatePuts=true,

 

replicateUpdates=true, replicateUpdatesViaCopy=false, replicateRemovals=true"/>

 

</cache>

 

<cache name="userCache" maxElementsInMemory="10000" eternal="true"

 

overflowToDisk="true" timeToIdleSeconds="0" timeToLiveSeconds="0"

 

diskPersistent="false" diskExpiryThreadIntervalSeconds="120">

 

<cacheEventListenerFactory class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"

 

properties="replicateAsynchronously=true, replicatePuts=true,

 

replicateUpdates=true, replicateUpdatesViaCopy=false, replicateRemovals=true"/>

 

</cache>

 

<cache name="resourceCache" maxElementsInMemory="10000"

 

eternal="true" overflowToDisk="true" timeToIdleSeconds="0"

 

timeToLiveSeconds="0" diskPersistent="false"

 

diskExpiryThreadIntervalSeconds="120">

 

<cacheEventListenerFactory class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"

 

properties="replicateAsynchronously=true, replicatePuts=true,

 

replicateUpdates=true, replicateUpdatesViaCopy=false, replicateRemovals=true"/>

 

</cache>

 

</ehcache>
分享到:
评论

相关推荐

    Ehcache分布式缓存与其在SpringBoot应用

    1. **环境配置**:首先,你需要在SpringBoot的`pom.xml`文件中添加Ehcache和相关依赖。同时,在`application.properties`或`application.yml`文件中配置Ehcache的相关参数,例如缓存的大小、过期时间等。 2. **...

    Ehcache分布式缓存与其在spring中的使用

    #### 四、Ehcache分布式缓存集群配置 为了实现Ehcache在分布式环境下的缓存一致性,可以采用不同的网络连接策略: - **RMI**:远程方法调用,适用于小型或中型应用。 - **JGroups**:一种组通信库,适用于大型集群...

    基于JGROUPS的ehcache的分布式缓存复制

    总的来说,基于JGROUPS的Ehcache分布式缓存复制是提高Java应用性能和可扩展性的一个有效途径。它结合了Ehcache的强大缓存功能和JGROUPS的可靠集群通信能力,为开发者提供了一种灵活、高效的数据共享解决方案。通过...

    集群环境中使用_EhCache_缓存系统&Ehcache配置文件的详细说明

    本篇文章将深入探讨EhCache在集群环境中的应用及其配置文件的详细设置。 一、EhCache概述 EhCache是由Terracotta公司开发的内存缓存系统,它支持本地缓存和分布式缓存两种模式。EhCache的特点包括:快速存取、内存...

    EHCache 分布式配置文件

    总结来说,EHCache 的分布式配置文件是实现高效、可靠的分布式缓存系统的核心,通过精细调整这些配置,我们可以优化缓存性能,提升整个系统的响应速度,并确保数据的一致性和可用性。理解并熟练掌握这些配置是成为一...

    分布式缓存EhCache用户指南.docx

    【分布式缓存 EhCache 用户指南】 EhCache 是一个广泛使用的Java缓存库,自1.2版本开始,它引入了分布式缓存的功能,允许在多台机器间共享和同步数据,以支持大规模、高并发的应用场景。分布式缓存通过插件机制实现...

    Spring Boot 2.x基础教程:使用EhCache缓存集群.docx

    对于每个实例,我们需要创建一个特定的配置文件,例如`ehcache-1.xml`,`ehcache-2.xml`等,以适应集群环境。在配置文件中,我们需要指定`cacheEventListenerFactory`和`cacheManagerPeerProviderFactory`来实现缓存...

    ehcache使用,以及集群配置

    在集群环境中,Ehcache 使用`terracotta-server`作为分布式管理器。以下是集群配置的关键步骤: 1. **安装Terracotta Server**: 在每台服务器上安装并启动Terracotta Server,它是Ehcache的分布式管理组件。 2. **...

    异步复制与同步复制 以及ehcache的小tips和我的配置

    它支持本地缓存、分布式缓存,并提供了多种缓存策略,包括LRU(最近最少使用)、LFU(最不经常使用)和FIFO(先进先出)等。Ehcache还支持缓存的热备份和复制,以便在节点故障时保持数据的可用性。 配置Ehcache进行...

    java缓存实现与spring托管

    0. 文档介绍 2 0.1 文档目的 2 0.2 文档范围 2 0.3 读者对象 2 0.4 参考文献 2 0.5 术语与缩写解释 2 1. 概述 3 1.1背景 3 1.2 主要特征 3 ...4. 分布式缓存集群环境配置 19 4.1 集群配置方式 19 5. 测试用例 28

    Ehcache通过Jgroups做集群

    2. 配置Ehcache的`ehcache.xml`,设置`distributed`为`true`启用分布式缓存,并指定Jgroups配置文件的位置。 3. 在代码中初始化Ehcache,确保使用的是集群配置。 测试集群配置,可以启动多台虚拟机或实际的服务器,...

    分布式缓存架构11.docx

    ### 分布式缓存架构与Ehcache集群详解 #### 一、为什么使用缓存? 在现代互联网系统中,为了提高系统的响应速度和降低数据库的压力,缓存技术被广泛应用。缓存通过暂时存储数据副本的方式来加快数据访问的速度,...

    Ehcache集群实例

    8. **性能优化**:在集群环境中,考虑缓存数据的分布策略,例如使用一致性哈希或基于键的分区,以优化数据访问。同时,调整JGroups和Ehcache的配置参数,如心跳间隔、超时值等,以适应特定的网络环境和性能需求。 9...

    ehcache rmi集群demo

    Ehcache是一款广泛使用的开源Java缓存库,它允许应用程序高效地存储和...通过这个demo,开发者可以学习到如何利用Ehcache构建分布式缓存,以及如何通过RMI实现节点间的通信,为日后的分布式系统开发打下坚实的基础。

    Java缓存框架 EhCache

    3. **多级缓存**:EhCache支持构建分布式缓存系统,通过Terracotta服务器实现多个节点之间的缓存共享,形成分布式缓存集群。这使得多台服务器上的应用可以共享同一份缓存,提高数据一致性并分散负载。 4. **缓存...

    jboss集成 ehcache 快捷部署缓存

    Ehcache则是一款广泛使用的Java缓存解决方案,它可以提供本地内存缓存以及分布式缓存的能力,有效地减少数据库的访问压力,提高系统的响应速度。 首先,我们需要了解ehcache的基本概念和工作原理。Ehcache分为两级...

    金民一期婚姻管理测试系统_开发框架功能点-分布式缓存.pdf

    本文将深入探讨该系统中采用的分布式缓存——Ehcache,以及其在系统中的应用与配置。 1. **Ehcache介绍** Ehcache是一款开源、高性能、轻量级的Java缓存库,广泛应用于Java应用程序中,提供本地内存缓存和分布式...

    Ehcache Java 缓存框架 v3.10.8.zip

    8. **分布式缓存**:利用 Terracotta Server 集群,Ehcache可以实现跨节点的缓存共享,增强了应用的可伸缩性。 9. **缓存复制**:在网络中的节点之间,Ehcache能够自动复制数据,确保数据的一致性。 10. **缓存...

    ehcache集群方案

    理解并掌握Ehcache的集群配置、复制策略、一致性模型以及JGroups的相关知识,对于构建高性能、高可用的分布式缓存系统至关重要。在实际应用中,应根据业务需求和环境特点,灵活调整和优化这些配置,以获得最佳的性能...

Global site tag (gtag.js) - Google Analytics