Terracotta学习之一介绍

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环境的。

免责声明:本网信息来自于互联网,目的在于传递更多信息,并不代表本网赞同其观点。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,并请自行核实相关内容。本站不承担此类作品侵权行为的直接责任及连带责任。如若本网有任何内容侵犯您的权益,请及时联系我们,本站将会在24小时内处理完毕。
相关文章
返回顶部