apache+tomcat負載均衡

火星人 @ 2014-03-03 , reply:0


apache+tomcat負載均衡

apache+tomcat負載均衡

3台伺服器centos5.3  32bit
192.168.1.244(apache)  192.168.1.242(tomcat)  192.168.1.247(tomcat),每台tomcat分別安裝建立2個普通用戶每個用戶跑一個tomcat實例。
安裝環境說明:
apache伺服器這裡安裝了 LAMP+tomcat+jdk+tomcat-connectors-1.2.30-src.tar.gz(native)
tomcat伺服器只需要安裝jdk和tomcatyum -y install httpd httpd-devel php mysql mysql-server php-mysql zlib gd freetype libpng jpeg freetype-devel libpng-devel php-mysql mysql-php php-
pdo openssl gcc glib  auto make mod_auth_mysql php-mcrypt php-gd php-xml php-mbstring php-ldap php-pear php-xmlrpc mysql-connector-odbc mysql-devel
libdbi-dbd-mysql mod_ssl mod_perl mod_auth_mysql php-gd php-xml php-mbstring php-ldap php-pear php-xmlrpc  mysql-connector-odbc mysql-devel libdbi-
dbd-mysql
再安裝jdk-6u22-linux-i586.bin
mkdir /usr/local/java
./jdk-6u22-linux-i586.bin
配置環境:
vi /etc/profile
JAVA_HOME=/usr/local/java/jdk1.6.0_22
JRE_HOME=$JAVA_HOME/jre
PATH=$JAVA_HOME/bin:JRE_HOME/bin:$PATH
CLASSPATH=.:$JAVA_HOME/lib/jt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
export JAVA_HOME JRE_HOME PATH CLASSPATH
保存退出
再source  /etc/profile  使環境配置生效。
# java -version
java version "1.6.0_22"
Java(TM) SE Runtime Environment (build 1.6.0_22-b04)
Java HotSpot(TM) Client VM (build 17.1-b03, mixed mode, sharing)
開始配置apache
編輯httpd.conf:
LoadModule jk_module modules/mod_jk.so
JkWorkersFile conf/workers.properties
JkLogFile logs/mod_jk.log
# Set the jk log level
JkLogLevel error
# Select the log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
# JkOptions indicate to send SSL KEY SIZE,
#JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
#JkOptions +ForwardKeySize +ForwardURICompatUnparsed -ForwardDirectories
JkOptions +ForwardKeySize +ForwardURIEscaped -ForwardDirectories
#JkOptions +ForwardKeySize +ForwardURICompat +ForwardURIEscaped +ForwardDirectories +ForwardLocalAddress
# JkRequestLogFormat set the request format
JkRequestLogFormat "%w %V %T"
DirectoryIndex index.jsp index.php index.html index.html.var
AddType application/x-httpd-php .php

# pwd
/etc/httpd/conf
# cat workers.properties
worker.list=loadbalancer
# Define the first node...
#####1#####
worker.tomcat1.port=8002
worker.tomcat1.host=192.168.1.242
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor=1
worker.tomcat1.redirect=tomcat1
worker.tomcat1.local_worker=1
worker.tomcat1.cachesize=100
#####2######
worker.tomcat2.port=7009
worker.tomcat2.host=192.168.1.242
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor=1
worker.tomcat2.redirect=tomcat2
worker.tomcat2.local_worker=1
worker.tomcat2.cachesize=100
#####3#######
worker.tomcat3.port=8002
worker.tomcat3.host=192.168.1.247
worker.tomcat3.type=ajp13
worker.tomcat3.lbfactor=1
worker.tomcat3.redirect=tomcat3
worker.tomcat3.local_worker=1
worker.tomcat3.cachesize=100
######4###########
worker.tomcat4.port=7009
worker.tomcat4.host=192.168.1.247
worker.tomcat4.type=ajp13
worker.tomcat4.lbfactor=1
worker.tomcat4.redirect=tomcat4
worker.tomcat4.local_worker=1
worker.tomcat4.cachesize=100
# Now we define the load-balancing behaviour
worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=tomcat1,tomcat2,tomcat3,tomcat4
worker.loadbalancer.sticky_session=1
#
# ls
httpd.conf  magic  workers.properties
# cd ..
# cd conf.d/
# ls
auth_mysql.conf    jk_mount.conf    perl.conf  proxy_ajp.conf  ssl.conf
httpd-vhosts.conf  jk_unmount.conf  php.conf   README          welcome.conf
# cat jk_mount.conf
JkMount /WEB-INF/* loadbalancer
JkMount /*.jsp loadbalancer
# cat jk_unmount.conf
JkUnMount /images/* loadbalancer  
JkUnMount /css/* loadbalancer  
JkUnMount /js/* loadbalancer  
JkUnMount /fckeditor/* loadbalancer
JkUnMount /asweb/* loadbalancer
JkUnMount /*.cgi loadbalancer
JkUnMount /*.html loadbalancer
# cat proxy_ajp.conf
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
#
# When loaded, the mod_proxy_ajp module adds support for
# proxying to an AJP/1.3 backend server (such as Tomcat).
# To proxy to an AJP backend, use the "ajp://" URI scheme;
# Tomcat is configured to listen on port 8009 for AJP requests
# by default.
#
#
# Uncomment the following lines to serve the ROOT webapp
# under the /tomcat/ location, and the jsp-examples webapp
# under the /examples/ location.
#
#ProxyPass /tomcat/ ajp://localhost:8009/
#ProxyPass /examples/ ajp://localhost:8009/jsp-examples/

# cat httpd-vhosts.conf

    DocumentRoot /home/web1
    ServerName  www.dong.com
Include conf.d/jk_mount.conf
Include conf.d/jk_unmount.conf
   
      Options -Indexes FollowSymLinks
      AllowOverride All
      Order allow,deny
      Allow from all


#
apache 上的配置完成了。
下面就是tomcat伺服器上的配置了。
開始tomcat的配置
jdk的安裝配置上面已經講過了。
下載apache-tomcat-7.0.16 分別複製2份,分別2個普通用戶登錄運行起來。
監聽埠不同。
默認的配置即可使用。
也可以在server.xml里加<Host name="www.dong.com" debug="0" appBase="webapps/ROOT"
       unpackWARs="true" autoDeploy="true">
        <Logger className="org.apache.catalina.logger.FileLogger"
                 directory="logs"  prefix="localhost_log." suffix=".txt"
        timestamp="true"/>
           <Context path="" docBase="/home/java1/web" debug="0"
                           reloadable="true" crossContext="true">     
           
      詳情見附件。
apache處理除了jsp之外的類型文件,jsp的類型文件交給後端的tomcat來處理,輪詢機制。

還需要在程序的web.xml裡面前面加入以下這句話

session同步到這裡設置完畢




[火星人 via ] apache+tomcat負載均衡已經有287次圍觀

http://www.coctec.com/docs/service/show-post-1623.html