设为首页 收藏本站
查看: 333|回复: 0

[经验分享] apache+tomcat集群、负载均衡及session复制

[复制链接]

尚未签到

发表于 2017-1-10 09:35:54 | 显示全部楼层 |阅读模式
一、环境搭建:
* 准备机器两台: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例程,即使例程当机,只要还剩下一个例程,用户就感觉不到任何异常。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-326374-1-1.html 上篇帖子: apache配置多个域名访问同一个虚拟主机 下篇帖子: 用Apache Ivy实现项目里的依赖管理
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表