测试环境:
os:centos6.3
代理服务器:apache2.4.9 apr1.4以上 192.168.30.150
tomcat应用1: apache-tomcat-7.0.57 192.168.30.149
tomcat应用2: apache-tomcat-7.0.57 192.168.30.148
安装配置:
1.代理服务器
(1) 编译安装apr
yum groupinstall "Developmenttools" -y
yum groupinstall "DevelopmentLibraries" -y
# tar xf apr-1.5.1.tar.gz
# cd apr-1.5.1
# ./configure --prefix=/usr/local/apr
# make && make install
(2) 编译安装apr-util
# tar xf apr-util-1.5.3.tar.gz
# cd apr-util-1.4.1
# ./configure --prefix=/usr/local/apr-util--with-apr=/usr/local/apr
# make && make install
安装pcre
tar xf pcre-8.35.tar.gz
cd pcre-8.35
./configure --prefix=/usr/local/pcre
# make && make install
安装openssl
tar xf openssl-0.9.8zc.tar.gz
cd openssl-0.9.8zc
./config --prefix=/usr/local/openssl
make & make install
编译安装httpd-2.4.9
首先下载httpd-2.4.9到本地,而后执行如下命令进行编译安装过程:
# tar xf httpd-2.4.9.tar.bz2
# cd httpd-2.4.9
./configure --prefix=/usr/local/apache--sysconfdir=/etc/httpd --enable-so --enable-ssl=/usr/local/openssl/--enable-cgi --enable-rewrite --with-zlib --with-pcre=/usr/local/pcre/--with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util--enable-mpms-shared=all --with-mpm=event --enable-proxy --enable-proxy-http--enable-proxy-ajp --enable-proxy-balancer --enable-lbmethod-heartbeat --enable-heartbeat --enable-slotmem-shm --enable-slotmem-plain --enable-watchdog --enable-proxy-balancer --enable-lbmethod-heartbeat--enable-heartbeat --enable-slotmem-shm
--enable-slotmem-plain --enable-watchdog
# make && make install
修改httpd的主配置文件,设置其Pid文件的路径
编辑/etc/httpd/httpd.conf,添加如下行即可:
PidFile "/var/run/httpd.pid"
4、提供SysV服务脚本/etc/rc.d/init.d/httpd,内容如下:
#!/bin/bash
#
# httpd Startup script for the Apache HTTPServer
#
# chkconfig: - 85 15
# description: Apache is a World Wide Webserver. It is used to serve \
# HTML files and CGI.
# processname: httpd
# config: /etc/httpd/conf/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /var/run/httpd.pid
# Source function library.
. /etc/rc.d/init.d/functions
if [ -f /etc/sysconfig/httpd ]; then
. /etc/sysconfig/httpd
fi
# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-"C"}
# This will prevent initlog from swallowingup a pass-phrase prompt if
# mod_ssl needs a pass-phrase from theuser.
INITLOG_ARGS=""
# Set HTTPD=/usr/sbin/httpd.worker in/etc/sysconfig/httpd to use a server
# with the thread-based "worker"MPM; BE WARNED that some modules may not
# work correctly with a thread-based MPM;notably PHP will refuse to start.
# Path to the apachectl script, serverbinary, and short-form for messages.
apachectl=/usr/local/apache/bin/apachectl
httpd=${HTTPD-/usr/local/apache/bin/httpd}
prog=httpd
pidfile=${PIDFILE-/var/run/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0
start() {
echo -n $"Starting $prog: "
LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch ${lockfile}
return $RETVAL
}
stop() {
echo-n $"Stopping $prog: "
killproc-p ${pidfile} -d 10 $httpd
RETVAL=$?
echo
[$RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
echo -n $"Reloading $prog: "
if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
RETVAL=$?
echo $"not reloading due to configuration syntax error"
failure $"not reloading $httpd due to configuration syntaxerror"
else
killproc -p ${pidfile} $httpd -HUP
RETVAL=$?
fi
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status -p ${pidfile} $httpd
RETVAL=$?
;;
restart)
stop
start
;;
condrestart)
if[ -f ${pidfile} ] ; then
stop
start
fi
;;
reload)
reload
;;
graceful|help|configtest|fullstatus)
$apachectl$@
RETVAL=$?
;;
*)
echo$"Usage: $prog{start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"
exit1
esac
exit $RETVAL
而后为此脚本赋予执行权限:
# chmod +x /etc/rc.d/init.d/httpd
加入服务列表:
# chkconfig --add httpd
开启模块:(取消注释)
LoadModule slotmem_shm_modulemodules/mod_slotmem_shm.so
LoadModule slotmem_plain_modulemodules/mod_slotmem_plain.so
要使用mod_proxy与Tomcat实例连接,需要apache已经装载mod_proxy、mod_proxy_http、mod_proxy_ajp和proxy_balancer_module(实现Tomcat集群时用到)等模块:
# /usr/local/apache/bin/httpd -DDUMP_MODULES | grep proxy
proxy_module (shared)
proxy_connect_module (shared)
proxy_ftp_module (shared)
proxy_http_module (shared)
proxy_fcgi_module (shared)
proxy_scgi_module (shared)
proxy_ajp_module (shared)
proxy_balancer_module (shared)
proxy_express_module (shared)
基于mod_proxy实现负载均衡
在httpd.conf的全局配置中配置如下内容:
ProxyVia On
ProxyRequests Off
<proxy balancer://lbcluster1>
BalancerMember ajp://192.168.30.149:8009loadfactor=10 route=TomcatA
BalancerMember ajp://192.168.30.148:8009loadfactor=10 route=TomcatB
</proxy>
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName test.wtoip.com
ProxyPass / balancer://lbcluster1/stickysession=jsessionid
ProxyPassReverse / balancer://lbcluster1/
</VirtualHost>
后端tomcat的安装与配置
jdk安装与配置:
mkdir -p /usr/local/jvm
tar zxvf jdk-7u71-linux-x64.tar.gz -C/usr/local/jvm/
vim /etc/profile.d/java.sh
export JAVA_HOME=/usr/local/jvm/jdk1.7.0_71
export JRE_HOME=${JAVA_HOME}/jre
exportCLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
source /etc/profile
java �Cversion
安装tomcat
tar zxvf apache-tomcat-7.0.57.tar.gz -C /usr/local/
cd /usr/local/
ln -sv apache-tomcat-7.0.57/ tomcat
vi/etc/profile.d/tomcat.sh
export CATALINA_HOME=/usr/local/tomcat
export PATH=$PATH:$CATALINA_HOME/bin
source /etc/profile
catalina.sh start
检查开启状态:
netstat -tnlp
8080
8009
tomcat init 脚本
#!/bin/bash
#chkconfig2345
JAVA_HOME=/usr/java/jdk
CATALINA_HOME=/usr/local/tomcat
exportJAVA_HOME CATALINA_HOME
exec$CATALINA_HOME/bin/catalina.sh $*
测试应用:tomcata
<%@ page language="java" %>
<html>
<head><title>TomcatA</title></head>
<body>
<h1><font color="red">TomcatA</font></h1>
<table border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("abc","abc"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
演示效果,在TomcatB上某context中(如/test),提供如下页面
<%@ page language="java" %>
<html>
<head><title>TomcatB</title></head>
<body>
<h1><font color="blue">TomcatB</font></h1>
<table border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("abc","abc"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
Tomcat基于内存复制的集群(单播)
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster">
<ChannelclassName="org.apache.catalina.tribes.group.GroupChannel">
<ReceiverclassName="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="192.168.5.149"
port="4000"
selectorTimeout="100"
maxThreads="6"/>
<InterceptorclassName="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.StaticMembershipInterceptor">
<MemberclassName="org.apache.catalina.tribes.membership.StaticMember"
port="4001"
securePort="-1"
host="192.168.5.199"
domain="staging-cluster"
uniqueId="{0,1,2,3,4,5,6,7,8,9}"/>
</Interceptor>
</Channel>
<DeployerclassName="org.apache.catalina.ha.deploy.FarmWarDeployer"
tempDir="/usr/share/tomcat6/tempdir/"
deployDir="/usr/share/tomcat6/webapps/"
watchDir="/usr/share/tomcat6/watchdir/"
watchEnabled="true"/>
</Cluster>
以上内容定义在Engine容器中,则表示对所有主机均启动用集群功能。如果定义在某Host中,则表示仅对此主机启用集群功能。
此外,所有启用集群功能的web应用程序,其web.xml中都须添加<distributable/>才能实现集群功能。如果某web应用程序没有自己的web.xml,也可以通过复制默认的web.xml至其WEB-INF目录中实现。(<distributable/>加到web-app中)