http://blog.csdn.net/ppzlyg/archive/2011/01/31/6170684.aspx
apache-tomcat-7.0.6.tar.gz
httpd-2.2.17.tar.gz
jdk-6u23-linux-i586.bin
tomcat-connectors-1.2.31-src.tar.gz
1个apache 2个tomcat在一台机器上,修改其中一个tomcat相关端口,对应如下
8080 8980
8005 8905
8009 8909
4000 4001
1.安装httpd
tar zxvf httpd-2.2.17.tar.gz
cd httpd-2.2.17
./configure --prefix=/usr/local/apache2 --enable-so
make && make install
2.安装JDK
chmod +x jdk-6u18-linux-i586.bin
./jdk-6u18-linux-i586.bin
mv jdk1.6.0_18 /usr/local/jdk
rm -rf /usr/bin/java
rm -rf /usr/bin/javac
ln -s /usr/local/jdk/bin/java /usr/bin/java
ln -s /usr/local/jdk/bin/javac /usr/bin/javac
[root@centoslamp src]# java -version
java version "1.6.0_18"
Java(TM) SE Runtime Environment (build 1.6.0_18-b07)
Java HotSpot(TM) Client VM (build 16.0-b13, mixed mode, sharing)
ln -s /usr/local/jdk/jre /usr/local/jre
设置环境变量
vi /etc/profile 添加如下内容
JAVA_HOME=/usr/local/jdk
export JAVA_HOME
JRE_HOME=/usr/local/jre
export JRE_HOME
运行,使环境变量生效
source /etc/profile
3.安装tomcat
tar zxvf apache-tomcat-7.0.6.tar.gz
mv apache-tomcat-7.0.6 /usr/local/tomcat1
tar zxvf apache-tomcat-7.0.6.tar.gz
mv apache-tomcat-7.0.6 /usr/local/tomcat2
4.安装tomcat-connectors
tar zxvf tomcat-connectors-1.2.31-src.tar.gz
cd tomcat-connectors-1.2.31-src/native/
./configure --with-apxs=/usr/local/apache2/bin/apxs --with-java-home=/usr/local/jdk
make && make install
5. 配置httpd.conf 文件
vi /usr/local/apache2/conf/httpd.conf
添加
LoadModule jk_module modules/mod_jk.so
<IfModule jk_module>
JkWorkersFile conf/workers.properties
JkMountFile conf/uriworkermap.properties
JkLogFile logs/mod_jk.log
JkLogLevel warn
</IfModule>
6.创建并配置workers.properties文件
vi /usr/local/apache2/conf/workers.properties
worker.list=wlb , jkstatus
worker.worker1.port=8009
worker.worker1.host=localhost
worker.worker1.type=ajp13
worker.worker1.lbfactor=1
# Define preferred failover node for worker1
#worker.worker1.redirect=worker2
worker.worker2.port=8909
worker.worker2.host=localhost
worker.worker2.type=ajp13
worker.worker2.lbfactor=1
# Disable worker2 for all requests except failover
#worker.worker2.activation=disabled
worker.wlb.type=lb
worker.wlb.balance_workers=worker1,worker2
worker.jkstatus.type=status
以上注释掉的两行如果开启,则worker2成为worker1的备用,在worker1不可用的情况下才会向worker2请求
7.创建并配置uriworkermap.properties
vi /usr/local/apache2/conf/uriworkermap.properties
/admin/*=wlb
/manager/*=wlb
/jsp-examples/*=wlb
/servlets-examples/*=wlb
/examples/*=wlb
/*.jsp=wlb
!/servlets-examples/*.jpeg=wlb
/jkmanager=jkstatus
!/*.gif=wlb
!/*.jpg=wlb
!/*.png=wlb
!/*.css=wlb
!/*.js=wlb
!/*.htm=wlb
!/*.html=wlb
8.编辑server.xml文件
vi /usr/local/tomcat/conf/server.xml
修改相关对应端口,如果在不同的机器上则不需要修改
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<!-- A "Connector" using the shared thread pool-->
<!--
<Connector executor="tomcatThreadPool"
port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Server port="8005" shutdown="SHUTDOWN">
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
9.复制tomcat下webapps下的文件到/usr/local/apache2/htdocs
cp –R /usr/local/tomcat1/webapps/* /usr/local/apache2/htdocs
10.启动tomcat 和httpd
/usr/local/tomcat1/bin/startup.sh
/usr/local/tomcat2/bin/startup.sh
/usr/local/apache2/bin/apachectl start
到这里负载均衡完成
配置集群
11.修改tomcat1 下的server.xml
<Engine name="Catalina" defaultHost="localhost" jvmRoute="worker1">
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="8">
<Manager className="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>
<Channel className="org.apache.catalina.tribes.group.GroupChannel">
<Membership className="org.apache.catalina.tribes.membership.McastService"
address="228.0.0.4"
port="45564"
frequency="500"
dropTime="3000"/>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="192.168.15.124" port="4000" autoBind="100"
selectorTimeout="5000"
maxThreads="6"/>
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
</Channel>
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=""/>
<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
<Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>
<ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
12.修改tomcat2的server.xml文件
<Engine name="Catalina" defaultHost="localhost" jvmRoute="worker2">
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="8">
<Manager className="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>
<Channel className="org.apache.catalina.tribes.group.GroupChannel">
<Membership className="org.apache.catalina.tribes.membership.McastService"
address="228.0.0.4"
port="45564"
frequency="500"
dropTime="3000"/>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="192.168.15.124" port="4001" autoBind="100"
selectorTimeout="5000"
maxThreads="6"/>
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
</Channel>
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=""/>
<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
<Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>
<ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
13.查看
netstat –ant
tcp 0 0 ::ffff:192.168.15.124:4000 :::* LISTEN
tcp 0 0 ::ffff:192.168.15.124:4001 :::* LISTEN
14.修改应用程序的web.xml文件
在</web-app> 之前加上<distributable/> 用于session 复制
vi /usr/local/tomcat2/webapps/examples/WEB-INF/web.xml
vi /usr/local/tomcat2/webapps/examples/WEB-INF/web.xml
vi /usr/local/apache2/htdocs/examples/WEB-INF/web.xml
<distributable/>
</web-app>
15.测试session 复制
打开http://192.168.15.124/examples/servlets/servlet/SessionExample
输入相关内容,然后关闭其中的正在连接的tomcat 连接到第二个tomcat 查看之前提交的内容是否还在
Sessions ExampleSession ID: A45E7C637D98B3EBDEF052BEADF8E5F4.worker2 Created: Mon Jan 31 12:07:33 CST 2011
Last Accessed: Mon Jan 31 12:09:26 CST 2011The following data is in your session:
1 = 1
2 = 2
Sessions ExampleSession ID: A45E7C637D98B3EBDEF052BEADF8E5F4.worker1 Created: Mon Jan 31 12:07:33 CST 2011
Last Accessed: Mon Jan 31 12:10:09 CST 2011The following data is in your session:
1 = 1
2 = 2
相关文档
http://www.ibm.com/developerworks/cn/opensource/os-lo-apache-tomcat/
/usr/local/src/tomcat-connectors-1.2.31-src/conf/下的
httpd-jk.conf uriworkermap.properties workers.properties workers.properties.minimal
No comments:
Post a Comment