美文网首页JavaJava 程序猿的进阶屋
Tomcat 多实例多应用部署

Tomcat 多实例多应用部署

作者: 小尛酒窝 | 来源:发表于2018-08-21 15:44 被阅读244次

    一、前言

    最近在公司线上操作了一次Tomcat的多实例的创建启动,过程有点曲折,说明对这块的知识比较薄弱。回来自己整理学习了一下,重新模拟创建启动了一个多实例的Tomcat,现在整理成文。

    jdk包的下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

    Tomcat的下载地址:https://tomcat.apache.org/download-80.cgi

    系统环境:Centos 7.4,JDK-1.8.0,Tomcat-7.0.88

    二、安装JDK

    
    [root@localhost ~]# cd /usr/local/src/
    
    #解压下载好的jdk包
    
    [root@localhost src]# tar zxf jdk-8u181-linux-x64.tar.gz 
    
    [root@localhost src]# ln -sv /usr/local/src/jdk1.8.0_181/ /usr/local/jdk
    
    ‘/usr/local/jdk’ -> ‘/usr/local/src/jdk1.8.0_181/’
    
    [root@localhost src]# vim /etc/profile.d/jdk.sh
    
    JAVA_HOME=/usr/local/jdk/
    
    JAVA_BIN=/usr/local/jdk/bin
    
    JRE_HOME=/usr/local/jdk/jre
    
    PATH=$PATH:/usr/local/jdk/bin:/usr/local/jdk/jre/bin
    
    CLASSPATH=/usr/local/jdk/jre/lib:/usr/local/jdk/lib:/usr/local/jdk/jre/lib/charsets.jar
    
    export JAVA_HOME JAVA_BIN JRE_HOME PATH CLASSPATH
    
    [root@localhost src]# source /etc/profile.d/jdk.sh
    
    #验证安装
    
    [root@localhost src]# java -version
    
    java version "1.8.0_181"
    
    Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
    
    Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)
    
    

    三、部署Tomcat 多实例

    1、解压缩相应的tomcat包

    
    [root@localhost src]# tar zxf apache-tomcat-7.0.88.tar.gz 
    
    [root@localhost src]# ln -sv /usr/local/src/apache-tomcat-7.0.88 /usr/local/tomcat
    
    

    2、创建对应实例的启动脚本

    
    #创建两个实例对应目录
    
    [root@localhost src]# mkdir -pv /usr/local/{tomcat1,tomcat2}/bin
    
    [root@localhost src]# vim /usr/local/tomcat1/bin/startup.sh 
    
    #!/bin/bash
    
    export JRE_HOME=/usr/local/jdk
    
    export CATALINA_HOME=/usr/local/tomcat
    
    export CATALINA_BASE="/usr/local/tomcat1"
    
    export CATALINA_TMPDIR="$CATALINA_BASE/temp"
    
    export CATALINA_PID="$CATALINA_BASE/bin/tomcat.pid"
    
    export JAVA_OPTS="-server -Xms1024m -Xmx1024m -Djava.awt.headless=true -Dtomcat.name=tomcat1"
    
    #创建logs目录
    
    if [ ! -d "$CATALINA_BASE/logs" ]; then
    
      mkdir $CATALINA_BASE/logs
    
    fi
    
    #创建temp目录
    
    if [ ! -d "$CATALINA_BASE/temp" ]; then
    
      mkdir $CATALINA_BASE/temp
    
    fi
    
    # 调用tomcat启动脚本
    
    bash $CATALINA_HOME/bin/startup.sh "$@"        #此脚本实际还是调用了CATALINA_HOME中的bin的startup.sh启动脚本进行启动
    
    [root@localhost src]# chmod +x /usr/local/tomcat1/bin/startup.sh
    
    #将刚创建的tomcat1启动脚本复制到tomcat2 实例的bin目录下,并修改相应的路径名称
    
    [root@localhost src]# cp -a /usr/local/tomcat1/bin/startup.sh /usr/local/tomcat2/bin/startup.sh        
    
    

    3、创建对应实例的关闭脚本

    
    [root@localhost src]# vim /usr/local/tomcat1/bin/shutdown.sh 
    
    #!/bin/bash
    
    export JRE_HOME=/usr/local/jdk
    
    export CATALINA_HOME=/usr/local/tomcat
    
    export CATALINA_BASE="/usr/local/tomcat1"
    
    export CATALINA_TMPDIR="$CATALINA_BASE/temp"
    
    export CATALINA_PID="$CATALINA_BASE/bin/tomcat.pid"
    
    bash $CATALINA_HOME/bin/shutdown.sh "$@" 
    
    [root@localhost src]# chmod +x /usr/local/tomcat1/bin/shutdown.sh 
    
    #将刚创建的tomcat1关闭脚本复制到tomcat2 实例的bin目录下,并修改相应的路径名称
    
    [root@localhost src]# cp -a /usr/local/tomcat1/bin/shutdown.sh /usr/local/tomcat2/bin/shutdown.sh
    
    

    4、复制相应的目录到实例目录中

    
    [root@localhost src]# cp -a /usr/local/tomcat/{conf,webapps,temp,work,logs} /usr/local/tomcat1
    
    [root@localhost src]# cp -a /usr/local/tomcat/{conf,webapps,temp,work,logs} /usr/local/tomcat2
    
    

    5、修改每个tomcat实例中的server.xml文件中的端口

    
    #此处我保持tomcat1 端口不变,修改tomcat2的端口
    
    [root@localhost src]# vim /usr/local/tomcat2/conf/server.xml
    
    ....
    
    <Server port="8105" shutdown="SHUTDOWN">
    
    .....
    
        <Connector port="8180" protocol="HTTP/1.1"
    
                   connectionTimeout="20000"
    
                   redirectPort="9443" />
    
    .....
    
        <Connector port="8109" protocol="AJP/1.3" redirectPort="9443" />
    
    

    6、编辑创建tomcat实例对应的启动脚本

    
    [root@localhost src]# vim /etc/init.d/tomcat1
    
    #!/bin/bash
    
    # /etc/rc.d/init.d/tomcat
    
    # init script for tomcat precesses
    
    # processname: tomcat
    
    # description: tomcat is a j2se server
    
    # chkconfig: 2345 86 16
    
    # description: Start up the Tomcat servlet engine.
    
    if [ -f /etc/init.d/functions ]; then
    
    . /etc/init.d/functions
    
    elif [ -f /etc/rc.d/init.d/functions ]; then
    
    . /etc/rc.d/init.d/functions
    
    else
    
    echo -e "\atomcat: unable to locate functions lib. Cannot continue."
    
    exit -1
    
    fi
    
    RETVAL=$?
    
    CATALINA_BASE="/usr/local/tomcat1"     #tomcat安装目录,你安装在什么目录下就复制什么目录
    
    start() {
    
    if [ -f $CATALINA_BASE/bin/startup.sh ];
    
    then
    
    echo $"Starting Tomcat"
    
    $CATALINA_BASE/bin/startup.sh
    
    fi
    
    }
    
    stop() {
    
    if [ -f $CATALINA_BASE/bin/shutdown.sh ];
    
    then
    
    echo $"Stopping Tomcat"
    
    $CATALINA_BASE/bin/shutdown.sh
    
    fi
    
    }
    
    status() {
    
    pid=`ps ax --width=1000 | grep "$CATALINA_BASE" | grep -v grep | awk '{printf $1 " "}'`
    
    if [ "$pid" != "" ]
    
    then
    
    echo -n "tomcat is running... pid is $pid"
    
    echo
    
    else
    
    echo "Tomcat is stopped"
    
    fi
    
    }
    
    case "$1" in
    
    start)
    
            start
    
    ;;
    
    stop)
    
    stop
    
    ;;
    
    restart)
    
            stop
    
            sleep 2
    
            start
    
    ;;
    
    status)
    
           status
    
    ;;
    
    *)
    
    echo $"Usage: $0 {start|stop|status|restart}"
    
    exit 1
    
    ;;
    
    esac
    
    exit $RETVAL
    
    [root@localhost src]# chmod +x /etc/init.d/tomcat1
    
    #重复上述操作,创建tomcat2的启动脚本
    
    

    7、测试启动tomcat实例

    
    [root@localhost src]# service tomcat1 start
    
    Starting tomcat1 (via systemctl): [ OK ]
    
    [root@localhost src]# service tomcat2 start
    
    Starting tomcat2 (via systemctl): [ OK ]
    
    [root@localhost src]# ss -tnl 
    
    State Recv-Q Send-Q Local Address:Port Peer Address:Port              
    
    LISTEN 0 25 *:514 *:*                  
    
    LISTEN 0 128 *:22 *:*                  
    
    LISTEN 0 100 127.0.0.1:25 *:*                  
    
    LISTEN 0 25 :::514 :::*                  
    
    LISTEN 0 1 ::ffff:127.0.0.1:8005 :::*                  
    
    LISTEN 0 1 ::ffff:127.0.0.1:8105 :::*                  
    
    LISTEN 0 100 :::8009 :::*                  
    
    LISTEN 0 100 :::8109 :::*                  
    
    LISTEN 0 100 :::8080 :::*                  
    
    LISTEN 0 100 :::8180 :::*                  
    
    LISTEN 0 128 :::22 :::*                  
    
    LISTEN 0 100 ::1:25 :::*                  
    
    [root@localhost src]# ps aux | grep tomcat 
    
    root 63218 9.0 16.7 3086072 167224 ? Sl 15:01 0:02 /usr/local/jdk/bin/java -Djava.util.logging.config.file=/usr/local/tomcat1/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -server -Xms512m -Xmx1024m -Djava.awt.headless=true -Dtomcat.name=tomcat1 -Djdk.tls.ephemeralDHKeySize=2048 -Dignore.endorsed.dirs= -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat1 -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat1/temp org.apache.catalina.startup.Bootstrap start
    
    root 63295 12.6 15.4 3088120 153992 ? Sl 15:01 0:02 /usr/local/jdk/bin/java -Djava.util.logging.config.file=/usr/local/tomcat2/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -server -Xms1024m -Xmx1024m -Djava.awt.headless=true -Dtomcat.name=tomcat2 -Djdk.tls.ephemeralDHKeySize=2048 -Dignore.endorsed.dirs= -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat2 -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat2/temp org.apache.catalina.startup.Bootstrap start
    
    root 63350 0.0 0.0 112704 972 pts/1 R+ 15:01 0:00 grep --color=auto tomcat
    
    

    由上述看到,相应的端口和进程均启动正常。

    此时访问访问相应的tomcat端口8080和8180应能正常访问到tomcat管理界面,如:


    Tomcat1实例的访问界面 Tomcat2实例的访问界面

    8、以非root身份启动tomcat

    
    [root@localhost src]# service tomcat1 stop
    
    Stopping tomcat1 (via systemctl): [ OK ]
    
    [root@localhost src]# service tomcat2 stop
    
    Stopping tomcat2 (via systemctl): [ OK ]
    
    [root@localhost src]# chown tomcat.tomcat /usr/local/src/apache-tomcat-7.0.88 -R
    
    [root@localhost src]# chown tomcat.tomcat /usr/local/tomcat1 -R
    
    [root@localhost src]# chown tomcat.tomcat /usr/local/tomcat2 -R
    
    [root@localhost src]# useradd tomcat
    
    [root@localhost src]# echo "123456" | passwd --stdin tomcat
    
    [tomcat@localhost ~]$ /usr/local/tomcat1/bin/startup.sh 
    
    Using CATALINA_BASE: /usr/local/tomcat1
    
    Using CATALINA_HOME: /usr/local/tomcat
    
    Using CATALINA_TMPDIR: /usr/local/tomcat1/temp
    
    Using JRE_HOME: /usr/local/jdk
    
    Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
    
    Using CATALINA_PID: /usr/local/tomcat1/bin/tomcat.pid
    
    Tomcat started
    
    [tomcat@localhost ~]$ /usr/local/tomcat2/bin/startup.sh 
    
    Using CATALINA_BASE: /usr/local/tomcat2
    
    Using CATALINA_HOME: /usr/local/tomcat
    
    Using CATALINA_TMPDIR: /usr/local/tomcat2/temp
    
    Using JRE_HOME: /usr/local/jdk
    
    Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
    
    Using CATALINA_PID: /usr/local/tomcat2/bin/tomcat.pid
    
    Tomcat started.
    
    [tomcat@localhost ~]$ ps aux | grep tomcat        #进程已经以tomcat身份启动
    
    tomcat 63866 9.4 16.9 3086072 169592 pts/1 Sl 15:09 0:02 /usr/local/jdk/bin/java -Djava.util.logging.config.file=/usr/local/tomcat1/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -server -Xms512m -Xmx1024m -Djava.awt.headless=true -Dtomcat.name=tomcat1 -Djdk.tls.ephemeralDHKeySize=2048 -Dignore.endorsed.dirs= -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat1 -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat1/temp org.apache.catalina.startup.Bootstrap start
    
    tomcat 63921 21.6 15.2 3088120 152056 pts/1 Sl 15:09 0:02 /usr/local/jdk/bin/java -Djava.util.logging.config.file=/usr/local/tomcat2/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -server -Xms1024m -Xmx1024m -Djava.awt.headless=true -Dtomcat.name=tomcat2 -Djdk.tls.ephemeralDHKeySize=2048 -Dignore.endorsed.dirs= -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat2 -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat2/temp org.apache.catalina.startup.Bootstrap start
    
    tomcat 63968 0.0 0.1 155324 1864 pts/1 R+ 15:09 0:00 ps aux
    
    

    三、多实例集群会话同步

    1、修改tomcat1实例的server.xml配置

    [root@localhost ~]# vim /usr/local/tomcat1/conf/server.xml
            <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
                     channelSendOptions="8">
    
              <Manager className="org.apache.catalina.ha.session.DeltaManager"
                       expireSessionsOnShutdown="false"
                       notifyListenersOnReplication="true"/>
    
              <Channel className="org.apache.catalina.tribes.group.GroupChannel">
                <Membership className="org.apache.catalina.tribes.membership.McastService"
                            address="228.0.0.4"        #此处配置为集群的同步广播地址,集群内的主机需一致
                            port="45564"
                            frequency="500"
                            dropTime="3000"/>
                <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
                          address="auto"
                          port="4000"        #此端口为集群主机接受集群信息的端口,由于我们的多实例是运行在同一个主机上,因此实例间的端口不能相同
                          autoBind="100"
                          selectorTimeout="5000"
                          maxThreads="6"/>
    
                <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
                  <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
                </Sender>
                <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
                <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
              </Channel>
    
              <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
                     filter=""/>
              <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
    
              <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
                        tempDir="/tmp/war-temp/"
                        deployDir="/tmp/war-deploy/"
                        watchDir="/tmp/war-listen/"
                        watchEnabled="false"/>
    
              <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
              <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
            </Cluster>
    

    2、修改tomcat2实例的server.xml配置

    [root@localhost ~]# vim /usr/local/tomcat2/conf/server.xml
            <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
                     channelSendOptions="8">
    
              <Manager className="org.apache.catalina.ha.session.DeltaManager"
                       expireSessionsOnShutdown="false"
                       notifyListenersOnReplication="true"/>
    
              <Channel className="org.apache.catalina.tribes.group.GroupChannel">
                <Membership className="org.apache.catalina.tribes.membership.McastService"
                            address="228.0.0.4"
                            port="45564"
                            frequency="500"
                            dropTime="3000"/>
                <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
                          address="auto"
                          port="4001"        #调整为4001,接口范围为4000-4100
                          autoBind="100"
                          selectorTimeout="5000"
                          maxThreads="6"/>
    
                <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
                  <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
                </Sender>
                <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
                <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
              </Channel>
    
              <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
                     filter=""/>
              <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
    
              <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
                        tempDir="/tmp/war-temp/"
                        deployDir="/tmp/war-deploy/"
                        watchDir="/tmp/war-listen/"
                        watchEnabled="false"/>
    
              <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
              <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
            </Cluster>
    

    3、创建各实例的session测试页面

    [root@localhost ~]# mkdir -pv /usr/local/tomcat1/webapps/test/{WEB-INF,META-INF,classes,lib}
    [root@localhost ~]# mkdir -pv /usr/local/tomcat2/webapps/test/{WEB-INF,META-INF,classes,lib}
    [root@localhost ~]# vim /usr/local/tomcat1/webapps/test/index.jsp
    <%@ page language="java" %>
    <html>
      <head><title>Tomcat1</title></head>
      <body>
        <h1><font color="red">Tomcat1.example.com</h1>
        <table align="centre" border="1">
          <tr>
            <td>Session ID</td>
        <% session.setAttribute("example.com","example.com"); %>
            <td><%= session.getId() %></td>
          </tr>
          <tr>
            <td>Created on</td>
            <td><%= session.getCreationTime() %></td>
         </tr>
        </table>
      </body>
    </html>
    [root@localhost ~]# vim /usr/local/tomcat2/webapps/test/index.jsp 
    
    <%@ page language="java" %>
    <html>
      <head><title>Tomcat2</title></head>
      <body>
        <h1><font color="green">Tomcat2.example.com</h1>
        <table align="centre" border="1">
          <tr>
            <td>Session ID</td>
        <% session.setAttribute("example.com","example.com"); %>
            <td><%= session.getId() %></td>
          </tr>
          <tr>
            <td>Created on</td>
            <td><%= session.getCreationTime() %></td>
         </tr>
        </table>
      </body>
    </html>
    
    

    4、修改各实例webapps目录下的指定web.xml

    #在对应的web.xml的文件中的<web-app>和<web-app/>之间添加<distributable/>
    [root@localhost ~]# cp /usr/local/tomcat/webapps/ROOT/WEB-INF/web.xml /usr/local/tomcat1/webapps/test/WEB-INF/
    [root@localhost ~]# vim /usr/local/tomcat1/webapps/test/WEB-INF/web.xml
    <distributable/>
    [root@localhost ~]# cp /usr/local/tomcat/webapps/ROOT/WEB-INF/web.xml /usr/local/tomcat2/webapps/test/WEB-INF/
    [root@localhost ~]# vim /usr/local/tomcat2/webapps/test/WEB-INF/web.xml
    <distributable/>
    

    5、重启tomcat并测试

    [root@localhost ~]# service tomcat1 restart
    [root@localhost ~]# service tomcat2 restart
    

    此时访问两个实例的网页所获取到的session ID 应为一致。


    Session ID 一致

    参考链接:https://blog.csdn.net/xyang81/article/details/51997053

    相关文章

      网友评论

      本文标题:Tomcat 多实例多应用部署

      本文链接:https://www.haomeiwen.com/subject/dxrjiftx.html