利用Terracotta同步tomcat的Web应用的Session
在一台机器上测试
1.安装tomcat
安装两个tomcat,一个Http端口为8080,另一个Http端口为9080
2.安装Terracotta
3.编写tc-config.xml文件
<?xml version="1.0" encoding="UTF-8"?> <tc:tc-config xsi:schemaLocation="http://www.terracotta.org/schema/terracotta-4.xsd" xmlns:tc="http://www.terracotta.org/config" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <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"> <dso-port>9510</dso-port> </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-6.0" version="2.0.2"/> </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.TimeZone</include> <include>sun.util.calendar.ZoneInfo</include> </additional-boot-jar-classes> <instrumented-classes> <include> <class-expression>num.NumberGuessBean</class-expression> </include> <include> <class-expression>cal.*</class-expression> </include> </instrumented-classes> <web-applications> <web-application>examples</web-application> </web-applications> </dso> </application> </tc:tc-config>
修改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> <class-expression>num.NumberGuessBean</class-expression> |
重新启动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 |