apache+tomcat 實現負載平衡叢集

來源:互聯網
上載者:User

標籤:Proxy 伺服器   local   

環境介紹:

192.168.101.58 apacheProxy 伺服器 apache,apr,apr-util,tomcat-connector
192.168.101.62 tomcat-A伺服器

tomcat,jdk

192.168.101.63 tomcat-B伺服器 tomcat多執行個體,ajp_port:8019
192.168.101.63 tomcat-預設頁面伺服器 tomcat多執行個體,ajp_port:8029


apache支援兩種代理協議

    1.http

    2.ajp


tomcat及jdk的安裝略過。

編譯安裝apache的httpd

1、編譯安裝apr

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

2、編譯安裝apr-util

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

3、編譯安裝apache

3.1、安裝

[[email protected] src]# wget [[email protected] src]# tar -zxvf httpd-2.4.20.tar.gz[[email protected] src]# cd httpd-2.4.20[[email protected] 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[[email protected] src]# make && make install

3.2、編寫啟動指令碼

[[email protected] 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/functionsif [ -f /etc/sysconfig/httpd ]; then        . /etc/sysconfig/httpdfi# 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/apachectlhttpd=${HTTPD-/usr/local/http/bin/httpd}prog=httpdpidfile=${PIDFILE-/var/run/httpd.pid}lockfile=${LOCKFILE-/var/lock/subsys/httpd}RETVAL=0start() {        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 $httpdRETVAL=$?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} $httpdRETVAL=$?;;  restart)stopstart;;  condrestart)if [ -f ${pidfile} ] ; thenstopstartfi;;  reload)        reload;;  graceful|help|configtest|fullstatus)$apachectl [email protected]RETVAL=$?;;  *)echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"exit 1esacexit $RETVAL

3.3、啟動指令碼賦予許可權

[[email protected] src]# chmod +x /etc/init/d/httpd[[email protected] src]# chkconfig --add httpd

3.4、啟動apache(故障排查)

    3.4.1、查看httpd進程

[[email protected] src]# service httpd start[[email protected] src]# ps -ef |grep httpdroot     14445 14189  0 15:21 pts/0    00:00:00 grep httpd

    3.4.2、查看連接埠狀態

[[email protected] src]# netstat -tlnp |grep 80tcp        0      0 :::80            :::*          LISTEN      14300/httpd[[email protected] src]#

    3.4.3、查看錯誤記錄檔

[[email protected] 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模組

[[email protected] src]# vim /etc/httpd/httpd.confLoadModule slotmem_shm_module modules/mod_slotmem_shm.soLoadModule slotmem_plain_module modules/mod_slotmem_plain.so


3.4.4、重新啟動測試

[[email protected] src]# service httpd start[[email protected] src]# ps -ef |grep httpdroot     14300     1  0 12:52 ?        00:00:00 /usr/local/http/bin/httpddaemon   14302 14300  0 12:52 ?        00:00:01 /usr/local/http/bin/httpddaemon   14303 14300  0 12:52 ?        00:00:01 /usr/local/http/bin/httpddaemon   14304 14300  0 12:52 ?        00:00:01 /usr/local/http/bin/httpdroot     14443 14428  0 15:17 pts/1    00:00:00 vim /etc/init.d/httpdroot     14451 14189  0 15:28 pts/0    00:00:00 grep httpd[[email protected] src]# netstat -tlnp |grep 80tcp        0      0 :::80          :::*            LISTEN      14300/httpd


4、編譯apache的jk模組

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


5、配置(在httpd.conf中virtual host中添加jk的設定檔)

[[email protected] native]# vim /etc/httpd/httpd.confinclude /etc/httpd/extra/httpd-jk.conf

5.1編輯httpd-jk.conf

[[email protected] native]# vim /etc/httpd/extra/httpd-jk.confLoadModule jk_module modules/mod_jk.soJkWorkersFile /etc/httpd/extra/workers.propertiesJkLogFile logs/mod_jk.logJkLogLevel errorJkMount /* 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的ipworker.tomcat1.type=ajp13              #ajp協議版本,目前1.3worker.tomcat1.lbfactor=1              #權重#========tomcat2========worker.tomcat2.port=8019worker.tomcat2.host=192.168.101.63worker.tomcat2.type=ajp13worker.tomcat2.lbfactor=1#========tomcat3========worker.tomcat3.port=8029worker.tomcat3.host=192.168.101.63worker.tomcat3.type=ajp13worker.tomcat3.lbfactor=1#========controllerServer Load Balancer控制器========worker.controller.type=lb                          #指定controller類型worker.controller.balanced_workers=tomcat1,tomcat2,tomcat3 #指定Server Load Balancer的tomcatworker.controller.sticky_session=true              #指定是否粘性sessionworker.controller.sticky_session_force=falseworker.connection_pool_size=3000worker.connection_pool_minsize=50worker.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測試。


650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/84/50/wKioL1eMpR6RYPFVAAD5qpk2ZTk748.png-wh_500x0-wm_3-wmp_4-s_3778160687.png" style="float:none;" title="a.png" alt="wKioL1eMpR6RYPFVAAD5qpk2ZTk748.png-wh_50" />

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/84/50/wKiom1eMpR6waQD_AACe2qi7QAk192.png-wh_500x0-wm_3-wmp_4-s_2861830046.png" style="float:none;" title="b.png" alt="wKiom1eMpR6waQD_AACe2qi7QAk192.png-wh_50" />

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/84/50/wKioL1eMpR_h5FHNAAKKY2s7jVI021.png-wh_500x0-wm_3-wmp_4-s_1519754624.png" style="float:none;" title="c.png" alt="wKioL1eMpR_h5FHNAAKKY2s7jVI021.png-wh_50" />



本部落格參照了:http://cuisuqiang.iteye.com/blog/2070526  文章,望大神勿怪。

本文出自 “初心、始終” 部落格,請務必保留此出處http://gouyc.blog.51cto.com/1594451/1827470

apache+tomcat 實現負載平衡叢集

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.