Apache mod_jk整合Tomcat
Apache
mod_jk整合Tomcat
官方网站:
http://tomcat.apache.org
http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
环境:
CentOS 7.2
httpd-2.4.6
tomcat-connectors-1.2.41
tomcat-8.5.5
jdk-1.8
httpd: 192.168.8.254
tomcat1: 192.168.8.101
tomcat2: 192.168.8.102
一.apache2.4安装
二.tomcat8安装
三.tomcat-connectors安装
wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.41-src.tar.gz
tar -xvf
/mnt/samba/tomcat8/tomcat-connectors-1.2.41-src.tar.gz -C
/usr/local/src
cd /usr/local/src/tomcat-connectors-1.2.41-src/native
./configure --with-apxs=/opt/apache/httpd/bin/apxs
#apache使用光盘rpm包则apxs由httpd-devel提供
make -j4 && make install
安装完成后会在apche的modules目录生成一个模块mod_jk.so
ls /opt/apache/httpd/modules/mod_jk.so
四.配置
httpd中加入tomcat-connector的配置项
http://tomcat.apache.org/connectors-doc/reference/workers.html
retries参数等高级参数
http://tomcat.apache.org/connectors-doc/reference/workers.html#Advanced Worker Directives
retries参数等高级参数
http://tomcat.apache.org/connectors-doc/reference/workers.html#Advanced Worker Directives
1.httpd-jk.conf
/usr/local/src/tomcat-connectors-1.2.41-src/conf/{httpd-jk.conf,uriworkermap.properties,workers.properties}
可以直接用tomcat-connector源码里的模板配置文件,也可以直接根据需求自定义httpd-jk.conf(文件的位置依环境不同而不同,我这里是/opt/apache/httpd/conf/extra/httpd-jk.conf),但最主要的几行如下
JkWorkersFile conf/extra/workers.properties
JkMountFile conf/extra/uriworkermap.properties
JkMount /*.jsp balancer
该配置主要指定哪些内容下发给不同的应用代理,以balancer为例,只要是后缀为.jsp的就转发给后端的balancer应用代理
注意:不要忘记在主配置文件中将httpd-jk.conf Include进来
vim /opt/apache/httpd/conf/httpd.conf
Include conf/extra/httpd-jk.conf
2.workers.properties
测试环境可以在模板中修改,生产环境建议直接定义,更清爽也更方便管理。如,后端两台tomcat,
一台权重为1,一台权重为3,tomcat默认ajp端口为8009
cat >workers.properties <<EOF
worker.list=balancer
worker.balancer.type=lb
worker.balancer.balance_workers=node1,node2
worker.balancer.max_packet_size=65536
worker.node1.host=192.168.8.101
worker.node1.port=8009
worker.node1.activation=A
worker.node1.lbfactor=1
worker.node1.type=ajp13
worker.node1.connection_pool_size=256
worker.node1.connection_pool_minsize=128
worker.node1.connection_pool_timeout=300
worker.node1.socket_timeout=300
worker.node1.max_reply_timeouts=10
worker.node1.error_escalation_time=0
worker.node2.host=192.168.8.102
worker.node2.port=8009
worker.node2.activation=A
worker.node2.lbfactor=3
worker.node2.type=ajp13
worker.node2.connection_pool_size=256
worker.node2.connection_pool_minsize=128
worker.node2.connection_pool_timeout=300
worker.node2.socket_timeout=300
worker.node2.max_reply_timeouts=10
worker.node2.error_escalation_time=0
EOF3.重启httpd
五.测试
1.jsp
通过httpd(我这里httpd监听在8000)访问tomcat上的examples示例代码
192.168.8.254:8000/examples/
3.负载均衡
可以在两台tomcat的默认根目录下放入一个测试的.jsp, 内容不一样
在少量有限请求数的情况下,如上配置的tomcat1权重为1,tomcat2权重为3,在刷新页面时会看到tomcat2的内容会显示3次后再跳到tomcat1