由于php搭建动态网站当访问量到达一定上限时,就会成为整个系统的瓶颈,这是由于php本身的缺陷造成的。因此tomcat的出现,能够很好的解决php搭建的动态网站带来的问题。这是因为tomcat本身用现在比较流行的java语言开发的,然后在java语言有一种特点,一次编译,到处运行,当然前提是需要提供其运行环境。至于tomcat有哪些好处,这是里就不再介绍。
这次实现apache做tomcat的反代。而apache提供mod_proxy与mod_jk分别实现tomcat的反代。为什么有两种,前面一种是利用http协议,后一种是利用ajp协议。至于为什么要用两种,这就没办法回答你。谷歌,百度随便搜。。。。
在tomcat中自己提供一种特殊的集群,并能将对方服务器所产生的会话复制给自己,这就是会话共享。会话共享的作用现代网站中有着巨大的作用。当用户打开一个网站将商品加入到购物车时,如果刷新浏览器,选购的商品就没有了,如果你是一名消费者,你还会去这个网站上购物吗?答案肯定不会,除非你要买的商品,只有这家有卖。因此会话共享在电子商务的网站中有着巨大的作用。说到这里你或许就应该知道会话是啥东西。当用户正在选购商品的服务器突然发生故障,如果会话做了共享,那么用户所选的商品并不突然不见。
实验拓扑图如下:
一,apache为源码编译,详情不在此博文中描述参照http://bingodeng.blog.51cto.com/1038075/923845。httpd-2.4.2.tar.bz2
-
#tar xf apr-1.4.6.tar.bz2
-
#cd apr-1.4.6
-
#./configure --prefix=/usr/local/apr
-
#make && make install
-
#tar xf apr-util-1.4.1.tar.bz2
-
#cd apr-util-1.4.1
-
#./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
-
#make && make install
-
#tar xf httpd-2.4.2
-
#cd httpd-2.4.2
-
#./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --enable-so
-
--enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre
-
--with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util
-
--enable-proxy --enable-proxy-http --enable-proxy-ajp
-
#make && make install
二,给apache提供一个服务控制脚本上传上附中,下载的脚本,直接使用。
三,vim /etc/profile 添加一行。并执行. /etc/profile
-
PATH=$PAHT:/usr/local/apache/bin/ 新增一行
-
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC 在一行上面添加就ok
四,将其添加到chkconfig中。执行chkconfig --add httpd。并启动服务,查看httpd安装是否正常。service httpd start。如果正常,则继续下一步
下面开始给两台服务安装tomcat。由于后面两台服务器做成tomcat集群,才能共享会话。因此需要将两台服务的时间调成一样。
-
#/usr/java/jdk1.7.0_05/bin/java -version 显示下列信息表示安装成功
-
java version "1.7.0_05"
-
Java(TM) SE Runtime E
-
nvironment (build 1.7.0_05-b05)
-
Java HotSpot(TM) Client VM (build 23.1-b03, mixed mode, sharing)
-
JAVA_HOME=/usr/java/jdk1.7.0_05
-
PATH=$PATH:$JAVA_HOME/bin
-
export JAVA_HOME PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC
-
# . /etc/profile
-
#tar xf apache-tomcat-7.0.29.tar.gz -C /usr/local
-
#cd /usr/local/
-
#ln -sv apache-tomcat-7.0.29.tar.gz tomcat
-
#cd tomcat
-
#bin/catalina
-
#bin/catalina.sh start 启动服务,
-
# cd /usr/local/tomcat/webapps/
-
# mkdir -pv sess/WEB-INF/{classes,lib}
-
# cd sess
-
vim index.jsp
-
<%@ page language="java" %>
-
<html>
-
<head><title>TomcatA</title></head>
-
<body>
-
<h1><fontcolor="red">TomcatA </font></h1>
-
<tablealign="centre"border="1">
-
<tr>
-
<td>Session ID</td>
-
<td><%= session.getId() %></td>
-
</tr>
-
<tr>
-
<td>Created on</td>
-
<td><%= session.getCreationTime() %></td>
-
</tr>
-
</table>
-
</body>
-
</html>
-
#vim webapps/sess/WEB-INF/web.xml
-
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.5">
<display-name>TomcatDemo</display-name>
<distributable/> //添加这一行
</web-app>
-
#vim conf/server.xml
-
<Enginename="Catalina"defaultHost="localhost"jvmRoute="TomcatA"> 添加jvmRoute="TomcatA"测试效果时需要用到
-
添加下面这些代码在Engine 的下面
-
<ClusterclassName="org.apache.catalina.ha.tcp.SimpleTcpCluster"
-
channelSendOptions="8">
-
<ManagerclassName="org.apache.catalina.ha.session.DeltaManager"
-
expireSessionsOnShutdown="false"
-
notifyListenersOnReplication="true"/>
-
<ChannelclassName="org.apache.catalina.tribes.group.GroupChannel">
-
<MembershipclassName="org.apache.catalina.tribes.membership.McastService"
-
address="228.50.10.1"bind="172.16.150.3"port="45564"
-
frequency="500"dropTime="3000"/>
-
<ReceiverclassName="org.apache.catalina.tribes.transport.nio.NioReceiver"
-
address="172.16.150.3"port="4000"autoBind="100"
-
selectorTimeout="5000"maxThreads="6"/>
-
<SenderclassName="org.apache.catalina.tribes.transport.ReplicationTransmitter">
-
<TransportclassName="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
-
</Sender>
-
<InterceptorclassName="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
-
<InterceptorclassName="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
-
</Channel>
-
<ValveclassName="org.apache.catalina.ha.tcp.ReplicationValve"
-
filter=".*\.gif;.*\.js;.*\.jpg;.*\.htm;.*\.html;.*\.txt;"/>
-
<ValveclassName="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
-
<DeployerclassName="org.apache.catalina.ha.deploy.FarmWarDeployer"
-
tempDir="/tmp/war-temp/"deployDir="/tmp/war-deploy/"
-
watchDir="/tmp/war-listen/"watchEnabled="false"/>
-
<ClusterListenerclassName="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
-
<ClusterListenerclassName="org.apache.catalina.ha.session.ClusterSessionListener"/>
-
</Cluster>
-
#cd /usr/local/tomcat
-
#bin/catalina.sh stop
-
#bin/catalina.sh start
-
#vim /etc/http/http.conf
-
LoadModule slotmem_shm_module modules/mod_slotmem_shm.so 启用这个模块。
-
#DocumentRoot "/usr/local/apache/htdocs" 这一行注释
-
# Virtual hosts
-
Include /etc/httpd/extra/httpd-vhosts.conf 启用虚拟机配置文件,这样做方便管理。
-
#cd /etc/httpd/extra/
-
#vim httpd-vhosts.conf
-
ProxyRequests Off
-
<proxy balancer://lbcluster1>
-
BalancerMember ajp://172.16.150.3:8009 loadfactor=10route=TomcatA
-
(10表示服务器权重值,为了演示效果将其设置一样TomcatA与jvmRoute="TomcatA这要保持一致,下面一样)
-
BalancerMember ajp://172.16.150.4:8009 loadfactor=10route=TomcatB ()
-
proxySet lbmethod=bytraffic 调度算法
-
</proxy>
-
ProxyPass /lbmanager ! 如果访问http://192.168.0.130/lbmanager表示不转发。下同
-
ProxyPass /status !
-
ProxyPass / balancer://lbcluster1/
-
ProxyPassReverse / balancer://lbcluster1/
-
ProxyStatus On
-
<Location /lbmanager>
-
SetHandler balancer-manager
-
Require all granted
-
</location>
-
<Location /status>
-
SetHandler server-status
-
Require all granted
-
</location>
-
#tar xf tomcat-connectors-1.2.37-src.tar.gz
-
# cd tomcat-connectors-1.2.37-src
-
# cd native/
-
# ./configure --with-apxs=/usr/local/apache/bin/apxs
-
# make && make install
-
八,vim /etc/httpd/httpd.conf
-
# Virtual hosts
-
#Include /etc/httpd/extra/httpd-vhosts.conf 将这行注释
-
Include /etc/httpd/extra/httpd-jk.conf 添加一行
-
#cd /etc/httpd/extra/
-
#vim /etc/httpd/extra/httpd-jk.conf
-
LoadModule jk_module modules/mod_jk.so
-
JkWorkersFile /etc/httpd/extra/workers.properties
-
JkLogFile logs/mod_jk.log
-
JkLogLevel debug
-
JkMount /* lbcluster1
-
JkMount /status stat1
-
#vim workers.properties
-
worker.list=lbcluster1,stat1
-
worker.TomcatA.port=8009
-
worker.TomcatA.host=172.16.150.3
-
worker.TomcatA.type=ajp13
-
worker.TomcatA.lbfactor=2
-
worker.TomcatB.port=8009
-
worker.TomcatB.host=172.16.150.4
-
worker.TomcatB.type=ajp13 ajp协议13版
-
worker.TomcatB.lbfactor=1 权重值
-
worker.lbcluster1.sticky_session = 0
-
worker.lbcluster1.type=lb
-
worker.lbcluster1.balance_workers=TomcatA,TomcatB
-
worker.stat1.type=status