April 2024
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930  

Categories

April 2024
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930  

Tomcat 8 clustering and Load Balancing Redhat Fedora and Centos

Drawing1
Let us build 3 node Tomcat 8 cluster  with Apache

web Server  – > 192.168.2.40

cluster2 -> 192.168.1.41
cluster3 -> 192.168.1.42

Web server is  CLUSTER1

The characteristics of this cluster are:

Session affinity: sessions are associated with single servers.
Failover: if a server dies, a connection will be directed to the nearest available server. (NOTE: sessions are not replicated)
Failback: when a server comes back online, it will rejoin the cluster.
Weighted load balancing: the load balancing can be controlled to take into account machine differences

rpm -ivh jdk-7u51-linux-i586.rpm

alternatives –install /usr/bin/java java /usr/java/jdk1.7.0_51/bin/java 2

alternatives –config java

java -version

mkdir /usr/tomcat

wget  http://mirror.nus.edu.sg/apache/tomcat/tomcat-8/v8.0.5/bin/apache-tomcat-8.0.5.tar.gz

[root@cluster1 tomcat]# ls
apache-tomcat-8.0.5
[root@cluster1 tomcat]# cd apache-tomcat-8.0.5/
[root@cluster1 apache-tomcat-8.0.5]# ls
bin  conf  lib  LICENSE  logs  NOTICE  RELEASE-NOTES  RUNNING.txt  temp  webapps  work
[root@cluster1 apache-tomcat-8.0.5]# cd bin/
[root@cluster1 bin]# ls
bootstrap.jar       commons-daemon.jar            daemon.sh         setclasspath.sh  startup.sh            tool-wrapper.sh
catalina.bat        commons-daemon-native.tar.gz  digest.bat        shutdown.bat     tomcat-juli.jar       version.bat
catalina.sh         configtest.bat                digest.sh         shutdown.sh      tomcat-native.tar.gz  version.sh
catalina-tasks.xml  configtest.sh                 setclasspath.bat  startup.bat      tool-wrapper.bat
[root@cluster1 bin]# ./startup.sh
Using CATALINA_BASE:   /usr/tomcat/apache-tomcat-8.0.5
Using CATALINA_HOME:   /usr/tomcat/apache-tomcat-8.0.5
Using CATALINA_TMPDIR: /usr/tomcat/apache-tomcat-8.0.5/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/tomcat/apache-tomcat-8.0.5/bin/bootstrap.jar:/usr/tomcat/apache-tomcat-8.0.5/bin/tomcat-juli.jar
Tomcat started.
[root@cluster1 bin]# ps -ef | grep java
root      1437     1 18 04:58 pts/2    00:00:01 /usr/bin/java -Djava.util.logging.config.file=/usr/tomcat/apache-tomcat-8.0.5/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/usr/tomcat/apache-tomcat-8.0.5/endorsed -classpath /usr/tomcat/apache-tomcat-8.0.5/bin/bootstrap.jar:/usr/tomcat/apache-tomcat-8.0.5/bin/tomcat-juli.jar -Dcatalina.base=/usr/tomcat/apache-tomcat-8.0.5 -Dcatalina.home=/usr/tomcat/apache-tomcat-8.0.5 -Djava.io.tmpdir=/usr/tomcat/apache-tomcat-8.0.5/temp org.apache.catalina.startup.Bootstrap start
root      1459  1319  0 04:58 pts/2    00:00:00 grep java
[root@cluster1 bin]#

[root@cluster2 software]# mv apache-tomcat-8.0.5 /usr/tomcat/
[root@cluster2 software]# ls
apache-tomcat-8.0.5-src.tar.gz  apache-tomcat-8.0.5.tar.gz  jdk-7u51-linux-i586.rpm
[root@cluster2 software]# cd /usr/tomcat/
[root@cluster2 tomcat]# ls
apache-tomcat-8.0.5
[root@cluster2 tomcat]# cd apache-tomcat-8.0.5/
[root@cluster2 apache-tomcat-8.0.5]# ls
bin  conf  lib  LICENSE  logs  NOTICE  RELEASE-NOTES  RUNNING.txt  temp  webapps  work
[root@cluster2 apache-tomcat-8.0.5]# cd bin/
[root@cluster2 bin]# ls
bootstrap.jar       commons-daemon.jar            daemon.sh         setclasspath.sh  startup.sh            tool-wrapper.sh
catalina.bat        commons-daemon-native.tar.gz  digest.bat        shutdown.bat     tomcat-juli.jar       version.bat
catalina.sh         configtest.bat                digest.sh         shutdown.sh      tomcat-native.tar.gz  version.sh
catalina-tasks.xml  configtest.sh                 setclasspath.bat  startup.bat      tool-wrapper.bat
[root@cluster2 bin]# ./startup.sh
Using CATALINA_BASE:   /usr/tomcat/apache-tomcat-8.0.5
Using CATALINA_HOME:   /usr/tomcat/apache-tomcat-8.0.5
Using CATALINA_TMPDIR: /usr/tomcat/apache-tomcat-8.0.5/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/tomcat/apache-tomcat-8.0.5/bin/bootstrap.jar:/usr/tomcat/apache-tomcat-8.0.5/bin/tomcat-juli.jar
Tomcat started.

[root@cluster2 bin]# ls
bootstrap.jar       commons-daemon.jar            daemon.sh         setclasspath.sh  startup.sh            tool-wrapper.sh
catalina.bat        commons-daemon-native.tar.gz  digest.bat        shutdown.bat     tomcat-juli.jar       version.bat
catalina.sh         configtest.bat                digest.sh         shutdown.sh      tomcat-native.tar.gz  version.sh
catalina-tasks.xml  configtest.sh                 setclasspath.bat  startup.bat      tool-wrapper.bat

[root@cluster2 bin]# ./version.sh
Using CATALINA_BASE:   /usr/tomcat/apache-tomcat-8.0.5
Using CATALINA_HOME:   /usr/tomcat/apache-tomcat-8.0.5
Using CATALINA_TMPDIR: /usr/tomcat/apache-tomcat-8.0.5/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/tomcat/apache-tomcat-8.0.5/bin/bootstrap.jar:/usr/tomcat/apache-tomcat-8.0.5/bin/tomcat-juli.jar
Server version: Apache Tomcat/8.0.5
Server built:   Mar 24 2014 05:29:50
Server number:  8.0.5.0
OS Name:        Linux
OS Version:     2.6.32-431.5.1.el6.i686
Architecture:   i386
JVM Version:    1.7.0_51-b13
JVM Vendor:     Oracle Corporation
[root@cluster2 bin]# ps -ef |  grep java
root      1740     1  7 21:22 pts/0    00:00:01 /usr/bin/java -Djava.util.logging.config.file=/usr/tomcat/apache-tomcat-8.0.5/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/usr/tomcat/apache-tomcat-8.0.5/endorsed -classpath /usr/tomcat/apache-tomcat-8.0.5/bin/bootstrap.jar:/usr/tomcat/apache-tomcat-8.0.5/bin/tomcat-juli.jar -Dcatalina.base=/usr/tomcat/apache-tomcat-8.0.5 -Dcatalina.home=/usr/tomcat/apache-tomcat-8.0.5 -Djava.io.tmpdir=/usr/tomcat/apache-tomcat-8.0.5/temp org.apache.catalina.startup.Bootstrap start
root      1786  1315  0 21:23 pts/0    00:00:00 grep java

[root@cluster2 bin]#
tmp/    tomcat/
[root@cluster3 software]# mv apache-tomcat-8.0.5 /usr/tomcat/
[root@cluster3 software]# cd /usr/tomcat/
[root@cluster3 tomcat]# ls
apache-tomcat-8.0.5
[root@cluster3 tomcat]# cd apache-tomcat-8.0.5/
[root@cluster3 apache-tomcat-8.0.5]# ls
bin  conf  lib  LICENSE  logs  NOTICE  RELEASE-NOTES  RUNNING.txt  temp  webapps  work
[root@cluster3 apache-tomcat-8.0.5]# cd bin/
[root@cluster3 bin]# ls
bootstrap.jar       commons-daemon.jar            daemon.sh         setclasspath.sh  startup.sh            tool-wrapper.sh
catalina.bat        commons-daemon-native.tar.gz  digest.bat        shutdown.bat     tomcat-juli.jar       version.bat
catalina.sh         configtest.bat                digest.sh         shutdown.sh      tomcat-native.tar.gz  version.sh
catalina-tasks.xml  configtest.sh                 setclasspath.bat  startup.bat      tool-wrapper.bat
[root@cluster3 bin]# ./startup.sh
Using CATALINA_BASE:   /usr/tomcat/apache-tomcat-8.0.5
Using CATALINA_HOME:   /usr/tomcat/apache-tomcat-8.0.5
Using CATALINA_TMPDIR: /usr/tomcat/apache-tomcat-8.0.5/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/tomcat/apache-tomcat-8.0.5/bin/bootstrap.jar:/usr/tomcat/apache-tomcat-8.0.5/bin/tomcat-juli.jar
Tomcat started.

[root@cluster2 conf]# cp server.xml server.xml.org
[root@cluster2 conf]# pwd
/usr/tomcat/apache-tomcat-8.0.5/conf
[root@cluster2 conf]#

Web Server install on the server

[root@cluster1 ~]# yum install httpd*
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: centos.mirror.secureax.com
* epel: ftp.jaist.ac.jp
* extras: kartolo.sby.datautama.net.id
* updates: mirror.smartmedia.net.id
Setting up Install Process
Resolving Dependencies
–> Running transaction check
—> Package httpd.i686 0:2.2.15-29.el6.centos will be updated
—> Package httpd.i686 0:2.2.15-30.el6.centos will be an update
—> Package httpd-devel.i686 0:2.2.15-30.el6.centos will be installed
–> Processing Dependency: apr-util-devel for package: httpd-devel-2.2.15-30.el6.centos.i686
–> Processing Dependency: apr-devel for package: httpd-devel-2.2.15-30.el6.centos.i686
—> Package httpd-itk.i686 0:2.2.22-6.el6 will be installed
—> Package httpd-manual.noarch 0:2.2.15-30.el6.centos will be installed
—> Package httpd-tools.i686 0:2.2.15-29.el6.centos will be updated
—> Package httpd-tools.i686 0:2.2.15-30.el6.centos will be an update
–> Running transaction check
—> Package apr-devel.i686 0:1.3.9-5.el6_2 will be installed
—> Package apr-util-devel.i686 0:1.3.9-3.el6_0.1 will be installed
–> Processing Dependency: openldap-devel for package: apr-util-devel-1.3.9-3.el6_0.1.i686
–> Processing Dependency: expat-devel for package: apr-util-devel-1.3.9-3.el6_0.1.i686
–> Processing Dependency: db4-devel for package: apr-util-devel-1.3.9-3.el6_0.1.i686
–> Running transaction check
—> Package db4-devel.i686 0:4.7.25-18.el6_4 will be installed
–> Processing Dependency: db4-cxx = 4.7.25-18.el6_4 for package: db4-devel-4.7.25-18.el6_4.i686
–> Processing Dependency: libdb_cxx-4.7.so for package: db4-devel-4.7.25-18.el6_4.i686
—> Package expat-devel.i686 0:2.0.1-11.el6_2 will be installed
—> Package openldap-devel.i686 0:2.4.23-34.el6_5.1 will be installed
–> Processing Dependency: cyrus-sasl-devel >= 2.1 for package: openldap-devel-2.4.23-34.el6_5.1.i686
–> Running transaction check
—> Package cyrus-sasl-devel.i686 0:2.1.23-13.el6_3.1 will be installed
—> Package db4-cxx.i686 0:4.7.25-18.el6_4 will be installed
–> Finished Dependency Resolution

Dependencies Resolved

=========================================================================================================================================
Package                             Arch                      Version                                  Repository                  Size
=========================================================================================================================================
Installing:
httpd-devel                         i686                      2.2.15-30.el6.centos                     updates                    150 k
httpd-itk                           i686                      2.2.22-6.el6                             epel                       140 k
httpd-manual                        noarch                    2.2.15-30.el6.centos                     updates                    784 k
Updating:
httpd                               i686                      2.2.15-30.el6.centos                     updates                    828 k
httpd-tools                         i686                      2.2.15-30.el6.centos                     updates                     74 k
Installing for dependencies:
apr-devel                           i686                      1.3.9-5.el6_2                            base                       176 k
apr-util-devel                      i686                      1.3.9-3.el6_0.1                          base                        69 k
cyrus-sasl-devel                    i686                      2.1.23-13.el6_3.1                        base                       303 k
db4-cxx                             i686                      4.7.25-18.el6_4                          base                       605 k
db4-devel                           i686                      4.7.25-18.el6_4                          base                       6.6 M
expat-devel                         i686                      2.0.1-11.el6_2                           base                       121 k
openldap-devel                      i686                      2.4.23-34.el6_5.1                        updates                    1.1 M

Transaction Summary
=========================================================================================================================================
Install      10 Package(s)
Upgrade       2 Package(s)

Total download size: 11 M
Is this ok [y/N]:
Downloading Packages:
(1/12): apr-devel-1.3.9-5.el6_2.i686.rpm                                                                          | 176 kB     00:00
(2/12): apr-util-devel-1.3.9-3.el6_0.1.i686.rpm                                                                   |  69 kB     00:00
(3/12): cyrus-sasl-devel-2.1.23-13.el6_3.1.i686.rpm                                                               | 303 kB     00:00
(4/12): db4-cxx-4.7.25-18.el6_4.i686.rpm                                                                          | 605 kB     00:00
(5/12): db4-devel-4.7.25-18.el6_4.i686.rpm                                                                        | 6.6 MB     00:12
(6/12): expat-devel-2.0.1-11.el6_2.i686.rpm                                                                       | 121 kB     00:00
(7/12): httpd-2.2.15-30.el6.centos.i686.rpm                                                                       | 828 kB     00:06
(8/12): httpd-devel-2.2.15-30.el6.centos.i686.rpm                                                                 | 150 kB     00:00
(9/12): httpd-itk-2.2.22-6.el6.i686.rpm                                                                           | 140 kB     00:00
(10/12): httpd-manual-2.2.15-30.el6.centos.noarch.rpm                                                             | 784 kB     00:03
(11/12): httpd-tools-2.2.15-30.el6.centos.i686.rpm                                                                |  74 kB     00:00
(12/12): openldap-devel-2.4.23-34.el6_5.1.i686.rpm                                                                | 1.1 MB     00:05
—————————————————————————————————————————————–
Total                                                                                                    331 kB/s |  11 MB     00:33
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : apr-devel-1.3.9-5.el6_2.i686                                                                                         1/14
Installing : expat-devel-2.0.1-11.el6_2.i686                                                                                      2/14
Installing : cyrus-sasl-devel-2.1.23-13.el6_3.1.i686                                                                              3/14
Installing : openldap-devel-2.4.23-34.el6_5.1.i686                                                                                4/14
Installing : db4-cxx-4.7.25-18.el6_4.i686                                                                                         5/14
Installing : db4-devel-4.7.25-18.el6_4.i686                                                                                       6/14
Installing : apr-util-devel-1.3.9-3.el6_0.1.i686                                                                                  7/14
Updating   : httpd-tools-2.2.15-30.el6.centos.i686                                                                                8/14
Updating   : httpd-2.2.15-30.el6.centos.i686                                                                                      9/14
Installing : httpd-devel-2.2.15-30.el6.centos.i686                                                                               10/14
Installing : httpd-manual-2.2.15-30.el6.centos.noarch                                                                            11/14
Installing : httpd-itk-2.2.22-6.el6.i686                                                                                         12/14
Cleanup    : httpd-2.2.15-29.el6.centos.i686                                                                                     13/14
Cleanup    : httpd-tools-2.2.15-29.el6.centos.i686                                                                               14/14
Verifying  : apr-util-devel-1.3.9-3.el6_0.1.i686                                                                                  1/14
Verifying  : openldap-devel-2.4.23-34.el6_5.1.i686                                                                                2/14
Verifying  : httpd-2.2.15-30.el6.centos.i686                                                                                      3/14
Verifying  : httpd-tools-2.2.15-30.el6.centos.i686                                                                                4/14
Verifying  : httpd-itk-2.2.22-6.el6.i686                                                                                          5/14
Verifying  : httpd-devel-2.2.15-30.el6.centos.i686                                                                                6/14
Verifying  : db4-devel-4.7.25-18.el6_4.i686                                                                                       7/14
Verifying  : db4-cxx-4.7.25-18.el6_4.i686                                                                                         8/14
Verifying  : expat-devel-2.0.1-11.el6_2.i686                                                                                      9/14
Verifying  : cyrus-sasl-devel-2.1.23-13.el6_3.1.i686                                                                             10/14
Verifying  : apr-devel-1.3.9-5.el6_2.i686                                                                                        11/14
Verifying  : httpd-manual-2.2.15-30.el6.centos.noarch                                                                            12/14
Verifying  : httpd-2.2.15-29.el6.centos.i686                                                                                     13/14
Verifying  : httpd-tools-2.2.15-29.el6.centos.i686                                                                               14/14

Installed:
httpd-devel.i686 0:2.2.15-30.el6.centos        httpd-itk.i686 0:2.2.22-6.el6        httpd-manual.noarch 0:2.2.15-30.el6.centos

Dependency Installed:
apr-devel.i686 0:1.3.9-5.el6_2               apr-util-devel.i686 0:1.3.9-3.el6_0.1      cyrus-sasl-devel.i686 0:2.1.23-13.el6_3.1
db4-cxx.i686 0:4.7.25-18.el6_4               db4-devel.i686 0:4.7.25-18.el6_4           expat-devel.i686 0:2.0.1-11.el6_2
openldap-devel.i686 0:2.4.23-34.el6_5.1

Updated:
httpd.i686 0:2.2.15-30.el6.centos                                httpd-tools.i686 0:2.2.15-30.el6.centos

Install MOD_JK

[root@cluster1 native]# yum groupinstall ‘Development Tools’

wget http://www.apache.org/dist/tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.40-src.tar.gz
tar -zxvf tomcat-connectors-1.2.40-src.tar.gz
cd tomcat-connectors-1.2.40-src
cd /root/software/tomcat-connectors-1.2.40-src/native
./buildconf.sh

./configure –with-apxs=/usr/sbin/apxs
make
libtool –finish /usr/lib/httpd/modules/
make install

httpd-jk.conf

<VirtualHost *:80>
ServerName cluster1.rmohan.com
JkMount /* lb
RewriteEngine On
</VirtualHost>

LoadModule jk_module modules/mod_jk.so

<IfModule jk_module>

# We need a workers file exactly once
# and in the global server
JkWorkersFile conf.d/workers.properties

# Our JK error log
# You can (and should) use rotatelogs here
JkLogFile logs/mod_jk.log

# Our JK log level (trace,debug,info,warn,error)
JkLogLevel info

# Our JK shared memory file
JkShmFile logs/mod_jk.shm

# Define a new log format you can use in any CustomLog in order
# to add mod_jk specific information to your access log.
# LogFormat “%h %l %u %t \”%r\” %>s %b \”%{Referer}i\” \”%{User-Agent}i\” \”%{Cookie}i\” \”%{Set-Cookie}o\” %{pid}P %{tid}P %{JK_LB_FIRST_NAME}n %{JK_LB_LAST_NAME}n ACC %{JK_LB_LAST_ACCESSED}n ERR %{JK_LB_LAST_ERRORS}n BSY %{JK_LB_LAST_BUSY}n %{JK_LB_LAST_STATE}n %D” extended_jk

# This option will reject all requests, which contain an
# encoded percent sign (%25) or backslash (%5C) in the URL
# If you are sure, that your webapp doesn’t use such
# URLs, enable the option to prevent double encoding attacks.
# Since: 1.2.24
# JkOptions +RejectUnsafeURI

# After setting JkStripSession to “On”, mod_jk will
# strip all “;jsessionid=…” from request URLs it
# does *not* forward to a backend.
# This is useful, if all links in a webapp use
# URLencoded session IDs and parts of the static
# content should be delivered directly by Apache.
# Of course you can also do it with mod_rewrite.
# Since: 1.2.21
# JkStripSession On

# Start a separate thread for internal tasks like
# idle connection probing, connection pool resizing
# and load value decay.
# Run these tasks every JkWatchdogInterval seconds.
# Since: 1.2.27
JkWatchdogInterval 60

# Configure access to jk-status and jk-manager
# If you want to make this available in a virtual host,
# either move this block into the virtual host
# or copy it logically there by including “JkMountCopy On”
# in the virtual host.
# Add an appropriate authentication method here!
<Location /jk-status>
# Inside Location we can omit the URL in JkMount
JkMount jk-status
Order deny,allow
Deny from all
Allow from 127.0.0.1
</Location>
<Location /jk-manager>
# Inside Location we can omit the URL in JkMount
JkMount jk-manager
Order deny,allow
Deny from all
Allow from 127.0.0.1
</Location>

# If you want to put all mounts into an external file
# that gets reloaded automatically after changes
# (with a default latency of 1 minute),
# you can define the name of the file here.
# JkMountFile conf/extra/uriworkermap.properties

# Example for Mounting a context to the worker “balancer”
# The URL syntax “a|b” instantiates two mounts at once,
# the first one is “a”, the second one is “ab”.
# JkMount /myapp|/* balancer

# Example for UnMounting requests for all workers
# using a simple URL pattern
# Since: 1.2.26
# JkUnMount /myapp/static/* *

# Example for UnMounting requests for a named worker
# JkUnMount /myapp/images/* balancer

# Example for UnMounting requests using regexps
# SetEnvIf REQUEST_URI “\.(htm|html|css|gif|jpg|js)$” no-jk

# Example for setting a reply timeout depending on the request URL
# Since: 1.2.27
# SetEnvIf Request_URI “/transactions/” JK_REPLY_TIMEOUT=600000

# Example for disabling reply timeouts for certain request URLs
# Since: 1.2.27
# SetEnvIf Request_URI “/reports/” JK_REPLY_TIMEOUT=0

# IMPORTANT: Mounts and virtual hosts
# If you are using VirtualHost elements, you
# – can put mounts only used in some virtual host into its VirtualHost element
# – can copy all global mounts to it using “JkMountCopy On” inside the VirtualHost
# – can copy all global mounts to all virtual hosts by putting
#   “JkMountCopy All” into the global server
# Since: 1.2.26

</IfModule>

uriworkermap.properties

# The ASF licenses this file to You under the Apache License, Version 2.0
# (the “License”); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an “AS IS” BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# uriworkermap.properties – IIS
#
# This file provides sample mappings for example wlb
# worker defined in workermap.properties.minimal
# The general syntax for this file is:
# [URL]=[Worker name]

/ClusterWebApp/*=lb
/admin/*=lb
/manager/*=lb
/jsp-examples/*=lb
/servlets-examples/*=lb
/examples/*=lb

# Optionally filter out all .jpeg files inside that context
# For no mapping the url has to start with exclamation (!)

!/servlets-examples/*.jpeg=lb

#
# Mount jkstatus to /jkmanager
# For production servers you will need to
# secure the access to the /jkmanager url
#
/jk-manager=jk-status

[root@cluster1 conf.d]# cat workers.properties
worker.list=lb,jk-status

worker.node1.type=ajp13
worker.node1.host=192.168.1.41
worker.node1.port=8009
worker.node1.lbfactor=1

worker.node2.type=ajp13
worker.node2.host=192.168.1.42
worker.node2.port=8009
worker.node2.lbfactor=1

worker.lb.type=lb
worker.lb.balance_workers=node1, node2
worker.lb.method=Busyness

worker.jk-status.type=status

Deploy the war to test the loadbalancing

mkdir test

vi test.jsp

<html>
<body>
<%@ page import=”java.net.InetAddress” %>
<h1><font color=”red”>Session serviced by NODE_02</font></h1>
<table align=”center” border=”1″>
<tr>
<td>
Session ID
</td>
<td>
<%= session.getId() %></td>
</td>
<% session.setAttribute(“abc”,”abc”);%>
</tr>
<tr>
<td>
Created on
</td>
<td>
<%= session.getCreationTime() %>
</td>
</tr>
<tr>
<td>
Hostname:
</td>
<td>
<%
InetAddress ia = InetAddress.getLocalHost();
out.println(ia.getHostName());
%>
</td>
</tr>
</table>
</body>
</html>

jar cvf test.war .

cp test.war  /usr/tomcat/apache-tomcat-8.0.5/webapps

wget https://github.com/jaysensharma/MiddlewareMagicDemos/blob/master/ClusterTest_WebApp/ClusterWebApp.war?raw=true

mv ClusterWebApp.war?raw=true   ClusterWebApp.war

Need to the clustering Part on the tomcat server

route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0

Add in server.xml on both the tomcat server

<Cluster className=”org.apache.catalina.ha.tcp.SimpleTcpCluster”
channelSendOptions=”6″>

<Manager className=”org.apache.catalina.ha.session.BackupManager”
expireSessionsOnShutdown=”false”
notifyListenersOnReplication=”true”
mapSendOptions=”6″/>
<!–
<Manager className=”org.apache.catalina.ha.session.DeltaManager”
expireSessionsOnShutdown=”false”
notifyListenersOnReplication=”true”/>
–>
<Channel className=”org.apache.catalina.tribes.group.GroupChannel”>
<Membership className=”org.apache.catalina.tribes.membership.McastService”
address=”228.0.0.4″
port=”45564″
frequency=”500″
dropTime=”3000″/>
<Receiver className=”org.apache.catalina.tribes.transport.nio.NioReceiver”
address=”auto”
port=”5000″
selectorTimeout=”100″
maxThreads=”6″/>

<Sender className=”org.apache.catalina.tribes.transport.ReplicationTransmitter”>
<Transport className=”org.apache.catalina.tribes.transport.nio.PooledParallelSender”/>
</Sender>
<Interceptor className=”org.apache.catalina.tribes.group.interceptors.TcpFailureDetector”/>
<Interceptor className=”org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor”/>
<Interceptor className=”org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor”/>
</Channel>

<Valve className=”org.apache.catalina.ha.tcp.ReplicationValve”
filter=”.*\.gif|.*\.js|.*\.jpeg|.*\.jpg|.*\.png|.*\.htm|.*\.html|.*\.css|.*\.txt”/>

<Deployer className=”org.apache.catalina.ha.deploy.FarmWarDeployer”
tempDir=”/tmp/war-temp/”
deployDir=”/tmp/war-deploy/”
watchDir=”/tmp/war-listen/”
watchEnabled=”false”/>

<ClusterListener className=”org.apache.catalina.ha.session.ClusterSessionListener”/>
</Cluster>

After Addding restart the tomcat server

/usr/tomcat/apache-tomcat-8.0.5/bin/shutdown.sh
/usr/tomcat/apache-tomcat-8.0.5/bin/startup.sh

tail -f catalina.out

14-May-2014 02:24:35.084 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 2543 ms
14-May-2014 02:24:37.319 INFO [Tribes-Task-Receiver-1] org.apache.catalina.tribes.io.BufferPool.getBufferPool Created a buffer pool with max size:104857600 bytes of type: org.apache.catalina.tribes.io.BufferPool15Impl
14-May-2014 02:24:37.908 INFO [Membership-MemberAdded.] org.apache.catalina.ha.tcp.SimpleTcpCluster.memberAdded Replication member added:org.apache.catalina.tribes.membership.MemberImpl[tcp://{192, 168, 1, 42}:5000,{192, 168, 1, 42},5000, alive=1212, securePort=-1, UDP Port=-1, id={-67 107 6 86 -126 -125 75 75 -81 40 118 14 106 -83 -104 -17 }, payload={}, command={}, domain={}, ]
14-May-2014 02:24:39.183 INFO [Tribes-Task-Receiver-3] org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor.report ThroughputInterceptor Report[
Tx Msg:1 messages
Sent:0.00 MB (total)
Sent:0.00 MB (application)
Time:0.00 seconds
Tx Speed:0.14 MB/sec (total)
TxSpeed:0.14 MB/sec (application)
Error Msg:0
Rx Msg:2 messages
Rx Speed:0.00 MB/sec (since 1st msg)
Received:0.00 MB]

http://cluster1.rmohan.com/clusterjsp/HaJsp.jsp

Cluster – HA JSP Sample
HttpSession Information: •Served From Server: cluster1.rmohan.com
•Server Port Number: 80
•Executed From Server: cluster2.rmohan.com
•Executed Server IP Address: 192.168.1.41
•Session ID: 980075D2C3D0E9BC693C61CECFD43979.node2
•Session Created: Wed May 14 02:30:59 SGT 2014
•Last Accessed: Wed May 14 02:31:29 SGT 2014
•Session will go inactive in 1800 seconds

Enter session attribute data:

Name of Session Attribute:
Value of Sesion Attribute:

Data retrieved from the HttpSession:  •ada = ads
•2 = 2
•646 = 5656

INSTRUCTIONS •Add session data using the form. Upon pressing ADD SESSION DATA, the current session data will be listed.
•Click on RELOAD PAGE to display the current session data without adding new data.
•Click on CLEAR SESSION to invalidate the current session.

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>