`
jerryzl
  • 浏览: 16327 次
  • 性别: Icon_minigender_1
  • 来自: 黑龙江
文章分类
社区版块
存档分类
最新评论

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

阅读更多

Terracotta 简介;

Terracotta 是一款由美国Terracotta 公司开发的著名开源Java 集群平台。它在JVMJava 应用之间实现了一个专门处理集群功能的抽象层,以其特有的增量检测、智能定向传送、分布式协作、服务器镜像、分片等技术,允许用户在不改变现有系统代码的情况下实现单机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.首先下载terracotta 3.2.1_1。jar,前提是机器已经安装jdk及配置好了java_home,利用java -jar terracotta 3.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>

<Valve className="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.jarehcache-terracotta-2.0.1.jarslf4j-api-1.5.8.jarslf4j-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>  

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

<property



 name



=



"hibernate.cache.region.factory_class"



>



net.sf.ehcache.hibernate.EhCacheRegionFactory</property>




For Hibernate 3.2则是上面加粗部分配置。

ehcache.xml文件的配置为大致如下:

 

< defaultCache

      maxElementsInMemory = "10"

      eternal = "false"

      timeToIdleSeconds = "120"

       timeToLiveSeconds = "120"

      >

    < terracotta />

    </ defaultCache >

  < terracottaConfig url = "localhost:9510" />

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

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

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

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

快捷模式不需要 tc-config Xml 文件的支持,直接启动start-tc-serverBat (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 -f tc-config.xml

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

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

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

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

  • 大小: 107.7 KB
  • 大小: 110.3 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics