这个文档告诉你怎样为一个用Ehcache cache的应用添加Terracotta 集群。
前提是这个应用运行在:
1、 在一个单独的jvm上
2、 一个用Ehcache 响应的集群
步骤如下:
一、 需要条件
JDK1.5及以上版本
Terracotta 3.1.1及以上版本
所有被集群的对象是可串行化的
二、 安装分布cache
添加以下JAR文件到你项目的CLASSPATH中
- ${TERRACOTTA_HOME}/distributed-cache/ehcache-terracotta-<version>.jar
- ${TERRACOTTA_HOME}/distributed-cache/ehcache-core-<version>.jar
如果你用的是WAR文件,将这些JAR文件添加到WEB-INF/lib 文件夹中。
三、 配置分布式cache
Ehcache的配置文件,默认为ehcache.xml,需要在你项目的classpath中。如果你用的是WAR文件,需要将Ehcache配置文件放入WEB-INF/lib 文件夹中。
添加Terracotta配置文件
为需要用Terracotta实现集群的cache,在ehcache.xml添加sub-element<terracotta/>到cache的<cache>块中。举例如下:
你还需要用<terracottaConfig>为应用服务器指出terracotta的位置。
客户端需要加载配置从一个文件或者Terracotta服务器中。下面的例子展示了一个加载于本地主机上的Terracotta服务器的配置文件。
url属性的值需要包含Terracotta服务器的主机名和它的DSO端口号(默认为9510)。
编辑不相容的配置
对于任何被集群的cache,你必须删除、disable、或者编辑那些在Terracotta集群时不相容的配置元素。被集群的caches有个<terracotta>或者<terracotta clustered=”true”>元素。
下面这些Ehcache配置属性或者元素需要删除或者disable。
- DiskStore-related attributes overflowToDisk and diskPersistent .
Terracotta 服务器自动提供一个磁盘储存。
- Replication-related configuration elements, such as <cacheManagerPeerProviderFactory>, <cacheManagerPeerListenerFactory>, <bootstrapCacheLoaderFactory>, <cacheEventListenerFactory>.
当一个变化发生在Terracotta集群的时候,所以包含变化元素和对象的节点都会更新。 Unlike the replication methods used to cluster Ehcache, cache event listeners are not (and do not need to be) notified of remote changes. Listeners are still aware of local changes.
- Replication-related attributes such as replicateAsynchronously and replicatePuts .
MemoryStoreEvictionPolicy属性需要被设置成LFU或者LRU。设置MemoryStoreEvictionPolicy为FIFO将会引起IllegalArgumentException错误。
四、 启动集群
1、启动Terracotta服务
- UNIX/Linux
- [PROMPT] ${TERRACOTTA_HOME}/bin/start-tc-server.sh &
- Microsoft Windows
- [PROMPT] ${TERRACOTTA_HOME}/bin/start-tc-server.bat
2、启动应用服务器
3、启动Terracotta开发控制台
- UNIX/Linux
- [PROMPT] ${TERRACOTTA_HOME}/bin/dev-console.sh &
- Microsoft Windows
- [PROMPT] ${TERRACOTTA_HOME}/bin/dev-console.bat
4、连接Terracotta集训
点击Terracotta开发控制台中的Connect…
5、点击集群导航窗口中的Ehcache节点查看Terracotta集群中的caches。
五、编辑Terracotta配置
这一步告诉你如何在不同的机器上启动客户端和服务器,和故障转移(高效)。你可以扩展Terracotta集群和添加高效性,通过做以下的事情:
1、 将Terracotta服务器转移到单独的机器上。
2、 部署一个由多个Terracotta服务器组织的集群器。
3、 部署多个应用节点。
操作程序:
1、 关闭Terracotta集群器。
2、 建立Terracotta配置文件tc-config.xml包含像下面这样的内容:
3、 安装Terracotta3.1.1在每一台你在tc-config.xml中配置的服务器上。
4、 复制tc-config.xml文件到一个Terracotta服务器们可以进入的位置。
5、 执行二、安装分布cache 和 三、配置分布式cache 在每一个你想要集群的应用节点上。
确定你安装了你的应用或者任何应用服务器在那些节点上。
6、 添加以下语句到Ehcache配置文件ehcache.xml中:
7、 复制ehcache.xml到每一个应用节点,确保它在你应用的classpath中。如果你用的是WAR文件,将这些JAR文件添加到WEB-INF/lib 文件夹中。
8、 启动Terracotta服务以下面的方法,将tc-config.xml中的’Server1’换成任意你取的名字。
- UNIX/Linux
- [PROMPT] ${TERRACOTTA_HOME}/bin/start-tc-server.sh -f <path/to/tc-config.xml> -n Server1 &
- Microsoft Windows
- [PROMPT] ${TERRACOTTA_HOME}/bin/start-tc-server.bat -f <path/to/tc-config.xml> -n Server1 &
如果你配置了第2台服务器,启动那个服务用同样的方法在那台机器上,在 –n 标记后输入它的名字。第2台服务器成为热备份。任何你配置的其他服务器都可以开启变成备份服务器。
9、 打开所有的应用服务器。
10、打开Terracotta开发终端,监视集群器。
六、了解更多
http://www.terracotta.org/documentation/ga/product-documentation-2.html#50552833_Step%202
http://www.terracotta.org/documentation/ga/product-documentation-13.html#50552839_38131
http://www.terracotta.org/documentation/ga/product-documentation-4.html#50552833_53258
http://ehcache.org/