Mod_JK是Apache的一个模块,其通过AJP协议实现与Tomcat之间的通讯,Tomcat通过设置Connector监听AJP的连接请求。
安装Mod_JK:
从http://tomcat.apache.org/download-connectors.cgi下载源码:tomcat-connectors-1.2.40-src.tar.gz
tar zxvf tomcat-connectors-1.2.40-src.tar.gz
cd tomcat-connectors-1.2.40-src/
cd native/
./configure --with-apxs=/home/soft/httpd-2.4.10/bin/apxs --prefix=/home/soft/tomcat-connectors-1.2.40
make
make install
可选
make clean
make distclean
注:安装结果貌似不会装到/tomcat-connectors-1.2.40,但会往本地apache httpd 写入文件,比如添加了:modules/mod_jk.so
负载均衡配置步骤:
1.在apache安装目录下conf目录中找到httpd.conf,在最后加上配置 Include conf/mod_jk.conf
2.将mod_jk.so文件放入Apache\modules文件夹
3.新建mod_jk.conf文件并配置
4.新建workers.properties和uriworkermap.properties文件并配置
5.修改各个tomcat配置文件server.xml,主要对应端口和设置engine的jvmRoute属性
mod_jk.conf 内容:
# 加载 mod_jk 模块
LoadModule jk_module modules/mod_jk.so
# workers.properties 位置
JkWorkersFile conf/mine_jk/workers.properties
#加载workers的请求处理分配文件
JkMountFile conf/mine_jk/uriworkermap.properties
# 日志存放位置
JkLogFile logs/mine_mod_jk.log
# 设置日志级别 [debug/error/info]
JkLogLevel info
# Select the timestamp log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
workers.properties 内容:
# 定义worker,此处的list名称必须和mod_jk.conf文件中的 dispatcher 名称一致
worker.list=dispatcher,status
# Define Node1
worker.tomcat1.port=8019
worker.tomcat1.host=localhost
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor=1
#worker.tomcat1.connection_pool_size=10
#worker.tomcat1.connection_pool_timeout=600
#worker.tomcat1.socket_connect_timeout=300
# Define Node2
worker.tomcat2.port=8029
worker.tomcat2.host=localhost
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor=1
#worker.tomcat2.connection_pool_size=10
#worker.tomcat2.connection_pool_timeout=600
#worker.tomcat2.socket_connect_timeout=300
# Load-balancing behaviour
worker.dispatcher.type=lb
worker.dispatcher.balance_workers=tomcat1, tomcat2
#sticky_session session亲和的设置
worker.dispatcher.sticky_session=0
worker.dispatcher.sticky_session_force=1
#loadbalancer
worker.status.type=status
uriworkermap.properties 内容:
#指定请求交给tomcat处理,"dispatcher"为在workers.propertise里指定的负载分配控制器
/*.jsp=dispatcher
/jkstatus=status
!/*.gif=dispatcher
!/*.jpg=dispatcher
!/*.png=dispatcher
!/*.css=dispatcher
!/*.js=dispatcher
!/*.htm=dispatcher
!/*.html=dispatcher
Tomcat设置:
准备两个tomcat,将它们的router分别设成tomcat1和tomcat2,配置它们的connector为ajp,端口分别为8019和8029。
配置完成后,启动apache和tomcat,访问 http://localhost:8080/jkstatus 可以查看集群状态(前提是配置了status类型的worker)
有可以看看其他人写的,很详细:http://aokunsang.iteye.com/blog/1699914