1、前言
首先要回答一個(gè)問題,為什么要用單機(jī)多實(shí)例?
在不宕機(jī)的情況下,webapps里面存在多個(gè)項(xiàng)目,可能由于其中一個(gè)項(xiàng)目過度使用內(nèi)存或者其他不確定的因素使得tomcat掛了,那么同一tomcat下的項(xiàng)目也會(huì)一同掛了;而使用不同的tomcat,同一臺(tái)服務(wù)器下,每個(gè)tomcat的進(jìn)程是不一樣的額,一個(gè)項(xiàng)目出現(xiàn)問題tomcat掛了,那么由于是在不同進(jìn)程,其他項(xiàng)目不會(huì)影響的。
還有一個(gè)問題就是不同tomcat使用了不同端口,最后域名只有一個(gè)怎么分配?
其實(shí)這個(gè)使用nginx的反向代理,根據(jù)請(qǐng)求的前綴,代理到相應(yīng)的tomcat項(xiàng)目服務(wù)端口對(duì)應(yīng)的nginx server即可。
2、系統(tǒng)環(huán)境
系統(tǒng):16.04.5 LTS
JDK版本:openjdk 1.8
tomcat版本:apache-tomcat-9.0.13
3、環(huán)境搭建
3.1、下載tomcat
安裝jdk:
apt-get install openjdk-8-jdk
訪問官網(wǎng):https://tomcat.apache.org/download-90.cgi
找到core,點(diǎn)擊tar.gz帶彈出下載連接復(fù)制下載地址。
然后使用命令下載tomcat
wget http://mirrors.hust.edu.cn/apache/tomcat/tomcat-9/v9.0.13/bin/apache-tomcat-9.0.13.tar.gz
解壓tomcat:
tar -xzvf apache-tomcat-9.0.13.tar.gz
建一個(gè)程序目錄:
##創(chuàng)建一個(gè)程序目錄
mkdir /data
##移動(dòng)解壓文件到data目錄下
mv ./apache-tomcat-9.0.13 /data/
cp /etc/profile /etc/profile.bak
echo "export CATALINA_HOME=/data/apache-tomcat-9.0.13" >> /etc/profile
echo "export PATH=\$PATH:\$CATALINA_HOME/bin">> /etc/profile && source /etc/profile
##創(chuàng)建sh文件
touch tomcat-start.sh tomcat-stop.sh
##更改權(quán)限
chmod 760 /data/tomcat-start.sh /data/tomcat-stop.sh
備份profile,并寫入tomcat的CATALINA_HOME到環(huán)境變量,激活環(huán)境變量。
創(chuàng)建 tomcat-start.sh,內(nèi)容如下:
#!/bin/bash
##這里的CATALINA_BASE,是當(dāng)前腳本的的父目錄,如果不在CATALINA_BASE的子目錄的話,記得修改
export CATALINA_BASE=$(cd $(dirname $0); cd .. ; pwd)
echo $CATALINA_BASE
TOMCAT_ID=`ps aux |grep "java"|grep "Dcatalina.base=$CATALINA_BASE "|grep -v "grep"|awk '{ print $2}'`
if [ -n "$TOMCAT_ID" ] ; then
echo "tomcat($TOMCAT_ID) still running now , please shutdown it first";
exit 2;
fi
TOMCAT_START_LOG=`$CATALINA_HOME/bin/startup.sh`
if [ "$?" = "0" ]; then
echo "shell script: $0"
echo "CATALINA_BASE: $CATALINA_BASE"
echo "tomcat thread: $TOMCAT_ID"
echo "start succeed!!!"
else
echo "$0 $CATALINA_BASE start failed"
echo $TOMCAT_START_LOG
fi
創(chuàng)建 tomcat-stop.sh,內(nèi)容如下:
#!/bin/bash
##這里的CATALINA_BASE,是當(dāng)前腳本的的父目錄,如果不在CATALINA_BASE的子目錄的話,記得修改
export CATALINA_BASE=$(cd $(dirname $0); cd .. ; pwd)
echo $CATALINA_BASE
TOMCAT_ID=`ps aux |grep "java"|grep "[D]catalina.base=$CATALINA_BASE "|awk '{ print $2}'`
if [ -n "$TOMCAT_ID" ] ; then
TOMCAT_STOP_LOG=`$CATALINA_HOME/bin/shutdown.sh`
else
echo "Tomcat instance not found : $CATALINA_BASE"
exit
fi
if [ "$?" = "0" ]; then
echo "shell script: $0"
echo "CATALINA_BASE: $CATALINA_BASE"
echo "stop succeed!!!"
else
echo "$0 $CATALINA_BASE stop failed"
echo $TOMCAT_STOP_LOG
fi
復(fù)制兩個(gè)tomcat:
##完成刪除lib和bin文件夾內(nèi)容,生成空的bin文件夾
cp -r apache-tomcat-9.0.13 /data/apache-tomcat-test1 && cd /data/apache-tomcat-test1 && rm -rf lib/ bin/ && mkdir bin && cd -
##復(fù)制啟動(dòng)和停止腳本到bin文件夾,帶權(quán)限復(fù)制
cp -p tomcat-start.sh tomcat-stop.sh /data/apache-tomcat-test1/bin/
##一個(gè)同樣的tomcat目錄,帶權(quán)限復(fù)制
cp -Rp /data/apache-tomcat-test1/ /data/apache-tomcat-test2/
配置 server.xml 端口
你知道的,同一個(gè)服務(wù)器部署不同 Tomcat 要設(shè)置不同的端口,不然會(huì)報(bào)端口沖突,所以我們只需要修改conf/server.xml中的其中前三個(gè)端口就行了。但它有四個(gè)分別是:
- Server Port:該端口用于監(jiān)聽關(guān)閉tomcat的shutdown命令,默認(rèn)為8005
- Connector HTTP Port:該端口用于監(jiān)聽HTTP的請(qǐng)求,默認(rèn)為8080
- Connector AJP Port:該端口用于監(jiān)聽AJP( Apache JServ Protocol )協(xié)議上的請(qǐng)求,通常用于整合Apache Server等其他HTTP服務(wù)器,默認(rèn)為8009
- Redirect Port:重定向端口,出現(xiàn)在Connector配置中,如果該Connector僅支持非SSL的普通http請(qǐng)求,那么該端口會(huì)把 https 的請(qǐng)求轉(zhuǎn)發(fā)到這個(gè)Redirect Port指定的端口,默認(rèn)為8443;
去掉注釋的版本:
<?xml version="1.0" encoding="UTF-8"?>
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
</Engine>
</Service>
</Server>
tomcat-test1改為:
- Server Port:9015
- Connector HTTP Port:9010
- Connector AJP Port:9019
tomcat-test2改為:
- Server Port:9025
- Connector HTTP Port:9020
- Connector AJP Port:9029
修改標(biāo)識(shí):
echo "test1"> /data/apache-tomcat-test1/webapps/ROOT/index.jsp
echo "test2"> /data/apache-tomcat-test2/webapps/ROOT/index.jsp
修改完成,接著啟動(dòng)tomcat:
/data/apache-tomcat-test1/bin/tomcat-start.sh
/data/apache-tomcat-test2/bin/tomcat-start.sh
curl的時(shí)候是比較慢的,因?yàn)檫€沒啟動(dòng)完成。啟動(dòng)完之后就好了。
curl 127.0.0.1:9010
curl 127.0.0.1:9020
這里注意訪問端口是Connector HTTP Port
對(duì)應(yīng)的端口
4、后記
其實(shí)搭起來不太難,經(jīng)過參考文章的思路,就是公用一個(gè)tomcat的lib和bin,這樣子升級(jí)的時(shí)候替換lib即可,bin都同一用CATALINA_HOME的腳本,其實(shí)CATALINA_HOME下的bin下的start和shutdown腳本統(tǒng)一調(diào)用了catalina.sh,而單機(jī)多實(shí)例的則是巧妙運(yùn)用了catalina.sh是通過環(huán)境中的CATALINA_HOME和CATALINA_BASE變量啟動(dòng)tomcat的
,通過改變CATALINA_BASE的路徑達(dá)到同一條腳本啟動(dòng)tomcat在不同目錄下
。
5、問題
5.1、tomcat啟動(dòng)慢
可以看到因?yàn)樯蓅ession ID用了1分06秒,太久了,導(dǎo)致應(yīng)用deploying整體的時(shí)間大大增加,這個(gè)問題是可以解決的,可以再啟動(dòng)的時(shí)候增加jvm參數(shù)-Djava.security.egd=file:/dev/./urandom
,但是這樣做應(yīng)用產(chǎn)生隨機(jī)數(shù)的能力被減弱,或者說隨機(jī)不夠均勻,對(duì)于經(jīng)常使用加密的應(yīng)用可能有安全影響。
修改一下啟動(dòng)腳本,添加了JAVA_OPTS的設(shè)置,能使用JAVA_OPTS,是因?yàn)閏atalina.sh會(huì)讀出這個(gè)變量。
#!/bin/bash
##這里的CATALINA_BASE,是當(dāng)前腳本的的父目錄,如果不在CATALINA_BASE的子目錄的話,記得修改
export CATALINA_BASE=$(cd $(dirname $0); cd .. ; pwd)
echo $CATALINA_BASE
TOMCAT_ID=`ps aux |grep "java"|grep "Dcatalina.base=$CATALINA_BASE "|grep -v "grep"|awk '{ print $2}'`
export JAVA_OPTS="-Djava.security.egd=file:/dev/./urandom";
if [ -n "$TOMCAT_ID" ] ; then
echo "tomcat($TOMCAT_ID) still running now , please shutdown it first";
exit 2;
fi
TOMCAT_START_LOG=`$CATALINA_HOME/bin/startup.sh`
if [ "$?" = "0" ]; then
echo "shell script: $0"
echo "CATALINA_BASE: $CATALINA_BASE"
echo "tomcat thread: $TOMCAT_ID"
echo "start succeed!!!"
else
echo "$0 $CATALINA_BASE start failed"
echo $TOMCAT_START_LOG
fi
5.2、遠(yuǎn)程ssh時(shí),環(huán)境變量不生效問題
使用source命令刷新當(dāng)前環(huán)境變量。
具體修改如下:
tomcat-start.sh :
#!/bin/bash
##這里的CATALINA_BASE,是當(dāng)前腳本的的父目錄,如果不在CATALINA_BASE的子目錄的話,記得修政
source /etc/profile
export CATALINA_BASE=$(cd $(dirname $0); cd .. ; pwd)
export JAVA_OPTS="-Djava.security.egd=file:/dev/./urandom";
TOMCAT_ID=`ps aux |grep "java"|grep "Dcatalina.base=$CATALINA_BASE "|grep -v "grep"|awk '{ print $2}'`
echo "----------------------------------"
echo "Using CATALINA_BASE:$CATALINA_BASE"
echo "Using CATALINA_HOME:$CATALINA_HOME"
echo "----------------------------------"
if [ -n "$TOMCAT_ID" ] ; then
echo "tomcat($TOMCAT_ID) still running now , please shutdown it first";
exit 2;
fi
TOMCAT_START_LOG=`$CATALINA_HOME/bin/startup.sh`
if [ "$?" = "0" ]; then
echo "shell script: $0"
echo "tomcat thread: $TOMCAT_ID"
echo "start succeed!!!"
else
echo "$0 $CATALINA_BASE start failed"
echo "CATALINA_BASE: $CATALINA_BASE"
echo $TOMCAT_START_LOG
fi
tomcat-stop.sh
#!/bin/bash
## 這里的CATALINA_BASE,是當(dāng)前腳本的的父目錄,如果不在CATALINA_BASE的子目錄的話,記得修政
source /etc/profile
export CATALINA_BASE=$(cd $(dirname $0); cd .. ; pwd)
TOMCAT_ID=`ps aux |grep "java"|grep "[D]catalina.base=$CATALINA_BASE "|awk '{ print $2}'`
echo "----------------------------------"
echo "Using CATALINA_BASE:$CATALINA_BASE"
echo "Using CATALINA_HOME:$CATALINA_HOME"
echo "----------------------------------"
if [ -n "$TOMCAT_ID" ] ; then
TOMCAT_STOP_LOG=`$CATALINA_HOME/bin/shutdown.sh`
else
echo "Tomcat instance not found : $CATALINA_BASE"
exit
fi
if [ "$?" = "0" ]; then
echo "shell script: $0"
echo "stop succeed!!!"
else
echo "$0 $CATALINA_BASE stop failed"
echo "CATALINA_BASE: $CATALINA_BASE"
echo $TOMCAT_STOP_LOG
fi
附上restart腳本:
tomcat-restart.sh :
#!/bin/bash
source /etc/profile
export CATALINA_BASE=$(cd $(dirname $0); cd .. ; pwd)
GREEN_COLOR='\E[1;32m' #綠
RES='\E[0m'
TOMCAT_ID=`ps aux |grep "java"|grep "Dcatalina.base=$CATALINA_BASE "|grep -v "grep"|awk '{ print $2}'`
SLEEP_TIME=1
echo "----------------------------------"
echo "Using CATALINA_BASE:$CATALINA_BASE"
echo "Using CATALINA_HOME:$CATALINA_HOME"
echo "----------------------------------"
if [ -n "$TOMCAT_ID" ] ; then
echo -e "${GREEN_COLOR}found tomcat instance in pid $TOMCAT_ID , shutdown now!${RES}";
echo -e "${GREEN_COLOR}---------------start shutdown-------------------${RES}"
source $(dirname $0)/tomcat-stop.sh;
echo -e "${GREEN_COLOR}--------------- end shutdown -------------------${RES}"
fi
while [ -n "$TOMCAT_ID" ]
do
sleep $SLEEP_TIME
echo wait "$SLEEP_TIME" s
TOMCAT_ID=`ps aux |grep "java"|grep "Dcatalina.base=$CATALINA_BASE "|grep -v "grep"|awk '{ print $2}'`
done
echo -e "${GREEN_COLOR}---------------start startup-------------------${RES}"
source $(dirname $0)/tomcat-start.sh
echo -e "${GREEN_COLOR}---------------end startup-------------------${RES}"
參考博客:
聊聊 Tomcat 的單機(jī)多實(shí)例
Spring Boot應(yīng)用首次啟動(dòng)慢的問題
到此這篇關(guān)于tomcat單機(jī)多實(shí)例的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)tomcat單機(jī)多實(shí)例內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!