`

Ehcache缓存同步

阅读更多
Ehcache缓存同步有几种方式:(1)RMI (2)Terrocotta (3)JMS (4)JGroups
先介绍下,利用RMI进行缓存同步。
测试类1:在sampleDistributedCache2缓存中查找是否存在ehcache键,如果没找到,则打印NotFound;如果找到了,则打印相应值
view plaincopy to clipboardprint?
1. package my.test.ehcache1; 
2.  
3. import java.io.InputStream; 
4. import java.lang.management.ManagementFactory; 
5.  
6. import javax.management.MBeanServer; 
7.  
8. import net.sf.ehcache.Cache; 
9. import net.sf.ehcache.CacheManager; 
10. import net.sf.ehcache.Element; 
11. import net.sf.ehcache.management.ManagementService; 
12.  
13. public class EHCacheTest { 
14.  
15.     public static void main(String[] args) { 
16.         InputStream is = EHCacheTest.class 
17.                 .getResourceAsStream("/my/test/ehcache1/ehcache.xml"); 
18.         //读入配置 
19.         CacheManager cacheManager = new CacheManager(is); 
20.         //打印初始缓存 
21.         String[] cacheNames = cacheManager.getCacheNames(); 
22.         printNames(cacheNames); 
23.         //移除缓存 
24.         cacheManager.removeCache("sampleDistributedCache1"); 
25.         cacheNames = cacheManager.getCacheNames(); 
26.         printNames(cacheNames); 
27.  
28.         //新建缓存 
29.         Cache cache = new Cache("Test1", 100, true, false, 10, 10); 
30.         cacheManager.addCache(cache); 
31.         cacheNames = cacheManager.getCacheNames(); 
32.         printNames(cacheNames); 
33.         cache.put(new Element("test1", "value1")); 
34.          
35.         //得到缓存并插入值(这里监听器被调用) 
36.         cache = cacheManager.getCache("sampleCache3"); 
37.         for (int i = 0; i < 20; i++) { 
38.             cache.put(new Element("key" + i, "value" + i)); 
39.         } 
40.         cache.get("key10"); 
41.          
42.         // distributed -- rmi同步 
43.         cache = cacheManager.getCache("sampleDistributedCache2"); 
44.         for (int i = 0; i < 100; i++) { 
45.             cache.put(new Element("key" + i , "value" + i)); 
46.         } 
47.          
48.         //注册被管理的Bean 
49.         // JMX -- jconsole(MBeanServer) 
50.         MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); 
51.         ManagementService.registerMBeans(cacheManager, mBeanServer, true, true, 
52.                 true, true); 
53.  
54.         for (int i = 0; i < 10; i++) { 
55.             Element temp = cache.get("ehcache"); 
56.             if (temp != null) { 
57.                 System.out.println(temp.getValue()); 
58.             } else { 
59.                 System.out.println("NotFound"); 
60.             } 
61.             try { 
62.                 Thread.sleep(10000); 
63.             } catch (InterruptedException e) { 
64.                 e.printStackTrace(); 
65.             } 
66.         } 
67.  
68.         // distributed cache using RMI 
69.         // 1.Peer Discovery -- cacheManagerPeerProviderFactory 
70.         // 2.CacheManager -- cacheManagerPeerListenerFactory 
71.         // 3.cache replication -- cacheEventListenerFactory 
72.         // 4.Bootstrap -- 启动后同步 
73.     } 
74.  
75.     private static void printNames(String[] names) { 
76.         System.out.println("======================="); 
77.         for (int i = 0; i < names.length; i++) { 
78.             System.out.println(names[i]); 
79.         } 
80.     } 
81.  
82. } 

配置文件1:将官方样例文件中相应位置替换即可
(1)其他JVM提供缓存的rmiUrl地址
view plaincopy to clipboardprint?
1. <cacheManagerPeerProviderFactory 
2.             class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory" 
3.             properties="peerDiscovery=manual, 
4.                         rmiUrls=//localhost:40002/sampleDistributedCache2" 
5.             propertySeparator="," 
6.     /> 

(2)监听来自于其他复制节点消息的本JVM的host,port
view plaincopy to clipboardprint?
1. <cacheManagerPeerListenerFactory 
2.             class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory" 
3.             properties="hostName=localhost, port=40001, socketTimeoutMillis=2000"  
4.     /> 

(3)配置复制选项,启动时同步等
view plaincopy to clipboardprint?
1. <cache name="sampleDistributedCache2" 
2.            maxElementsInMemory="10" 
3.            eternal="false" 
4.            timeToIdleSeconds="100" 
5.            timeToLiveSeconds="100" 
6.            overflowToDisk="false"> 
7.         <cacheEventListenerFactory 
8.                 class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" 
9.                 properties="replicateAsynchronously=true, replicatePuts=true, 
10.                             replicatePutsViaCopy=true, replicateUpdates=true, 
11.                             replicateUpdatesViaCopy=true, replicateRemovals=true, 
12.                             asynchronousReplicationIntervalMillis=200"/> 
13.         <bootstrapCacheLoaderFactory 
14.                 class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"/> 
15.     </cache> 

测试类2:向sampleDistributedCache2缓存中添加ehcache键
view plaincopy to clipboardprint?
1. package my.test.ehcache2; 
2.  
3. import java.io.InputStream; 
4. import java.lang.management.ManagementFactory; 
5.  
6. import javax.management.MBeanServer; 
7.  
8. import net.sf.ehcache.Cache; 
9. import net.sf.ehcache.CacheManager; 
10. import net.sf.ehcache.Element; 
11. import net.sf.ehcache.Statistics; 
12. import net.sf.ehcache.management.ManagementService; 
13. import net.sf.ehcache.statistics.LiveCacheStatistics; 
14.  
15.  
16. public class EHCacheTest { 
17.      
18.     public static void main(String[] args) { 
19.         try { 
20.             Thread.sleep(3 * 1000); 
21.         } catch (InterruptedException e) { 
22.             e.printStackTrace(); 
23.         } 
24.         //读入配置 
25.         InputStream is = EHCacheTest.class.getResourceAsStream("/my/test/ehcache2/ehcache.xml"); 
26.         CacheManager cacheManager = new CacheManager(is); 
27.         //打印初始缓存 
28.         String[] cacheNames = cacheManager.getCacheNames(); 
29.         printNames(cacheNames); 
30.         //注册管理Bean 
31.         MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); 
32.         ManagementService.registerMBeans(cacheManager, mBeanServer, true, true, true, true); 
33.          
34.         //distributed 
35.         Cache cache = cacheManager.getCache("sampleDistributedCache2"); 
36.         printCache(cache); 
37.         //添加值后另一个虚拟机的缓存通过RMI会同步缓存,并读到这个值 
38.         cache.put(new Element("ehcache", "newaddvalue")); 
39.     } 
40.      
41.      
42.     private static void printNames(String[] names) { 
43.         System.out.println("======================="); 
44.         for (int i = 0; i < names.length; i++) { 
45.             System.out.println(names[i]); 
46.         } 
47.     } 
48.      
49.     private static void printCache(Cache cache) { 
50.         int size = cache.getSize(); 
51.         long memSize = cache.getMemoryStoreSize(); 
52.         long diskSize = cache.getDiskStoreSize(); 
53.         Statistics stat = cache.getStatistics(); 
54.         LiveCacheStatistics liveStat = cache.getLiveCacheStatistics(); 
55.         long hits = stat.getCacheHits(); 
56.         long missed = stat.getCacheMisses(); 
57.         long hitsOnDisk = stat.getOnDiskHits(); 
58.         long liveHits = liveStat.getCacheHitCount(); 
59.         long liveMissed = liveStat.getCacheMissCount(); 
60.          
61.         StringBuilder sb = new StringBuilder(); 
62.         sb.append("size=" + size + ";memsize=" + memSize); 
63.         sb.append(";diskSize=" + diskSize + ";hits=" + hits); 
64.         sb.append(";missed=" + missed + ";liveHits=" + liveHits); 
65.         sb.append(";liveMissed=" + liveMissed + ";hitsOnDisk=" + hitsOnDisk); 
66.         System.out.println(sb.toString()); 
67.     } 
68. } 

配置文件:将官方样例文件中相应位置替换即可
view plaincopy to clipboardprint?
1. <cacheManagerPeerProviderFactory 
2.             class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory" 
3.             properties="peerDiscovery=manual, 
4.                         rmiUrls=//localhost:40001/sampleDistributedCache2" 
5.             propertySeparator="," 
6.     /> 

view plaincopy to clipboardprint?
1. <cacheManagerPeerListenerFactory 
2.             class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory" 
3.             properties="hostName=localhost, port=40002, socketTimeoutMillis=2000"  
4.     /> 

view plaincopy to clipboardprint?
1. <cache name="sampleDistributedCache2" 
2.            maxElementsInMemory="10" 
3.            eternal="false" 
4.            timeToIdleSeconds="100" 
5.            timeToLiveSeconds="100" 
6.            overflowToDisk="false"> 
7.         <cacheEventListenerFactory 
8.                 class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" 
9.                 properties="replicateAsynchronously=true, replicatePuts=true, 
10.                             replicatePutsViaCopy=true, replicateUpdates=true, 
11.                             replicateUpdatesViaCopy=true, replicateRemovals=true, 
12.                             asynchronousReplicationIntervalMillis=200"/> 
13.         <bootstrapCacheLoaderFactory 
14.                 class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"/> 
15.     </cache> 

运行结果:
JVM1
2009-12-13 12:24:04 net.sf.ehcache.distribution.RMICacheManagerPeerListener <init>
警 告: Explicitly setting the listener hostname to 'localhost' is not recommended. It will only work if all CacheManager peers are on the same machine.
=======================
sampleDistributedCache2
sampleCache2
sampleCache1
sample/DistributedCache3
sampleDistributedCache1
sampleCache3
=======================
sampleDistributedCache2
sampleCache2
sampleCache1
sample/DistributedCache3
sampleCache3
=======================
sampleDistributedCache2
sampleCache2
Test1
sampleCache1
sample/DistributedCache3
sampleCache3
key0=value0 has been added
key1=value1 has been added
key2=value2 has been added
key3=value3 has been added
key4=value4 has been added
key5=value5 has been added
key6=value6 has been added
key7=value7 has been added
key8=value8 has been added
key9=value9 has been added
key7=value7 has been evicted
key10=value10 has been added
key0=value0 has been evicted
key11=value11 has been added
key9=value9 has been evicted
key12=value12 has been added
key3=value3 has been evicted
key13=value13 has been added
key1=value1 has been evicted
key14=value14 has been added
key4=value4 has been evicted
key15=value15 has been added
key13=value13 has been evicted
key16=value16 has been added
key8=value8 has been evicted
key17=value17 has been added
key17=value17 has been evicted
key18=value18 has been added
key18=value18 has been evicted
key19=value19 has been added
NotFound
newaddvalue
JVM2:
2009-12-13 12:24:13 net.sf.ehcache.distribution.RMICacheManagerPeerListener <init>
警 告: Explicitly setting the listener hostname to 'localhost' is not recommended. It will only work if all CacheManager peers are on the same machine.
=======================
sampleDistributedCache2
sampleCache2
sampleCache1
sample/DistributedCache3
sampleDistributedCache1
sampleCache3
size=10;memsize=10;diskSize=0;hits=0;missed=0;liveHits=0;liveMissed=0;hitsOnDisk=0

分享到:
评论
2 楼 huashuizhuhui 2013-01-30  
hostname 陪一下 host文件
1 楼 atgoingguoat 2013-01-30  
Tomcat7
下面也报这个错。
警告: Explicitly setting the listener hostname to 'localhost' is not recommended. It will only work if all CacheManager peers are on the same machine.

怎么解?

相关推荐

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

    JGROUP是一个用于构建可靠集群通信的库,可以提供组成员资格、故障检测、传输协议等多种功能,使得Ehcache能够在多个节点之间共享和同步缓存数据。Ehcache的分布式特性允许在大型系统中进行负载均衡,提高应用性能,...

    EHCache缓存技术介绍

    【EHCache缓存技术介绍】 缓存技术是提高软件系统性能的重要手段,特别是在处理大量数据时,通过将常用数据存储在内存中,可以显著减少对硬盘或数据库的访问,从而加快数据获取速度。EHCache是一种广泛使用的开源...

    Ehcache缓存

    **Ehcache缓存** Ehcache是一种广泛使用的开源Java分布式缓存系统,它为高性能应用程序提供了内存存储和缓存解决方案。在Java世界中,尤其是在持久化框架如Hibernate的使用中,Ehcache扮演了至关重要的角色。由于...

    ehcache使用,以及集群配置

    这意味着对缓存的任何修改都会立即同步到其他节点。 4. **故障转移**: 如果一个服务器宕机,Ehcache可以自动切换到另一个可用的服务器,保持服务的连续性。 5. **监控与管理**: Terracotta Server 提供了管理...

    EHcache缓存框架

    **EHcache缓存框架** EHcache是一款开源的Java缓存框架,它被广泛应用于提高应用程序的性能和响应速度,通过存储频繁访问的数据到内存中,避免了每次请求时都进行昂贵的数据库查询。EHcache的设计目标是轻量级、高...

    SpringBoot 集成Ehcache实现缓存

    下面将详细介绍如何在一个Spring Boot项目中集成并使用Ehcache缓存。 ##### 1. 创建项目 首先,使用IDEA创建一个Maven类型的Spring Boot项目。确保项目结构符合Spring Boot的标准。 ##### 2. 数据库初始化 为了...

    ehcache缓存依赖的jar

    "ehcache缓存依赖的jar"这个标题暗示我们将讨论Ehcache的核心库及其依赖关系。 Ehcache的核心JAR文件是`ehcache.jar`,它包含了Ehcache的所有核心组件和接口。这个文件提供了缓存管理、缓存配置、缓存策略(如LRU、...

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

    为了解决这个问题,我们需要配置EhCache缓存集群,以确保数据更新能在各个进程中同步。以下是如何使用EhCache实现缓存集群的详细步骤: 首先,确保缓存对象是可序列化的。在上述例子中,`User`实体需要实现`...

    ehcache 缓存

    **Ehcache缓存系统详解** Ehcache是一款开源、高性能、轻量级的Java缓存框架,广泛应用于各种Java应用程序中,以提高数据访问速度并降低数据库负载。它的核心特性包括内存缓存、磁盘存储和分布式缓存,使得在大数据...

    ehcache项目缓存技术

    3. **分布式缓存**:Ehcache支持分布式部署,通过 Terracotta Server 集群,可以在多台服务器间共享和同步缓存,提供高可用性和负载均衡。 4. **缓存策略**:除了LRU和LFU,Ehcache还提供了TTL(Time To Live)和...

    Java流行ehcache缓存

    - **异常处理**:正确处理缓存未命中、数据同步等问题,确保系统稳定性。 Ehcache作为Java缓存解决方案的重要一环,其灵活性和易用性使其成为许多开发者首选的缓存工具。理解并掌握Ehcache的配置和使用,对于提升...

    Spring+EhCache缓存实例

    **Spring+EhCache缓存实例详解** 在现代的Java企业级应用中,缓存技术扮演着至关重要的角色,它能够显著提升系统性能,减少数据库负载。Spring框架与EhCache的结合,为开发者提供了一种高效、易用的缓存解决方案。...

    ehcache缓存的配置

    ### ehcache缓存的配置详解 #### 一、ehcache简介 ehcache 是一款开源的高性能 Java 缓存框架,广泛应用于 Java 应用程序中,用于提高应用程序性能。通过在内存或磁盘中存储数据副本,ehcache 可以减少数据库访问...

    Ehcache缓存配置.doc

    `CacheManagerPeerProvider`配置则涉及分布式缓存,它可以让你设置Ehcache如何与其他节点进行通信,实现分布式缓存的同步。然而,这个部分在提供的内容中未给出完整的配置示例,通常会包含如`rmiServerHost`、`rmi...

    ehcache 缓存技术

    **EHCache缓存技术** EHCache是一款高性能、轻量级的Java缓存框架,它广泛应用于各种Java应用程序中,特别是需要提升数据访问速度和减少数据库负载的场景。EHCache是基于内存的,但同时支持持久化,能有效地提高...

    ehcache缓存方法返回结果(一)-MethodCacheInterceptor

    《ehcache缓存方法返回结果(一)-MethodCacheInterceptor》 在软件开发中,缓存是一种优化性能的重要手段,尤其是在高并发、大数据量的场景下。本文将深入探讨 Ehcache,一个流行的Java缓存库,以及如何通过...

    hibernate缓存ehcache用法

    更新时,Hibernate会同步更新缓存中的数据。 总结来说,这篇博客可能涵盖了如何在Hibernate项目中集成Ehcache,包括添加依赖、配置缓存策略、实体类缓存注解等步骤。了解和正确使用Hibernate与Ehcache的缓存机制,...

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

    Ehcache是一个高性能、轻量级的Java分布式缓存库,它被广泛应用于提升应用程序的性能,通过存储经常访问的数据来减少对数据库的依赖,从而加快系统的响应速度。本入门案例将带你了解如何使用Ehcache实现分布式缓存,...

    缓存框架-Ehcache学习笔记

    Ehcache 支持通过 Terracotta 平台实现集群间的缓存同步。 ### 3. Ehcache 使用 - **配置**: Ehcache 配置可以通过 XML 文件或 Java API 进行。XML 文件通常命名为 `ehcache.xml`,包含缓存的大小、过期策略等设置...

    Ehcache缓存框架的简单应用

    5. **缓存更新策略**:如同步更新、异步更新等。 **Ehcache的使用** 在Java项目中,可以使用以下步骤来集成并使用Ehcache: 1. 引入依赖:将Ehcache的JAR包添加到项目的类路径中。 2. 创建缓存管理器:通过`...

Global site tag (gtag.js) - Google Analytics