0. 说明
将多个Java EE项目部署到一个Tomcat下可能会造成许多不良后果,比如
- 多个项目不容易排查问题,尤其是调试的时候
- 一旦一个项目需要重启Tomcat,其他项目也会受影响
可以用部署Tomcat多个实例的方法来解决上述问题。
本文只介绍在Windows7系统下Tomcat7多实例部署的步骤,参考来自Tomcat根目录下的RUNNING
文件。
1. Multiple Instances Summary
Tomcat supports installation of multiple instances. You can have a single installation of Tomcat with multiple instances running on different IP/port combinations, or multiple Tomcat versions, each running one or more instances on different IP/ports.
2. Each instance folder will need the following structure:
- conf
- logs
- temp
- webapps
- work
At a minimum, conf should contain a copy of the following files from CATALINA_HOME\conf. Any files not copied and edited, will be picked up by default from CATALINA_HOME\conf, i.e. CATALINA_BASE\conf files override defaults from CATALINA_HOME\conf.
- server.xml
- web.xml
3. Modify the configuration of each instance
You must edit CATALINA_BASE\conf\server.xml
to specify a unique IP/port for the instance to listen on. Find the line that contains <Connector port="8080" ...
and add an address attribute and/or update the port number so as to specify a unique IP/port combination.
4. Install first instance
To install an instance, first set the CATALINA_HOME
environment variable to the name of the Tomcat installation directory. Then create a second environment variable CATALINA_BASE
and point this to the instance folder. Then run service.bat install
command specifying a service name.
set "CATALINA_HOME=c:\tomcat_7"
set "CATALINA_BASE=c:\tomcat_7\instances\instance1"
service.bat install instance1
To modify the service settings, you can run tomcat7w //ES//instance1-service
.
5. Add an instance
For additional instances, create additional instance folder, update the CATALINA_BASE
environment variable, and run the service install again.
set CATALINA_BASE=c:\tomcat_7\instances\instance2
service install instance2
6. Write the commands above in a bat file can be
@echo off
set "CATALINA_HOME=c:\tomcat_7"
set "SERVICE_INSTALLER=%CATALINA_HOME\bin\service.bat"
rem instance1
set "CATALINA_BASE=c:\tomcat_7\instances\instance1"
call "SERVICE_INSTALLER" install instance1-service
rem instance2
set "CATALINA_BASE=c:\tomcat_7\instances\instance2"
rem java configuration because of memeory leak
set "JAVA_OPTS=-Xms1024m -Xmx2000m"
call "SERVICE_INSTALLER" install instance2-service
rem instance3
set "CATALINA_BASE=c:\tomcat_7\instances\instance3"
call "SERVICE_INSTALLER" install instance3-service