读书人

terracotta集成session复制及Hibernat

发布时间: 2012-10-14 14:55:08 作者: rapoo

terracotta集成session复制及Hibernate二级缓存笔记

Terracotta简介;

Terracotta是一款由美国Terracotta公司开发的著名开源Java集群平台。它在JVM与Java应用之间实现了一个专门处理集群功能的抽象层,以其特有的增量检测、智能定向传送、分布式协作、服务器镜像、分片等技术,允许用户在不改变现有系统代码的情况下实现单机Java应用向集群话应用的无缝迁移。使得用户可以专注于商业逻辑的开发,由Terracotta负责实现高性能、高可用性、高稳定性的企业级Java集群.Terracotta公司在2009年收购了著名的Java开源缓存项目Ehcache以及Java任务调度项目Quartz。经过对该项目的深度整合,Terracotta推出了易用性更高的分布式缓存、分布式任务调度以及分布式Web Session等快捷解决方案.以上引自老马的文章,具体可参加作者博客了解http://lima01.iteye.com/。

配置环境:

我的配置环境是:winxp,jdk1.5—17,spring2.5,hibernate3.3,tomcat5.5,terracotta版本是3.2.1_1,目前最新版本是3.2.1_2,可在http://www.terracotta.org/dl/oss-download-catalog下载。

实战:

首先需要说明的是terracotta分为express mode(快捷模式)和客户化模式,express mode能够让大家快速上手,不需要复杂的配置就可运行起来,而客户化模式则有很多高级功能,而且需要配置相应的文件,初学可以先运行express mode作为学习。

首先以express mode模式为例说明

1.首先下载terracotta3.2.1_1。jar,前提是机器已经安装jdk及配置好了java_home,利用java -jar terracotta3.2.1_1。jar命令安装terracotta,安装时最好保持jdk及terracotta安装目录名称都没有空格。

2.首先集成session复制,将terracotta安装目录下的sessions文件夹下的terracotta-session-1.0.1.jar拷贝到tomcat-5.5\server\lib文件夹下,然后

将context.xml文件(此文件可参见安装目录下的sessions\conf-samples的例子文件)拷贝到我们自己项目的META-INF文件夹下,具体内容根据使用的不同中间件进行不同配置,一下是使用tomcat5.5的配置,其他的也可看光放文档说明http://www.terracotta.org/documentation/ga/product-documentation

例如:

<Context>

<ValveclassName="org.terracotta.session.TerracottaTomcat55xSessionValve"tcConfigUrl="localhost:9510"/>

</Context>

?

<!-- tomcat 5.5.x -->

将我们的web工程发布到webapps下,启动tomcat5.5/bin/下的startup.bat即可,然后到Terracotta Console控制台查看object brower下是否出现tc:session。也可运行terracotta自带的例子,位于安装目录下的sessions\samples\下,验证时客气起双tomcat分配不同的端口号,打开两个浏览器窗口进行验证。

?

客户化模式的session复制可参见老马博客文章。

2.集成hibernate二级缓存

集成的时候需要注意将将安装后的terracotta文件夹下的ehcache-core-2.0.1.jar、ehcache-terracotta-2.0.1.jar、slf4j-api-1.5.8.jar、slf4j-jdk14-1.5.8.jar等文件拷贝到项目的lib文件夹下,配置hibernate属性

<prop key="hibernate.cache.use_query_cache">true</prop>
??? ??? ??? <prop key="hibernate.cache.use_second_level_cache">true</prop>
??? ??? ??? ? <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>?

terracotta集成session复制及Hibernate二级缓存札记

加粗部分根据使用的hibernate不同版本配置,在hibernate3.3中就需要配置为

<defaultCache

????? maxElementsInMemory="10"

????? eternal="false"

????? timeToIdleSeconds="120"

?????timeToLiveSeconds="120"

????? >

??? <terracotta/>

??? </defaultCache>

? <terracottaConfigurl="localhost:9510" />

?? 需要注意的是快捷模式下无法使用valueMode="identity"模式

具体可根据需要进行扩展。下面要讲述的客户化模式配置会有很大却别。

以上是express模式的大致配置。下面记录客户化模式

3.客户化模式下的session复制及缓存

快捷模式不需要tc-config。Xml文件的支持,直接启动start-tc-server。Bat(win下)就可以,而客户化模式需要在tc-config.Xml文件中进行配置。

验证session复制的时候就不需要惊醒context。xml的配置及terracotta-session-1.0.1.jar的拷贝,一切都在tc-config.Xml配置文件中配置大致配置如下:


?<servers>

?<!-- For more information on working with the Terracotta configuration file, including how to add
????? more servers, see http://www.terracotta.org/web/display/docs/About+Terracotta+Configuration+Files.
?????
????? Edit the following server element so that its host attribute contains the value matching the
????? hostname for the Terracotta server. -->
???
???? <server host="localhost">
??? ?<data>data/server-data</data>?
????? <logs>logs/server-logs</logs>?
????? <l2-group-port>9530</l2-group-port>?
<dso>
???????? <persistence>
?????????? <mode>permanent-store</mode>
???????? </persistence>
?????? </dso>

?? </server>
????


?????
??? <update-check>
?????? <enabled>true</enabled>
??? </update-check>

?</servers>
?
?<!-- Check that the TIM shown in the <module> element's name attribute matches the Tomcat version you
????? are using. The TIM listed here, "tim-tomcat-5.5" is for Tomcat 5.5. For example, if you are using
????? Tomcat 6.0, edit the name to "tim-tomcat-6.0".? -->
??????
? <clients>
??? <modules>
????? <module name="tim-tomcat-5.5" version="2.1.2" />
????? <module name="tim-tomcat-common" version="2.1.2" />
????? <module name="tim-session-common" version="2.1.2" />
??? ? <module name="tim-ehcache-2.0" version="1.5.2" />
??
???? <module name="tim-concurrent-collections" version="1.3.2" />
??? ? <module name="tim-distributed-cache" version="1.3.2" />
????
??? ??? ? <module name="tim-svt" version="1.3.2" />
??? ??? ?<module name="tim-quartz-1.7" version="1.3.2" />
??? ??? ?<module name="tim-async-processing" version="1.3.2" />
??? ??? ?<module name="tim-annotations" version="1.5.1" />

??? ??
??? </modules>
? </clients>

?<!-- Using <web-application> elements, enter the context root of the web application being clustered.
????? If you are clustering more than one web application, use one <web-application> element per application.
????? Two example <web-application> elements are shown below. Edit or remove these elements as needed.
????? To set the value of the <web-application> element to the default web-application context, use the
????? special value "ROOT" (without quotation marks). -->

?<application>
? <dso>
????? <additional-boot-jar-classes>
??????? <include>java.util.Locale</include>
??? ??? ?<include>java.lang.String$Case

?<instrumented-classes>
???????? <include>
?????????? <class-expression>org.acegisecurity.captcha.CaptchaSecurityContextImpl</class-expression>
???????? </include>
???????
????
?????? </instrumented-classes>
??? ?
??? <web-applications>
????? <web-application >myapplication</web-application>
??? </web-applications>
? </dso>
?</application>
</tc:tc-config>

?

首先前提是通过已经通过tim-get.bat install命令安装了相应的tim jar文件。

在? <instrumented-classes>

????????<include>

??????????<class-expression>org.acegisecurity.captcha.CaptchaSecurityContextImpl</class-expression>

????????</include>

中配置需要在terracotta中共享的类,

一般在如果使用了hibernate情况下,test.hbm.xml文件中many-to-one关系对应的class="com.test.oa.domain.BaAt"都需要在<include>中配置,不然启动中间件时会报错,控制台也会提示您需要include相应的类,

这里如果使用的acegi安全框架,目前terracotta1版本只能在客户化模式下配置,不能使用express模式。

?

???<web-applications>

?????<web-application >myWebApplication</web-application>

</web-applications>

?

配置相应的web工程.

如果集成ehcache时,还需要将tim-ehcache-2.0-1.5.2.jar、tim-distributed-cache-1.3.2.jar、tim-concurrent-collections-1.3.2.jar、等拷贝到项目的lib下(express模式不需要拷贝),可参看官方文档说明。

?

客户化模式的启动需要startupTC.bat的支持,还是以tomcat为例,在tomcat的bin目录下建立startupTC.bat内容如下:

set TC_INSTALL_DIR=D:\terracotta(安装目录)

set TC_CONFIG_PATH=D:\terracotta\bin\tc-config.xml(安装目录下的自建配置文件)

call%TC_INSTALL_DIR%\bin\dso-env.bat -q

set JAVA_OPTS=%JAVA_OPTS% %TC_JAVA_OPTS% -Xms256m -Xmx1024m -XX:MaxPermSize=128m

pause

startup.bat

红色部分很重要。

客户化模式还需要根据tc-config。Xml重新生成boot.jar,位置在terracotta\lib\dso-boot下

?

常用命令如下:

tim-get.bat upgrade tc-config.xml

make-boot-jar.bat -ftc-config.xml

start-tc-server.bat -n jerry -fD:\terracotta\bin\tc-config.xml

多server时需要启动服务端时指定name名称,如上所示,配置多server时刻以很容易实现容灾数据备份等,一个server down了,另一个server可无感知切换至激活状态

启动startupTC.bat后进入terracotta控制台查看效果,如附件所示。

以上大致写了一些,以作备忘。

读书人网 >软件架构设计

热点推荐