zoukankan      html  css  js  c++  java
  • Tomcat集群搭建

    关于如何搭建Tomcat集群网上还是能搜到很多相关的教程,这里结合我自己在实际应用中的操作做下备忘。

    案例说明:

      这里以在本机部署的2个tomcat来做集群。当然,tomcat集群可以是分布式的,而差异也仅仅是在地址-端口的配置上,文章后面会讲到。

    部署环境:

    操作系统: Red Hat Enterprise Linux Server release 6.0 (Santiago)(x64)
    JDK版本: 1.6.0_45

    集群构成:

    httpd + tomcat + mod_jk

    版本选择:

    httpd-2.2.26
    apache-tomcat-7.0.42
    mod_jk-1.2.31-httpd-2.2.3 #这里写的是httpd-2.2.3,但其实跟httpd-2.2.26也能适配

    以上文件(都是我从Apache官网下载的)我已经做了打包并上传至百度云盘,需要的朋友可以使用下面的链接下载:

    把所需文件上传至服务器以后,我们就可以动手来搭建了。

    1.首先安装httpd,过程很简单如下:

    #解压httpd包
    tar -zxf httpd-2.2.26.tar.gz
    cd httpd-2.2.26/
    
    #配置预编译选项,这里只关心输出目录就可以了
    ./configure --prefix=~/apache2
    #编译
    make
    #安装
    make install

    2.部署tomcat

    #解压tomcat包,并复制成2份
    tar -zxf apache-tomcat-7.0.42.tar.gz
    mv apache-tomcat-7.0.42/ tomcat1/
    cp -r tomcat1/ tomcat2/

    2.1配置tomcat属性,这里以tomcat1、2为例(这里需要注意的地方主要是端口,设置时不要造成冲突即可):

    #修改tomcat配置文件server.xml,主要修改:Shutdown端口,Connect服务端口,AJP端口
    <Server port="8006" shutdown="SHUTDOWN">
    <Connector port="8081" protocol="HTTP/1.1" redirectPort="8443" />
    <Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />
    #修改集群名称和tomcat别名
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
    #去掉cluster节点的注释
    <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
    #修改以上端口是为了避免本机内多个tomcat同时运行造成端口冲突
    #修改tomcat配置文件context.xml,为<Context>节点增加distributable="true"属性

    同理,tomcat2的配置:

    #修改tomcat配置文件server.xml,主要修改:Shutdown端口,Connect服务端口,AJP端口
    <Server port="8007" shutdown="SHUTDOWN">
    <Connector port="8082" protocol="HTTP/1.1" redirectPort="8443" />
    <Connector port="8011" protocol="AJP/1.3" redirectPort="8443" />
    #修改集群名称和tomcat别名
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">
    #去掉cluster节点的注释
    <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
    #修改以上端口是为了避免本机内多个tomcat同时运行造成端口冲突
    #修改tomcat配置文件context.xml,为<Context>节点增加distributable="true"属性

    2.2为tomcat增加个测试文件,后期测试集群性能使用:

    <!--编辑webapps/test/test.jsp,主要用来测试负载均衡情况-->
    
    <%@ page contentType="text/html; charset=GB2312" %>
    <html>
        <head>
            <title>tomcat-cluster-test-page</title>
        </head>
        <body>
            SessionID: <%=session.getId() %> 
        </body>
    </html>

    OK,基本上tomcat的配置到这里就结束了。

    3.为httpd增加集群配置文件

    #在apache2/conf/下增加wokers.properties文件
    #文件内添加内容,形式如下:
    
    #server
    worker.list=loadbalancer,jkstatus
    
    #tomcat1
    worker.tomcat1.port=8010 #该端口是tomcat1的AJP服务端口,设置方式如2.1所示
    worker.tomcat1.host=localhost #如果tomcat部署在其他机器上,可填写其服务器IP
    worker.tomcat1.type=ajp13
    worker.tomcat1.lbfactor=1
    
    #tomcat2
    worker.tomcat2.port=8011 #该端口是tomcat2的AJP服务端口,设置方式如2.1所示
    worker.tomcat2.host=localhost
    worker.tomcat2.type=ajp13
    worker.tomcat2.lbfactor=1
    
    #loadbalancer
    worker.loadbalancer.type=lb
    worker.loadbalancer.balance_workers=tomcat1,tomcat2
    worker.loadbalancer.sticky_session=1
    worker.jkstatus.type=status

    3.1将mod_jk解压到apache2/modules下并命名文件为mod_jk.so,接下来修改配置文件如下:

    #编辑apache2/conf/httpd.conf,在底部增加配置:
    
    #加载mod_jk模块
    LoadModule jk_module modules/mod_jk.so
    
    #指定负载均衡配置文件
    JkWorkersFile conf/workers.properties
    
    #定义日志输出
    JkLogFile logs/mod_jk.log
    JkLogLevel debug
    
    #mod_jk按照访问路径来做请求分发
    #负载均衡的监控页面请求
    JkMount /jkstatus jkstatus
    
    #将其他任意请求都交由loadbalancer控制器处理 
    JkMount /* loadbalancer

    3.2为httpd手写一个启动控制器:

    #!/bin/bash
    #     file: run.sh
    #     what: httpd-controller, Usage: run.sh [OPTIONS] <start|status|stop>
    
    case "$1" in
        start)
            /tomcat/apache2/bin/apachectl -f /tomcat/apache2/conf/httpd.conf
            echo "apache-httpd started."
            ;;
        stop)
            kill -TERM `cat /tomcat/apache2/logs/httpd.pid`
            echo "apache-httpd stopped."
            ;;
        status)
            if [ -z "`ps -ef|grep httpd|grep -v grep`" ]; then
                echo "apache-httpd not started."
            else
                echo "apache-httpd is running."
            fi
            ;;
        *)
            echo "Usage: $0 [OPTIONS] <start|stop|status>"
            ;;
    esac

    4.启动tomcat和httpd:

    tomcat1/bin/startup.sh 
    tomcat2/bin/startup.sh 
    
    apache2/bin/run.sh start

    启动后,通过浏览器访问下监控页面http://192.168.1.100:8080/jkstatus

    访问http://192.168.1.100:8080/test/test.jsp,页面显示正常:

    那么我们来找个机器测试下负载均衡性能,测试结果如下:

    [tomcat@server ~/test]$for((i=0;i<1000;i++))
    > do
    >   wget http://192.168.1.100:8080/test/test.jsp 2>/dev/null
    > done
    [tomcat@server ~/test]$awk -F. '/SessionID/{a[$2]++}END{for(i in a){print i, a[i]}}' test.jsp*
    tomcat1   500
    tomcat2   500

    从结果来看:负载后,tomcat接收到的请求占比差不多1:1,还是不错的。

    关掉tomcat1,然后测试下访问会不会出现问题:

    [tomcat@server ~]tomcat1/bin/shutdown.sh
    [tomcat@server ~]cd test/
    [tomcat@server ~/test]$for((i=0;i<1000;i++))
    >do
    >wget http://192.168.1.100:8080/test/test.jsp 2>/dev/null
    >done
    [tomcat@server ~/test]
    [tomcat@server ~/test]$awk -F. '/SessionID/{a[$2]++}END{for(i in a){print i, a[i]}}' test.jsp*
    tomcat2 1000

    OK,所有请求都转发到tomcat2去了,说明节点故障也可以应付。tomcat集群搭建成功。

    以上即是tomcat集群搭建的全过程了,文章如有不当之处各位请不吝赐教。

  • 相关阅读:
    APEX安装
    SNN对抗攻击笔记
    Exploring Adversarial Attack in Spiking Neural Networks with Spike-Compatible Gradient
    Inherent Adversarial Robustness of Deep Spiking Neural Networks: Effects of Discrete Input Encoding and Non-Linear Activations
    NeuroAttack: Undermining Spiking Neural Networks Security through Externally Triggered Bit-Flips
    Training spiking neural networks for reinforcement learning
    Kernel methods on spike train space for neuroscience: a tutorial
    Eligibility Traces and Plasticity on Behavioral Time Scales: Experimental Support of neoHebbian Three-Factor Learning Rules
    python操作MySQL数据库
    连接mysql报错:error 2003 (hy000):can't connect to mysql server on 'localhost' (10061)
  • 原文地址:https://www.cnblogs.com/lichmama/p/4155344.html
Copyright © 2011-2022 走看看