安装环境简介
本次使用三台服务器,其中安装Apache作为负载均衡转发,另外两个tomcat模拟应用服务器。
(集群早在2013年就研究过,那时候在WinXP上面研究,参考文档如下:https://yunpan.cn/crcYiCPKrkJ82 访问密码 0e99)
Apache 192.168.1.113
tomcat:192.168.1.106/192.168.1.109
步骤一、安装Tomcat
tomcat安装在此省略,先安装JDK,配置JAVA_HOME环境变量,然后下载解压tomcat
步骤二、安装Apache
[root@node1 opt]# mkdir -p /usr/local/apache2/
[root@node1 opt]# tar -zxvf httpd-2.2.23.tar.gz
[root@node1 opt]# cd httpd-2.2.23
[root@node1 httpd-2.2.23]# ./configure --prefix=/usr/local/apache2/ --enable-module=most --enable-shared=max --enable-so
[root@node1 httpd-2.2.23]# make
modules/http/.libs/libmod_http.a(byterange_filter.o): In function `ap_set_byterange':
byterange_filter.c:(.text+0x12c1): undefined reference to `apr_array_clear'
collect2: ld returned 1 exit status
make[1]: *** [httpd] Error 1
make[1]: Leaving directory `/opt/httpd-2.2.23'
make: *** [all-recursive] Error 1
解决方案
[root@node1 httpd-2.2.23]# make clean
[root@node1 httpd-2.2.23]# ./configure --prefix=/usr/local/apache2 --enable-mods-shared=most --with-included-apr
[root@node1 httpd-2.2.23]# make
[root@node1 httpd-2.2.23]# make install
测试apache服务器
/usr/local/apache2/bin/apachectl –k start|stop|restart 分别为启动 停止 重启
步骤三、负载均衡配置
1、上传文件
/usr/local/apache2/modules/mod_jk-1.2.23-apache-2.2.x-linux-x86_64.so
2、配置负载均衡文件
/usr/local/apache2/conf/workers.properties
worker.list = controller,tomcat1,tomcat2 #server 列表
#========tomcat1========
worker.tomcat1.port=8009 #ajp13 端口号,在tomcat下server.xml配置,默认8009
worker.tomcat1.host=192.168.1.106 #tomcat的主机地址,如不为本机,请填写ip地址
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor = 1 #server的加权比重,值越高,分得的请求越多
#========tomcat2========
worker.tomcat2.port=8009 #ajp13 端口号,在tomcat下server.xml配置,默认8009
worker.tomcat2.host=192.168.1.109 #tomcat的主机地址,如不为本机,请填写ip地址
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor = 1 #server的加权比重,值越高,分得的请求越多
#========controller,负载均衡控制器========
worker.controller.type=lb
worker.controller.balanced_workers=tomcat1,tomcat2 #指定分担请求的tomcat
worker.controller.sticky_session=1
注意:work端口对应Tomcat的server.xml文件标红色部分端口
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
3、新建文件 mod_jk.conf 添加相关内容
LoadModule jk_module modules/mod_jk-1.2.23-apache-2.2.x-linux-x86_64.so
JkWorkersFile conf/workers.properties
JkMount /* controller
把新建的mod_jk.conf文件添加到/usr/local/apache2/conf/目录下面
4、修改/usr/local/apache2/conf/httpd.conf文件
在文件末尾加上include "/usr/local/apache2/conf/mod_jk.conf"
即如下所示:
include "/usr/local/apache2/conf/mod_jk.conf"
"/usr/local/apache2/conf/httpd.conf" 465L, 15985C written
步骤四、测试负载均衡
http://192.168.1.113/
另一实现负载均衡方式参考:nginx+tomcat实现负载均衡
永久链接: http://gaojingsong.iteye.com/blog/2267299
[root@node1 logs]# tail -f mod_jk.log
[Tue Jan 19 14:29:39 2016] [24407:52512] [info] ajp_service::jk_ajp_common.c (1902): (tomcat2) receiving from tomcat failed, recoverable operation attempt=0
[Tue Jan 19 14:29:39 2016] [24407:52512] [info] ajp_service::jk_ajp_common.c (1941): (tomcat2) sending request to tomcat failed, recoverable operation attempt=1
[Tue Jan 19 14:48:14 2016] [24762:52512] [info] ajp_connect_to_endpoint::jk_ajp_common.c (876): Failed opening socket to (192.168.1.109:8009) (errno=111)
[Tue Jan 19 14:49:03 2016] [24765:52512] [info] ajp_connect_to_endpoint::jk_ajp_common.c (876): Failed opening socket to (192.168.1.106:8009) (errno=111)
[Tue Jan 19 14:49:11 2016] [24766:52512] [info] ajp_send_request::jk_ajp_common.c (1244): (tomcat2) increase the backend idle connection timeout or the connection_pool_minsize
[Tue Jan 19 14:49:11 2016] [24766:52512] [info] ajp_service::jk_ajp_common.c (1941): (tomcat2) sending request to tomcat failed, recoverable operation at