Terracotta3.3.7+ tomcat6.0 + apache2.2.2集群配置
一.目的
Tomcat集群配置,实现集群服务器中session共享
二.准备材料:
1. terracotta-3.7.7-2013-08-19_16-03-48-installer.jar
2. httpd-2.2.25-win32-x86-openssl-0.9.8y.msi
3. apache-tomcat-6.0.45-windows-x64.zip
4. tomcat-connectors-1.2.40-windows-i386-httpd-2.2.x.zip
5.jdk环境必须为1.6,terracotta3.7.7只能支持1.6(后来测试中改为了1.7 发现也能使用)
三.配置两个tomcat6.0
新建两个文件夹tom1,tom2, 将apache-tomcat-6.0.45-windows-x64.zip解压后,分别放置到两个文件夹下,如图所示:
因为需要同时启动两个tomcat,所以必须要修改两个端口,配置文件如下:
Tom1中的server.xml
<?xmlversion='1.0' encoding='utf-8'?>
<!--
Licensed to the Apache Software Foundation(ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional informationregarding copyright ownership.
The ASF licenses this file to You under theApache License, Version 2.0
(the "License"); you may not usethis file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreedto in writing, software
distributed under the License is distributedon an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express or implied.
See the License for the specific languagegoverning permissions and
limitations under the License.
-->
<!--Note: A "Server" is not itselfa "Container", so you may not
define subcomponents such as"Valves" at this level.
Documentation at /docs/config/server.html
-->
<Serverport="8006" shutdown="SHUTDOWN">
<!--APR library loader. Documentation at/docs/apr.html -->
<ListenerclassName="org.apache.catalina.core.AprLifecycleListener"SSLEngine="on" />
<!--Initialize Jasper prior to webapps areloaded. Documentation at /docs/jasper-howto.html -->
<ListenerclassName="org.apache.catalina.core.JasperListener" />
<!-- Prevent memory leaks due to use ofparticular java/javax APIs-->
<ListenerclassName="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>
<!--JMX Support for the Tomcat server. Documentation at /docs/non-existent.html-->
<ListenerclassName="org.apache.catalina.mbeans.ServerLifecycleListener" />
<ListenerclassName="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
<GlobalNamingResources>
<Resource name="UserDatabase"auth="Container"
type="org.apache.catalina.UserDatabase"
description="User databasethat can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
<Service name="Catalina">
<Connector port="8081"protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8445"/>
<Connector port="8010"protocol="AJP/1.3" redirectPort="8445" />
<Enginename="Catalina" defaultHost="localhost"jvmRoute="tomcat1">
<ClusterclassName="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
<!-- This Realm uses the UserDatabaseconfigured in the global JNDI
resources under the key"UserDatabase". Any edits
that are performed against thisUserDatabase are immediately
available for use by the Realm. -->
<RealmclassName="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
<Hostname="localhost" appBase="webapps"
unpackWARs="true"autoDeploy="true"
xmlValidation="false"xmlNamespaceAware="false">
</Host>
</Engine>
</Service>
</Server>
Tom2 中的server.xml
<?xml version='1.0' encoding='utf-8'?>
<Server port="8007" shutdown="SHUTDOWN">
<!--APR library loader.Documentation at /docs/apr.html -->
<ListenerclassName="org.apache.catalina.core.AprLifecycleListener"SSLEngine="on" />
<!--Initialize Jasperprior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
<ListenerclassName="org.apache.catalina.core.JasperListener" />
<!-- Prevent memoryleaks due to use of particular java/javax APIs-->
<ListenerclassName="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>
<!-- JMX Support forthe Tomcat server. Documentation at /docs/non-existent.html -->
<ListenerclassName="org.apache.catalina.mbeans.ServerLifecycleListener" />
<ListenerclassName="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
<GlobalNamingResources>
<Resourcename="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
<Servicename="Catalina">
<Connectorport="8083" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8446" />
<Connectorport="8011" protocol="AJP/1.3"redirectPort="8446" />
<Enginename="Catalina" defaultHost="localhost"jvmRoute="tomcat2">
<ClusterclassName="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
<RealmclassName="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
<Hostname="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
</Host>
</Engine>
</Service>
</Server>
同时将两个tomcat都加入windows服务中,
对于tom1,修改bin目录下的tomcat6.exe为tomcat6_1.exe,tomcat6w.exe为tomcat6w_1.exe,用记事本打开service.bat,将里面的tomcat6全部改为tomcat6_1,同时修改set SERVICE_NAME=Tomcat6为set SERVICE_NAME=Tomcat6_1,
同理对于tom2修改为tomcat_2,
通过cmd界面,cd 进入tom1的bin目录,执行service install命令安装服务
同理,安装好tom2.
四.配置apache
双击httpd-2.2.25-win32-x86-openssl-0.9.8y.msi进行安装,选择文件夹为D:\soft\apache (按自己喜欢的位置来),安装成功后,开始进行配置。
解压tomcat-connectors-1.2.40-windows-i386-httpd-2.2.x.zip,得到mod_jk.so,将其放入至apache安装目录的modules下面,然后打开conf文件夹下httpd.conf配置文件,向其中添加一条语句 Includeconf/mod_jk.conf
接着在conf里面新建mod_jk.conf,填写一些内容如下:
LoadModulejk_module "D:/soft/apache/modules/mod_jk.so"
JkWorkersFile"conf/workers.properties"
JkMount/*.jsp controller
JkMount/*.do controller
以上就配置好了apache的mod_jk模块(负载均衡),将*.jsp和*.do转发给tomcat
接着在conf里面新建一个workers.properties,内容如下:
#server
worker.list =controller
#========tomcat1========
worker.tomcat1.port=8010
worker.tomcat1.host=localhost
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor= 1
#========tomcat2========
worker.tomcat2.port=8011
worker.tomcat2.host=localhost
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor= 1
#========controller,负载均衡控制器========
worker.controller.type=lb
worker.controller.balanced_workers=tomcat1,tomcat2
worker.controller.sticky_session=false
worker.controller.sticky_session_force=1
#worker.controller.sticky_session=1
至此apache配置结束。
(其实到此就可以实现session共享了,只不过是tomcat自带的粘性session,一当session过多后就会导致性能下降,可以写个小web项目sharesession测试一下,修改web.xml文件,在<web-app></web-app>之间加入</description>,这是配置tomcat进行粘性session的标签,最后新建index.jsp页面,内容如下:)
<%@ pagelanguage="java" import="java.util.*"pageEncoding="UTF-8"%>
<%@ pageimport="java.util.*" %>
<html><head><title>ClusterApp Test</title></head>
<body>
Server Info:
<%
out.println(request.getLocalAddr()+ " : " + request.getLocalPort()+"<br>");%>
<%
out.println("<br> ID " +session.getId()+"<br>");
// 如果有新的 Session 属性设置
String dataName =request.getParameter("dataName");
if (dataName != null &&dataName.length() > 0) {
String dataValue =request.getParameter("dataValue");
session.setAttribute(dataName, dataValue);
}
out.println("<b>Session 列表</b><br>");
System.out.println("********************");
Enumeration e =session.getAttributeNames();
while (e.hasMoreElements()) {
String name =(String)e.nextElement();
String value =session.getAttribute(name).toString();
out.println( name + " = " +value+"<br>");
System.out.println( name + " =" + value);
}
%>
<form action="index.jsp"method="POST">
名称:<input type=text size=20name="dataName">
<br>
值:<input type=text size=20name="dataValue">
<br>
<input type=submit>
</form>
</body>
</html>
打开浏览器后,输入地址127.0.0.1:80/sharesession/index.jsp,即可访问,多打开几个页面会发现,session并不会改变,改变的只是不同tomcat对请求进行处理。
四.配置terracotta
双击terracotta-3.7.7-2013-08-19_16-03-48-installer.jar安装至D:\terracotta目录下,在该目录下添加tc-config.xml文件,内容如下:
<?xmlversion="1.0" encoding="UTF-8" ?>
<tc:tc-configxmlns:tc="http://www.terracotta.org/config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.terracotta.org/schema/terracotta-6.xsd">
<tc-properties>
<propertyname="l2.nha.dirtydb.autoDelete" value="true"/>
<propertyname="l1.cachemanager.enabled" value="true"/>
<propertyname="logging.maxLogFileSize" value="1024"/>
</tc-properties>
<system>
<configuration-model>development</configuration-model>
</system>
<servers>
<server host="127.0.0.1"name="localhost" bind="0.0.0.0">
<statistics>/opt/terracotta/server-statistics</statistics>
<dso-portbind="127.0.0.1">9510</dso-port>
<jmx-portbind="127.0.0.1">9520</jmx-port>
<l2-group-portbind="127.0.0.1">9530</l2-group-port>
<authentication/>
<dso>
<client-reconnect-window>120</client-reconnect-window>
<persistence>
<mode>temporary-swap-only</mode>
</persistence>
<garbage-collection>
<enabled>true</enabled>
<verbose>false</verbose>
<interval>3600</interval>
</garbage-collection>
</dso>
</server>
<mirror-groups>
<mirror-groupgroup-name="group1">
<members>
<member>localhost</member>
</members>
<ha>
<mode>networked-active-passive</mode>
<networked-active-passive>
<election-time>5</election-time>
</networked-active-passive>
</ha>
</mirror-group>
</mirror-groups>
<ha>
<mode>networked-active-passive</mode>
<networked-active-passive>
<election-time>5</election-time>
</networked-active-passive>
</ha>
<update-check>
<enabled>true</enabled>
<period-days>10</period-days>
</update-check>
</servers>
<clients>
<logs>logs-%i</logs>
</clients>
</tc:tc-config>
将D:\terracotta\sessions目录下的terracotta-session-XXXXXXX.jar以及
D:\terracotta\common目录下的terracotta-toolkit-1.6-runtime-XXXXX.jar
复制到tomcat的lib目录下,然后在刚刚sharesession测试项目的META-INF文件夹下添加context.xml文件,内容如下:
<?xml version="1.0"encoding="UTF-8"?>
<Context>
<Valve className="org.terracotta.session.TerracottaTomcat60xSessionValve"tcConfigUrl="127.0.0.1:9510" />
</Context>
配置到此结束了!
开始启动terracotta,cmd中进入terracotta安装目录,然后执行 start-tc-server.bat批处理文件,如下图:
启动成功!
接着打开两个tomcat6.0
均启动成功!
最后打开apache,
启动成功!
打开浏览器输入
很明显 session实现了共享, session值是一样的,不一样的只是请求处理的服务器。同时可以使用terracotta的Terracotta Developer Console进行查看
结果:测试中发现 Terracotta3.3.7 存储的对象同样也需要序列化,用了两个tomcat,通过apache协调调度tomcat,有效的均衡了浏览器url请求,并且在后来的使用中将jdk改为了1.7,并将tomcat6.0改为了tomcat8.0,运行正常。