`

分布式ehcache缓存

阅读更多

今天在这里了记录一下学习ehcache分布式集群的过程。
ehcache的三种最为常用集群方式,分别是 RMI、JGroups 以及 JMS 。

ehcache的集群,是在每台缓存服务器上都复制存储相同的内容,而是不是像redis3.0集群一样,进行分开存储。当一台服务启动时,会把其它节点已缓存的数据同步过来。

 

1.rmi方式

 

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false">
	<diskStore path="java.io.tmpdir/Tmp_EhCache" />
	<!-- 配置提供者 1、peerDiscovery,提供者方式,有两种方式:自动发现(automatic)、手动配置(manual) 2、rmiUrls,手动方式时提供者的地址,多个的话用|隔开 -->
	<cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory" properties="peerDiscovery=manual,rmiUrls=//127.0.0.1:40002/baseCache" />

	<!-- 配置监听器 1、hostName 主机地址 2、port 端口 3、socketTimeoutMillis socket子模块的超时时间,默认是2000ms -->
	<cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory" properties="hostName=127.0.0.1, port=40001, socketTimeoutMillis=2000" />

	<!-- 默认配置 -->
	<defaultCache maxElementsInMemory="5000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" memoryStoreEvictionPolicy="LRU" overflowToDisk="false" />

	<cache name="baseCache" maxElementsInMemory="10000" maxElementsOnDisk="100000">
		<!-- 配置缓存事件监听器 replicateAsynchronously 操作是否异步,默认值为true. replicatePuts 添加操作是否同步到集群内的其他缓存,默认为true. replicateUpdates 更新操作是否同步到集群内的其他缓存,默认为true. replicateUpdatesViaCopy 更新之后的对象是否复制到集群中的其他缓存(true); replicateRemovals 删除操作是否同步到集群内的其他缓存,默认为true. -->
		<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" properties="
                replicateAsynchronously=true,
                replicatePuts=true,
                replicateUpdates=true,
                replicateUpdatesViaCopy=true,
                replicateRemovals=true " />

		<!-- 初始化缓存,以及自动设置 -->
		<bootstrapCacheLoaderFactory class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" />
	</cache>

</ehcache>

 

       @CachePut(value = "baseCache", key = "#key")
	public String put(String key, String value) {
	    System.out.println("保存数据, " + key + " : " + value);
	    return value;
	}

	@Cacheable(value = "baseCache", key = "#name")
	public String getName(String name) { 
		System.out.println("数据, " + name);
	    return String.valueOf(System.currentTimeMillis());
	}

 

2.jgroups方式

<?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应用

    Ehcache 是一种广泛使用的开源 Java 分布式缓存。主要面向通用缓存,Java EE 和轻量级容器。它具有内存和磁盘存储,缓存加载器,缓存扩展,缓存异常处理程序,一个 gzip 缓存 servlet 过滤器,支持 REST 和 SOAP api...

    ehcache分布式缓存实例

    java ehcache分布式缓存实例 RMI方式实现.

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

    从 1.2 版本开始,Ehcache 可以使用分布式的缓存了。 分布式这个特性是以 plugin 的方式实现的。Ehcache 自带了一些默认的分布式缓存插件实现,这些插件可以满足大部分应用的需要。如果需要使用其他的插件那就需要...

    Ehcache分布式缓存入门案例demo_文件转树结构_

    Ehcache分布式缓存入门案例demo,简单易用上手快,帮助您快速解决应用中的单体缓存问题,提高系统响应速度。

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

    主要讲解下encache的原理、分布式缓存集群环境配置、与在spring中的使用

    分布式缓存.docx

    介绍ehcache做jvm缓存,ehcache做分布式缓存,redis做分布式缓存,redis分片集群,redis哨兵,redis基本数据,redis主从复制,redis和ehcache的二级缓存,redis雪崩效应,redis缓存穿透(整个环境搭建基于Centos,代码...

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

    NULL 博文链接:https://wiselyman.iteye.com/blog/2114715

    EhCache用户指南(分布式缓存)

    超好的东西,分布式缓存所需配置都在里面,感谢为我们翻译的大牛~~

    分布式多级缓存实践

    此文可借鉴作为分布式缓存中间件实现方案

    EhCache缓存技术

    【EhCache】Java缓存框架使用EhCache结合Spring AOP ... 可以通过RMI、可插入API等方式进行分布式缓存。 6. 具有缓存和缓存管理器的侦听接口 。 7. 支持多缓存管理器实例,以及一个实例的多个缓存区域 等特点。

    SpringShiro分布式缓存版

    -- 配置ehcache缓存,如果是本机,没分布式的话,可以考虑就选择ehcache缓存 --&gt; &lt;!-- 如果有多台机子的话,可以考虑部署redis分布式缓存.. --&gt; &lt;/bean&gt; &lt;!-- 用户授权信息Cache, 采用EhCache,需要的话就配置...

    Ehcache分布式缓存NoSQL深入浅出

    Ehcache是Java语言编写使用最广的分布式缓存。本套课程讲解全新的Ehcache?3.1版本,内容全面实用。涵盖缓存分层、缓存过期和剔除策略、缓存层序列化、多种缓存使用模式、事件监听器、XA事务、分布式缓存集群。

    基于storm实时热点统计的分布式并行缓存预热

    四、基于双重zookeeper分布式锁完成分布式并行缓存预热的代码开发 =================================== 1、服务启动的时候,进行缓存预热 2、从zk中读取taskid列表 3、依次遍历每个taskid,尝试获取分布式锁,...

    Ehcache Java 缓存框架 v3.6.1

    EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。主要的特性有:1. 快速.2. 简单.3. 多种缓存策略4. 缓存数据有两级:内存和磁盘,因此无需担心容量问题5. 缓存...

    分布式缓存架构11.docx

    ehcache集群

    EHCache缓存

    ehcache是一个用Java实现的使用简单,高速,实现线程安全的缓存管理类库,ehcache提供了用内存,磁盘文件存储,以及分布式存储方式等多种灵活的cache管理方案,是hibernate中默认的CacheProvider。

    Java缓存框架 Ehcache.zip

    可以通过RMI、可插入API等方式进行分布式缓存7. 具有缓存和缓存管理器的侦听接口8. 支持多缓存管理器实例,以及一个实例的多个缓存区域9. 提供Hibernate的缓存实现10. 等等 标签:缓存

    EhcacheJava缓存框架v3.6.1

    EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。 主要的特性有: 1. 快速. 2. 简单. 3. 多种缓存策略 4. 缓存数据有两级:内存和磁盘,因此无需担心...

    springboot+mybatis+ehcache实现缓存数据

    ehcache是一个快速内存缓存框架,java项目里用起来很方便,本来想实现分布式缓存的,但是实践下来redis更适合分布式,所以这里面把分布缓存的配置注释掉了

    ehcache2.6.6缓存相关jar

    做ehcache分布式缓存用的! ehcache-2.6.6.jar和terracotta-toolkit-1.6-runtime-5.5.0.jar 主要是给自己写的博客中做一个下载资源链接!

Global site tag (gtag.js) - Google Analytics