ehcache jgroups 集群配置

Ehcache的学习文档跟配置文档

Ehcache默认会读取src目录下的ehcache.xml来进行配置.

 

基本的单机下的配置ehcache.xml配置.

 

[html]  view plain  copy
 
  1. <?xmlversionxmlversion="1.0" encoding="UTF-8"?>  
  2. <ehcache>  
  3.          <diskStorepathdiskStorepath="java.io.tmpdir"/>  
  4.          <defaultCache  
  5.             maxElementsInMemory="10000"  
  6.             eternal="false"  
  7.             timeToIdleSeconds="120"  
  8.             timeToLiveSeconds="120"  
  9.             diskSpoolBufferSizeMB="30"  
  10.             maxElementsOnDisk="10000000"  
  11.             diskExpiryThreadIntervalSeconds="120">  
  12.             <persistence strategy="localTempSwap"/>  
  13.          </defaultCache>  
  14.          <cache  name="resourceCache"  
  15.                             maxElementsInMemory="10000"   
  16.                             eternal="false"   
  17.                             timeToIdleSeconds="120"   
  18.                             timeToLiveSeconds="120"   
  19.                             overflowToDisk="true"   
  20.                             maxElementsOnDisk="10000000"   
  21.                             diskPersistent="false"   
  22.                             diskExpiryThreadIntervalSeconds="120"   
  23.                             memoryStoreEvictionPolicy="LRU">  
  24.          </cache>  
  25. </ehcache>  
  26.    



 

配置参数简单讲解:

diskStore  配置为Java.io.tmpdir的具体路径为system.getProperty("java.io.tmpdir")

cache在内存中的存的的Element超过了maxElementsInMemory配置的数了,

会将超出的部分进行存储的地址.

         maxElementsInMemory:最大的在内存中存储Element的个数

      eternal="false" :对象时候永久有效,设置了这个timeOut将会失效..(不太懂)

timeToIdleSeconds="120"  :对象多久没被访问就会失效.

timeToLiveSeconds="120"对象可以存活的时间

diskSpoolBufferSizeMB="30"这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。

maxElementsOnDisk="10000000"每个cache最多有多少个element在磁盘中.( 硬盘最大缓存个数。)

diskExpiryThreadIntervalSeconds="120" 磁盘失效线程运行时间间隔,默认是120秒。

  <persistencestrategy="localTempSwap"/>  程序当掉了,数据恢复的策略.这样会让之前的数据,仍然有效.  localTempSwap :个人理解是:内存当中的element不会失效,磁盘上的element会失效.

 

memoryStoreEvictionPolicy="LRU"  移除元素的策略

复习一下ehcache中缓存的3种清空策略:

1 FIFO,first in first out,这个是大家最熟的,先进先出,不多讲了

2 LFU, Less Frequently Used,就是上面例子中使用的策略,直白一点就是讲一直以来最少被使用的。如上面所讲,缓存的元素有一个hit属性,hit值最小的将会被清出缓存。

2 LRU,Least Recently Used,最近最少使用的,缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。

 

单机下的java测试程序:

 

[java]  view plain  copy
 
  1. package com.zwy.ehcach;  
  2.    
  3. import net.sf.ehcache.CacheManager;  
  4. import net.sf.ehcache.Element;  
  5. import net.sf.ehcache.config.CacheConfiguration;  
  6. import net.sf.ehcache.config.Configuration;  
  7.    
  8. public class zwyehcach {  
  9.          private staticConfiguration config;  
  10.    
  11.          public static voidmain(String[] args) {  
  12.                    System.out.println(System.getProperty("java.io.tmpdir"));  
  13.                     
  14.                    finalCacheManager manager = CacheManager.create();  
  15.                    config = newConfiguration();  
  16.    
  17.                    System.out.println(manager.getCache("D_ID_CACHE"));  
  18.                    System.out.println(manager.getCache("AutoAssignUsers"));  
  19.                    //manager.addCacheIfAbsent("D_ID_CACHE");如果D_ID_CACHE在ehcache.xml中没有配置创建,可以通过这个方法进行创建  
  20.                    manager.getCache("AutoAssignUsers").put(newElement("name","AutoAssignUsersa"));  
  21.                    System.out.println( manager.getCache("AutoAssignUsers"));  
  22.                    System.out.println( manager.getCache("AutoAssignUsers").get("name"));  
  23. //manager.getCache("AutoAssignUsers").get("bean0").getObjectValue()用这个方法来获取对应的Value,可以强转为对象.  
  24.                   //manager.shutdown(); ehcache进行关闭  
  25.          }  
  26.    
  27. }  


 

 

对于ehcache 的集群有好几种方案:

1.Terracotta

2.RMI

3.JMS

4.JGroups

5.EhCache Server

我对JGroups的方案进行了配置.

所需要的jar包

ehcache-jgroupsreplication-1.7.jar

jgroups-3.6.3.Final.jar

ehcache-2.7.4.jar

slf4j-api-1.6.6.jar

log4j-1.2.17.jar

slf4j-jdk14-1.6.6.jar

根据官网配置过程中出的的错误以及解决办法.

错误1.Problem(Abstract)

jgroups tagproperty_string using TCP instead of UDP will not connect on startup with errorand will give the following error :-

ALL 000000000000GLOBAL_SCOPE java.lang.IllegalArgumentException: [JGRP00001] configurationerror: the following properties in TCP are not recognized: {start_port=50061}

 需要把start_port改为bind_port

错误2.

ehcacheJGRP000001: the following properties in TCPPING are not recognized:{up_thread=true, down_thread=true}

解决参考: http://www-01.ibm.com/support/docview.wss?uid=swg21693135

是jgroups的版本不一致导致的…

我重新换了配置方法.

具体见配置文件.jroups tcp的配置xml如下:

 

[html]  view plain  copy
 
  1. <?xmlversionxmlversion="1.0" encoding="UTF-8"?>  
  2. <ehcache>  
  3.    
  4.     <diskStorepathdiskStorepath="java.io.tmpdir" />                     
  5. <cacheManagerPeerProviderFactory  
  6. class="net.sf.ehcache.distribution.jgroups.JGroupsCacheManagerPeerProviderFactory"  
  7. properties="connect=TCP(bind_addr=192.168.1.3;bind_port=50062):  
  8. TCPPING(initial_hosts=192.168.1.3[50061],192.168.1.3[50062];  
  9. port_range=1;timeout=5000;num_initial_members=2):MERGE2(min_interval=3000;max_interval=5000):  
  10. FD_ALL(interval=5000;timeout=20000):FD(timeout=5000;max_tries=48;):VERIFY_SUSPECT(timeout=1500):pbcast.NAKACK(retransmit_timeout=100,200,300,600,1200,2400,4800;discard_delivered_msgs=true):pbcast.STABLE(stability_delay=1000;desired_avg_gossip=20000;max_bytes=0):pbcast.GMS(print_local_addr=true;join_timeout=5000)"  
  11. propertySeparator="::"/>  
  12.              
  13.     <defaultCachemaxElementsInMemorydefaultCachemaxElementsInMemory="10000" overflowToDisk="false"  
  14.         eternal="false"memoryStoreEvictionPolicy="LRU"  
  15.         maxElementsOnDisk="10000000"diskExpiryThreadIntervalSeconds="600"  
  16.         timeToIdleSeconds="3600"timeToLiveSeconds="100000"  
  17.         diskPersistent="false" >  
  18.         <cacheEventListenerFactory  
  19.        class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"  
  20.        properties="replicateAsynchronously=truereplicatePuts=true,replicateUpdates=true,  
  21.         replicateUpdatesViaCopy=true,replicateRemovals=true "/>  
  22.         </defaultCache>  
  23.      
  24.     <cache name="resourceCache"maxElementsInMemory="10000" eternal="false"  
  25.         overflowToDisk="false"timeToIdleSeconds="120" timeToLiveSeconds="120"  
  26.        memoryStoreEvictionPolicy="LRU">  
  27.     <cacheEventListenerFactory  
  28.        class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"  
  29.        properties="replicateAsynchronously=truereplicatePuts=true,replicateUpdates=true,  
  30.         replicateUpdatesViaCopy=true,replicateRemovals=true "/>  
  31.     </cache>  
  32.          
  33. </ehcache>  

 

一台服务器用两个端口来模拟两台服务器:分别为是192.168.1.3: 50061, 192.168.1.3: 50062

配置过程中,两个程序不一样的地方是在bind_addr=ip1;bind_port=port1,要分别配置对应的ip和端口,其他的都一样.

Jgroups cacheManagerPeerProviderFactory参数详解:

https://developer.jboss.org/wiki/BelaBansJGroupsManualTranslationSerialIV-

cacheEventListenerFactory的配置参数: 

replicatePuts=true| false - whether new elements placed in a cache are replicated to others.Defaults to true.

新元素的添加是否会被复制到其他机器上.

replicateUpdates=true| false - whether new elements which override an element already existing withthe same key are replicated. Defaults to true.

元素的更新是否会被复制到其他机器上.

replicateRemovals=true- whether element removals are replicated. Defaults to true.

元素的移除是否会被更新到到其他机器上.

 replicateAsynchronously=true| false - whether replications are asyncrhonous (true) or synchronous (false).Defaults to true.

同步操作是异步还是同步的.默认是异步的.

replicateUpdatesViaCopy=true| false - whether the new elements are copied to other caches (true), orwhether a remove message is sent. Defaults to true.

元素的复制消息或者删除消息是否会被同步.默认是true.

asynchronousReplicationIntervalMillisdefault 1000ms Time between updates when replication is asynchroneous

asynchronousReplicationIntervalMillis所有的更新操作是异步的时候,会在1秒内同步完成.

配置jgroups udp 集群.

Ehcache.xml如下

 

[html]  view plain  copy
 
  1. <?xmlversionxmlversion="1.0" encoding="UTF-8"?>  
  2. <ehcache>  
  3.     <diskStorepathdiskStorepath="java.io.tmpdir" />                         
  4.          <cacheManagerPeerProviderFactory  
  5.          class="net.sf.ehcache.distribution.jgroups.JGroupsCacheManagerPeerProviderFactory"  
  6.          properties="connect=UDP(mcast_addr=231.12.21.132;mcast_port=45566;bind_port=33433):PING:  
  7.          MERGE2:FD_SOCK:VERIFY_SUSPECT:pbcast.NAKACK:UNICAST:pbcast.STABLE:FRAG:pbcast.GMS"  
  8.          propertySeparator="::"  
  9.          />  
  10.              
  11.     <defaultCachemaxElementsInMemorydefaultCachemaxElementsInMemory="10000" overflowToDisk="false"  
  12.         eternal="false"memoryStoreEvictionPolicy="LRU"  
  13.         maxElementsOnDisk="10000000"diskExpiryThreadIntervalSeconds="600"  
  14.         timeToIdleSeconds="3600"timeToLiveSeconds="100000"  
  15.         diskPersistent="false" >  
  16.         <cacheEventListenerFactory  
  17.        class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"  
  18.        properties="replicateAsynchronously=truereplicatePuts=true,replicateUpdates=true,  
  19.         replicateUpdatesViaCopy=true,replicateRemovals=true "/>  
  20.         </defaultCache>  
  21.      
  22.     <cache name="resourceCache"maxElementsInMemory="10000" eternal="false"  
  23.         overflowToDisk="false"timeToIdleSeconds="1" timeToLiveSeconds="600"  
  24.        memoryStoreEvictionPolicy="LRU">  
  25.     <cacheEventListenerFactory  
  26.        class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"  
  27.        properties="replicateAsynchronously=truereplicatePuts=true,replicateUpdates=true,  
  28.         replicateUpdatesViaCopy=true,replicateRemovals=true "/>  
  29.     </cache>         
  30. </ehcache>  


配置完成后:记得要把端口mcast_port;bind_port打开.

 

碰见了错误:

14:54:11,953[ main ] [ ERROR ]:342 - failed setting ip_ttl 

在java的编译参数中添加   -Djava.NET.preferIPv4Stack=true

   

参考:

http://ehcache.org/documentation/2.8/configuration/fast-restart

http://nassir.iteye.com/blog/1602372

http://www.oschina.net/translate/jgroups-writing-a-simple-application?cmp&p=1#

官方: http://ehcache.org/documentation/2.8/replication/jgroups-replicated-caching

 

实例下载

http://download.csdn.net/detail/w329636271/8845777

猜你喜欢

转载自lishuaishuai.iteye.com/blog/2382202