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

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. <server host="192.168.1.131" name="Server2">
  11. <data>%(user.home)/terracotta/server-data</data>
  12. <logs>%(user.home)/terracotta/server-logs</logs>
  13. </server>
  14. <!-- If using more than one server, add an <ha>
  15. section. -->
  16. <ha>
  17. <mode>networked-active-passive</mode>
  18. <networked-active-passive>
  19. <election-time>5</election-time>
  20. </networked-active-passive>
  21. </ha>
  22. </servers>
  23. <!-- 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. -->
  24. <clients>
  25. <logs>%(user.home)/terracotta/client-logs</logs>
  26. </clients>
  27. </tc:tc-config></span>
<?xml version="1.0" encoding="UTF-8" ?> <tc:tc-config xmlns:tc="http://www.terracotta.org/config">   <servers>     <!-- Sets where the Terracotta server can be found. Replace the value of host with the server's IP address. -->      <server host="192.168.1.100" name="Server1">       <data>%(user.home)/terracotta/server-data</data>       <logs>%(user.home)/terracotta/server-logs</logs>     </server>     <!-- If using a standby Terracotta server, also referred to as an ACTIVE-PASSIVE configuration, add the second server here. -->         <server host="192.168.1.131" name="Server2">      <data>%(user.home)/terracotta/server-data</data>     <logs>%(user.home)/terracotta/server-logs</logs>   </server>    <!-- If using more than one server, add an <ha>    section. -->     <ha>    <mode>networked-active-passive</mode>   <networked-active-passive>     <election-time>5</election-time>   </networked-active-passive> </ha>  </servers> <!-- 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. -->    <clients>          <logs>%(user.home)/terracotta/client-logs</logs>   </clients> </tc:tc-config>

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

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

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服务器都加入进去就可以了。

Ehcache与terracotta集群配置相关推荐

  1. Terracotta 集群

    2019独角兽企业重金招聘Python工程师标准>>> 如何实现集群的. 测试代码编写 我们首先写一个demo,该demo在没有terracotta的环境下执行一次,看看结果 我们首 ...

  2. JAVAWEB部署在Terracotta集群上

    JAVAWEB部署在Terracotta集群上 把web项目部署在Terracotta集群上时要考虑这个项目是否使用缓存.如果使用缓存了.就要先做好缓存跟Terracotta的集成.          ...

  3. terracotta 集群(转)

    Terracotta是一款由美国Terracotta公司开发的著名开源Java集群平台.它在JVM与Java应用之间实现了一个专门处理集群功能的抽象层,以其特有的增量检测.智能定向传送.分布式协作.服 ...

  4. Hadoop集群配置(最全面总结)

    Hadoop集群配置(最全面总结) 通常,集群里的一台机器被指定为 NameNode,另一台不同的机器被指定为JobTracker.这些机器是masters.余下的机器即作为DataNode也作为Ta ...

  5. 基于redis的cas集群配置(转)

    1.cas ticket统一存储 做cas集群首先需要将ticket拿出来,做统一存储,以便每个节点访问到的数据一致.官方提供基于memcached的方案,由于项目需要,需要做计入redis,根据官方 ...

  6. redis+主从复制+集群配置

    redis+主从复制+集群配置 redis是一个key-value存储系统.和memcached类似,不过redis支持的value类型更多,主要有:string(字符串).list(链表).set( ...

  7. JavaEE进阶知识学习-----SpringCloud(四)Eureka集群配置

    Eureka集群配置 microservicecloud-eureka-7001使EurekaServer服务注册中心,一旦这个出现问题,那么微服务就不能正常的工作,为防止这种情况,所以出现了集群,就 ...

  8. FastDFS 集群配置(转载)

    本文转载自::https://blog.csdn.net/xiaoweiqb/article/details/68065618 配置最终目标 以4台服务做集群,其中2台作为tracker服务器又作为s ...

  9. Redis高可用之集群配置(六)

    0.Redis目录结构 1)Redis介绍及部署在CentOS7上(一) 2)Redis指令与数据结构(二) 3)Redis客户端连接以及持久化数据(三) 4)Redis高可用之主从复制实践(四) 5 ...

最新文章

  1. Protel中的快捷键使用(网上资源)
  2. java aspose重叠_Aspose.Words - 在特定位置合并两个文档
  3. python pp模块_python常用模块
  4. 无人驾驶出租车遭警察拦截后欲“潜逃”,AI 是原罪?
  5. 改善深层神经网络:超参数调整、正则化以及优化——2.7 RMSprop
  6. js Indexof的用法
  7. 跟我学Spring Cloud(Finchley版)-18-Zuul深入
  8. dijkstra + 优先队列(C++)
  9. cmd 批量复制文件/文件夹
  10. http协议及基于http协议的文件下载
  11. 互联网晚报 | 1月10日 星期一 | 天猫年货节正式开启;哪吒汽车第10万台量产车下线;三星永久关闭Tizen应用商店...
  12. 计算机通识培训,通识培训研修日志
  13. Python实现爬取腾讯招聘网岗位信息
  14. 什么是top sql
  15. This computer doesn’t have VT-X/AMD-v enabled. Enabling it in the BIOS is mandatory“!
  16. autojs ui界面漂亮模板2
  17. ElasticSearch技术方案(二)——站内搜索
  18. 《高质量C/C++编程指南》摘要
  19. 高可用性HA(High Availability)双机热备
  20. webgl径向模糊实现体积光

热门文章

  1. 【deepin】安装x11vnc和xrdp,使用windows远程deepin
  2. 巧用邮件营销技巧获客
  3. vite build
  4. 《幸福就在你身边》第五课、幸福就在当下【哈佛大学幸福课精华】
  5. 聚观早报 | 《三体》将于2023年上映;李恩祐加入京东董事会
  6. 雷达人体存在感应器,智能照明方案,实时精准感知
  7. 2023中国(苏州)国际电源工业展览会暨高端论坛
  8. mysql 中 RC、RR隔离级别的原理及区别
  9. 服务器配置与软硬件推荐参考资料
  10. 数据天下:阿里巴巴一淘背后的数据野心