Tomcat7 Clustering using Apache HTTP server & Microsoft IIS server
1. Creating multiple tomcat instances and configuring it to enable clustering
Download tomcat 7 server from the link below.
Unzip the folder and name it as Tomcat1, create an exact copy of the folder and name it as tomcat2.
Now edit the server.xml file present in the conf directory of both tomcat instances to enable clustering. Server shutdown port, HTTP 1.1 connector port, AJP 1.3 connector port and jvmRoute values should be distinct in each tomcat instance.
Configurations for tomcat 1
Configuring different attributes
HTTP 1.1 connector port
AJP 1.3 connector port
Now the most important step is to uncomment cluster tag in server.xml files of both the tomcat instances. And configure it with clustering options. A sample configuration is given below.
This cluster element should be inside engine or host element. Now clustering is enabled between both the tomcat instances.
<Cluster className="org.<span id="vnlmx9_5" class="vnlmx9"><span id="irmdol_5" class="irmdol">apache</span></span>.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="8"> <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="188.8.131.52" port="45564" frequency="500" dropTime="3000"/> <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" address="auto" port="4000" autoBind="100" selectorTimeout="5000" 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"/> </Channel> <Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=""/> <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/> <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.JvmRouteSessionIDBinderListener"/> <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/> </Cluster> ## Reference : http://tomcat.apache.org/tomcat-7.0-doc/cluster-howto.html
Now deploy the application in both the tomcat instances’ webapps directory. And for clustering to work, <distributable /> tag should be inserted inside web.xml of the application.
Configurations for tomcat instances are over. Now we can move to configurations of webservers for load balancing and managing requests to tomcat instances.
2. Configurations for Apache HTTP Server
We will use mod_jk for load balancing. For this first download mod_jk.so file and place it in the modules folder under Apache home directory. We can either get the native source file which can be compiled to create the .so file or directly use a precompiled mod_jk.so file. Please download this file from net. Now create two files namely, mod-jk.conf and workers.properties inside the conf folder of apache home directory.
Now include the line highlighted in the picture to httpd.conf file
We can also set the server root and listener port attributes of the server. If we copy the server folder from installed location to a new location, change the server root attribute to new location. Similarly, by default the server listener port is 80. If port 80 is already being used by another process we can change this port to another port, say 8080, in the httpd.conf file.
1) mod-jk.conf file
Here the last line JKMount /examples/TestApp/*.jsp loadbalancer indicates that request to any jsp file in the path /examples/TestApp of tomcat instance’s webapps directory will be load balanced by the worker ‘loadbalancer’. This load balancer worker is defined in the worker.properties file.
2) Workers.properties file
Here loadbalancer is the worker that balances load between two tomcat instances. Node1 is the worker corresponding to tomcat1 and node2 is the worker corresponding to tomcat2. This should match with the value we set for jvmRoute attribute set in server.xml of corresponding tomcat instance and also the worker ports should match with that of ajp connector port in server .xml. Enabling sticky session ensures that once a request is being served by a tomcat instance the subsequent requests from same session are forwarded to the same tomcat instance until there is a server failure. Now we have successfully configured the settings for tomcat clustering and load balancing with apache http server.
3. Configurations for IIS 7 server
In IIS we use isapi_redirect.dll file for connecting to tomcat instances. A working version of this can be downloaded from the following url.
First we need to configure the jk 1.2 Connector which will allow IIS to effectively act as a proxy and forward requests on to Tomcat. For this we require isapi_redirect.dll file and isapi_redirect.properties file. The isapi_redirect.properties file tells the connector where to find its configuration files and also where the isapi_redirect.dll file is located. The folder structure I used is given.
isapi_redirect.dll and isapi_redirect.properties files are kept in ISAPI folder. Logs folder is for keeping log files. The workers.properties and uniworkermap.properties files are kept in conf folder.
1) isapi_redirect.properties file
2) workers.properties file
Here wlb is the load balancer worker and node1 and node2 corresponds to tomcat workers. The configurations are same as in the case of apache Http server.
3) uriworkermap.properties file
Now we need to configure IIS. Go to run and type inetmgr or directly access IIS manager. For this you need to have admin right on the machine. The start page looks like as shown below.
Now we will create a new site for our application. For this right click on the ‘Site’ in the left pane. And select Add Web site.
The window will appear as shown. Select different fields as applicable.
Now create a virtual directory by right clicking on the site name and enter the fields.
Here the Alias name should match with the extension_uri line of our isapi_redirect.properties file.
Next we need to configure the virtual directory to have execute permission. For this click on the jakarta virtual directory on the left pane and then double-click Handler Mappings as shown below.
Within the Handler Mappings feature click Edit Feature Permissions in the Actions Pane.
Check ‘Execute’ in the Edit Feature Permissions dialog box and click OK. Now the isapi dll files will be enabled in handler mappings feature.
Next we need to add an ISAPI filter on the website. To do this click on the website and double click ISAPI Filters feature.
In the Actions pane click Add. In the Add ISAPI Filter dialog box enter a name and the path to the isapi_redirect.dll file and click OK.
The final step is to configure the ISAPI and CGI Restrictions feature in IIS. Go to the Server Home and then double click on the ISAPI and CGI Restrictions feature.
In the ISAPI and CGI Restrictions feature click Add on the Actions pane. In the Add ISAPI or CGI Restriction dialog box enter a name and the path to the isapi_redirect.dll file, tick the Allow extension path to execute option and click OK.
The configurations are completed now and now we need to test the functionality.
4. Testing the functionality
I have created a counter.jsp file as shown below.
Here counter is a session variable and it is set to 1 when a session starts. For each subsequent visit the counter value increases for the single session and is again reset to 1 when a new session begins. Session Id and creation time is also displayed. The first line indicates which tomcat server serves the request. Now create a copy of this jsp and Change the first line to
‘Tomcat 2’. Create a folder named TestApp inside the “webapps/examples” folder in both the tomcat instances. And paste the corresponding jsp file inside this folders.
Now start the webserver(either Apache or IIS) and both tomcat instances.
Run http://localhost:8081/examples/TestApp/counter.jsp in the browser. It will show the request served by first tomcat instance.
On refreshing the page the counter will increase and it will be shown as an existing session.
Next open a new browser and check http://localhost:8082/examples/TestApp/counter.jsp. This will show the request served by second tomcat instance.
Now open new browser and run http://localhost/examples/TestApp/counter.jsp. This request is handled by the webserver and it redirects the request to any of the tomcat instance.
Let the request be now served by Tomcat 1.
Now shut down Tomcat 1. And run the above url, now the request will be redirected to Tomcat 2 and the session will be retained.
Have a look at my other hubs
- Cloud Computing : Properties & Overview
- Different Types of Network Attacks And Security Threats and Counter Measures
- Network Architectures : Layers of OSI model and TCP/IP model
- Various Plants used for Generation of Electric Power : Hydroelectric, Nuclear and Thermal Energy Pla
- Steps In PCB Fabrication Process : PCB Manufacturing technique