tomcat 7 用mod_jk做 负载均衡

时间:2021-07-09 14:20:40

在Win7中使用apache为tomcat做负载均衡,各组件及版本如下:

两个tomcat v 7.0.57

一个apache v 2.2.14

一个mod_jk v 1.2.33(for windows i386 httpd 2.2.x),下载地址:http://archive.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/windows/tomcat-connectors-1.2.33-windows-i386-httpd-2.2.x.zip

配置方法:

1.安装apache(略)

 在安装目录(conf所在目录)再增加一个文件夹:conf.d, 在此文件夹下面,编辑如下文件:

 mod_jk.conf

# Load mod_jk module
LoadModule jk_module modules/mod_jk.so
# Where to find workers.properties
JkWorkersFile conf.d/workers.properties
# Where to put jk logs
JkLogFile logs/mod_jk.log
# Set the jk log level [debug/error/info]
JkLogLevel info
# 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 servlet for context /examples to worker named ajp13
#JkMount /servlet/* controller
# Send JSPs for context /examples to worker named ajp13
JkMount /* controller

这个配置JKMoun /* controller表示将apache的所有请求都转向tomcat。再编辑一个文件如下:

workers.properties

#
# workers.properties
#

# list the workers by name
worker.list=controller
worker.controller.type=lb
worker.controller.sticky_session=1
worker.controller.error_escalation_time=0
worker.controller.max_reply_timeouts=10

# localhost server 1
# ------------------------
worker.jvm1.reference=worker.template
worker.jvm1.port=8019 #对应tomcat ajp 端口
worker.jvm1.host=localhost
worker.jvm1.lbfactor = 1 #权重
worker.jvm1.activation=A

# localhost server 2
# ------------------------
worker.jvm2.reference=worker.template
worker.jvm2.port=8029
worker.jvm2.host=localhost
worker.jvm2.lbfactor = 1
worker.jvm2.activation=A

worker.template.type=ajp13
worker.template.socket_connect_timeout=5000
worker.template.socket_keepalive=true
worker.template.ping_mode=A
worker.template.ping_timeout=10000
worker.template.connection_pool_minsize=0
worker.template.connection_pool_timeout=600
worker.template.reply_timeout=300000
worker.template.recovery_options=3

worker.controller.balance_workers=jvm1, jvm2
#对应两个tomcat jvmRoute

然后,编辑conf/httpd.conf,在最后加入如下一行:

Include conf.d/mod_jk.conf

将上面下载的mod_jk.so,复制到apache的安装目录modules下面。

2.配置tomcat

  修改第一个tomcat的server.xml,如下所示:

...
<Server port="8015" shutdown="SHUTDOWN">
...
<Connector port="8091" protocol="HTTP/1.1"
connectionTimeout
="20000"
redirectPort
="8443" />
....
<Connector port="8019" protocol="AJP/1.3" redirectPort="8443" />
...
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
...
<!---->
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
...

  同理修改第二个tomcat的配置:

...
<Server port="8025" shutdown="SHUTDOWN">
...
<Connector port="8092" protocol="HTTP/1.1"
connectionTimeout
="20000"
redirectPort
="8443" />
....
<Connector port="8029" protocol="AJP/1.3" redirectPort="8443" />
...
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm2">
...
<!---->
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
...

最后两个tomcat的web.xml,都加入一行<distributable/>到如下位置:

...
<distributable/>

</web-app>

启动apache,和两个tomcat,即可按apache的监听端口进行访问,tomcat的监听端口忽略不用,如:

http://localhost 或 http://127.0.0.1