Ehcache与terracotta集群配置

转自:http://bluewind1521.iteye.com/blog/1636283


 以前一直想研究一下Ehcache与terracotta的分布式缓存,因为工作原因,再加上本人比较懒,一直没时间鼓捣,今天突然下定决心开始动手实践,果断上官网看文档后开始动手搭建,虽然搭建时也遇到了一些问题,最终在坚持不懈的啃官方文档后(感谢terracotta团队,文档写的很详细),终于把环境搭建完成。

首先下载Ehcache,我下载的是ehcache-2.5.2,下载后解压文件,目录结构如下:

          

主要说明下terracotta这个目录,这个是terracotta服务器,里面有个bin目录,包含启动服务器和关闭服务的脚本,以及开发控制台:

start-tc-server.bat   开启服务器

 dev-console.bat    开发控制台(可以监控terracotta服务器)

    下面开始进入正题(搭建ehcache环境、java环境的省略了,大家自己动动手),主要说明下服务器,为了与正式环境贴近,我在自己电脑上开了个虚拟机,这样就相当于2台电脑了:

A服务器:192.168.1.100

 B服务器:192.168.1.131

然后将ehcache分别放到A服务器和B服务器里。服务器搭建好后,开始动手写ehcache测试类,分别建立一个ehcache(放到A服务器中)与ehcache_1(放到B服务器中)的项目(项目代码基本一样,ehcache项目有点区别),下面说明2个项目的ehcache.xml的配置信息:

Ehcache项目中的ehcache.xml的配置信息:


Xml代码   收藏代码
  1. <span style="font-size: medium;"><?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.          xsi:noNamespaceSchemaLocation="ehcache.xsd"  
  5.          updateCheck="true" monitoring="off"  
  6.          dynamicConfig="true">  
  7.   
  8.     
  9.     <cacheManagerEventListenerFactory class="" properties=""/>  
  10.   
  11.     <terracottaConfig url="192.168.1.100:9510,192.168.1.131:9510"/> <!--terracotta服务器配置,默认端口为9510,多个服务器用,分隔  -->  
  12.   
  13.     <defaultCache  
  14.            maxEntriesLocalHeap="0"  
  15.            eternal="false"  
  16.            overflowToDisk="false"  
  17.            timeToIdleSeconds="30"  
  18.            timeToLiveSeconds="60">  
  19.            <terracotta clustered="true" /> <!-- 开启集群 -->  
  20.     </defaultCache>  
  21.       
  22.     <cache name="demoCache"   
  23.             maxBytesLocalHeap="200M"  
  24.             eternal="false"  
  25.             overflowToDisk="false"  
  26.             timeToIdleSeconds="30"  
  27.             timeToLiveSeconds="60"  
  28.             memoryStoreEvictionPolicy="LFU"  
  29.             maxElementsOnDisk="100000"  
  30.             >  
  31.     <terracotta clustered="true" />   
  32.     </cache>  
  33.   
  34. </ehcache>  
  35. </span>  


 Ehcache_1项目中的ehcache.xml的配置信息与ehcache项目中的一样。

项目配置好后,准备启动terracotta服务器,启动服务器之前,先写一段terracotta的配置文件,新建一个tc-config.xml的文件,内容如下:


Xml代码   收藏代码
  1. <span style="font-size: medium;"><?xml version="1.0" encoding="UTF-8" ?>  
  2.  <tc:tc-config xmlns:tc="http://www.terracotta.org/config">  
  3.    <servers>  
  4.      <!-- Sets where the Terracotta server can be found. Replace the value of host with the server's IP address. -->   
  5.      <server host="192.168.1.100" name="Server1">  
  6.        <data>%(user.home)/terracotta/server-data</data>  
  7.        <logs>%(user.home)/terracotta/server-logs</logs>  
  8.      </server>  
  9.      <!-- If using a standby Terracotta server, also referred to as an ACTIVE-PASSIVE configuration, add the second server here. -->   
  10.        
  11.    <server host="192.168.1.131" name="Server2">   
  12.      <data>%(user.home)/terracotta/server-data</data>  
  13.      <logs>%(user.home)/terracotta/server-logs</logs>  
  14.    </server>   
  15.    <!-- If using more than one server, add an <ha>   
  16.    section. -->  
  17.     
  18.    <ha>   
  19.    <mode>networked-active-passive</mode>  
  20.    <networked-active-passive>  
  21.      <election-time>5</election-time>  
  22.    </networked-active-passive>  
  23.  </ha>  
  24.   
  25.   </servers>  
  26.  <!-- Sets where the generated client logs are saved on clients. Note that the exact location of Terracotta logs on client machines may vary based on the value of user.home and the local disk layout. -->   
  27.    <clients>  
  28.           <logs>%(user.home)/terracotta/client-logs</logs>  
  29.    </clients>  
  30.  </tc:tc-config></span>  


 上面的配置信息中配置了2个server,一个是本机的(A服务器),另外一个是B服务器的,如果此处只配置一个服务器,比如本机的服务器,而不将B服务器配置在这里,将无法建立集群。

  然后将tc-config.xml分别放到A服务器和B服务器的ehcache\terracotta\bin下,下面开始分别启动A、B服务器的terracotta,在ehcache\terracotta\bin下找到start-tc-server.bat,下面是启动命令:

start-tc-server.bat –f tc-config.xml ---f表示从指定配置文件启动

启动结果如下图:

A服务器:


 

B服务器:


 


从启动结果中可以看到NodeID[XXXXXX] joined the cluster,节点已加入集群,代表terracotta结群建立成功,下面运行测试代码:
A服务器:

Java代码   收藏代码
  1. <span style="font-size: medium;">import net.sf.ehcache.Cache;  
  2. import net.sf.ehcache.CacheManager;  
  3. import net.sf.ehcache.Element;  
  4.   
  5. public class EhcacheDemo {  
  6.       
  7.     private static final CacheManager CACHE_MANAGER = CacheManager.create();  
  8.       
  9.     public static Cache getCache(String cacheName){  
  10.         return CACHE_MANAGER.getCache(cacheName);  
  11.     }  
  12.       
  13.     public static Object getObjectValue(String key){  
  14.          Element e = getCache("demoCache").get(key);  
  15.          Object val = null;  
  16.          if(e == null){  
  17.                 System.out.println("缓存中不存在,读取数据");  
  18.                 /**用于测试集群是否正常工作,当缓存中的数据超时时加入新数据,同时将B服务器中的terracotta服务器关闭,测试B服务器中的程序是否能够获取数据 
  19.                  *  
  20.                  * */  
  21.                 EhcacheDemo.put("name""11111");  
  22.                 EhcacheDemo.put("password""333");  
  23.          }else{  
  24.              val = e.getObjectValue();  
  25.          }  
  26.          return val;  
  27.     }  
  28.       
  29.     public static void put(String key, Object val){  
  30.         Element e = new Element(key, val);  
  31.         getCache("demoCache").put(e);  
  32.     }  
  33.       
  34.     public static void delete(String key){  
  35.         getCache("demoCache").remove(key);  
  36.     }  
  37.       
  38.       
  39.     public void close(){  
  40.         CACHE_MANAGER.shutdown();  
  41.     }  
  42.   
  43.     /** 
  44.      * @param args 
  45.      */  
  46.     public static void main(String[] args) {  
  47.         EhcacheDemo.put("name""sssssss");  
  48.         EhcacheDemo.put("password""22222");  
  49.         Thread t = new Thread(new Runnable() {  
  50.               
  51.             @Override  
  52.             public void run() {  
  53.                 while(true){  
  54.                     try {  
  55.                         Thread.currentThread().sleep(10000L);  
  56.                     } catch (InterruptedException e) {  
  57.                         e.printStackTrace();  
  58.                     }  
  59.                     System.out.println(EhcacheDemo.getObjectValue("name") +"   "+ getObjectValue("password"));  
  60.                 }  
  61.                   
  62.             }  
  63.         });  
  64.   
  65.         t.start();  
  66.     }  
  67.   
  68. }  
  69. </span>  

 

B服务器:

Java代码   收藏代码
  1. <span style="font-size: medium;">import net.sf.ehcache.Cache;  
  2. import net.sf.ehcache.CacheManager;  
  3. import net.sf.ehcache.Element;  
  4.   
  5. public class EhcacheDemo {  
  6.       
  7.     private static final CacheManager CACHE_MANAGER = CacheManager.create();  
  8.       
  9.     public static Cache getCache(String cacheName){  
  10.         return CACHE_MANAGER.getCache(cacheName);  
  11.     }  
  12.       
  13.     public static Object getObjectValue(String key){  
  14.          Element e = getCache("demoCache").get(key);  
  15.          Object val = null;  
  16.          if(e == null){  
  17.                 System.out.println("缓存中不存在,读取数据");  
  18.          }else{  
  19.              val = e.getObjectValue();  
  20.          }  
  21.          return val;  
  22.     }  
  23.       
  24.     public static void put(String key, Object val){  
  25.         Element e = new Element(key, val);  
  26.         getCache("demoCache").put(e);  
  27.     }  
  28.       
  29.     public static void delete(String key){  
  30.         getCache("demoCache").remove(key);  
  31.     }  
  32.       
  33.       
  34.     public void close(){  
  35.         CACHE_MANAGER.shutdown();  
  36.     }  
  37.   
  38.     /** 
  39.      * @param args 
  40.      */  
  41.     public static void main(String[] args) {  
  42.         EhcacheDemo.put("name""sssssss");  
  43.         EhcacheDemo.put("password""22222");  
  44.         Thread t = new Thread(new Runnable() {  
  45.               
  46.             @Override  
  47.             public void run() {  
  48.                 while(true){  
  49.                     try {  
  50.                         Thread.currentThread().sleep(10000L);  
  51.                     } catch (InterruptedException e) {  
  52.                         e.printStackTrace();  
  53.                     }  
  54.                     System.out.println(EhcacheDemo.getObjectValue("name") +"   "+ getObjectValue("password"));  
  55.                 }  
  56.                   
  57.             }  
  58.         });  
  59.   
  60.         t.start();  
  61.     }  
  62.   
  63. }  
  64. </span>  



A与B服务器中的程序有一点区别,详见注释部分,主要为了测试集群是否工作正常,当一个节点挂掉之后,B服务器上的测试程序是否能获取新的缓存数据。

同时运行2个测试程序后,我们可以观察terracotta是否正常工作,运行ehcache\terracotta\bin下的dev-console.bat并连接到2个服务器的terracotta服务器:


                     


从上图中可以看到数据与请求已经同步(connected clients),下面我们再测试将B服务器中的terracotta服务器关闭,然后观察B服务器上的测试数据是否能获取数据:


                            


从上图可以看出,可以获取数据。代表数据获取正常而B服务器中的terracotta服务器已经关闭了(如下图):


                        



     至此Ehcache与terracotta的分布式缓存配置就完成了。下面说下配置过程中需要注意的位置,也是本人刚开始搭建的时候遇到的问题,主要是数据与请求不能同步的问题,该问题主要是tc-config.xml没有配置好,刚开始我只配置了一个server,也就是分别只配置了本机的IP,后来查看官方文档后,将A、B服务器的terracotta服务器都加入进去就可以了。

   以上就是所有的配置过程,在此记录一下备忘,也与大家分享一下,如有不正确的位置感谢大家拍砖。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值