wsaer 发表于 2018-11-19 12:45:58

apache+tomcat 实现负载均衡集群

  环境介绍:
192.168.101.58apache代理服务器apache,apr,apr-util,tomcat-connector192.168.101.62tomcat-A服务器  tomcat,jdk

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

  apache支持两种代理协议
  1.http

  2.ajp

  

  tomcat及jdk的安装略过。
  编译安装apache的httpd
  1、编译安装apr
# wget http://apache.fayea.com/apr/apr-1.5.2.tar.gz
# tar -zxf apr-1.5.2.tar.gz
# cd apr-1.5.2
# ./configure --prefix=/usr/local/apr
# make && make install  2、编译安装apr-util
# wget http://apache.fayea.com/apr/apr-util-1.5.4.tar.gz
# tar -zxvf apr-util-1.5.4.tar.gz
# cd apr-util-1.5.4
# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/
# make && make install  3、编译安装apache
  3.1、安装
# wget
# tar -zxvf httpd-2.4.20.tar.gz
# cd httpd-2.4.20
#./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
# make && make install  3.2、编写启动脚本
# 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、启动脚本赋予权限
# chmod +x /etc/init/d/httpd
# chkconfig --add httpd  3.4、启动apache(故障排查)
  3.4.1、查看httpd进程

# service httpd start
# ps -ef |grep httpd
root   14445 141890 15:21 pts/0    00:00:00 grep httpd  3.4.2、查看端口状态

# netstat -tlnp |grep 80
tcp      0      0 :::80            :::*          LISTEN      14300/httpd#  3.4.3、查看错误日志

# cat /usr/local/http/logs/error_log
AH01177: Failed to lookup provider 'shm' for 'slotmem': is mod_slotmem_shm loaded??
[:emerg] AH00020: Configuration Failed, exiting
AH01177: Failed to lookup provider 'shm' for 'slotmem': is mod_slotmem_shm loaded??
[:emerg] AH00020: Configuration Failed, exiting
AH01177: Failed to lookup provider 'shm' for 'slotmem': is mod_slotmem_shm loaded??  好像是slotmem模块没有加载,编辑httpd配置文件启用slotmem模块
# 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、重新启动测试
# service httpd start
# ps -ef |grep httpd
root   14300   10 12:52 ?      00:00:00 /usr/local/http/bin/httpd
daemon   14302 143000 12:52 ?      00:00:01 /usr/local/http/bin/httpd
daemon   14303 143000 12:52 ?      00:00:01 /usr/local/http/bin/httpd
daemon   14304 143000 12:52 ?      00:00:01 /usr/local/http/bin/httpd
root   14443 144280 15:17 pts/1    00:00:00 vim /etc/init.d/httpd
root   14451 141890 15:28 pts/0    00:00:00 grep httpd
# netstat -tlnp |grep 80
tcp      0      0 :::80          :::*            LISTEN      14300/httpd  

  4、编译apache的jk模块
# wget
# tar -zxvf tomcat-connectors-1.2.41-src.tar.gz
# cd tomcat-connectors-1.2.41-src/native/
# ./configure --with-apxs=/usr/local/http/bin/apxs
# make && make install  

  5、配置(在httpd.conf中virtual host中添加jk的配置文件)
# vim /etc/httpd/httpd.conf
include /etc/httpd/extra/httpd-jk.conf  5.1编辑httpd-jk.conf
# 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帖子,望大神勿怪。



页: [1]
查看完整版本: apache+tomcat 实现负载均衡集群