Friday, April 13, 2012

Running Multiple Tomcat Instances

We first see the  tomcat directory structure. .

here each folder uses following purpose.

bin -  It contains all binary and script files for running tomcat.

lib - contains all shared libraries used for tomcat

conf - contains configuration information like which port tomcat can bind , etc...

logs - it contain all logging details

temp - this folder tomcat used for temporary files purpose

webapps - this folder is very important. here we put all application war files.

work - If application contain any jsp then jsp is translated and converted into servlet its stores here.

In when run the tomcat its uses 5 environment variables. They are 

in above list CATALINA_HOME and JAVA_HOME is mandatory environment variables. all others are optional and its can be calculated using CATALINA_HOME.

CATALINA_HOME - this environment variable should point to tomcat base folder, where tomcat binary are  installed/extracted. so based on CATALINA_HOME we can get bin and lib folder

CATALINA_BASE - If we not specified then CATALINA_HOME value is set. This variable pointed to configuration and webapps folder. Based on this variable server uses conf, logs, temp, webapps, work folders.

Usual ways to run the tomcat is only set CATALINA_HOME environment variable. and run the script file. this file automatically calculate and assign the values of other variables what we are not set. file set the environment variable and  then call file. this files is read CATALINA_BASE value and attach conf i.e $CATALINA_BASE/conf folder and get server.xml. this file is heart of tomcat. it contains all configuration information. like which tomcat uses as shoutdown port, connector post, host name, application folder ,.. for example usually tomcat uses 8080 is a connector port, so we can access http://localhost:8080/ 

if we set the $CATALINA_BASE explicitly then tomcat uses our variable to search and get the server.xml file from our target place, what we specified in CATALINA_BASE. 

this is trick to run multiple tomcat in single machine. we don't change CATALINA_HOME value. we need to change CATALINA_BASE value before start/shutdown the tomcat.

create one folder named "tomcat-instance1" anywhere, and copy conf, logs, temp, webapps, work folder from CATALINA_HOME folder and change conf/server.xml file in tomcat-instance1. we need to change 3 port shutdown port, connector port and ajp port.

shutdown port - this port is used for shutdown the tomcat. when we call the script they send signal to shutdown port. this port listen by tomcat java process. if signal is received the that process then its cleanup and exit by itself.

connector Port -This port is actual port to expose the application to outside client. 

ajp port - this port is used to apache httpd server  communicate to tomcat. this port used when we setup load balanced server.

see the sample server.xml file
.... ..

  1. <server port="8005" shutdown="SHUTDOWN">  
  2. .....  
  3.  <connector connectiontimeout="20000" port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol" redirectport="8443">  
  5. <connector port="8009" protocol="AJP/1.3" redirectport="8443">  
  6. </connector></connector></server>  
so we change these three port to different number, because once this port is binded  then other process can't bind it again. so wee bind different port. so tomcat-instance1/conf/server.xml file i configured server port =8105, connector port = 8181, ajp port = 8109.
  1. <server port="8105" shutdown="SHUTDOWN">  
  2. .....  
  3.  <connector connectiontimeout="20000" port="8181" protocol="org.apache.coyote.http11.Http11NioProtocol" redirectport="8443">  
  4. <connector port="8109" protocol="AJP/1.3" redirectport="8443">  
  5. </connector></connector></server>  

now we can create two script file for startup and shutdown the tomcat-instance1.
export CATALINA_BASE= /home/ramki/tomcat-instance1
export CATALINA_BASE= /home/ramki/tomcat-instance1

here we explicitly set the CATALINA_BASE variable and point to new tomcat-instance1
the we go to CATALINA_HOME/bin folder because all binary for running tomcat is still present in CATALINA_HOME folder then startup/shutdown the script.

Based on above technique we can create many instance folder and change conf/server.xml file port values and run that instance with own newly created script files.


No comments:

Post a Comment