免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4038 | 回复: 1
打印 上一主题 下一主题

apache+tomcat整合集群负载均衡 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-02-09 10:23 |只看该作者 |倒序浏览
apache+tomcat整合集群负载均衡
环境:redhat 4-2.6.9-89.ELsmp
      httpd-2.2.6.tar.gz
      jakarta-tomcat-connectors-1.2.15-src.tar.tar
      apache-tomcat-5.5.23.tar.gz
      jdk-6u5-linux-i586.bin
说明:本服务器IP为192.168.2.117,以上所有包都安装在该服务器上
步骤
一、安装apache
httpd-2.2.6.tar.gz上传到/usr/local目录
[root@rh4-tomcat local]# tar -zxvf httpd-2.2.6.tar.gz
[root@rh4-tomcat local]# cd httpd-2.2.6
[root@rh4-tomcat httpd-2.2.6]#./configure --prefix=/usr/local/apache2 --enable-modules=most --enable-mods-shared=all --enable-so
[root@rh4-tomcat httpd-2.2.6]#make
[root@rh4-tomcat httpd-2.2.6]#make install
启动测试
[root@rh4-tomcat httpd-2.2.6]# cd /usr/local/apache2/bin/
[root@rh4-tomcat bin]# ./apachectl start
[root@rh4-tomcat bin]# ps -ef|grep httpd
root     10762     1  0 21:03 ?        00:00:00 /usr/local/apache2/bin/httpd -k start
nobody   10763 10762  0 21:03 ?        00:00:00 /usr/local/apache2/bin/httpd -k start
nobody   10764 10762  0 21:03 ?        00:00:00 /usr/local/apache2/bin/httpd -k start
nobody   10765 10762  0 21:03 ?        00:00:00 /usr/local/apache2/bin/httpd -k start
nobody   10766 10762  0 21:03 ?        00:00:00 /usr/local/apache2/bin/httpd -k start
nobody   10767 10762  0 21:03 ?        00:00:00 /usr/local/apache2/bin/httpd -k start
nobody   10770 10762  0 21:03 ?        00:00:00 /usr/local/apache2/bin/httpd -k start
使用
http://192.168.2.117
即可以访问到It works字样,则说明安装正确
二、安装jk-connect
该软件包已经提前上传到/usr/local/目录下
[root@rh4-tomcat local]# tar zxvf jakarta-tomcat-connectors-1.2.15-src.tar.tar
[root@rh4-tomcat local]# cd jakarta-tomcat-connectors-1.2.15-src
[root@rh4-tomcat jakarta-tomcat-connectors-1.2.15-src]# cd jk/native/
[root@rh4-tomcat native]# ./buildconf.sh
[root@rh4-tomcat native]# ./configure --with-apxs=/usr/local/apache2/bin/apxs
[root@rh4-tomcat native]# make
[root@rh4-tomcat native]# make install
[root@rh4-tomcat native]# cd apache-2.0/
[root@rh4-tomcat apache-2.0]# cp mod_jk.so /usr/local/apache2/modules/
在拷贝前可以先备份modules目录下本身就存在的mod_jk.so文件,改名为mod_jk.so-bak
三、修改httpd.conf文件
在102行的下面添加如下一行
LoadModule jk_module modules/mod_jk.so
如下:
...........
...........
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule jk_module modules/mod_jk.so
在最底部添加如下行
JkWorkersFile /usr/local/apache2/conf/workers.properties
JkMountFile /usr/local/apache2/conf/uriworkermap.properties
JkLogFile /usr/local/apache2/logs/mod_jk.log
Jkmount /servlet/* ajp13
Jkmount /*.jsp ajp13
JkLogLevel info
将115,116行内容进行修改
User daemon
Group daemon
改成
User nobody
Group nobody
四、创建并配置workers.properties和uriworkermap.properties两个文件
[root@rh4-tomcat conf]# pwd
/usr/local/apache2/conf
[root@rh4-tomcat conf]# more workers.properties
worker.list=tomcat1,tomcat2,loadbalancer
worker.tomcat1.port=8180
worker.tomcat1.host=192.168.2.117
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor=1

worker.tomcat2.port=8280
worker.tomcat2.host=192.168.2.117
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor=1

worker.loadbalancer.type=lb
worker.loadbalancer.balanced_workers=tomcat1,tomcat2
worker.loadbalancer.sticky_session=0
[root@rh4-tomcat conf]# more uriworkermap.properties
/*=tomcat1
!/*.jpg=tomcat1
!/*.gif=tomcat1
!/*.png=tomcat1
!/*.bmp=tomcat1
!/*.html=tomcat1
!/*.swf=tomcat1
!/*.css=tomcat1
!/*.js=tomcat1
#!/*.jsp=tomcat1
!/*.jsp=loadbalancer
五、安装JDK
该软件包提前已经上传到/tomcatsoft
[root@rh4-tomcat tomcatsoft]# chmod +x jdk-6u5-linux-i586.bin
[root@rh4-tomcat tomcatsoft]# ./jdk-6u5-linux-i586.bin
安装需要一小段过程,中途会需要选择一个YES
六、配置/etc/profile文件
在对下面添加如下内容
JAVA_HOME=/usr/local/jdk1.6.0_05
CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib:$JAVA_HOME/bin
export JAVA_HOME CLASSPATH

#tianyu-tomcat
CATALINA_BASE=/tomcatsoft/tomcat1
CATALINA_HOME=/tomcatsoft/tomcat1
TOMCAT_HOME=/tomcatsoft/tomcat1
PATH=$PATH:$TOMCAT_HOME/bin
export CATALINA_BASE CATALINA_HOME TOMCAT_HOME PATH


#jiajia-tomcat
CATALINA_2_BASE=/usr/tomcat2
#CATALINA_2_HOME=/usr/tomcat2
TOMCAT_2_HOME=/usr/tomcat2
PATH=$PATH:$TOMCAT_2_HOME/bin
export CATALINA_2_BASE TOMCAT_2_HOME PATH
[root@rh4-tomcat tomcatsoft]# source /etc/profile
查看版本说明已经安装成功
[root@rh4-tomcat tomcatsoft]# java -version
java version "1.6.0_05"
gcj (GCC) 3.4.6 20060404 (Red Hat 3.4.6-11)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
五、安装第一个tomcat
该软件包提前已经上传到/tomcatsoft目录下
[root@rh4-tomcat tomcatsoft]# tar -zxvf apache-tomcat-5.5.23.tar.gz
[root@rh4-tomcat tomcatsoft]# mv apache-tomcat-5.5.23 tomcat1
六、修改第一个tomcat的配置文件
[root@rh4-tomcat conf]# pwd
/tomcatsoft/tomcat1/conf
[root@rh4-tomcat conf]# vi server.xml    注意修改前将其先copy备份一下
13行
       ---默认端口为8005
77行

298行下添加一个mcastBindAddress
                 className="org.apache.catalina.cluster.mcast.McastService"
                  mcastBindAddress="192.168.2.117"
307行修改如下
               tcpListenAddress="192.168.2.117"  --默认为auto
                        
[root@rh4-tomcat bin]# more startup.sh
# resolve links - $0 may be a softlink
添加如下内容
export JAVA_HOME=/usr/local/jdk1.6.0_05
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=$JAVA_HOME/lib
#export CATALINA_HOME=$CATALINA_HOME
export CATALINA_BASE=$CATALINA_BASE
[root@rh4-tomcat bin]# more shutdown.sh
# resolve links - $0 may be a softlink
添加如下内容
export JAVA_HOME=/usr/local/jdk1.6.0_05
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=$JAVA_HOME/lib
#export CATALINA_HOME=$CATALINA_HOME
export CATALINA_BASE=$CATALINA_BASE
[root@rh4-tomcat bin]# ./startup.sh
Using CATALINA_BASE:   /tomcatsoft/tomcat1
Using CATALINA_HOME:   /tomcatsoft/tomcat1
Using CATALINA_TMPDIR: /tomcatsoft/tomcat1/temp
Using JRE_HOME:       /usr/local/jdk1.6.0_05
[root@rh4-tomcat bin]# ps -ef|grep tomcat
root     11112     1 30 22:25 pts/4    00:00:01 /usr/local/jdk1.6.0_05/bin/java -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file=/tomcatsoft/tomcat1/conf/logging.properties -Djava.endorsed.dirs=/tomcatsoft/tomcat1/common/endorsed -classpath :/tomcatsoft/tomcat1/bin/bootstrap.jar:/tomcatsoft/tomcat1/bin/commons-logging-api.jar -Dcatalina.base=/tomcatsoft/tomcat1 -Dcatalina.home=/tomcatsoft/tomcat1 -Djava.io.tmpdir=/tomcatsoft/tomcat1/temp org.apache.catalina.startup.Bootstrap start
root     11154 10950  0 22:25 pts/4    00:00:00 grep tomcat
[root@rh4-tomcat bin]# ./shutdown.sh
Using CATALINA_BASE:   /tomcatsoft/tomcat1
Using CATALINA_HOME:   /tomcatsoft/tomcat1
Using CATALINA_TMPDIR: /tomcatsoft/tomcat1/temp
Using JRE_HOME:       /usr/local/jdk1.6.0_05
[root@rh4-tomcat bin]# ps -ef|grep tomcat
root     11182 10950  0 22:28 pts/4    00:00:00 grep tomcat
七、安装第二个tomcat
该软件包已经提前上传到/usr/目录
[root@rh4-tomcat usr]# tar -zxvf apache-tomcat-5.5.23.tar.gz
[root@rh4-tomcat usr]# mv apache-tomcat-5.5.23 tomcat2
八、修改第二个tomcat的配置文件
[root@rh4-tomcat conf]# pwd
/tomcatsoft/tomcat1/conf
[root@rh4-tomcat conf]# vi server.xml    注意修改前将其先copy备份一下
13行
       ---默认端口为8005,可以不变,只要不和tomcat1冲突即可
77行

298行下添加一个mcastBindAddress
                 className="org.apache.catalina.cluster.mcast.McastService"
                  mcastBindAddress="192.168.2.117"
307,308行修改如下
               tcpListenAddress="192.168.2.117"  --默认为auto
               tcpListenPort="4002"              --默认为4001

[root@rh4-tomcat bin]# more startup.sh
# resolve links - $0 may be a softlink
添加如下内容
export JAVA_HOME=/usr/local/jdk1.6.0_05
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=$JAVA_HOME/lib
#export CATALINA_HOME=$CATALINA_2_HOME
export CATALINA_BASE=$CATALINA_2_BASE
[root@rh4-tomcat bin]# more shutdown.sh
# resolve links - $0 may be a softlink
添加如下内容
export JAVA_HOME=/usr/local/jdk1.6.0_05
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=$JAVA_HOME/lib
#export CATALINA_HOME=$CATALINA_2_HOME
export CATALINA_BASE=$CATALINA_2_BASE
[root@rh4-tomcat bin]# ./startup.sh
Using CATALINA_BASE:   /usr/tomcat2
Using CATALINA_HOME:   /tomcatsoft/tomcat1
Using CATALINA_TMPDIR: /usr/tomcat2/temp
Using JRE_HOME:       /usr/local/jdk1.6.0_05
[root@rh4-tomcat bin]# ps -ef|grep tomcat
root     11227     1 13 22:50 pts/4    00:00:01 /usr/local/jdk1.6.0_05/bin/java -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file=/tomcatsoft/tomcat1/conf/logging.properties -Djava.endorsed.dirs=/tomcatsoft/tomcat1/common/endorsed -classpath :/tomcatsoft/tomcat1/bin/bootstrap.jar:/tomcatsoft/tomcat1/bin/commons-logging-api.jar -Dcatalina.base=/tomcatsoft/tomcat1 -Dcatalina.home=/tomcatsoft/tomcat1 -Djava.io.tmpdir=/tomcatsoft/tomcat1/temp org.apache.catalina.startup.Bootstrap start
root     11278     1 30 22:50 pts/4    00:00:01 /usr/local/jdk1.6.0_05/bin/java -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file=/usr/tomcat2/conf/logging.properties -Djava.endorsed.dirs=/tomcatsoft/tomcat1/common/endorsed -classpath :/tomcatsoft/tomcat1/bin/bootstrap.jar:/tomcatsoft/tomcat1/bin/commons-logging-api.jar -Dcatalina.base=/usr/tomcat2 -Dcatalina.home=/tomcatsoft/tomcat1 -Djava.io.tmpdir=/usr/tomcat2/temp org.apache.catalina.startup.Bootstrap start
root     11320 10950  0 22:50 pts/4    00:00:00 grep tomcat
九、在tomcat1中创建index.jsp文件进行测试
[root@rh4-tomcat webapps]# pwd
/tomcatsoft/tomcat1/webapps
[root@rh4-tomcat webapps]# mkdir test
[root@rh4-tomcat test]# more index.jsp

  tomcat1 test
this is tomcat1
0)
{
  String dataValue=request.getParameter("dataValue");
  session.setAttribute(dataName,dataValue);
}
out.print("Session:");
Enumeration e=session.getAttributeNames();
while(e.hasMoreElements())
{
  String name=(String)e.nextElement();
  String value=session.getAttribute(name).toString();
  out.println(name+"="+value);
}
%>
name:
value:
[root@rh4-tomcat test]# mkdir WEB-INI/
[root@rh4-tomcat test]# cd WEB-INI/
[root@rh4-tomcat WEB-INI]# more web.xml

            TomcatDemo
            
     
十、在tomcat2中创建index.jsp文件进行测试
[root@rh4-tomcat webapps]# pwd
/usr/tomcat2/webapps
[root@rh4-tomcat webapps]# mkdir test
[root@rh4-tomcat test]# more index.jsp

  tomcat2 test
this is tomcat2
0)
{
  String dataValue=request.getParameter("dataValue");
  session.setAttribute(dataName,dataValue);
}
out.print("Session:");
Enumeration e=session.getAttributeNames();
while(e.hasMoreElements())
{
  String name=(String)e.nextElement();
  String value=session.getAttribute(name).toString();
  out.println(name+"="+value);
}
%>
name:
value:
[root@rh4-tomcat test]# mkdir WEB-INI/
[root@rh4-tomcat test]# cd WEB-INI/
[root@rh4-tomcat WEB-INI]# more web.xml

            TomcatDemo
            
     
十一、测试
见图1,当在name输入xxy,value输入123456,点提交查询内容后,马上会出现图2的情况
图1
图2

若此时再刷新一下,则访问到的又是tomcat1,再刷新,访问到的又回到tomcat2,依次类推,说明tomcat1和tomcat2之间已经实现负载均衡

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/93765/showart_2180535.html

论坛徽章:
0
2 [报告]
发表于 2011-10-17 14:43 |只看该作者
测试的jsp不如不做,没有半点作用。两个tomcat?工程放在哪里???
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP