Thursday, January 27, 2011

Tomcat Clustering and Distribution on Windows

Download tomcat5.5.zip from apache website instead of executable.
Steps to Make web pages + clustered and distributed.
1. Download tomcat 5.5
2. Make copy as TomcatA and TomcatB.
    Configure Tomcat A by setting the following in server.xml
         <Server port="8105" shutdown="SHUTDOWN">
        Define a non-SSL HTTP/1.1 Connector on port 8080
        -->
        <Connector port="8081" protocol="HTTP/1.1"
        connectionTimeout="20000"
        redirectPort="8443" />

         <!-- Define an AJP 1.3 Connector on port 8009 -->
        <Connector port="8109" protocol="AJP/1.3" redirectPort="8443" />

        <Engine name="Standalone" defaultHost="localhost" jvmRoute="jvm1">
        -->
        <Engine name="Catalina" defaultHost="localhost" jvmRoute="TomcatA">

    Configure Tomcat B by setting the following in server.xml
         <Server port="8205" shutdown="SHUTDOWN">
        Define a non-SSL HTTP/1.1 Connector on port 8080
        -->
        <Connector port="8082" protocol="HTTP/1.1"
        connectionTimeout="20000"
        redirectPort="8443" />

         <!-- Define an AJP 1.3 Connector on port 8009 -->
        <Connector port="8209" protocol="AJP/1.3" redirectPort="8443" />

        <Engine name="Standalone" defaultHost="localhost" jvmRoute="jvm1">
        -->
        <Engine name="Catalina" defaultHost="localhost" jvmRoute="TomcatB">

3. Add <distributable /> tag in web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<distributable />

</web-app>

4. Download apache_2.0.55-win32-x86-no_ssl.msi from http://httpd.apache.org/download.cgi
 Set network and domain address as localhost.
Apache Group\Apache2\conf\httpd.txt file change the listen to Listen 8080
if listen 80 does not work.
5. Create errors.log in log folder.
6. Start Apapche from Apache Group\Apache2\bin\Apache on one command prompt.
7. Download connector from location http://www.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/win32/jk-1.2.28/
  Connector mod_jk-1.2.28-httpd-2.0.52.so
 Rename it to mod_jk.so match it with setting below
 and place it in apache2/module folder.
8. Add following settings to apache -> httpd.conf
LoadModule jk_module modules/mod_jk.so
JkWorkersFile "C:\cluster\Apache\conf\workers.properties"
JkLogFile "logs/mod_jk.log"
JkLogLevel error
JkMount /cluster loadbalancer
JkMount /cluster/* loadbalancer

9. Create workers.properties file at location C:\cluster\Apache\conf
workers.tomcat_home=/tomcatA
workers.java_home=$JAVA_HOME
ps=/
worker.list=tomcatA,tomcatB,tomcatC,loadbalancer
worker.tomcatA.port=8109
worker.tomcatA.host=localhost
worker.tomcatA.type=ajp13
worker.tomcatA.lbfactor=1
worker.tomcatB.port=8209
worker.tomcatB.host=localhost
worker.tomcatB.type=ajp13
worker.tomcatB.lbfactor=1
worker.tomcatC.port=8309--this will remain same for all
worker.tomcatC.host=localhost--change this for horizontal servers
worker.tomcatC.type=ajp13
worker.tomcatC.lbfactor=1
worker.loadbalancer.type=lb
worker.loadbalancer.balanced_workers=tomcatA,tomcatB,tomcatC
worker.loadbalancer.sticky_session=1

10. Create a cluster folder in TomcatA & Tomcat B/webapps
      Add test.jsp
<%
session.setAttribute("a","a");
%>
<html>
<head>
<title>Test JSP</title>
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr bgcolor="#CCCCCC">
<td width="13%">TomcatA Machine</td>
<td width="87%">&nbsp;</td>
</tr>
<tr>
<td>Session ID :</td>
<td><%=session.getId()%></td>
</tr>
</table>
</body>
</html>
11. web.xml would be in WEB-INF and setting would be
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">
<distributable />

  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>
</web-app>

12. Start Apache. TomcatA and Tomcat B.
  Using CATALINA_BASE:   C:\TomcatA
Using CATALINA_HOME:   C:\TomcatA
Using CATALINA_TMPDIR: C:\TomcatA\temp
Using JRE_HOME:        "C:\Java\jre6"
these settings should point to respective folders for each server.

13. Connect using http://localhost:8080/cluster/test.jsp
        where 8080 is the listen port set in apache config file.

Usefull links
1. http://www.easywayserver.com/implementation-tomcat-clustering.htm
2. http://www.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/win32/jk-1.2.28/
3. http://archive.apache.org/dist/tomcat/tomcat-5/v5.5.27/bin/
4. http://easywayserver.com/apache-installation.htm

No comments:

Post a Comment