ehcache jgroups 集群配置

时间:2022-10-22 08:10:18

Ehcache的学习文档跟配置文档

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

 

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

<?xmlversion="1.0" encoding="UTF-8"?>
<ehcache>
         <diskStorepath="java.io.tmpdir"/>
         <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            diskSpoolBufferSizeMB="30"
            maxElementsOnDisk="10000000"
            diskExpiryThreadIntervalSeconds="120">
            <persistence strategy="localTempSwap"/>
         </defaultCache>
         <cache  name="resourceCache"
                            maxElementsInMemory="10000" 
                            eternal="false" 
                            timeToIdleSeconds="120" 
                            timeToLiveSeconds="120" 
                            overflowToDisk="true" 
                            maxElementsOnDisk="10000000" 
                            diskPersistent="false" 
                            diskExpiryThreadIntervalSeconds="120" 
                            memoryStoreEvictionPolicy="LRU">
         </cache>
</ehcache>
 


配置参数简单讲解:

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测试程序:

package com.zwy.ehcach;
 
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import net.sf.ehcache.config.CacheConfiguration;
import net.sf.ehcache.config.Configuration;
 
public class zwyehcach {
         private staticConfiguration config;
 
         public static voidmain(String[] args) {
                   System.out.println(System.getProperty("java.io.tmpdir"));
                  
                   finalCacheManager manager = CacheManager.create();
                   config = newConfiguration();
 
                   System.out.println(manager.getCache("D_ID_CACHE"));
                   System.out.println(manager.getCache("AutoAssignUsers"));
                   //manager.addCacheIfAbsent("D_ID_CACHE");如果D_ID_CACHE在ehcache.xml中没有配置创建,可以通过这个方法进行创建
                   manager.getCache("AutoAssignUsers").put(newElement("name","AutoAssignUsersa"));
                   System.out.println( manager.getCache("AutoAssignUsers"));
                   System.out.println( manager.getCache("AutoAssignUsers").get("name"));
//manager.getCache("AutoAssignUsers").get("bean0").getObjectValue()用这个方法来获取对应的Value,可以强转为对象.
                  //manager.shutdown(); ehcache进行关闭
         }
 
}

 

对于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如下:

 

<?xmlversion="1.0" encoding="UTF-8"?>
<ehcache>
 
    <diskStorepath="java.io.tmpdir" />                   
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.jgroups.JGroupsCacheManagerPeerProviderFactory"
properties="connect=TCP(bind_addr=192.168.1.3;bind_port=50062):
TCPPING(initial_hosts=192.168.1.3[50061],192.168.1.3[50062];
port_range=1;timeout=5000;num_initial_members=2):MERGE2(min_interval=3000;max_interval=5000):
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)"
propertySeparator="::"/>
           
    <defaultCachemaxElementsInMemory="10000" overflowToDisk="false"
        eternal="false"memoryStoreEvictionPolicy="LRU"
        maxElementsOnDisk="10000000"diskExpiryThreadIntervalSeconds="600"
        timeToIdleSeconds="3600"timeToLiveSeconds="100000"
        diskPersistent="false" >
        <cacheEventListenerFactory
       class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
       properties="replicateAsynchronously=true, replicatePuts=true,replicateUpdates=true,
        replicateUpdatesViaCopy=true,replicateRemovals=true "/>
        </defaultCache>
   
    <cache name="resourceCache"maxElementsInMemory="10000" eternal="false"
        overflowToDisk="false"timeToIdleSeconds="120" timeToLiveSeconds="120"
       memoryStoreEvictionPolicy="LRU">
    <cacheEventListenerFactory
       class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
       properties="replicateAsynchronously=true, replicatePuts=true,replicateUpdates=true,
        replicateUpdatesViaCopy=true,replicateRemovals=true "/>
    </cache>
       
</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如下

<?xmlversion="1.0" encoding="UTF-8"?>
<ehcache>
    <diskStorepath="java.io.tmpdir" />                       
         <cacheManagerPeerProviderFactory
         class="net.sf.ehcache.distribution.jgroups.JGroupsCacheManagerPeerProviderFactory"
         properties="connect=UDP(mcast_addr=231.12.21.132;mcast_port=45566;bind_port=33433):PING:
         MERGE2:FD_SOCK:VERIFY_SUSPECT:pbcast.NAKACK:UNICAST:pbcast.STABLE:FRAG:pbcast.GMS"
         propertySeparator="::"
         />
           
    <defaultCachemaxElementsInMemory="10000" overflowToDisk="false"
        eternal="false"memoryStoreEvictionPolicy="LRU"
        maxElementsOnDisk="10000000"diskExpiryThreadIntervalSeconds="600"
        timeToIdleSeconds="3600"timeToLiveSeconds="100000"
        diskPersistent="false" >
        <cacheEventListenerFactory
       class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
       properties="replicateAsynchronously=true, replicatePuts=true,replicateUpdates=true,
        replicateUpdatesViaCopy=true,replicateRemovals=true "/>
        </defaultCache>
   
    <cache name="resourceCache"maxElementsInMemory="10000" eternal="false"
        overflowToDisk="false"timeToIdleSeconds="1" timeToLiveSeconds="600"
       memoryStoreEvictionPolicy="LRU">
    <cacheEventListenerFactory
       class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
       properties="replicateAsynchronously=true, replicatePuts=true,replicateUpdates=true,
        replicateUpdatesViaCopy=true,replicateRemovals=true "/>
    </cache>       
</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