对于现在server的配置,内存4G,动辄几十G内存,完全可以一机多tomcat完全利用起来。一机多tomcat需要修改tomcat配置文件几处地方。
1.tomcat环境变量
环境变量中自定义多tomcat主目录
vi /etc/profile
- export CATALINA_1_HOME=/usr/local/tomcat1
- export TOMCAT_1_HOME=/usr/local/tomcat1
- export CATALINA_2_HOME=/usr/local/tomcat2
- export TOMCAT_2_HOME=/usr/local/tomcat2
- export CATALINA_3_HOME=/usr/local/tomcat3
- export TOMCAT_3_HOME=/usr/local/tomcat3
2.tomcat配置文件server.xml端口JVM虚拟机名称
每个tomcat的8005 8080 8009 8443端口不能一样,比如:
tomcat1为:
8005改为8501
8080改为8801
8009改为8901
8443改为8401
tomcat2为:
8005改为8502
8080改为8802
8009改为8902
8443改为8402
tomcat3为:
8005改为8503
8080改为8803
8009改为8903
8443改为8403
修改JVM名称:
找到每个tomcat的<Engine name="Catalina" defaultHost="localhost">配置分别修改为修改为(每个tomcat的jvm名称不能一致):
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat3">
3.修改tomcat启动、停止脚本
vi /etc/init.d/tomcat1
- #############################################
- export JAVA_HOME=/usr/java/default
- export CATALINA_HOME=$CATALINA_1_HOME
- case "$1" in
- 'start')
- /usr/local/tomcat1/bin/startup.sh
- touch /var/lock/subsys/tomcat1
- ;;
- 'stop')
- /usr/local/tomcat1/bin/shutdown.sh
- rm -f /var/lock/subsys/tomcat1
- ;;
- 'restart')
- $0 stop
- $0 start
- ;;
- *)
- echo "Usage: tomcat1 { start | stop | restart }"
- exit 1
- esac
- exit 0
- ###############################################
vi /etc/init.d/tomcat2
- #############################################
- export JAVA_HOME=/usr/java/default
- export CATALINA_HOME=$CATALINA_2_HOME
- case "$1" in
- 'start')
- /usr/local/tomcat2/bin/startup.sh
- touch /var/lock/subsys/tomcat2
- ;;
- 'stop')
- /usr/local/tomcat2/bin/shutdown.sh
- rm -f /var/lock/subsys/tomcat2
- ;;
- 'restart')
- $0 stop
- $0 start
- ;;
- *)
- echo "Usage: tomcat2 { start | stop | restart }"
- exit 1
- esac
- exit 0
- ###############################################
vi /etc/init.d/tomcat3
- #############################################
- export JAVA_HOME=/usr/java/default
- export CATALINA_HOME=$CATALINA_3_HOME
- case "$1" in
- 'start')
- /usr/local/tomcat3/bin/startup.sh
- touch /var/lock/subsys/tomcat3
- ;;
- 'stop')
- /usr/local/tomcat3/bin/shutdown.sh
- rm -f /var/lock/subsys/tomcat3
- ;;
- 'restart')
- $0 stop
- $0 start
- ;;
- *)
- echo "Usage: tomcat3 { start | stop | restart }"
- exit 1
- esac
- exit 0
- ###############################################
配置完成后需要给/etc/init.d/目录中的tomcat1 tomcat2 tomcat3文件添加执行权限。
- chmod u+x /etc/init.d/tomcat1
- chmod u+x /etc/init.d/tomcat2
- chmod u+x /etc/init.d/tomcat3
然后启动tomcat使用以下命令就可以启动了:
- service tomcat1 start
- service tomcat2 start
- service tomcat3 start
停止tomcat的命令与启动命令基本相同,start改为tomcat就OK了。