Tomcat的负载均衡(apache的mod_jk来实现):
1、基于Apache的mod_jk来实现:
apache
/ \
tom1 tom2
在完成两台tomcat1,tomcat2的安装之后,希望可以实现负载均衡,使用mod_jk可以实现节点的故障恢复功能,即一个节点出现问题,mod_jk会其不参与负载,在节点故障恢复后,mod_jk会让其参与负载,对此节点是否失败,对用户而言是透明的。
实现流程:
a、用户访问数据达到Apache后,如果是静态内容,Apache立即回应;
b、如果Apache发现用户请求的是动态内容如jsp,就将请求丢给其中的一台tomcat来处理。
c、每次用户重复访问时,都被分配到同一台机器上(可以做会话粘滞或是会话复制)
本文环境在之前的基础上新增加一台机器(做负载均衡器),其实负责负载均衡的主要是由一个配置文件完成!那就是workers.propertise。通过定义worker.list,port,host,type,lbfactor的值来定义每一台tomcat的主机;通过lbfactor来区别每台机器的权重,权重数值越大得到的用户请求数就越多。所以,只要重点关注这个文件的内容就行了。
实施:
a、安装Aapche
# tar -xzf httpd-2.2.22.tar.gz
# chown root.root -R httpd-2.2.22
# cd httpd-2.2.22
# ./configure --prefix=/usr/local/apache
# make && make install
b、编译mod_jk模块
# tar -xzf tomcat-connectors-1.2.32-src.tar.gz
# cd tomcat-connectors-1.2.32-src
# cd native/
# ./configure --with-apxs=/usr/local/apache/bin/apxs
# make && make install
# ll /usr/local/apache/modules/mod_jk.so
-rwxr-xr-x 1 root root 1076923 Feb 20 14:17 /usr/local/apache/modules/mod_jk.so
c、加载mod_jk
# vim /usr/local/apache/conf/httpd.conf
#增加的内容
LoadModule jk_module modules/mod_jk.so
JkWorkersFile /usr/local/apache/conf/workers.properties
JkMountFile /usr/local/apache/conf/uriworkermap.properties
JkLogFile /usr/local/apache/logs/mod_jk.log
JkLogLevel info
JkLogStampformat "[%a %b %d %H:%M:%S %Y]"
#告诉apache,如何处理相关类形的文件(这几行可要可不要,只要urlworkermap.properties里面有定义也是可以的! :)
JkMount /* controller
JkMount /*.jsp controller
JkMount /*.action controller
# cat /usr/local/apache/conf/workers.properties
# cat workers.properties
worker.list=controller
#========tomcat1========
worker.tomcat1.port=8009
worker.tomcat1.host=192.168.10.56
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor = 1
worker.tomcat12.connection_pool_timeout=750
worker.tomcat12.socket_keepalive=0
worker.tomcat12.socket_timeout=3000
worker.tomcat12.connect_timeout=1000
worker.tomcat12.reply_timeout=3300
#========tomcat2========
worker.tomcat2.port=8009
worker.tomcat2.host=192.168.10.57
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor = 1
worker.tomcat12.connection_pool_timeout=750
worker.tomcat12.socket_keepalive=0
worker.tomcat12.socket_timeout=3000
worker.tomcat12.connect_timeout=1000
worker.tomcat12.reply_timeout=3300
#=======balance controller====
worker.controller.type=lb
worker.retries=3
worker.controller.balance_workers=tomcat1,tomcat2
worker.controller.sticky_session=1
相关参数说明: