Apache Tomcat Load Balancing

By ukmodak | March 31st 2024 10:34:01 AM | viewed 22 times

Load Balancing Tomcat

Tools::

  • Download Apache http server "httpd-2.4.41-win64-VS16.zip" and install
  • Download Apache Tomcat "apache-tomcat-8.5.39.exe"
  • Download Tomcat connector(mod_jk) "tomcat-connectors-1.2.40-windows-x86_64-httpd-2.4.x.zip"

Step1:

Install apache tomcat on various port like s1,s2,s3,s4,s5 on the following port series

  • Shutdown port:7xxx
  • HTTP connector port:8xxx
  • AJP port:9xxx

Add the following code for heap stack size in the the location:tomcat/bin/setenv.bat

default jvm option as follows

-ea
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=%APP_LOGS_DIR%\
-XX:ErrorFile=%APP_LOGS_DIR%%FILE_SEPARATOR%\hs_err_pid%%\p.log
-Dfile.encoding=UTF-8
*Djava.awt.headless=true
-XX:MaxPermSize=150m
-XX:MaxMetaspaceSize=150m
-Xmx600m
-Dorg.eclipse.jetty.server.Request.maxFormContentSize=5000000
-Dorg.eclipse.jetty.server.Request.maxFormKeys=10000
set "JAVA_OPTS=%JAVA_OPTS% -Xms512m -Xmx8192m -XX:MaxPermSize=256m -server"
Or
set "JAVA_OPTS=%JAVA_OPTS% -XX:MaxPermSize=128m  -XX:MaxNewSize=256m -Xms768m -Xmx8000m -XX:SurvivorRatio=128 -XX:MaxTenuringThreshold=0"
Or
set "JAVA_OPTS=%JAVA_OPTS% -Xms1024m -Xmx8192m -XX:MaxPermSize=512m -server"
or for tomcat 8 VVI
set "JAVA_OPTS=%JAVA_OPTS% -Xms1024m -Xmx8192m -XX:MetaspaceSize=512m -server"
or
set "JAVA_OPTS=-Djava.awt.headless=true -server -Xms512m -Xmx2048m -XX:PermSize=512m -XX:MaxPermSize=1024m -XX:+CMSClassUnloadingEnabled -XX:+CMSPermGenSweepingEnabled"

Or add the following parameter in the location: tomcat/bin>Tomcats1w.exe

  • Initial Memory pool:512 MB
  • Maximum Memory pool:8000 MB
  • Tread Stack size:512 MB

Add the following code for increase tomcat cache size and lower the cacheTtl value by something lower than 5000 milliseconds in the the location:tomcat/conf/context.xml

<Context>
    <Resources cachingAllowed="true" cacheMaxSize="51200" />
	<Resources cacheTtl="2000" />
</Context>

add project.war in the location:apache-tomcat-9.0.85\webapps\

go to location .../bin>catalina.bat run for each server and check with each connection port:like "localhost:8xxx/project"

Step2:

Extract "httpd-2.4.41-win64-VS16.zip" and copy "Apache24" in the location:c:\

Go to location:c:\Apache24\conf\ and edit "httpd.conf" and change port:80 to whatever you like

Go to location:c:\Apache24\bin>httpd.exe -k install

Go to location:c:\Apache24\bin>httpd.exe -k uninstall

If message will come as service can not staty then go to search:service ->Apache24 -> start

Step3:

Extract "tomcat-connectors-1.2.40-windows-x86_64-httpd-2.4.x.zip" and copy "mod_jk.so" and past in the location:c:\Apache24\modules\

Step4:

Go to location:c:\Apache24\conf\ and edit "httpd.conf" and write the following code at the bottom as bellow:

LoadModule jk_module modules/mod_jk.so
JkWorkersFile  conf/workers.properties

JkLogFile      logs/mod_jk.log
JkLogLevel     info

#or
LoadModule jk_module modules/mod_jk.so
JkWorkersFile  conf/workers.properties

JkLogFile     logs/mod_jk.log
JkLogLevel    emerg
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
JkOptions     +ForwardKeySize +ForwardURICompat -ForwardDirectories
JkRequestLogFormat     "%w %V %T"

#end or


JkMount        /oms* worker1  
JkMount        /oms* worker2  
JkMount        /oms* worker3

#or

JkMount        /* balancer          // JkMount  /oms* balancer
JkMount        /status stat

# end or

Note:or option is best

Step5:

Create a file workers.properties in the location:c:\Apache24\conf\ and edit "workers.properties" and write the following code at the top as bellow:

worker.list=worker1,worker2,worker3  

#or  
worker.list=balancer,stat
#end or

worker.worker1.port=9000
worker.worker1.host=192.168.66.21
worker.worker1.type=ajp13
worker.worker1.lbfactor=10

worker.worker2.port=9100
worker.worker2.host=192.168.66.21
worker.worker2.type=ajp13
worker.worker2.lbfactor=10

worker.worker3.port=9200
worker.worker3.host=192.168.66.21
worker.worker3.type=ajp13
worker.worker3.lbfactor=10

#or
worker.balancer.type=lb
worker.balancer.balance_workers=worker1,worker2,worker3
worker.stat.type=status
#end or

Note:or option is best

Step6:

To Create session affinity to each tomcat location:tomcat1/conf/ and edit "server.xml" and add the following code

 <Engine name="Catalina" defaultHost="localhost" jvmPath="worker1">

go to search:service ->Apache24 -> start/restart

Type in the browser as:192.168.66.21:8084 and 192.168.66.21:8084/oms and 192.168.66.21:8084/

Type in the browser as:192.168.66.21:8084/status

go to search:service ->tomcat -> start/stop and check 192.168.66.21:8084/status

Step7:

To view active session of each tomcat to go location:tomcat1/conf/ and edit "tomcat-users.xml" and add the following code

<role rolename="tomcat"/>
<role rolename="role1"/>
<user username="tomcat" password="tomcat" roles="tomcat"/>
<user username="both" password="tomcat" roles="tomcat,role1"/>
<user username="admin" password="admin" roles="manager-gui"/>

To view active session of each tomcat to go location:tomcat1\webapps\manager\META-INF and edit "context.xml" and add the following code

<Context antiResourceLocking="false" privileged="true" >
  
  <Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>
</Context>

Type in the browser as:192.168.66.21:8000/manaer and user:admin pass:admin

bONEandALL
Visitor

Total : 27273

Today :9

Today Visit Country :

  • United States
  • Sweden
  • Singapore
  • Russia