环境准备:(Nginx,tomcat的安装部署本例不赘述。) 192.168.1.13 nginx 192.168.1.225 tomcat7,terracotta 192.168.1.226 tomcat7 部署terracotta+tomcat集群: 一:安装java程序至系统,配置java环境。 # vi /etc/profile
export JAVA_HOME=/usr/local/javaexport PATH=/usr/local/java/bin:$PATH
二:部署单terracotta服务。 下载terracotta:http://www.terracotta.org/downloads 本例使用terracotta3.7.7版本,每个版本的部署配置略有不同。 # tar -zxvf terracotta-3.7.7.tar.gz
# mv terracotta-3.7.7 /usr/local/terracotta
开启terracotta服务:
# /usr/local/terracotta/bin/start-tc-server.sh &
三:整合Tomcat,使之成为terracotta的客户端。 复制terracotta的相关jar包到Tomcat的lib目录下:(tomcat家目录以$TOMCAT_HOME替代) # cp /usr/local/terracotta/common/terracotta-toolkit-1.6-runtime-5.7.0.jar $TOMCAT_HOME/lib/
# cp /usr/local/terracotta/sessions/terracotta-session-1.3.7.jar $TOMCAT_HOME/lib
修改context.xml文件,添加terracotta连接配置: # vi $TOMCAT_HOME/conf/context.xml
<ValveclassName="org.terracotta.session.TerracottaTomcat70xSessionValve"tcConfigUrl="192.168.1.225:9510"/>
集群中的Tomcat配置都基本如上。
# vi/usr/local/nginx/conf/nginx
upstream session{
server 192.168.1.225:8080 weight=5 max_fails=2 fail_timeout=30s;
server 192.168.1.226:8080 weight=5 max_fails=2 fail_timeout=30s;
}
server {
listen 80 default_server;
server_name 0.0.0.0;
location /session {
proxy_passhttp://session;
}
}
五:开启tomcat、nginx。 # /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
# $TOMCAT_HOME/bin/startup.sh
# $TOMCAT_HOME/bin/startup.sh
六:session测试。 在所有tomcat的session配置目录下,新建session测试页面。 #vi test.jsp
<%@ pagesession="true" %>
<html>
<head>
<title>test Host1</title><!-- //Host2就写为"test Host1" 以示区分 -->
</head>
<body>
<%
out.println("SessionId:"+request.getSession().getId()+"<br />"); out.println("CreationTime:"+request.getSession().getCreationTime());
Stringname=(String)session.getAttribute("name");
if(name==null||name.equals("")){
session.setAttribute("name","HelloHost1!"); //Host2就写为"Hello Host2!"
out.println(session.getAttribute("name"));
}else{
out.println(name);
}
%>
</body>
</html>
使用nginx跳转访问tomcat的test.jsp页面,效果如下: 刷新页面多次,可以清楚看到页面是在两个tomcat之间互相跳转。SessionId除了最后的标识改变外,其余的都保持一直,且内容value一直都是HelloHost2。证明了页面的session id 与内容都保持了一直,session共享是生效的。 另外,还可以在terracotta服务器上开启监控工具,来查看集群状态: #/usr/local/terracotta/bin/dev-console.sh
Terracotta双机配置: 部署两台Terracotta服务器,在Terracotta目录下,新建tc-config.xml #vi tc-config.xml
<?xmlversion="1.0" encoding="UTF-8"?>
<!--All content copyright Terracotta, Inc., unless otherwise indicated.
All rights reserved. -->
<tc:tc-config
xsi:schemaLocation="http://www.terracotta.org/schema/terracotta-5.xsd"
xmlns:tc="http://www.terracotta.org/config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<servers>
<!-- Sets where the Terracotta servercan be found. Replace the value
of host with the server's IP address.-->
<server host="192.168.1.225" name="Server1">
<data>%(user.home)/terracotta/server-data</data>
<logs>%(user.home)/terracotta/server-logs</logs>
</server>
<!-- If using a standby Terracottaserver, also referred to as an
ACTIVE-PASSIVEconfiguration, add the second server here. -->
<server host="192.168.1.226" name="Server2">
<data>%(user.home)/terracotta/server-data</data>
<logs>%(user.home)/terracotta/server-logs</logs>
</server>
<!--If using more than one server, add an <ha> section. -->
<ha>
<mode>networked-active-passive</mode>
<networked-active-passive>
<election-time>5</election-time>
</networked-active-passive>
</ha>
</servers>
<!-- Sets where the generated client logsare saved on clients. -->
<clients>
<logs>%(user.home)/terracotta/client-logs</logs>
</clients>
</tc:tc-config>
然后分别用该配置文件,开启terratocca服务。 #/bin/start-tc-server.sh-f /usr/local/terracotta/tc-config.xml -n Server1 &
#/bin/start-tc-server.sh-f /usr/local/terracotta/tc-config.xml -n Server2 &
|