Ehcache与terracotta集群配置

article/2025/11/8 7:28:14

首先下载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> 
<?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.       
  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> 
<?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.  
  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. </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.  
  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. </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服务器都加入进去就可以了。


http://chatgpt.dhexx.cn/article/jgXINDrY.shtml

相关文章

Terracotta 分布式缓存机制深入

Terracotta已收购Ehcache 数据缓存&#xff1a;&#xff08;无缝集成&#xff0c;代码注入方式&#xff0c;不需修改原代码&#xff09; Terracotta 功能&#xff08;JVM级POJO集群&#xff09;&#xff1a;DSO&#xff08;jvm本地线程加锁外&#xff0c;JVM集群上加排它锁&am…

分布式缓存之Ehcache与terracotta - Terracotta服务器概念篇

1、介绍 Terracotta服务器为Terracotta产品提供分布式数据平台。Terracotta服务器集群被称为Terracotta服务器阵列(TSA)。Terracotta服务器阵列可以从单个服务器&#xff0c;到一个用于高可用性(HA)的基本的双服务器串联&#xff0c;再到一个提供可配置的规模、高性能和深度故障…

IOCP与ASIO关系

笔记 IOCP&#xff1a;Input/Output Completion Port&#xff0c;Windows独有的内核对象&#xff0c;内核级完成IO的传输 &#xff0c;数据到来的时候自动把数据搬运到用户态的内存区&#xff0c;不需要拷贝文件描述符FileDescription&#xff08;fd&#xff09;到内核态查询状…

IOCP工作原理

文章来源&#xff1a;http://blog.csdn.net/zhongguoren666/article/details/7386592 本文主要探讨一下windows平台上的完成端口开发及其与之相关的几个重要的技术概念&#xff0c;这些概念都是与基于IOCP的开发密切相关的&#xff0c;对开发人员来讲&#xff0c;又不得不给予足…

Windows IOCP

Windows IOCP IOCP全称I/O Completion Port&#xff0c;中文译为I/O完成端口。IOCP是一个异步I/O的Windows API&#xff0c;它可以高效地将I/O事件通知给应用程序&#xff0c;类似于Linux中的Epoll。 简介 IOCP模型属于一种通讯模型&#xff0c;适用于Windows平台下高负载服务器…

IOCP小结

文章目录 一 什么是完成端口&#xff08;completion port&#xff09;对象二 使用IOCP的方法创建完成端口对象I/O服务线程和完成端口完成端口和重叠I/O 三 恰当地关闭IOCP四 IOCP大概的处理流程五 一个简单示例具体编程流程 当应用程序必须一次管理多个套接字时&#xff0c;完成…

IOCP高性能服务器的实现

应用场景说明&#xff1a;完成端口在面向现实应用的许多网络通信中应用很广泛&#xff0c;例如大型多人在线游戏&#xff0c;大型即时通信系统&#xff0c;网吧管理系统以及企业管理系统等具有大量并发用户请求的场合。 实现目标说明&#xff1a;通过完成端口模型构建一款服务…

IOCP 详解

IOCP 详解 网络上关于epoll的介绍资料多如牛毛&#xff0c;大多数已经讲解的非常细致。相比而言epoll只有三个接口调用&#xff0c;IOCP却有一堆的接口调用&#xff0c;更关键的是Windows的闭源性质&#xff0c;我们不知道调用之后Windows究竟做了哪些操作。众所周知IOCP是基于…

IOCP简介

1.1 环境要求本文读者需要熟悉C、TCP/IP、Socket编程、MFC&#xff0c;和多线程。源码使用Winsock 2.0和IOCP技术&#xff0c;要求&#xff1a;Windows NT/2000或以上&#xff1a;要求Windows NT3.5或以后版本Windows 95/98/ME&#xff1a;不支持Visual C.NET&#xff0c;或完整…

什么是IOCP

百度词条 输入输出完成端口&#xff08;Input/Output Completion Port&#xff0c;IOCP&#xff09;, 是支持多个同时发生的异步I/O操作的应用程序编程接口 一个IOCP对象&#xff0c;在操作系统中可关联着多个Socket和&#xff08;或&#xff09;文件控制端。 IOCP对象内部有一…

IOCP

载自&#xff1a;http://blog.csdn.net/markman101/article/details/6235516 本文主要探讨一下windows平台上的完成端口开发及其与之相关的几个重要的技术概念&#xff0c;这些概念都是与基于IOCP的开发密切相关的&#xff0c;对开发人员来讲&#xff0c;又不得不给予足够重视的…

IOCP模型与网络编程

IOCP模型与网络编程 一。前言&#xff1a; 在老师分配任务&#xff08;“尝试利用IOCP模型写出服务端和客户端的代码”&#xff09;给我时&#xff0c;脑子一片空白&#xff0c;并不知道什么是IOCP模型&#xff0c;会不会是像软件设计模式里面的工厂模式&#xff0c;装…

Windows下的IOCP模型(一):介绍与简单使用

一、IOCP简介 IOCP&#xff08;I/O Completion Port&#xff0c;I/O完成端口&#xff09;是Windows操作系统中伸缩性最好的一种I/O模型。I/O 完成端口是应用程序使用线程池处理异步 I/O 请求的一种机制。处理多个并发异步I/O请求时&#xff0c;使用 I/O 完成端口比在 I/O 请求时…

完成端口(IOCP)编程探讨

FW: http://www.zhuaxia.com/item/473350387 完成端口(IOCP)编程探讨 2007-08-26 16:06:00 来自&#xff1a;C博客-首页原创精华区 新建目录...根目录新手试用频道 本文主要探讨一下windows平台上的完成端口开发及其与之相关的几个重要的技术概念&#xff0c;这些概念都是与…

异步通信之IOCP详解

一、 概述 学习完网络基础&#xff0c;在写C/S应用程序时&#xff0c;大多童靴写服务器基本都没有用到io模型&#xff0c;基本都是采用“accept同步拥塞通讯和多线程方式”与客户端通讯。但当有成千上万客户端请求连接并与服务器通讯时&#xff0c;多线程的创建与CPU上下文的切…

IOCP详解

IOCP详解 IOCP&#xff08;I/O Completion Port&#xff0c;I/O完成端口&#xff09;是性能最好的一种I/O模型。它是应用程序使用线程池处理异步I/O请求的一种机制。在处理多个并发的异步I/O请求时&#xff0c;以往的模型都是在接收请求是创建一个线程来应答请求。这样就有很多…

IOCP技术详解

这几周我接触了Windows网络通讯中的IOCP模型,自己在网上找了相关的知识进行学习&#xff0c;自己又下了好多服务器端的代码&#xff0c;但都运行不了&#xff0c;也是自己菜&#xff0c;能力还需加强。幸好我师父资助了我一个能运行的服务端IOCP代码&#xff0c;自己参照网上的…

Python正则表达式模式

在 Python 程序中&#xff0c;模式字符串使用如下特殊的语法来表示一个正则表达式&#xff1a; 字母和数字表示它们自身&#xff0c;一个正则表达式模式中的字母和数字匹配同样的字符串&#xff1b;当大多数字母和数字前加一个反斜杠时&#xff0c;它们会拥有不同的含义&#…

Python正则表达式实例详解

一、正则表达式语法 正则表达式是用匹配或者描述字符串的工具。 用处&#xff1a; a.判断字符串是否满足某个条件—判断输入的字符串是否是邮箱/手机号码。是否是ip地址 b.提取满足条件的字符串 c.字符串替换 Python中通过re模块中相应的方法来支持正则表达式的匹配、查找和替…

Python正则表达式语法快速入门

文章目录 1 正则符号初阶代码举例1&#xff1a;不同符号的组合代码举例2&#xff1a;符号加&#xff0c;代表连续的一个或多个代码举例3&#xff1a;匹配字符串开始代码举例4&#xff1a;匹配字符串结束代码举例5&#xff1a;匹配单词边界 2 正则符号进阶代码举例1&#xff1a;…