Terracotta学习之一介绍

“With Terracotta, we are able to reduce the heap size, and get fast, local access for large amounts of data. It’s much easier for us to manage, with less GC tuning and more predictable application performance.”— Joey Caisse, CTO of News Digital Media

terracotta是非常优秀的集群框架,在前面的博客中曾数度提及,本人也是经过了两个月的折腾,从今天开始系统的完成几篇文章。

首先介绍一些学习terracotta的地址:

开源版本下载:http://terracotta.org/downloads/open-source/catalog

使用eclipse插 件:http://www.terracotta.org/confluence/display/docs/DSO+Eclipse+Plugin+Guide

书籍:《Apress.The.Definitive.Guide.to.Terracotta.Cluster.the.JVM.for.Spring.Hibernate.and.POJO.Scalability.Jun.2008》

论坛:http://forums.terracotta.org/forums/forums/list.page 

开发者SVN库:http://svn.terracotta.org/fisheye/browse/TerracottaForge

maven plugin :http://forge.terracotta.org/releases/projects/tc-maven-plugin/index.html

3.5新版本docs:http://www.terracotta.org/confluence/display/docs/Home

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

terracotta的集群原理:


Terracotta 和其他的集群的策略的不同。因为tc不是基于序列化的RPC,JAX等,它抽象了对数据的操作就是简单的增删改查,这好比是数据库操作。所以,tc的作者们认为,只要熟悉数据库的人,都可以学会tc。

tc是靠扫描字符流,在编译之前自动识别哪些root在哪里被修改了。就好象是get,read,write和update等,然后建立一个很大的cache,内部保存root的不同状态,使用worker和master的规则。在不同jvm之间同步root的status,在数据被操作之前都会在server上进行一次通知,这多线程的操作,同样是基于乐观锁和悲观锁的,可以让大家按照需要指定。

对于这些操作,在每次dso-java 编译时,都要加载一个 .xml的配置文件。这是集群的关键。

也就是说,在每个app运行之前,要有一个server被启动。server的启动方式很多,也可以在java代码里完成。

下面进入配置文件:

  <servers>
    <server host="%i" name="localhost">
      <dso-port bind="0.0.0.0">9510</dso-port>
      <jmx-port bind="0.0.0.0">9520</jmx-port>
      <data>terracotta/server-data</data>
      <logs>terracotta/server-logs</logs>
      <statistics>terracotta/cluster-statistics</statistics>
    </server>
  </servers>

我们可以定义多个server,在某个server crash后,另外一个server会立即的成为整个集群新的master,tc强大吧?因为基于状态的集群是可以热备份的。目前我使用的tc license 可以集群999台机器和350GB的cache。对于一个普通企业来说,这样的计算能力,已足够了,而且很多的医院、火车站、仓库没有足够的资金搞定大的云计算环境,若能充分利用旧的x86设备,何乐不为呢?

 所以,tc将会成为java平台为用户提供网格计算和云计算需求新的低廉选择。当众多厂商,包括思科、惠普等投资于tc,我们有理由继续的学习它,使用它。目前,vmware等虚拟机厂商成为娇娇者,但是客户要使用虚拟桌面还是要选用大型服务器做后台,那么传统的PC就没有用了么?

在两次IBM Forum上,我遇见了很多的人,想知道到底什么是云计算?难道一定要因为自己的需求增加了,要引入大型机或IBM P X Z系列机器么?很多人,很多人,例如一个做全国连锁餐饮的,他们就想为用户提供VIP服务,客户可以在任意地点点菜预约,到任意地点就餐。想象一下,这个服务会造成多大的轰动,就像你去吃饭不需要排队一样。

但是,但他们来IBM参加讨论后,发现,原来要进行硬件的升级。这是多么的可惜。所以,TC的意义非同凡响。

谁能帮助穷人致富,谁就能把我云计算的先机。

说了两句感想。其实IBM本身对x86的集群也做了很多的工作,bigInsight 和 TSPP等都是很好的工具。

在client端配置server方法如上。

配置集群的根:

		<roots>

				<root>
					<field-name>com.sk.tc.example.HelloClusteredWorld.counter</field-name>
				</root>
			</roots>


除root外,我们还要知道哪些类进行了对root的操作,这些类要被包含在 <instrumented-classes>里面:

  <instrumented-classes>
        <!--The following <include> instruments 'demo.chatter.ChatManager' to be shared, but
             that fields described in the class as 'transient' should still behave as transient fields.

             By setting the value of <honor-transient> to 'true', fields declared as transient *are* transient
             and their state and value will not become available across instances of the app. Only local instances of the app
             will be able to create, read, and write to these fields.

             In 'demo.chatter.ChatManager' most members (transient and otherwise) are initialized upon creation. 
             However, when DSO finds that an object is already available from the server, additional instances of the app 
             will simply receive a reference to that object, and its constructor will not be called; transient fields 
             will not be initialized at this point, so the <on-load> declaration is used to indicate actions
             that the class needs to take when DSO loads that object from the server (actions that normally
             happens on class instantiation).-->
        <include>
          <class-expression>demo.chatter.ChatManager</class-expression>
          <honor-transient>true</honor-transient>
          <on-load>
            <method>init</method>
          </on-load>
        </include>
        <!--The following <include> sections cause the classes 'demo.chatter.Message' and
            'demo.chatter.User' to be treated as shareable. These objects are used to ferry around the messages
            (and information about the message) sent from chatter clients.-->
        <include>
          <class-expression>demo.chatter.Message</class-expression>
        </include>
        <include>
          <class-expression>demo.chatter.User</class-expression>
          <honor-transient>true</honor-transient>
        </include>
        <include>
          <class-expression>demo.chatter.Main</class-expression>
        </include>
      </instrumented-classes>


-The app requires these custom objects/classes to be shared - the following declarations
           tell DSO which ones they are. When the app runs under DSO, instances of these classes
           will broadcast changes in their state.


           A best practice (and an MVC pattern) when writing an app that you intend to cluster via Terracotta is to group the 
           classes you want to share under a single package. This makes the list of instrumented classes more concise.

配置锁

 <locks>
        <autolock>
          <method-expression>* demo.chatter.ChatManager.*(..)</method-expression>
        </autolock>
      </locks>

基于操作数据的方法是否进行集群,tc各种方式都考虑到了,尤其是对特殊的情况,个别的不集群的可以指出来,就是黑名单、白名单似的。

一个最简单的配置像如下:

<?xml version="1.0" encoding="UTF-8"?>
<con:tc-config xmlns:con="http://www.terracotta.org/config">
  <servers>
    <server host="%i" name="localhost">
      <dso-port bind="0.0.0.0">9510</dso-port>
      <jmx-port bind="0.0.0.0">9520</jmx-port>
      <data>terracotta/server-data</data>
      <logs>terracotta/server-logs</logs>
      <statistics>terracotta/cluster-statistics</statistics>
    </server>
  </servers>
  <clients>
    <logs>terracotta/client-logs</logs>
  </clients>
	<application>
		<dso>
			<roots>

				<root>
					<field-name>com.sk.tc.example.HelloClusteredWorld.counter</field-name>
				</root>
			</roots>
		</dso>

	</application>
</con:tc-config>

这里只是一个root,其他默认,如锁就是auto-lock。

package com.sk.tc.example;

public class HelloClusteredWorld
{
    private static int counter;

    public static void main(String[] args) 
    {
        counter++;
        System.out.println("Counter is: " + counter);
    }
}

具体的类就是上面,每运行一次程序,就会得到不同的值,如果使用javac编译的话,那么值就是一样的,tc中使用dso-java,因为它会加载bootjar.jar,这个类就是加载tc环境的。







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值