Postat la
02-Feb-2009
ora
09:45 am
de
[Chetroesu]
De cele mai multe ori se pune problema instalarii mai multor aplicatii Web pe aceiasi masina pe servere Tomcat. In cazul in care masina respectiva este una Windows intervin cateva probleme de configurare. Daca am instala toate aplicatiile web pe acelasi server ar fi simplu dar, aceasta abordare presupune eforturi in perioada de mentenanta. De exemplu, daca dorim sa facem update la una din aplicatii, presupune sa oprim tomcat-ul. Acest lucru presupune oprirea automata si a celorlalte aplicatii. Acum intervine intrebarea "nu se pot aplicatiile opri individual"? Se poate si acest lucru, dar la un moment dat, dupa mai multe operatiuni de acest fel acesta isi va unfla cash-ul suficient de mult incat sa nu mai poata functiona.
Solutia cea mai eleganta pentru rezolvarea acestei probleme este instalarea fiecarei aplicatii web pe o instanta diferita de Tomcat. Cum se poate face acest lucru? Am incercat sa folosesc chitul de instalare al Tomcat-ului, dar dupa toate eforturile mele nu am reusit 2 instalari care sa mearga independent. Am recurs la varianta utilizarii unei distributii de Tomcat care nu necesita instalare: apache-tomcat-5.5.20.zip. Acesta se dezarhiveaza in locatia unde vrem practic sa il instalam si asta e tot.
Pornirea si oprirea efectiva a tomcatului se poate face prin utilizarea comenzilor startup.bat si shutdown.bat localizate in folderul /bin din calea de instalare. Pentru o aplicatie web care sa functioneze in regim de productie, nu de dezvoltare va fi insa nevoie sa cream un serviciu windows care sa ruleze in permanenta. Comanda cu care se poate crea a un serviciu tomcat este urmatoarea:
service.bat -install nume_serviciu.
Sa detaliem putin pasii de instalare a 2 servicii diferite de Tomcat pe aceiasi masina Windows:
1. Se extrage continutul arhivei cu distributia de tomcat in 2 foldere diferite. Sa presupunem ca avem C:\Tomcat5.5_1\ si C:\Tomcat5.5_2\.
2. Se configureaza in fisierele server.xml ale celor doua tomcaturi cu porturile necesare rularii astfel (nu o sa explic ce inseamna fiecare dintre ele):
Server |
Shutdown |
AJP |
http |
def. |
8005 |
8009 |
8081 |
App1 |
8015 |
8019 |
8081 |
App2 |
8025 |
8029 |
8082 |
3. Se configureaza service.bat astfel incat sa creeze corect serviciul corespunzator fiecari instante de Tomcat. Practic va trebui schimbat modul in care se vor seta variabilele de environment utilizate:
Fisierul in forma originala:
@echo off
if "%OS%" == "Windows_NT" setlocal
rem ---------------------------------------------------------------------------
rem NT Service Install/Uninstall script
rem
rem Options
rem install Install the service using Tomcat5 as service name.
rem Service is installed using default settings.
rem remove Remove the service from the System.
rem
rem name (optional) If the second argument is present it is considered
rem to be new service name
rem
rem $Id: service.bat 414655 2006-06-15 18:56:42Z yoavs $
rem ---------------------------------------------------------------------------
rem Guess CATALINA_HOME if not defined
set CURRENT_DIR=%cd%
if not "%CATALINA_HOME%" == "" goto gotHome
set CATALINA_HOME=%cd%
if exist "%CATALINA_HOME%\bin\tomcat5.exe" goto okHome
rem CD to the upper dir
cd ..
set CATALINA_HOME=%cd%
:gotHome
if exist "%CATALINA_HOME%\bin\tomcat5.exe" goto okHome
echo The tomcat.exe was not found...
echo The CATALINA_HOME environment variable is not defined correctly.
echo This environment variable is needed to run this program
goto end
rem Make sure prerequisite environment variables are set
if not "%JAVA_HOME%" == "" goto okHome
echo The JAVA_HOME environment variable is not defined
echo This environment variable is needed to run this program
goto end
:okHome
if not "%CATALINA_BASE%" == "" goto gotBase
set CATALINA_BASE=%CATALINA_HOME%
:gotBase
set EXECUTABLE=%CATALINA_HOME%\bin\tomcat5.exe
rem Set default Service name
set SERVICE_NAME=Tomcat5
set PR_DISPLAYNAME=Apache Tomcat
if "%1" == "" goto displayUsage
if "%2" == "" goto setServiceName
set SERVICE_NAME=%2
set PR_DISPLAYNAME=Apache Tomcat %2
:setServiceName
if %1 == install goto doInstall
if %1 == remove goto doRemove
if %1 == uninstall goto doRemove
echo Unknown parameter "%1"
:displayUsage
echo.
echo Usage: service.bat install/remove [service_name]
goto end
:doRemove
rem Remove the service
"%EXECUTABLE%" //DS//%SERVICE_NAME%
echo The service '%SERVICE_NAME%' has been removed
goto end
:doInstall
rem Install the service
echo Installing the service '%SERVICE_NAME%' ...
echo Using CATALINA_HOME: %CATALINA_HOME%
echo Using CATALINA_BASE: %CATALINA_BASE%
echo Using JAVA_HOME: %JAVA_HOME%
rem Use the environment variables as an example
rem Each command line option is prefixed with PR_
set PR_DESCRIPTION=Apache Tomcat Server - http://tomcat.apache.org
set PR_INSTALL=%EXECUTABLE%
set PR_LOGPATH=%CATALINA_BASE%\logs
set PR_CLASSPATH=%CATALINA_HOME%\bin\bootstrap.jar
rem Set the server jvm from JAVA_HOME
set PR_JVM=%JAVA_HOME%\jre\bin\server\jvm.dll
if exist "%PR_JVM%" goto foundJvm
rem Set the client jvm from JAVA_HOME
set PR_JVM=%JAVA_HOME%\jre\bin\client\jvm.dll
if exist "%PR_JVM%" goto foundJvm
rem Check for JRockit JVM: Bugzilla 39674
set PR_JVM=%JAVA_HOME%\jre\bin\jrockit\jvm.dll
if exist "%PR_JVM%" goto foundJvm
set PR_JVM=auto
:foundJvm
echo Using JVM: %PR_JVM%
"%EXECUTABLE%" //IS//%SERVICE_NAME% --StartClass org.apache.catalina.startup.Bootstrap --StopClass org.apache.catalina.startup.Bootstrap --StartParams start --StopParams stop
if not errorlevel 1 goto installed
echo Failed installing '%SERVICE_NAME%' service
goto end
:installed
rem Clear the environment variables. They are not needed any more.
set PR_DISPLAYNAME=
set PR_DESCRIPTION=
set PR_INSTALL=
set PR_LOGPATH=
set PR_CLASSPATH=
set PR_JVM=
rem Set extra parameters
"%EXECUTABLE%" //US//%SERVICE_NAME% --JvmOptions "-Dcatalina.base=%CATALINA_BASE%;-Dcatalina.home=%CATALINA_HOME%;-Djava.endorsed.dirs=%CATALINA_HOME%\common\endorsed" --StartMode jvm --StopMode jvm
rem More extra parameters
set PR_LOGPATH=%CATALINA_BASE%\logs
set PR_STDOUTPUT=auto
set PR_STDERROR=auto
"%EXECUTABLE%" //US//%SERVICE_NAME% ++JvmOptions "-Djava.io.tmpdir=%CATALINA_BASE%\temp" --JvmMs 128 --JvmMx 256
echo The service '%SERVICE_NAME%' has been installed.
:end
cd %CURRENT_DIR%
Fisierul in forma noua, continand configurarile relative la variabilele de environment. Practic se specifica directorul de home pentru instanta de Tomcat pe care o configuram Nu ramane decat sa observati diferentele din cele 2 fisiere:
@echo off
if "%OS%" == "Windows_NT" setlocal
rem ---------------------------------------------------------------------------
rem NT Service Install/Uninstall script
rem
rem Options
rem install Install the service using Tomcat5 as service name.
rem Service is installed using default settings.
rem remove Remove the service from the System.
rem
rem name (optional) If the second argument is present it is considered
rem to be new service name
rem
rem $Id: service.bat 414655 2006-06-15 18:56:42Z yoavs $
rem ---------------------------------------------------------------------------
set CATALINA_HOME=C:\Tomcat5.5_1
set CATALINA_BASE=%CATALINA_HOME%
set EXECUTABLE=%CATALINA_HOME%\bin\tomcat5.exe
set SERVICE_NAME=%2
set PR_DISPLAYNAME=Apache Tomcat %2
if %1 == install goto doInstall
if %1 == remove goto doRemove
if %1 == uninstall goto doRemove
:doRemove
rem Remove the service
"%EXECUTABLE%" //DS//%SERVICE_NAME%
echo The service '%SERVICE_NAME%' has been removed
goto end
:doInstall
rem Install the service
echo Installing the service '%SERVICE_NAME%' ...
echo Using CATALINA_HOME: %CATALINA_HOME%
echo Using CATALINA_BASE: %CATALINA_BASE%
echo Using JAVA_HOME: %JAVA_HOME%
rem Use the environment variables as an example
rem Each command line option is prefixed with PR_
set PR_DESCRIPTION=Apache Tomcat Server - http://tomcat.apache.org
set PR_INSTALL=%EXECUTABLE%
set PR_LOGPATH=%CATALINA_BASE%\logs
set PR_CLASSPATH=%CATALINA_HOME%\bin\bootstrap.jar
rem Set the server jvm from JAVA_HOME
set PR_JVM=%JAVA_HOME%\jre\bin\server\jvm.dll
if exist "%PR_JVM%" goto foundJvm
rem Set the client jvm from JAVA_HOME
set PR_JVM=%JAVA_HOME%\jre\bin\client\jvm.dll
if exist "%PR_JVM%" goto foundJvm
rem Check for JRockit JVM: Bugzilla 39674
set PR_JVM=%JAVA_HOME%\jre\bin\jrockit\jvm.dll
if exist "%PR_JVM%" goto foundJvm
set PR_JVM=auto
:foundJvm
echo Using JVM: %PR_JVM%
"%EXECUTABLE%" //IS//%SERVICE_NAME% --StartClass org.apache.catalina.startup.Bootstrap --StopClass org.apache.catalina.startup.Bootstrap --StartParams start --StopParams stop
if not errorlevel 1 goto installed
echo Failed installing '%SERVICE_NAME%' service
goto end
:installed
rem Clear the environment variables. They are not needed any more.
set PR_DISPLAYNAME=
set PR_DESCRIPTION=
set PR_INSTALL=
set PR_LOGPATH=
set PR_CLASSPATH=
set PR_JVM=
rem Set extra parameters
"%EXECUTABLE%" //US//%SERVICE_NAME% --JvmOptions "-Dcatalina.base=%CATALINA_BASE%;-Dcatalina.home=%CATALINA_HOME%;-Djava.endorsed.dirs=%CATALINA_HOME%\common\endorsed" --StartMode jvm --StopMode jvm
rem More extra parameters
set PR_LOGPATH=%CATALINA_BASE%\logs
set PR_STDOUTPUT=auto
set PR_STDERROR=auto
"%EXECUTABLE%" //US//%SERVICE_NAME% ++JvmOptions "-Djava.io.tmpdir=%CATALINA_BASE%\temp" --JvmMs 128 --JvmMx 256
echo The service '%SERVICE_NAME%' has been installed.
:end
cd %CURRENT_DIR%