文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>apache+tomcat整合,实现负载均衡

apache+tomcat整合,实现负载均衡

时间:2007-03-27  来源:bleach

一、安装apache
a、下载最新源文件httpd-2.2.3.tar.gz。
wget http://apache.mirrors.hoobly.com/httpd/httpd-2.2.3.tar.gz
b、编译 apache
gunzip httpd-2.2.3.tar.gz
tar xvf httpd-2.2.3.tar
cd httpd-2.2.3
./configure --prefix=/home/apache --enable-so --with-mpm=worker
make
make install
c、简单配置:编辑httpd.conf,
把#ServerName www.example.com:80项中ServerName前的#号去掉,把xxxxx改成自己的机器全名,212中修改如下
#ServerName www.example.com:80改成ServerName 127.0.0.1
d、启动Apache: #cd /home/apache/bin
     #./apachectl start
e、测试Apache是否已经正确运行:http://202.202.202.15
二、安装tomcat
把主体资源的tomcat拷为/home/tomcat/tomcat11目录
cd /home/tomcat
tar cvf ../tool/tomcat.tar tomcat5 --exclude tomcat5/webapps --exclude tomcat5/work
cd /home/tool
tar xvf tomcat.tar
mv tomcat5 ../tomcat/tomcat11
修改tomcat的文件目录,将/home/tomcat/tomcat11/conf/server.xml
appBase="webapps"修改为
appBase="/home/tomcat/tomcat5/webapps"

修改tomcat的端口
tomcat1/conf/server.xml
<Server debug="0" port="8005" shutdown="SHUTDOWN">改为
<Server debug="0" port="11001" shutdown="SHUTDOWN">
<Connector URIEncoding="GBK" acceptCount="100" connectionTimeout="20000" debug="0" disableUploadTimeout="true" enableLookups="false" maxSpareThreads="75" maxThreads="150" minSpareThreads="25" port="8082" redirectPort="8443"/>改为
<Connector URIEncoding="GBK" acceptCount="100" connectionTimeout="20000" debug="0" disableUploadTimeout="true" enableLookups="false" maxSpareThreads="75" maxThreads="150" minSpareThreads="25" port="11002" redirectPort="8443"/>
<Connector debug="0" enableLookups="false" port="8009" protocol="AJP/1.3" redirectPort="8443"/>改为
<Connector debug="0" enableLookups="false" port="11003" protocol="AJP/1.3" redirectPort="8443"/>

cp -fR /home/tomcat/tomcat11 /home/tomcat/tomcat12
cp -fR /home/tomcat/tomcat11 /home/tomcat/tomcat13
cp -fR /home/tomcat/tomcat11 /home/tomcat/tomcat14
将相关端口修改为12001、12002和12003,13001、13002和13003,14001、14002和14003;将标识修改为tomcat12,tomcat13,tomcat14 f、启动tomcat:
#cd /home/tomcat/tomcat11/bin
#./startup.sh
#cd /home/tomcat/tomcat12/bin
#./startup.sh
#cd /home/tomcat/tomcat13/bin
#./startup.sh
#cd /home/tomcat/tomcat14/bin
#./startup.sh
g、测试tomcat是否已经正确运行:
http://202.202.202.15:11002
http://202.202.202.15:12002
http://202.202.202.15:13002
http://202.202.202.15:14002

三、apache + 2tomcat的负载均衡
a、下载tomcat-connectors的源代码,编译,生成/home/apache/modules/mod_jk.so文件
wget http://mirrors.ccs.neu.edu/Apache/dist/tomcat/tomcat-connectors/jk/source/jk-1.2.19/tomcat-connectors-1.2.19-src.tar.gz
gzip -d tomcat-connectors-1.2.19-src.tar.gz
tar xvf tomcat-connectors-1.2.19-src.tar
cd tomcat-connectors-1.2.19-src
cd native
./configure --with-apxs=/home/apache/bin/apxs
make
make install
b、修改httpd.conf配置
编辑Apache的配置文件Apache安装目录的conf/httpd.conf
把DocumentRoot "/mnt/sdd1/yanfa/vcmresource/apache/htdocs"改成
DocumentRoot "/home/tomcat/tomcat5/webapps/ROOT"
把<Directory "/mnt/sdd1/yanfa/vcmresource/apache/htdocs">改成
<Directory "/home/tomcat/tomcat5/webapps/ROOT">

<IfModule dir_module>
    DirectoryIndex index.html
</IfModule>
改为
<IfModule dir_module>
    DirectoryIndex index.jsp index.html
</IfModule>
取消以下注释
#EnableMMAP off
#EnableSendfile off
改为
EnableMMAP off
EnableSendfile off
#Include conf/extra/httpd-mpm.conf
改为
Include conf/extra/httpd-mpm.conf
#Include conf/extra/httpd-default.conf
改为
Include conf/extra/httpd-default.conf
加入以下内容
# Jk connectors
Include conf/mod_jk.conf

修改conf/extra/httpd-mpm.conf配置
# worker MPM
<IfModule mpm_worker_module>
    StartServers          2
    MaxClients          150
    MinSpareThreads      25
    MaxSpareThreads      75
    ThreadsPerChild      25
    MaxRequestsPerChild   0
</IfModule>
改为
# worker MPM
<IfModule mpm_worker_module>
    StartServers          5
    ServerLimit   60
    MaxClients         3000
    MinSpareThreads      25
    MaxSpareThreads      75
    ThreadsPerChild      50
    MaxRequestsPerChild   0
</IfModule>

d、在conf目录下创建mod_jk.conf文件,内容如下:
--------------------------------------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 warn

# 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 JSPs for context / to worker named loadBalancer
#JkMount /*.jsp loadBalancer
#JkMount /*.do* loadBalancer
JkMountFile conf/urimap.properties
--------------------------------------------mod_jk.conf------------------------------------
在conf目录下创建urimap.properties文件,内容如下:
/*=loadBalancer
!/*.gif=loadBalancer
!/*.jpg=loadBalancer
!/*.png=loadBalancer
!/*.css=loadBalancer
!/*.js=loadBalancer
!/*.htm=loadBalancer
!/*.html=loadBalancer
!/*.mid=loadBalancer
!/*.jpeg=loadBalancer
!/*.txt=loadBalancer
!/*.mp3=loadBalancer
!/*.swf=loadBalancer
!/*.wmv=loadBalancer
!/*.zip=loadBalancer
!/*.rar=loadBalancer
!/*.pl=loadBalancer

e、在 conf目录下创建workers.properties文件,该文件将告诉apache 每个负载均衡的tomcat
服务的运行参数,如机器名和端口号等。
-----------------------------------------workers.properties-------------------------------
#
# workers.properties
#
# list the workers by name worker.list=loadBalancer # ------------------------
# First tomcat server
# ------------------------
worker.tomcat11.port=11003
worker.tomcat11.host=202.202.202.15
worker.tomcat11.type=ajp13
# Specify the size of the open connection cache.
# worker.tomcat11.cachesize=1000
#
# Specifies the load balance factor when used with
# a load balancing worker.
# Note:
# ----> lbfactor must be > 0
# ----> Low lbfactor means less work done by the worker.
worker.tomcat11.lbfactor=10

# ------------------------
# Second tomcat server
# ------------------------
worker.tomcat12.port=12003
worker.tomcat12.host=202.202.202.15
worker.tomcat12.type=ajp13
# Specify the size of the open connection cache.
# worker.tomcat12.cachesize=1000
#
# Specifies the load balance factor when used with
# a load balancing worker.
# Note:
# ----> lbfactor must be > 0
# ----> Low lbfactor means less work done by the worker.
worker.tomcat12.lbfactor=10

# ------------------------
# Third tomcat server
# ------------------------
worker.tomcat13.port=13003
worker.tomcat13.host=202.202.202.15
worker.tomcat13.type=ajp13
# Specify the size of the open connection cache.
# worker.tomcat13.cachesize=1000
#
# Specifies the load balance factor when used with
# a load balancing worker.
# Note:
# ----> lbfactor must be > 0
# ----> Low lbfactor means less work done by the worker.
worker.tomcat13.lbfactor=10

# ------------------------
# Fourth tomcat server
# ------------------------
worker.tomcat14.port=14003
worker.tomcat14.host=202.202.202.15
worker.tomcat14.type=ajp13
# Specify the size of the open connection cache.
# worker.tomcat14.cachesize=1000
#
# Specifies the load balance factor when used with
# a load balancing worker.
# Note:
# ----> lbfactor must be > 0
# ----> Low lbfactor means less work done by the worker.
worker.tomcat14.lbfactor=10

# ------------------------
# Load Balancer worker
# ------------------------
#
# The loadbalancer (type lb) worker performs weighted round-robin
# load balancing with sticky sessions.
# Note:
# ----> If a worker dies, the load balancer will check its state
# once in a while. Until then all work is redirected to peer
# worker.
worker.loadBalancer.type=lb
worker.loadBalancer.balance_workers=tomcat11,tomcat12,tomcat13,tomcat14
worker.loadBalancer.method=B
#
# END workers.properties

f、配置Tomcat 服务器
修改conf/server.xml文件:
在tomcat1目录的conf/server.xml,在第128行, 替换:
<Engine debug="0" defaultHost="localhost" name="Catalina">为:
<Engine debug="0" defaultHost="localhost" name="Catalina" jvmRoute="tomcat11">
在tomcat2目录的conf/server.xml,替换为 jvmRoute="tomcat2".
<Engine debug="0" defaultHost="localhost" name="Catalina">为:
<Engine debug="0" defaultHost="localhost" name="Catalina" jvmRoute="tomcat12">
在tomcat3目录的conf/server.xml,替换为 jvmRoute="tomcat3".
<Engine debug="0" defaultHost="localhost" name="Catalina">为:
<Engine debug="0" defaultHost="localhost" name="Catalina" jvmRoute="tomcat13">
在tomcat4目录的conf/server.xml,替换为 jvmRoute="tomcat4".
<Engine debug="0" defaultHost="localhost" name="Catalina">为:
<Engine debug="0" defaultHost="localhost" name="Catalina" jvmRoute="tomcat14">
注意,一定是替换这一行,而包含在原来的engine里面的host不做更改。有的文档写的是在配置中加入一行
<Engine jvmRoute="tomcat1" name="Catalina" defaultHost="localhost" debug="0"><Engine>,其实是不对的,这样负载均衡是没有问题的,但是不能做session绑定了,也就是说同一用户会在两台服务器上跳来跳去。这是由于jvmRoute不正确引起的,所以一定要替换。而且这个jvmRoute一定要跟workers.properties中的名称完全匹配,否则也不能实现session绑定.

g、配置session亲和、监测各个tomcat的状态
vi /home/apache/conf/workers.properties
加入
worker.loadBalancer.sticky_session=1
修改
worker.list=loadBalancer为
worker.list=loadBalancer,status
加入
worker.status.type=status
vi /home/apache/conf/mod_jk.conf
加入
JkMount /resource-tomcat-status status
重启apache,可在http://202.202.202.15/jkstatus监测各个tomcat的情况
(status实际上mod_jk自带的一个应用,目的是观察每个tomcat的状态,并可以在运行时设置每个tomcat的某些参数)
如需用户/密码验证才能监测状态,配置如下
/home/apache/bin/htpasswd -c /home/apache/conf/jk_passwd vcmedu
vi /home/apache/conf/mod_jk.conf
去掉以下项
JkMount /jkstatus status
加如下面内容
<Location /resource-tomcat-status>
 JkMount status
 Order deny,allow
 AuthType Basic
 AuthName "jkstatus"
 AuthUserFile conf/jk_passwd
 require valid-user
</Location>
         
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载