Tomcat 多实例多应用部署

一、前言

最近在公司线上操作了一次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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,542评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,596评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,021评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,682评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,792评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,985评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,107评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,845评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,299评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,612评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,747评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,441评论 4 333
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,072评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,828评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,069评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,545评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,658评论 2 350

推荐阅读更多精彩内容