1. Introduction First of all, we need to answer a question: why use multiple instances on a single machine? 2. System environment System: 16.04.5 LTS 3. Environment Construction 3.1、Download Tomcat Install jdk: apt-get install openjdk-8-jdk Visit the official website: https://tomcat.apache.org/download-90.cgi Find core, click tar.gz and copy the download address when a download link pops up. wget http://mirrors.hust.edu.cn/apache/tomcat/tomcat-9/v9.0.13/bin/apache-tomcat-9.0.13.tar.gz Unzip tomcat: tar -xzvf apache-tomcat-9.0.13.tar.gz Create a program directory: ##Create a program directory mkdir /data ##Move the unzipped file to the data directory 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 ##Create sh file touch tomcat-start.sh tomcat-stop.sh ##Change permissions chmod 760 /data/tomcat-start.sh /data/tomcat-stop.sh Back up the profile, write tomcat's CATALINA_HOME to the environment variable, and activate the environment variable. create tomcat-start.sh, the content is as follows: #!/bin/bash ##CATALINA_BASE here is the parent directory of the current script. If it is not in the subdirectory of CATALINA_BASE, remember to modify 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 create tomcat-stop.sh, the content is as follows: #!/bin/bash ##CATALINA_BASE here is the parent directory of the current script. If it is not in the subdirectory of CATALINA_BASE, remember to modify 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 Copy two tomcats: ##Complete deleting the contents of the lib and bin folders and generating an empty bin folder cp -r apache-tomcat-9.0.13 /data/apache-tomcat-test1 && cd /data/apache-tomcat-test1 && rm -rf lib/ bin/ && mkdir bin && cd - ##Copy the start and stop scripts to the bin folder, copy with permissions cp -p tomcat-start.sh tomcat-stop.sh /data/apache-tomcat-test1/bin/ ##Copy the same tomcat directory with permissions cp -Rp /data/apache-tomcat-test1/ /data/apache-tomcat-test2/ Configure server.xml port As you know, different Tomcats deployed on the same server must set different ports, otherwise there will be port conflicts, so we only need to modify the first three ports in conf/server.xml. But there are four of them:
Uncommented version: <?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 is changed to:
tomcat-test2 is changed to:
Modify the logo: echo "test1"> /data/apache-tomcat-test1/webapps/ROOT/index.jsp echo "test2"> /data/apache-tomcat-test2/webapps/ROOT/index.jsp After the modification is completed, start tomcat: /data/apache-tomcat-test1/bin/tomcat-start.sh /data/apache-tomcat-test2/bin/tomcat-start.sh The curl process is relatively slow because it has not yet completed. After starting it up it's fine. curl 127.0.0.1:9010 curl 127.0.0.1:9020 Note that the access port here is the port corresponding to 4. Postscript In fact, it is not difficult to set up. According to the idea of the reference article, it is to share a tomcat lib and bin. In this way, you only need to replace the lib when upgrading, and the bin uses the same script of CATALINA_HOME. In fact, the start and shutdown scripts under the bin under CATALINA_HOME uniformly call catalina.sh, and the single-machine multi-instance system 5. Question 5.1. Tomcat starts slowly It can be seen that because it takes 1 minute and 6 seconds to generate the session ID, which is too long, the overall time of application deployment is greatly increased. This problem can be solved by adding the jvm parameter #!/bin/bash ##CATALINA_BASE here is the parent directory of the current script. If it is not in the subdirectory of CATALINA_BASE, remember to modify 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. When using remote ssh, the environment variables do not take effect Use the source command to refresh the current environment variables. The specific modifications are as follows: #!/bin/bash ##CATALINA_BASE here is the parent directory of the current script. If it is not in the subdirectory of CATALINA_BASE, remember to modify the 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
#!/bin/bash ## CATALINA_BASE here is the parent directory of the current script. If it is not in the subdirectory of CATALINA_BASE, remember to modify the 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 Attach the restart script: #!/bin/bash source /etc/profile export CATALINA_BASE=$(cd $(dirname $0); cd .. ; pwd) GREEN_COLOR='\E[1;32m' # Green 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}" Reference blog: Let's talk about Tomcat's single machine multiple instances Spring Boot application slow startup for the first time This is the end of this article about the implementation of multiple instances of tomcat on a single machine. For more relevant content about multiple instances of tomcat on a single machine, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: js to achieve image fade-in and fade-out effect
>>: Why MySQL should avoid large transactions and how to solve them
Table of contents 1. Introduction to FastDFS 1. I...
Table of contents 1. Number in JavaScript 2. Math...
Solution to mysql not closing: Right-click on the...
The color presentation on a web page will be affec...
1. Write the Dockerfile (1) Right-click the proje...
1. After installing the Windows version of Docker...
Table of contents 1. What are options? 2. What at...
Table of contents Enter the topic mysql add, dele...
I downloaded and installed the latest version of ...
This article explains how to install MySQL from a...
Windows installation mysql-5.7.17-winx64.zip meth...
1. Make a repo file Refer to the official install...
1.1 Introduction By enabling the slow query log, ...
Table of contents 1. beforeCreate & created 2...
Effective solution for Ubuntu in virtual machine ...