码迷,mamicode.com
首页 > Web开发 > 详细

apache+tomcat 实现负载均衡集群

时间:2016-07-19 11:35:18      阅读:322      评论:0      收藏:0      [点我收藏+]

标签:代理服务器   local   

环境介绍:

192.168.101.58apache代理服务器apache,apr,apr-util,tomcat-connector
192.168.101.62tomcat-A服务器

tomcat,jdk

192.168.101.63tomcat-B服务器tomcat多实例,ajp_port:8019
192.168.101.63tomcat-默认页面服务器tomcat多实例,ajp_port:8029


apache支持两种代理协议

    1.http

    2.ajp


tomcat及jdk的安装略过。

编译安装apache的httpd

1、编译安装apr

[root@localhost src]# wget 
[root@localhost src]# tar -zxf apr-1.5.2.tar.gz
[root@localhost src]# cd apr-1.5.2
[root@localhost src]# ./configure --prefix=/usr/local/apr
[root@localhost src]# make && make install

2、编译安装apr-util

[root@localhost src]# wget http://apache.fayea.com/apr/apr-util-1.5.4.tar.gz
[root@localhost src]# tar -zxvf apr-util-1.5.4.tar.gz 
[root@localhost src]# cd apr-util-1.5.4
[root@localhost src]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/
[root@localhost src]# make && make install

3、编译安装apache

3.1、安装

[root@localhost src]# wget 
[root@localhost src]# tar -zxvf httpd-2.4.20.tar.gz
[root@localhost src]# cd httpd-2.4.20
[root@localhost src]#  ./configure --prefix=/usr/local/http --sysconfdir=/etc/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-mpms-shared=all --with-mpm=event --enable-proxy --enable-proxy-http --enable-proxu-ajp --enable-proxy-balancer --enable-lbmethod-heartbeat --enable-heartbeat  --enable-slotmem-shm --enable-slotmem-plain --enable-watchdog
[root@localhost src]# make && make install

3.2、编写启动脚本

[root@localhost src]# vim /etc/init.d/httpd
#!/bin/bash
#
# httpd        Startup script for the Apache HTTP Server
#
# chkconfig: - 85 15
# description: Apache is a World Wide Web server.  It is used to serve #	       HTML files and CGI.
# processname: httpd
# config: /etc/httpd/conf/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /var/run/httpd.pid

# Source function library.
. /etc/rc.d/init.d/functions

if [ -f /etc/sysconfig/httpd ]; then
        . /etc/sysconfig/httpd
fi

# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-"C"}

# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""

# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
# with the thread-based "worker" MPM; BE WARNED that some modules may not
# work correctly with a thread-based MPM; notably PHP will refuse to start.

# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/local/http/bin/apachectl
httpd=${HTTPD-/usr/local/http/bin/httpd}
prog=httpd
pidfile=${PIDFILE-/var/run/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0

start() {
        echo -n $"Starting $prog: "
        LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch ${lockfile}
        return $RETVAL
}

stop() {
	echo -n $"Stopping $prog: "
	killproc -p ${pidfile} -d 10 $httpd
	RETVAL=$?
	echo
	[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
    echo -n $"Reloading $prog: "
    if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
        RETVAL=$?
        echo $"not reloading due to configuration syntax error"
        failure $"not reloading $httpd due to configuration syntax error"
    else
        killproc -p ${pidfile} $httpd -HUP
        RETVAL=$?
    fi
    echo
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  status)
        status -p ${pidfile} $httpd
	RETVAL=$?
	;;
  restart)
	stop
	start
	;;
  condrestart)
	if [ -f ${pidfile} ] ; then
		stop
		start
	fi
	;;
  reload)
        reload
	;;
  graceful|help|configtest|fullstatus)
	$apachectl $@
	RETVAL=$?
	;;
  *)
	echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"
	exit 1
esac

exit $RETVAL

3.3、启动脚本赋予权限

[root@localhost src]# chmod +x /etc/init/d/httpd
[root@localhost src]# chkconfig --add httpd

3.4、启动apache(故障排查)

    3.4.1、查看httpd进程

[root@localhost src]# service httpd start
[root@localhost src]# ps -ef |grep httpd
root     14445 14189  0 15:21 pts/0    00:00:00 grep httpd

    3.4.2、查看端口状态

[root@localhost src]# netstat -tlnp |grep 80
tcp        0      0 :::80            :::*          LISTEN      14300/httpd[root@localhost src]#

    3.4.3、查看错误日志

[root@localhost src]# cat /usr/local/http/logs/error_log
[Mon Jul 18 11:49:53.889462 2016] [proxy_balancer:emerg] [pid 14087:tid 140187546703616] AH01177: Failed to lookup provider ‘shm‘ for ‘slotmem‘: is mod_slotmem_shm loaded??
[Mon Jul 18 11:49:53.889750 2016] [:emerg] [pid 14087:tid 140187546703616] AH00020: Configuration Failed, exiting
[Mon Jul 18 11:50:01.041040 2016] [proxy_balancer:emerg] [pid 14127:tid 140379317155584] AH01177: Failed to lookup provider ‘shm‘ for ‘slotmem‘: is mod_slotmem_shm loaded??
[Mon Jul 18 11:50:01.041275 2016] [:emerg] [pid 14127:tid 140379317155584] AH00020: Configuration Failed, exiting
[Mon Jul 18 11:50:02.193534 2016] [proxy_balancer:emerg] [pid 14139:tid 140297643288320] AH01177: Failed to lookup provider ‘shm‘ for ‘slotmem‘: is mod_slotmem_shm loaded??

好像是slotmem模块没有加载,编辑httpd配置文件启用slotmem模块

[root@localhost src]# vim /etc/httpd/httpd.conf
LoadModule slotmem_shm_module modules/mod_slotmem_shm.so
LoadModule slotmem_plain_module modules/mod_slotmem_plain.so


3.4.4、重新启动测试

[root@localhost src]# service httpd start
[root@localhost src]# ps -ef |grep httpd
root     14300     1  0 12:52 ?        00:00:00 /usr/local/http/bin/httpd
daemon   14302 14300  0 12:52 ?        00:00:01 /usr/local/http/bin/httpd
daemon   14303 14300  0 12:52 ?        00:00:01 /usr/local/http/bin/httpd
daemon   14304 14300  0 12:52 ?        00:00:01 /usr/local/http/bin/httpd
root     14443 14428  0 15:17 pts/1    00:00:00 vim /etc/init.d/httpd
root     14451 14189  0 15:28 pts/0    00:00:00 grep httpd

[root@localhost src]# netstat -tlnp |grep 80
tcp        0      0 :::80          :::*            LISTEN      14300/httpd


4、编译apache的jk模块

[root@localhost src]# wget 
[root@localhost src]# tar -zxvf tomcat-connectors-1.2.41-src.tar.gz 
[root@localhost src]# cd tomcat-connectors-1.2.41-src/native/
[root@localhost native]# ./configure --with-apxs=/usr/local/http/bin/apxs
[root@localhost native]# make && make install


5、配置(在httpd.conf中virtual host中添加jk的配置文件)

[root@localhost native]# vim /etc/httpd/httpd.conf
include /etc/httpd/extra/httpd-jk.conf

5.1编辑httpd-jk.conf

[root@localhost native]# vim /etc/httpd/extra/httpd-jk.conf
LoadModule jk_module modules/mod_jk.so
JkWorkersFile /etc/httpd/extra/workers.properties
JkLogFile logs/mod_jk.log
JkLogLevel error
JkMount /* controller

说明:

    LoadModule:表示装载模块

    JkWorkersFile:定义workers属性的配置文件

    JkLogFile:       定义work日志的路径

    JkMount:        将/下的所有请求都交给controller集群处理,controller必须要在workers.properties中定义,否则报错    

5.2、编辑workers.properties配置文件,增加如下内容

worker.list=controller                #定义控制器

#========tomcat1========
worker.tomcat1.port=8009               #tomcat的ajp端口
worker.tomcat1.host=192.168.101.62     #tomcat的ip
worker.tomcat1.type=ajp13              #ajp协议版本,目前1.3
worker.tomcat1.lbfactor=1              #权重

#========tomcat2========
worker.tomcat2.port=8019
worker.tomcat2.host=192.168.101.63
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor=1

#========tomcat3========
worker.tomcat3.port=8029
worker.tomcat3.host=192.168.101.63
worker.tomcat3.type=ajp13
worker.tomcat3.lbfactor=1

#========controller负载平衡控制器========
worker.controller.type=lb                          #指定controller类型
worker.controller.balanced_workers=tomcat1,tomcat2,tomcat3 #指定负载平衡的tomcat
worker.controller.sticky_session=true              #指定是否粘性session
worker.controller.sticky_session_force=false
worker.connection_pool_size=3000
worker.connection_pool_minsize=50
worker.connection_pool_timeout=50000
# session配置说明:  
#当sticky_session,sticky_session_force都为true时不复制session,  
#sticky_session_force=false指集群中某台服务器多次请求没有响应,则转发到其它服务器处理,  
#sticky_session=false不使用粘性session,同时配置不复制session时,注意转发请求后可能会找不到原来的session.


到此,配置完成。下面访问http://192.168.101.58测试。


技术分享

技术分享

技术分享



本博客参照了:http://cuisuqiang.iteye.com/blog/2070526  帖子,望大神勿怪。

本文出自 “初心、始终” 博客,请务必保留此出处http://gouyc.blog.51cto.com/1594451/1827470

apache+tomcat 实现负载均衡集群

标签:代理服务器   local   

原文地址:http://gouyc.blog.51cto.com/1594451/1827470

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!