apache+tomcat集群、负载均衡及session复制

一、环境搭建:
* 准备机器两台:192.168.0.2、192.168.0.3

* 下载apache2.2.9,地址:http://httpd.apache.org/download.cgi ,安装在192.168.0.2的D:\Apache

* 下载apache-tomcat-5.5.26.zip,地址:http://tomcat.apache.org/download-55.cgi ,分别解压到192.168.0.2、192.168.0.3的D:\Tomcat5.5

* 下载mod_jk,在这里找:http://tomcat.apache.org/download-connectors.cgi ,或者直接下载:http://dev.xiaonei.com/apache-mirror/tomcat/tomcat-connectors/jk/binaries/win32/jk-1.2.26/mod_jk-1.2.26-httpd-2.2.4.so ,拷贝到D:\Apache\modules改名为:mod_jk

二、集群,负载配置:
* 修改D:\Apache\conf\httpd.conf,

修改前:

Java代码
#Listen 12.34.56.78:80 
Listen 80 
 
#  
# Dynamic Shared Object (DSO) Support 

#Listen 12.34.56.78:80
Listen 80

#
# Dynamic Shared Object (DSO) Support

修改后:

Java代码
#Listen 12.34.56.78:80 
#Listen 80 
 
Listen 192.168.0.2:80 
 
 
#引入配置文件  
 
Include conf/http-vhosts.conf  
Include conf/mod_jk.conf  
 
# add compress  start  
LoadModule headers_module modules/mod_headers.so  
LoadModule deflate_module modules/mod_deflate.so  
# add compress  end  
#  
# Dynamic Shared Object (DSO) Support 

#Listen 12.34.56.78:80
#Listen 80

Listen 192.168.0.2:80


#引入配置文件

Include conf/http-vhosts.conf
Include conf/mod_jk.conf

# add compress  start
LoadModule headers_module modules/mod_headers.so
LoadModule deflate_module modules/mod_deflate.so
# add compress  end
#
# Dynamic Shared Object (DSO) Support

* 在conf下面新建http-vhosts.conf内容如下:

Java代码
NameVirtualHost *:80 
 
<VirtualHost *:80>  
        ServerAdmin [email protected]  
        DocumentRoot "e:/site/apache/www.yoursite.com" 
        ServerName yoursite.com  
        ServerAlias www.yoursite.com  
        ErrorLog "logs/www.yoursite.com-error.log" 
        CustomLog "logs/www.yoursite.com-access.log" common  
 
# add compress  start  
<Location "/">  
    SetOutputFilter DEFLATE  
    BrowserMatch ^Mozilla/4 gzip-only-text/html  
    BrowserMatch ^Mozilla/4\.0[678] no-gzip  
    BrowserMatch \bMSIE !no-gzip !gzip-only-text/html  
    SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary  
    Header append Vary User-Agent env=!dont-vary  
</Location>  
# add compress  end  
 
<Directory "e:/site/apache/www.yoursite.com">  
    #  
    # Possible values for the Options directive are "None", "All",  
    # or any combination of:  
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews  
    #  
    # Note that "MultiViews" must be named *explicitly* --- "Options All" 
    # doesn't give it to you.  
    #  
    # The Options directive is both complicated and important.  Please see  
    # http://httpd.apache.org/docs/2.2/mod/core.html#options  
    # for more information.  
    #  
    Options Indexes FollowSymLinks  
 
    #  
    # AllowOverride controls what directives may be placed in .htaccess files.  
    # It can be "All", "None", or any combination of the keywords:  
    #   Options FileInfo AuthConfig Limit  
    #  
    AllowOverride None  
 
    #  
    # Controls who can get stuff from this server.  
    #  
    Order allow,deny  
    Allow from all  
 
</Directory>  
 
</VirtualHost> 

NameVirtualHost *:80

<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "e:/site/apache/www.yoursite.com"
ServerName yoursite.com
ServerAlias www.yoursite.com
ErrorLog "logs/www.yoursite.com-error.log"
CustomLog "logs/www.yoursite.com-access.log" common

# add compress  start
<Location "/">
SetOutputFilter DEFLATE
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
Header append Vary User-Agent env=!dont-vary
</Location>
# add compress  end

<Directory "e:/site/apache/www.yoursite.com">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.2/mod/core.html#options
    # for more information.
    #
    Options Indexes FollowSymLinks

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    AllowOverride None

    #
    # Controls who can get stuff from this server.
    #
    Order allow,deny
    Allow from all

</Directory>

</VirtualHost>

* 在conf下新建文件mod_jk.conf,内容如下:

Java代码
# Load mod_jk module  
LoadModule jk_module modules/mod_jk.so  
 
 
# Where to find workers.properties  
JkWorkersFile conf/workers.properties  
 
# Where to put jk shared memory  
JkShmFile     logs/httpd/mod_jk.shm  
 
# Where to put jk logs  
JkLogFile logs/mod_jk.log  
 
 
# Set the jk log level [debug/error/info]  
JkLogLevel info  
 
 
# Select the log format  
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] " 
 
 
# JkOptions indicate to send SSL KEY SIZE,  
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories  
 
 
# JkRequestLogFormat set the request format  
JkRequestLogFormat "%w %V %T %R" 
 
# All requests go to Tomcat by default 
JkMount   /*     www  
# Serve html, jpg and gif etc using Apache  
JkUnMount /*.css www  
JkUnMount /*.js  www  
JkUnMount /*.gif www  
JkUnMount /*.jpg www  
JkUnMount /*.htm www  
JkUnMount /*.swf www  
JkUnMount /*.xml www  
JkUnMount /*.ico www  
 
# png and flv mount to tomcat  
#JkUnMount /*.png www  
#JkUnMount /*.flv www 

# Load mod_jk module
LoadModule jk_module modules/mod_jk.so


# Where to find workers.properties
JkWorkersFile conf/workers.properties

# Where to put jk shared memory
JkShmFile     logs/httpd/mod_jk.shm

# Where to put jk logs
JkLogFile logs/mod_jk.log


# Set the jk log level [debug/error/info]
JkLogLevel info


# Select the log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "


# JkOptions indicate to send SSL KEY SIZE,
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories


# JkRequestLogFormat set the request format
JkRequestLogFormat "%w %V %T %R"

# All requests go to Tomcat by default
JkMount   /*     www
# Serve html, jpg and gif etc using Apache
JkUnMount /*.css www
JkUnMount /*.js  www
JkUnMount /*.gif www
JkUnMount /*.jpg www
JkUnMount /*.htm www
JkUnMount /*.swf www
JkUnMount /*.xml www
JkUnMount /*.ico www

# png and flv mount to tomcat
#JkUnMount /*.png www
#JkUnMount /*.flv www

* 在conf下新建文件workers.properties,内容如下:

Java代码
#  
# workers.properties  
#  
 
# list the workers by name  
 
worker.list=www  
worker.maintain=60 
 
# =============================================================================  
 
# ------------------------  
# www1 server 0.2 
# ------------------------  
worker.www1.port=8009 
worker.www1.host=192.168.0.2 
worker.www1.type=ajp13  
worker.www1.socket_timeout=0 
worker.www1.socket_keepalive=true 
worker.www1.lbfactor=10 
# Define preferred failover node for worker1  
#worker.www1.redirect=www2  
 
# ------------------------  
# www2 server 0.2 
# ------------------------  
worker.www2.port=9009 
worker.www2.host=192.168.0.2 
worker.www2.type=ajp13  
worker.www2.socket_timeout=0 
worker.www2.socket_keepalive=true 
worker.www2.lbfactor=10 
#Disable worker2 for all requests except failover  
#worker.worker2.activation=disabled  
 
 
# ------------------------  
# Load Balancer worker  
# ------------------------  
worker.www.type=lb  
#worker.www.balance_workers=www2  
worker.www.balance_workers=www1,www2  
worker.www.sticky_session=true 
#worker.www.method=Traffic  
 
# ----------------------  
#  
#-----------------------  
# Add the status worker to the worker list  
#worker.list=jkstatus  
# Define a 'jkstatus' worker using status  
#worker.jkstatus.type=status  
 
# Add the jkstatus mount point  
#JkMount /jkmanager/* jkstatus  
 
 
# ===========================================================================  
#  
# END workers.properties  


#
# workers.properties
#

# list the workers by name

worker.list=www
worker.maintain=60

# =============================================================================

# ------------------------
# www1 server 0.2
# ------------------------
worker.www1.port=8009
worker.www1.host=192.168.0.2
worker.www1.type=ajp13
worker.www1.socket_timeout=0
worker.www1.socket_keepalive=true
worker.www1.lbfactor=10
# Define preferred failover node for worker1
#worker.www1.redirect=www2

# ------------------------
# www2 server 0.2
# ------------------------
worker.www2.port=9009
worker.www2.host=192.168.0.2
worker.www2.type=ajp13
worker.www2.socket_timeout=0
worker.www2.socket_keepalive=true
worker.www2.lbfactor=10
#Disable worker2 for all requests except failover
#worker.worker2.activation=disabled


# ------------------------
# Load Balancer worker
# ------------------------
worker.www.type=lb
#worker.www.balance_workers=www2
worker.www.balance_workers=www1,www2
worker.www.sticky_session=true
#worker.www.method=Traffic

# ----------------------
#
#-----------------------
# Add the status worker to the worker list
#worker.list=jkstatus
# Define a 'jkstatus' worker using status
#worker.jkstatus.type=status

# Add the jkstatus mount point
#JkMount /jkmanager/* jkstatus


# ===========================================================================
#
# END workers.properties
#

三、session复制:参见[Tomcat 5集群中的SESSION复制|Tomcat 5集群中的SESSION复制详解],方法如下:
* 在每个工程的web.xml中</web-app>前面加上一行:<distributable/>(不要忘记了)
* 在tomcat配置文件的conf下的server.xml中去掉注释,如下:

Java代码
<Cluster className="org.apache.catalina.cluster.tcp.SimpleTcpCluster" 
                 managerClassName="org.apache.catalina.cluster.session.DeltaManager" 
                 expireSessionsOnShutdown="false" 
                 useDirtyFlag="true" 
                 notifyListenersOnReplication="true">  
 
            <Membership  
                className="org.apache.catalina.cluster.mcast.McastService" 
                mcastAddr="228.0.0.4" 
                mcastPort="45564" 
                mcastFrequency="500" 
                mcastDropTime="3000"/>  
 
            <Receiver  
                className="org.apache.catalina.cluster.tcp.ReplicationListener" 
                tcpListenAddress="auto" 
                tcpListenPort="4002" 
                tcpSelectorTimeout="100" 
                tcpThreadCount="6"/>  
 
            <Sender  
                className="org.apache.catalina.cluster.tcp.ReplicationTransmitter" 
                replicationMode="pooled" 
                ackTimeout="15000"/>  
 
            <Valve className="org.apache.catalina.cluster.tcp.ReplicationValve" 
                   filter=".*\.gif;.*\.js;.*\.jpg;.*\.htm;.*\.html;.*\.txt;"/>  
 
            <Deployer className="org.apache.catalina.cluster.deploy.FarmWarDeployer" 
                      tempDir="/tmp/war-temp/" 
                      deployDir="/tmp/war-deploy/" 
                      watchDir="/tmp/war-listen/" 
                      watchEnabled="false"/>  
        </Cluster> 

<Cluster className="org.apache.catalina.cluster.tcp.SimpleTcpCluster"
                 managerClassName="org.apache.catalina.cluster.session.DeltaManager"
                 expireSessionsOnShutdown="false"
                 useDirtyFlag="true"
                 notifyListenersOnReplication="true">

            <Membership
                className="org.apache.catalina.cluster.mcast.McastService"
                mcastAddr="228.0.0.4"
                mcastPort="45564"
                mcastFrequency="500"
                mcastDropTime="3000"/>

            <Receiver
                className="org.apache.catalina.cluster.tcp.ReplicationListener"
                tcpListenAddress="auto"
                tcpListenPort="4002"
                tcpSelectorTimeout="100"
                tcpThreadCount="6"/>

            <Sender
                className="org.apache.catalina.cluster.tcp.ReplicationTransmitter"
                replicationMode="pooled"
                ackTimeout="15000"/>

            <Valve className="org.apache.catalina.cluster.tcp.ReplicationValve"
                   filter=".*\.gif;.*\.js;.*\.jpg;.*\.htm;.*\.html;.*\.txt;"/>

            <Deployer className="org.apache.catalina.cluster.deploy.FarmWarDeployer"
                      tempDir="/tmp/war-temp/"
                      deployDir="/tmp/war-deploy/"
                      watchDir="/tmp/war-listen/"
                      watchEnabled="false"/>
        </Cluster>

* 下面是成功的正常的反应(关闭另一个tomcat,开始另一个tomcat):




现在你可是感受一下成果,session被复制到每个tomcat例程,即使例程当机,只要还剩下一个例程,用户就感觉不到任何异常。

你可能感兴趣的:(apache,tomcat,socket,css,cgi)