1.测试代码编写:
我们首先写一个demo,该demo在没有terracotta的环境下执行一次,看看结果
我们首先先写一个简单的计数器代码(我们这个例子制定共享TerracottaDemo类的demo对象,它包含的count和inst对象也就随之被整个集群共享了):
package com.broada.simpleparallel; public class Main implements Runnable{ private Object lock = new Object(); private int count = 0; private static Main inst = new Main(); /** * @param args */ public static void main(String[] args) { new Thread(inst).start(); new Thread(inst).start(); } public void run() { //keep increasing count by one every few seconds while(true){ synchronized(lock){ count++; System.out.println(Thread.currentThread().getName() + " increased count to:"+count); } try{ Thread.sleep((int)(5000*Math.random())); } catch(Exception e){ e.printStackTrace(); } } } }
执行,开启不同的进程进行执行,看看结果
<!--[endif]-->
我们可以看出,正常情况下各个进程调用各自JVM中的对象,并没有任何的共享
2.下载、安装(此文档主要是在windows单机环境下进行安装):
(1).下载Terracotta,下载前需要注册帐号才能进行下载:http://terracotta.org/
(2).注册成功后进行邮件验证,验证成功后点击网站”open source”后,可以对其产品Ehcache、Quartz、BigMemory下载,我们下载terracotta (目前最高版本: terracotta-3.7.5.tar.gz、terracotta-3.7.5-installer.jar),我们这里使用terracotta-3.6.2-installer.jar进行安装,安装步骤很简单,如下图所示
3.配置Terracotta
(1).创建tc-config.xml文件(可以通过config-samples文件夹下的tc-config-express-reference.xml文件进行修改),该文件是描述client节点在TC Server中行为的唯一信息,也是我们的程序作为Terracotta Client节点添加时主要的内容
1. <?xml version="1.0" encoding="UTF-8"?> 2. <!-- 3. 4. All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved. 5. 6. --> 7. <tc:tc-config xmlns:tc="http://www.terracotta.org/config" 8. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 9. xsi:schemaLocation="http://www.terracotta.org/schema/terracotta-6.xsd"> 10. <!-- Server is colocated with client and DSO is listening on 11. default port (9510). --> 12. <servers> 13. <server host="127.0.0.1" name="server1"/> 14. </servers> 15. 16. <!-- Tell DSO where to put the generated client logs. 17. See the Terracotta Configuration Guide and Reference for additional 18. information. --> 19. <clients> 20. <logs>H:/soft/Terracotta/terracotta-3.6.2/client-logs</logs> 21. </clients> 22. 23. <application> 24. <dso> 25. <!-- The app requires these custom objects/classes to be shared - the following declarations 26. tell DSO which ones they are. When the app runs under DSO, instances of these classes 27. will broadcast changes in their state. 28. 29. A best practice (and an MVC pattern) when writing an app that you intend to cluster via Terracotta is to group the 30. classes you want to share under a single package. This makes the list of instrumented classes more concise. 31. 32. The following <include> instruments all of the classes found in all of the 33. packages of our sample app. --> 34. <instrumented-classes> 35. <include> 36. <class-expression>com.*</class-expression> 37. </include> 38. </instrumented-classes> 39. 40. <!-- We declare the field 'com.broada.simpleparallel.Main.inst' a root, making it 41. available to all instances of our app that run via DSO. --> 42. <roots> 43. <root> 44. <field-name>com.broada.simpleparallel.Main.inst</field-name> 45. </root> 46. </roots> 47. <locks> 48. <autolock> 49. <method-expression>void com.broada.simpleparallel.Main.run()</method-expression> 50. <lock-level>write</lock-level> 51. </autolock> 52. </locks> 53. </dso> 54. </application> 55. </tc:tc-config>
(2). 拷贝tc-config.xml到%tc_home%/bin和%tc_home%/platform/bin下
(3). 把上面的线程的代码例子打成TerracottaDemo.jar包,拷贝到%tc_home%/platform/bin下
4.启动tc server
进入到%tc_home% /bin目录,执行start-tc-server.bat,未执行参数-f<tc-config.xml>启动时,启动程序会使用tc.jar包里自带的默认配置文件’com/tc/config/schema/setup/default-config.xml’
因此,我们要执行的命令是start-tc-server.bat –f tc-config.xml,以tc-config.xml配置的方式启动服务器,截图如下:
服务器成功启动后,可以开启Terracotta Developer Console控制台来看看:
5.运行demo
在windows命令行下进入%tc_home%/platform/bin,输入命令执行:
dso-java.bat –cp TerracottaDemo.jar com.broada.simpleparallel.Main
分别开启2个不同的线程执行此demo,观察结果
控制台上的界面:
可以发现两个客户端成功连上
6.分析测试结果:
可见计数器已经被2个Java程序实例所共享,每个程序有一个线程访问计数器,从上面可以看到,整个Java代码没有作任何改动。只是增加了一个tc-config.xml文件,从tc-config.xml文件中的配置内容可以看出,terracotta还是做了很多的工作的,而且已经比较完善,其实不管它是结合自己的产品ehcache、quartz进行整合,还是结合apache下的相关产品进行整合,terracotta可以整合的产品较多,因此我们也没有必要一个一个去搭建,它们的整合过程只是在配置的方式上有所不同,其实我们在深入了解它的原理后在进行其他产品的整合,其实都是一个简单的过程。
相关推荐
Terracotta
Demo of ehCache distributed caching with terracotta in glassFish v3 可以参考:http://blog.csdn.net/guobin0719/archive/2011/04/25/6361940.aspx
java ehcache terracotta 集群, 分布式缓存配置
terracotta集群负载,是个好东西
Terracotta 3.2.1 英文文档
terracotta-ee-3.5.2破解版
terracotta license.key ,
其实terracotta集群tomcat这方面的资料也算不少了··但还是会很容易出现错误·· 希望我这个文档能帮助大家!
Terracotta实现基于Tomcat的Web应用集群
Terracotta的基本原理是对于集群间共享的数据,当在一个节点发生变化的时候,Terracotta只把变化的部分发送给...在对比测试中,采用Terracotta搭建Tomcat集群,节点达到8个时候,整个集群的吞吐量还一直是线性增长的。
记载了terracotta如何与tomcat、jetty等服务器的集群,解释了tc-config.xml中各个配置的作用
ehcache的terracotta方案的比较.docx
terracotta-eclipse-plugin-3.7.7-2013-08-19_16-03-48.tar(terracotta的Eclipse插件) 发现官网挺卡的,有时候下不了,先传上来吧,供国内用户下载,这个算最新的吧。2014-02-17下载的。
2014年05月12日最新版本, Windows版本 terracotta-ee-4.1.2.jar,terracotta-license.key
通过_Terracotta实现基于Tomcat的Web应用集群
terracotta-3.7.7.tar.gz 官方网站卡得要死,有时候不一定能下载,把最近自己用的,算最新版吧,提供在国内网站下载吧。 2014-02-17下载的。
terracotta-toolkit-1.3-runtime-3.2.0.jar 集群实现JAR
terracotta完整开发插件,可以作为eclipse的插件,也可以单独使用,功能齐全;