`

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 admin@yoursite.com  
        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 admin@yoursite.com
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+Linux集群和均衡负载(Session同步复制

    Apache+Tomcat+Linux集群和均衡负载(Session同步复制

    Apache+Tomcat集群和负载均衡及Session绑定

    【Apache+Tomcat 集群与负载均衡及Session...通过以上配置,Apache+Tomcat集群可以实现高效的负载均衡和高可用性,同时通过Session绑定确保用户会话的连续性。注意定期监控和优化集群性能,以应对不断变化的业务需求。

    linux下apache+tomact基于集群负载均衡session复制

    ### Linux 下 Apache+Tomcat 基于集群负载均衡 Session 复制 #### 一、概述 在现代 Web 应用开发中,为了提高服务的可用性和响应速度,常常会采用集群技术。其中,Apache 和 Tomcat 组合是常用的方案之一。本篇...

    Apache+Tomcat+JK 实现集群负载均衡文档

    Apache+Tomcat+JK 实现集群负载均衡是一种常见的Web服务器和应用服务器的部署方式,它能够提高系统的可用性和响应速度,实现服务的高并发处理。本文将详细介绍如何配置这一环境。 首先,我们需要准备以下软件: 1. ...

    .在32位操作系统上配置apache+tomcat集群/负载均衡/session复制

    标题中的“在32位操作系统上配置apache+tomcat集群/负载均衡/session复制”涉及到的是在32位系统环境下,如何构建一个使用Apache HTTP Server作为前端负载均衡器,与多个Tomcat应用服务器协同工作的集群环境,并实现...

    apache+tomcat集群配置

    本文将详细介绍如何配置 Apache + Tomcat 集群,包括安装 Apache 和 Tomcat 服务器、配置 mod_jk 模块、配置 Apache 和 Tomcat 服务器、配置负载均衡控制器等。 一、安装 Apache 服务器 首先,需要安装 Apache ...

    Windows+Nginx+Tomcat做负载均衡同时实现session共享Demo

    总之,“Windows+Nginx+Tomcat做负载均衡同时实现session共享Demo”是一个实践性强的教学资源,它涵盖了Web服务集群中的关键技术和最佳实践,对于学习和提升系统架构能力非常有帮助。通过深入理解和实践这个Demo,...

    Apache+Tomcat实现负载均衡及集群(session同步)--五、session同步(3)使用memcache

    Apache和Tomcat的组合经常被用来搭建这样的架构,特别是在实现负载均衡和集群时。在这个场景中,session同步是一个核心问题,因为用户session数据的正确管理和共享是保持应用程序一致性和用户体验的关键。本文将深入...

    实现apache+tomcat集群和负载均衡的比较

    在部署Apache+Tomcat集群时,还需考虑session复制或共享以保证会话一致性,以及监控和管理工具的使用,如Nagios、Zabbix等,以确保集群的稳定运行。同时,为了提高安全性,建议使用SSL/TLS进行加密传输,通过配置...

    apache+tomcat集群均衡负载 简单实现步骤

    总结,实现Apache+Tomcat集群负载均衡的关键在于正确配置Apache的mod_proxy模块,以及确保Tomcat集群的会话一致性。通过这种方式,我们可以有效分摊高并发访问压力,提高系统的稳定性和响应速度。同时,持续监控和...

    apache + tomcat 集群+session复制解决方案

    Apache 和 Tomcat 集群配合 Session 复制是一种常见的高可用性和负载均衡解决方案,尤其适用于大型网站和企业级应用。以下将详细解释这个方案的关键点。 **整体方案策略** 1. **部署逻辑图** 部署逻辑通常包括...

    apache_server+Tomcat7集群负载均衡session共享

    JK模块负责通信、负载均衡以及session复制。 1. **JK模块配置**:`mod_jk.so`的配置主要在Apache的httpd.conf或独立的workers.properties文件中进行。需要定义每个Tomcat实例的工作者(worker),包括它们的IP地址...

    Apache+tomcat负载均衡

    Apache+Tomcat的负载均衡配置就是为了解决这一问题。 负载均衡的基本思想是将来自用户的请求分发到多个后端服务器,这样可以避免任何单一服务器过载,同时提高整体服务的可靠性和响应时间。Apache通常通过其模块mod...

    apache,tomcat负载均衡和session复制

    当我们谈论"Apache,tomcat负载均衡和session复制"时,这意味着我们要探讨如何在多台服务器之间分配负载,并确保用户会话的无缝迁移和一致性。 **负载均衡**是解决高并发、高可用性问题的重要策略。它通过将来自...

    Apache+Tomcat集群

    在实际应用中,Apache+Tomcat集群的配置可能更为复杂,需要考虑SSL支持、session复制、故障转移、健康检查等高级特性。此外,随着技术的发展,现代的负载均衡解决方案如Nginx、HAProxy等也可能替代Apache作为反向...

    Nginx+tomcat负载均衡集群session复制 windos

    本文将详细介绍如何在Windows环境下,利用Nginx作为反向代理服务器,与Tomcat集群配合实现负载均衡,并进行session复制,确保用户会话在不同服务器之间的一致性。 首先,Nginx是一款轻量级、高性能的HTTP和反向代理...

Global site tag (gtag.js) - Google Analytics