利用Terracotta同步tomcat的Web应用的Session

利用Terracotta同步tomcat的Web应用的Session

在一台机器上测试

1.安装tomcat

安装两个tomcat,一个Http端口为8080,另一个Http端口为9080

 

2.安装Terracotta

 

3.编写tc-config.xml文件

修改web-applications和web-application部分,把要共享session的应用的context配置上,比如:

<web-applications>
      <web-application>web_app1</web-application>
      <web-application>web_app2</web-application>
</web-applications>

修改Terracotta服务器的信息(所有tomcat都指向同一个terracotta server),比如:

<server host="localhost"> <!-- IP or host name of TC server-->
       <dso-port>9510</dso-port>
</server>

 

4.将tc-config.xml复制到TERRACOTTA_HOME目录

 

5.下载和安装tim-tomcat

http://forge.terracotta.org/releases/projects/tim-tomcat/index.html

运行TERRACOTTA_HOME/bin/tim-get.bat upgrade tc-config.xml

显示

Terracotta 3.1.1, as of 20091009-141004 (Revision 13788 by cruise@su10mo5 from 3.1)

* Parsing module: tim-tomcat-6.0:2.0.2: latest version 2.0.2
Installing tim-tomcat-6.0 2.0.2 and dependencies...
   INSTALLED: tim-tomcat-6.0 2.0.2 - Ok
   INSTALLED: tim-tomcat-5.5 2.0.2 - Ok
   INSTALLED: tim-tomcat-common 2.0.2 - Ok
   INSTALLED: tim-session-common 2.0.2 - Ok

Your original config file has been saved to D:/soft/terracotta/terracotta-3.1.1/tc-config.xml.original

Done.

 

6.在Tomcat安装目录的bin子目录中创建startupTC.bat

内容如下:

set CATALINA_HOME=<本地Tomcat目录>
set TC_INSTALL_DIR=<本地Terracotta安装目录>
set TC_CONFIG_PATH=<本地tc-config.xml的完整文件名>
call %TC_INSTALL_DIR%/bin/dso-env.bat -q
set JAVA_OPTS=%JAVA_OPTS% %TC_JAVA_OPTS%
echo %JAVA_OPTS%
pause
startup.bat

 

7.启动集群

(1)启动Terracotta

TERRACOTTA_HOME/bin/start-tc-server.bat

(2)启动Tomcat1

bin/startupTC.bat

(3)启动Tomcat2

bin/startupTC.bat

等Tomcat启动以后,看到如图所示,代表Terracotta和Tomcat结合成功:

 

8.验证集群

打开浏览器:

在第一台Tomcat服务器上配置好一天的日程http://localhost:8080/examples/jsp/cal/login.html,然后到第二台服务器上http://localhost:9080/examples/jsp/cal/login.html(注意:第二个浏览器url中的jsessionid应为第一个浏览器通过cookie发送的jsessionid,即应为http://localhost:9080/examples/jsp/cal/cal1.jsp;jsessionid=xxxxxx;因为服务器会优先使用cookie中的jsessionid,因此要禁止第二个浏览器中的cookie),打开相同的页面,您会看到前面输入的日程已经显示出来了。您还可以试着在第二台服务器上对日程进行修改,再到第一台上面刷新页面,前面的修改也会立即显示出来。这说明两台Tomcat服务器的session信息已经完全同步了。如图:

 

也可以在Terracotta的控制台中看到,如图:

9.问题

(1)可能会看到服务器抛出com.tc.exception.TCNonPortableObjectError异常。查看异常信息会发现该子程序所需的一些类没有包含在tc-config.xml的instrumented-classes配置中。因此需要添加如下配置到<application><dso>中:

<instrumented-classes>
       <include>
             <class-expression>cal.*</class-expression>

             <class-expression>num.NumberGuessBean</class-expression>
        </include>
    </instrumented-classes>

重新启动Tomcat。再打开前面的页面。
(2)可能还会遇见TCNonPortableObjectError,不过这次的错误是有关boot jar的:

com.tc.exception.TCNonPortableObjectError:
*******************************************************************************
Attempt to share an instance of a non-portable class referenced by a portable class. This unshareable class must be in the DSO boot jar. It also has superclasses which must be in the DSO boot jar. Please add all of these classes to the boot jar configuration and re-create the DSO boot jar.

阅读错误信息,按照提示的方法配置tc-config.xml文件,然后执行下面的命令重新生成boot jar文件,再重新启动Tomcat就可以了:

cd TERRACOTTA_HOME

bin/make-boot-jar.sh -f tc-config.xml

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值