1、前言
首先要回答一個問題,為什么要用單機多實例?
在不宕機的情況下,webapps里面存在多個項目,可能由于其中一個項目過度使用內(nèi)存或者其他不確定的因素使得tomcat掛了,那么同一tomcat下的項目也會一同掛了;而使用不同的tomcat,同一臺服務(wù)器下,每個tomcat的進程是不一樣的額,一個項目出現(xiàn)問題tomcat掛了,那么由于是在不同進程,其他項目不會影響的。
還有一個問題就是不同tomcat使用了不同端口,最后域名只有一個怎么分配?
其實這個使用nginx的反向代理,根據(jù)請求的前綴,代理到相應(yīng)的tomcat項目服務(wù)端口對應(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,點擊tar.gz帶彈出下載連接復制下載地址。
然后使用命令下載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
建一個程序目錄:
##創(chuàng)建一個程序目錄
mkdir /data
##移動解壓文件到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,是當前腳本的的父目錄,如果不在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,是當前腳本的的父目錄,如果不在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
復制兩個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 -
##復制啟動和停止腳本到bin文件夾,帶權(quán)限復制
cp -p tomcat-start.sh tomcat-stop.sh /data/apache-tomcat-test1/bin/
##一個同樣的tomcat目錄,帶權(quán)限復制
cp -Rp /data/apache-tomcat-test1/ /data/apache-tomcat-test2/
配置 server.xml 端口
你知道的,同一個服務(wù)器部署不同 Tomcat 要設(shè)置不同的端口,不然會報端口沖突,所以我們只需要修改conf/server.xml中的其中前三個端口就行了。但它有四個分別是:
- Server Port:該端口用于監(jiān)聽關(guān)閉tomcat的shutdown命令,默認為8005
- Connector HTTP Port:該端口用于監(jiān)聽HTTP的請求,默認為8080
- Connector AJP Port:該端口用于監(jiān)聽AJP( Apache JServ Protocol )協(xié)議上的請求,通常用于整合Apache Server等其他HTTP服務(wù)器,默認為8009
- Redirect Port:重定向端口,出現(xiàn)在Connector配置中,如果該Connector僅支持非SSL的普通http請求,那么該端口會把 https 的請求轉(zhuǎn)發(fā)到這個Redirect Port指定的端口,默認為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
修改標識:
echo "test1"> /data/apache-tomcat-test1/webapps/ROOT/index.jsp
echo "test2"> /data/apache-tomcat-test2/webapps/ROOT/index.jsp
修改完成,接著啟動tomcat:
/data/apache-tomcat-test1/bin/tomcat-start.sh
/data/apache-tomcat-test2/bin/tomcat-start.sh
curl的時候是比較慢的,因為還沒啟動完成。啟動完之后就好了。
curl 127.0.0.1:9010
curl 127.0.0.1:9020
這里注意訪問端口是Connector HTTP Port
對應(yīng)的端口
4、后記
其實搭起來不太難,經(jīng)過參考文章的思路,就是公用一個tomcat的lib和bin,這樣子升級的時候替換lib即可,bin都同一用CATALINA_HOME的腳本,其實CATALINA_HOME下的bin下的start和shutdown腳本統(tǒng)一調(diào)用了catalina.sh,而單機多實例的則是巧妙運用了catalina.sh是通過環(huán)境中的CATALINA_HOME和CATALINA_BASE變量啟動tomcat的
,通過改變CATALINA_BASE的路徑達到同一條腳本啟動tomcat在不同目錄下
。
5、問題
5.1、tomcat啟動慢
可以看到因為生成session ID用了1分06秒,太久了,導致應(yīng)用deploying整體的時間大大增加,這個問題是可以解決的,可以再啟動的時候增加jvm參數(shù)-Djava.security.egd=file:/dev/./urandom
,但是這樣做應(yīng)用產(chǎn)生隨機數(shù)的能力被減弱,或者說隨機不夠均勻,對于經(jīng)常使用加密的應(yīng)用可能有安全影響。
修改一下啟動腳本,添加了JAVA_OPTS的設(shè)置,能使用JAVA_OPTS,是因為catalina.sh會讀出這個變量。
#!/bin/bash
##這里的CATALINA_BASE,是當前腳本的的父目錄,如果不在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、遠程ssh時,環(huán)境變量不生效問題
使用source命令刷新當前環(huán)境變量。
具體修改如下:
tomcat-start.sh :
#!/bin/bash
##這里的CATALINA_BASE,是當前腳本的的父目錄,如果不在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,是當前腳本的的父目錄,如果不在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 的單機多實例
Spring Boot應(yīng)用首次啟動慢的問題
到此這篇關(guān)于tomcat單機多實例的實現(xiàn)的文章就介紹到這了,更多相關(guān)tomcat單機多實例內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!