ehcache 集群使用 rmi方式

11 篇文章 0 订阅

http://blog.csdn.net/xh199110/article/details/38474061?utm_source=tuicool&utm_medium=referral

ehcache 有几种方式集群,rmi,jgroup还有jms;这里讲一下ehcache的使用

ehcache 使用rmi方式复制缓存是可取的,原因如下:

1、rmi是Java 默认的远程机制

2、可以调优tcp选项

3、Elements因为要存到磁盘,所以肯定是早已序列化。所以不需要借助xml格式化什么的

4、通过配置可以通过防火墙

rmi方式集群交互图:

Ehcache的rmi方式是一种点对点的协议,因此它会产生很多局域网的内部通信,当然Ehcache会通过一种异步批处复制理机制类解决

如果要配置ehcache 需要配置一下元素

PeerProvider

CacheManagerPeerListener

配置Provider,这里有两种方式:自动发现、手动配置

自动方式:自动发现方式使用tcp广播来建立和包含一个广播组,它的特征是最小配置和对成员组的自动添加和管理。没有那个服务器是有优先级的。对等点每一秒中向广播组发送心跳,如果一个对等点在五秒钟内没发送过来,则此对等点将会被删除,如果有新的,则会被加入集群

cacheManagerPeerProviderFactory 的properties有以下配置:

peerDiscovery=automatic

multicastGroupAddress=230.0.0.1
multicastGroupPort=40001
timeToLive=0-255 
hostName=hostname

   peerDiscovery 方式:atutomatic 为自动 ;mulicastGroupAddress 广播组地址:230.0.0.1;mulicastGroupPort 广播组端口:40001;timeToLive是指搜索范围:0是同一台服务器,1是同一个子网,32是指同一站点,64是指同一块地域,128是同一块大陆,还有个256,我就不说了;hostName:主机名或者ip,用来接受或者发送信息的接口

在我这次试验中具体如下:

 

[html]  view plain  copy
  1. <cacheManagerPeerProviderFactory  
  2.     class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"  
  3.     properties="peerDiscovery=automaticmulticastGroupAddress=230.0.0.1,  
  4.     multicastGroupPort=4446timeToLive=32,hostName=192.168.1.101" />  

当然还有一种方式就是手动配置,贴上例子,但不作叙述了

server1

[html]  view plain  copy
  1. <cacheManagerPeerProviderFactory  
  2. class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"  
  3. properties="peerDiscovery=manual,  
  4. rmiUrls=//server2:40001/sampleCache11|//server2:40001/sampleCache12"/>  
server2

[html]  view plain  copy
  1. <cacheManagerPeerProviderFactory  
  2.         class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"  
  3.     properties="peerDiscovery=manual,  
  4. rmiUrls=//server1:40001/sampleCache11|//server1:40001/sampleCache12"/>  

将方式配好之后需要配置listener才会有用,接下来讲讲:Listener

Listener是用来监听从集群发送过来的信息

Listenner有两个属性:class和propertis

class 一个完整的工厂类名

properties 都好分割的对facotory有用的属性

此次实验具体配置如下:

[html]  view plain  copy
  1. <cacheManagerPeerListenerFactory  
  2.         class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"  
  3.         properties="hostName=192.168.1.101, port=40001,  
  4. socketTimeoutMillis=2000/>  
hostName指的是本机,这里注意如果使用的localhost,则只会对本机有效,请使用子网内的ip地址或者主机名,port端口 40001,socketTimeoutMillis是指socket子模块的超时时间,默认是2000ms,注意port两台主机可以相同可以不同。最好相同,个人建议

然后配置缓存的复制 Replicators:

本机配置如下:

[html]  view plain  copy
  1. <cache name="myCache" maxEntriesLocalHeap="10" eternal="false"  
  2.         timeToIdleSeconds="10000" timeToLiveSeconds="10000" overflowToDisk="false">  
  3.         <cacheEventListenerFactory  
  4.             class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"  
  5.             properties="replicateAsynchronously=truereplicatePuts=truereplicateUpdates=true,  
  6.                             replicateUpdatesViaCopy=falsereplicateRemovals=true,asynchronousReplicationIntervalMillis=1000"  
  7.                              />  
  8.          <bootstrapCacheLoaderFactory    
  9.                 class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"/>   
  10.     </cache>  
name为cache制定名字,maxEntriesLocalHeap:内存中可驻留最大Element数量,timeToLiveSeconds 生存周期 10000s;overflowToDisk:当内存不足,是否启用磁盘:这里为false;给myCache价格监听,然后是异步方式,在put,update,copy,remove操作是否复制,然后同步时间1s,bootstrapCacheLoaderFactory 工厂是指启动是指一启动就同步数据

以下是完整的配置

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"  
  5.     monitoring="autodetect" dynamicConfig="true">  
  6.   
  7.     <diskStore path="D:/ehcache/diskStore" />  
  8.   
  9.     <cacheManagerPeerProviderFactory  
  10.         class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"  
  11.         properties="peerDiscovery=automaticmulticastGroupAddress=230.0.0.1,  
  12.         multicastGroupPort=4446timeToLive=32,hostName=192.168.1.101" />  
  13.   
  14.     <cacheManagerPeerListenerFactory  
  15.         class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"  
  16.         properties="hostName=192.168.1.101, port=40001,  
  17.     socketTimeoutMillis=2000/>  
  18.   
  19.     <cache name="myCache" maxEntriesLocalHeap="10" eternal="false"  
  20.         timeToIdleSeconds="10000" timeToLiveSeconds="10000" overflowToDisk="false">  
  21.         <cacheEventListenerFactory  
  22.             class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"  
  23.             properties="replicateAsynchronously=truereplicatePuts=truereplicateUpdates=true,  
  24.                             replicateUpdatesViaCopy=falsereplicateRemovals=true,asynchronousReplicationIntervalMillis=1000"  
  25.                              />  
  26.          <bootstrapCacheLoaderFactory    
  27.                 class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"/>   
  28.     </cache>  
  29. </ehcache>  

在server2 也就是 192.168.1.116 在hostName配置成此地址,就行了

下面是测试代码

[java]  view plain  copy
  1. package com.ehcache;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5.   
  6. import net.sf.ehcache.Cache;  
  7. import net.sf.ehcache.CacheException;  
  8. import net.sf.ehcache.CacheManager;  
  9. import net.sf.ehcache.Element;  
  10.   
  11.   
  12.   
  13.   
  14.   
  15. public class Test2 {  
  16.        public static void main(String[] args) throws InterruptedException {    
  17.            InputStream is=null;  
  18.         CacheManager manager=null;  
  19.         try {  
  20.             is = Test2.class.getResourceAsStream("/ehcache.xml");  
  21.                manager = CacheManager.newInstance(is);  
  22.         } catch (CacheException e1) {  
  23.             try {  
  24.                 if(is!=null){  
  25.                 is.close();  
  26.                 is=null;  
  27.                 }  
  28.             } catch (IOException e) {  
  29.                 e.printStackTrace();  
  30.             }  
  31.             e1.printStackTrace();  
  32.         }  
  33.              
  34.            Cache cache = manager.getCache("myCache");    
  35.         
  36.            Element element = new Element("client3" + System.currentTimeMillis(), "client3");    
  37.            cache.put(element);    
  38.            int i=0;  
  39.            while (true)    
  40.            {    
  41.                Element element2 = new Element("client-3-"+i,i);   
  42.                cache.put(element2);  
  43.                Thread.sleep(3000);    
  44.                System.out.println("\n");    
  45.                for (Object key : cache.getKeys())    
  46.                {    
  47.                    System.out.println(key + ":" + cache.get(key).getObjectValue());    
  48.                }    
  49.                i++;  
  50.            }    
  51.         }  
  52. }  


server2 的测试代码将 element那里改成client-2-即可

然后贴上效果图一张:


client3 同步到client2的数据了

另外附叙述ehcache的淘汰缓存的算法

LRU是Least Recently Used 近期最少使用算法;

FIFO 以一种队列方式谁先进谁先出

LFUleast frequently used即最不经常使用页置换算法

这是工程文件的下载地址:点击打开链接


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值