tomcat的单机多实例启动
部署目的
单机多实例便于升级管理,充分利用系统资源
单机多实例部署的本质
1、对于启动脚本
启动Tomcat时,版本包中提供的启动脚本$CATALINA_HOME/bin/startup.sh就是调用了$CATALINA_HOME/bin/catalina.sh,并且catalina.sh是通过环境变量中的$CATALINA_HOME和$CATALINA_BASE来启动tomcat的
2、对于库文件
Tomcat启动依赖的库文件是相同的,包括$CATALINA_HOME/bin目录下实例化catalina的bootstrap.jar和lib目录下的jar文件等,多个实例版本更新的时候直接替换lib就可以了。
综上,通过修改$CATALINA_BASE就可以启动多实例了。
3、CATALINA_BASE与CATALINA_HOME的区别
# CATALINA_HOME May point at your Catalina "build" directory. # # CATALINA_BASE (Optional) Base directory for resolving dynamic portions # of a Catalina installation. If not present, resolves to # the same directory that CATALINA_HOME points to.
CATALINA_BASE是工作目录(存放工程项目的),CATALINA_HOME是家目录(存放可执行文件的),是不同的
当CATALINA_BASE不存在的时候会使用 CATALINA_HOME 配置的路径
安装过程
部署单实例可以参考: https://www.cnblogs.com/zh-dream/p/13511041.html
配置第二个实例
$ mkdir instance2 $ cp -r apache-tomcat-8.5.6 instance2/inst_sec $ cd instance2/inst_sec $ rm -rf bin/* lib/
启动脚本
$ vim bin/start.sh #!/bin/bash export CATALINA_BASE=`cd $(dirname $0)/../; pwd` tomcat_Pid=`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_Pid" ] ; then echo "tomcat($tomcat_Pid) still running now , please shutdown it first"; exit 2; fi star_Prg=`$CATALINA_HOME/bin/startup.sh` if [ "$?" = "0" ]; then echo "shell script: $0" echo "CATALINA_BASE: $CATALINA_BASE" echo "tomcat thread: $tomcat_Pid" echo "start succeed!!!" else echo "$0 $CATALINA_BASE start failed" echo $star_Prg fi
停止脚本
[test@tomcat inst_sec]$ vim bin/stop.sh #!/bin/bash export CATALINA_BASE=`cd $(dirname $0)/../; pwd` tomcat_Pid=`ps aux |grep "java"|grep "[D]catalina.base=$CATALINA_BASE "|awk '{ print $2}'`
if [ -n "$tomcat_Pid" ] ; then stop_Prg=`$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 $stop_Prg fi
重启脚本
[test@tomcat inst_sec]$ cat bin/restart.sh #!/bin/bash source ~/.bash_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)/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)/start.sh echo -e "${GREEN_COLOR}---------------end startup-------------------${RES}"
接下来,修改server.xml中不同协议对应的端口后启动服务即可
posted on 2021-06-03 15:35 hopeless-dream 阅读(184) 评论(0) 编辑 收藏 举报