环境准备:
apache2 + tomcat + mod_jk.so
一、安装
我之前的一篇博文CentOS下安装tsung中已经提到如何安装apache2,现在只讲tomcat和mod_jk.so的安装
1、tomcat
当然,首先需要安装JDK(本机已安装jdk1.6),tomcat官网下载apache-tomcat-6.0.37.tar.gz
# tar xzvf apache-tomcat-6.0.37.tar.gz
解压后自己找个位置放就可以,我选择放在/opt/目录下,配置环境变量
# vi /etc/profile 增加以下5行
JAVA_HOME=/usr/java/jdk1.6.0_45
CATALINA_HOME=/opt/apache-tomcat-6.0.37
PATH=$JAVA_HOME/BIN:$CATALINA_HOME/bin:$PATH
CLASSPATH=.:$JAVA_HOME/lib:$CATALINA_HOME/lib
export JAVA_HOME CATALINA_HOME PATH CLASSPATH
# . /etc/profile 使环境变量生效
另外修改主机的名称有两个文件/etc/hosts和/etc/sysconfig/network,后一个文件修改了,前一个文件也应该对应修改,不然启动tomcat,会报错不认识主机名
2、mod_jk.so
在tomcat官网上下载tomcat-connectors-1.2.37-src.tar.gz(目前最新版本)
# tar xzvf tomcat-connectors-1.2.37-src.tar.gz
# cd tomcat-connectors-1.2.37-src
# cd native
# ./configure --with-apxs=/usr/local/apache2/bin/apxs
# make 生成mod_jk.so
# cp cp apache-2.0/mod_jk.so /usr/local/apache2/modules/ 拷贝到apache目录下
二、配置
1、修改/usr/local/apache2/conf/httpd.conf,末尾加一句
include "/usr/local/apache2/conf/mod_jk.conf"
2、新建mod_jk.conf
#Load mod_jk module
LoadModule jk_module modules/mod_jk.so
#Where to find workers.properties
JkWorkersFile conf/workers.properties
#Where to put jk logs
JklogFile logs/mod_jk.log
#Set the jk log level[debug/error/info]
JkLogLevel info
#Select the log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
#JkOptions indicate to send SSL KEY SIZE,
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
#JkRequestLogFormat set the request format
JkRequestLogFormat "%w %V %T"
#Send servlet for context /examples to worker named ajp13
JkMount /* controller
#Send JSPs for context /examples to worker named ajp13
JkMount /*.jsp controller
3、新建workers.properties
#server
worker.list = controller
#========tomcat1========
worker.tomcat1.port=8009
worker.tomcat1.host=IP1
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor = 1
#========tomcat2========
worker.tomcat2.port=8009
worker.tomcat2.host=IP2
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor = 1
#========controller,负载均衡控制器========
worker.controller.type=lb
worker.controller.balance_workers=tomcat1,tomcat2
worker.controller.sticky_session=false
worker.controller.sticky_session_force=1
#worker.controller.sticky_session=1
4、修改tomcat的server.xml
3中配置了两个tomcat做负载均衡,需要修改server.xml
90行:<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
配置的port要和3中配置的一样
102行:<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
jvmRoute的名字和3中要一样,另外一个应该是tomcat2
108行:<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
这一行注释去掉,解禁出来
三、测试
新建web工程,添加testCluster.jsp,部署到tomcat1和tomcat2上
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.util.*" %>
<html><head><title>Cluster App 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 List</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="testCluster.jsp" method="POST">
Key:<input type=text size=20 name="dataName">
<br>
Value:<input type=text size=20 name="dataValue">
<br>
<input type=submit>
</form>
</body>
</html>
通过访问apache跳转到tomcat1和tomcat2,测试可以发现Session已经复制成功,已经实现共享