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.首先下载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.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>
加粗部分根据使用的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-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 -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
分享到:
相关推荐
Hibernate二级缓存是Java开发中使用Hibernate框架进行数据持久化时优化性能的一种重要技术。它在一级缓存(Session级别的缓存)的基础上,提供了一个全局的、跨会话的数据存储层,可以显著减少对数据库的访问,从而...
"Spring Boot 集成 Ehcache 2.x 用于 Hibernate 二级缓存" 本篇文章主要介绍了如何在 Spring Boot 中集成 Ehcache 2.x 作为 Hibernate 的二级缓存,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。 Ehcache ...
尽管Terracotta提供了强大的Session共享功能,但在实际工程项目中应用却面临着一定的挑战,如配置复杂性、与现有架构的集成问题等。解决这些问题需要深入理解Terracotta的工作原理及其与Java应用的交互方式,同时...
理解和充分利用一级缓存、二级缓存及查询缓存,可以帮助开发者设计出更高效的数据访问策略。然而,缓存虽然能提高性能,但也需要考虑数据一致性、缓存击穿等问题,确保系统稳定和数据的正确性。
通过以上步骤,我们可以有效地配置EhCache作为Hibernate的二级缓存。这不仅能够显著提高应用程序的性能,还能减少数据库的压力。EhCache的配置相对简单,但其提供的功能却十分强大。开发者可以根据实际的应用场景...
- **高可用性**:通过在多个节点上复制Session, Terracotta提高了系统的可用性和容错能力。 - **性能优化**: Terracotta的内存管理机制减少了网络通信和磁盘I/O,提升了应用性能。 - **易于扩展**:随着需求的增长...
总结,Ehcache二级缓存的配置涉及到多个方面,包括缓存管理、内存和磁盘存储、过期策略以及与Spring的集成。正确配置和使用Ehcache可以显著提高应用程序的响应速度和整体性能。在实际应用中,需要根据业务需求进行...
默认情况下,Hibernate提供了第一级缓存——Session缓存,但对于更复杂的缓存需求,如跨Session的共享缓存,Ehcache则作为第二级缓存被引入。第二级缓存可以存储更多数据,减少对数据库的依赖,提高并发性能。 **...
- **缓存扩展**: Ehcache 可以与其他缓存解决方案集成,如 Hibernate 的二级缓存。 ### 5. Ehcache 源码分析 深入学习 Ehcache 源码有助于理解其工作原理。主要关注以下几个关键类: - **Cache**: 实现缓存逻辑的...
Hibernate的二级缓存是指在SessionFactory级别共享的缓存,它可以在多个Session之间共享数据,从而减少对数据库的直接访问。二级缓存可以显著提升应用程序的性能,尤其是在高并发环境下。而ehcache是Hibernate默认的...
Terracotta 实现 Session 复制是指 Terracotta 的基本原理是对于集群间共享的数据,当在一个节点发生变化的时候,Terracotta 只把变化的部分发送给 Terracotta。 分布式 Session 方案的选择取决于系统的需求和要求...
二是全量复制 session,但会因大量序列化操作而增加网络负担。Terracotta 提供了一种更优的解决方案,它仅同步 session 的变化部分,从而兼顾了负载均衡和灾难恢复的需求。在实际测试中,当 Terracotta 集群规模扩展...
Terracotta通过集成到Tomcat,解决了这个问题,它能自动将一个服务器上的session同步到集群中的其他服务器,确保即使在服务器故障情况下,用户的session信息也不会丢失。 **terracotta-3.7.7.tar.gz安装步骤** 1. *...
- 一级缓存:默认存在于Hibernate Session中,是事务级别的缓存。 - 二级缓存:可以是应用级或分布式缓存,适用于数据更新频率低、频繁使用的场景。常见的第三方实现包括EhCache和Terracotta。 5. **Hibernate...
Ehcache是基于内存的缓存解决方案,它提供了本地缓存、持久化存储和二级缓存等功能。通过将频繁访问的数据保存在内存中,Ehcache可以显著降低数据库的负载,提高应用程序响应速度。 2. Terracotta集成 Terracotta是...
Ecache是Hibernate的一个二级缓存解决方案,由Terracotta公司开发。它提高了数据读取速度,减少了对数据库的直接访问,从而降低了系统负载。Ecache基于内存,支持分布式缓存,可以有效地处理大规模并发访问。在...
5. **Caching**:探讨了Hibernate的缓存机制,包括第一级缓存(Session缓存)和第二级缓存,以及第三方缓存集成如Ehcache和Terracotta。 6. **事件和拦截器**:解释了如何通过实现特定接口来监听和干预对象生命周期...
具体到session同步方面,Terracotta可以看作是对Tomcat自带的session复制机制的一种优化,既避免了对数据库的依赖,又能够有效减少网络通信压力,实现负载均衡和灾难恢复的目标。 根据测试结果,在采用Terracotta...
书中将详细解释如何配置和使用Terracotta与Hibernate的集成,确保在集群环境中的事务管理和缓存一致性。 此外,书中可能还会涉及其他相关技术,如Ehcache(一个常用的Java缓存解决方案,也支持与Terracotta的集成)...