`
prettyzhou
  • 浏览: 33524 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Terracotta环境搭建以及测试demo

 
阅读更多

 

 

1.测试代码编写:

我们首先写一个demo,该demo在没有terracotta的环境下执行一次,看看结果
我们首先先写一个简单的计数器代码(我们这个例子制定共享TerracottaDemo类的demo对象,它包含的countinst对象也就随之被整个集群共享了):

    

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”后,可以对其产品EhcacheQuartzBigMemory下载,我们下载terracotta (目前最高版本: terracotta-3.7.5.tar.gzterracotta-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.分析测试结果:

  可见计数器已经被2Java程序实例所共享,每个程序有一个线程访问计数器,从上面可以看到,整个Java代码没有作任何改动。只是增加了一个tc-config.xml文件,从tc-config.xml文件中的配置内容可以看出,terracotta还是做了很多的工作的,而且已经比较完善,其实不管它是结合自己的产品ehcachequartz进行整合,还是结合apache下的相关产品进行整合,terracotta可以整合的产品较多,因此我们也没有必要一个一个去搭建,它们的整合过程只是在配置的方式上有所不同,其实我们在深入了解它的原理后在进行其他产品的整合,其实都是一个简单的过程。

 

 

  • 大小: 77.3 KB
  • 大小: 123.8 KB
  • 大小: 100.8 KB
  • 大小: 159.2 KB
  • 大小: 134.8 KB
  • 大小: 230.4 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics