环境准备:(nginx,Tomcat的安装部署。这个例子就不赘述了。)
192.168.1.13nginx
192.168.1.225公猫7,赤褐色
192.168.1.226tomcat7
部署terracotta+tomcat集群:
1.将java程序安装到系统中,并配置java环境。
# vi /etc/profile export JAVA_HOME=/usr/local/java export PATH=/usr/local/java/bin:$PATH二。部署单一兵马俑服务。
下载兵马俑:http://www.terracotta.org/downloads
本例使用terracott3.7.7版本3.7.7,每个版本的部署配置略有不同。
# tar -zxvf terracotta-3.7.7.tar.gz # mv terracotta-3.7.7.tar.gz /usr/local/terracotta 开启terracotta服务: # /usr/local/terracotta/bin/start-tc-server.sh &第三,整合Tomcat成为terracotta的客户端。
将terracotta的相关jar包复制到tomcat的lib目录中:(Tomcat的home目录被$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配置基本上和上面一样。
第四,配置nginx,通过loadblance的方式访问后端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。
13: # /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 225:# $TOMCAT_HOME/bin/startup.sh 226:# $TOMCAT_HOME/bin/startup.sh六:会话测试。
在所有tomcat的会话配置目录下,创建一个新的会话测试页面。
#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保持不变,内容值始终为HelloHost2。证明页面的会话id和内容得到了维护,会话共享是有效的。
此外,您还可以在terracotta服务器上打开监控工具来检查集群状态:
#/usr/local/terracotta/bin/dev-console.shTerracotta双机配置:
部署两个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服务。
225:
#/bin/start-tc-server.sh-f /usr/local/terracotta/tc-config.xml -n Server1 &226:
#/bin/start-tc-server.sh-f /usr/local/terracotta/tc-config.xml -n Server2 &欢迎分享,转载请注明来源:内存溢出
评论列表(0条)