42.2. Load Balancing at the Web Level with mod_jk or Enhydra Director

This section describes how to configure Apache, Tomcat, and JOnAS to run the architecture shown in the following illustration:

Figure 42-2. Load balancing at the web level with mod_jk

This example uses mod_jk but an alternative configuration using Enhydra Director (http://director.objectweb.org) is also possible. (See Chapter 43 Load Balancing with Enhydra Director.)

42.2.1. Configuring the JK Module (mod_jk)

42.2.1.1. JK module principles

mod_jk is a plug-in that handles the communication between Apache and Tomcat.

mod_jk uses the concept of a worker. A worker is a Tomcat instance that is running to perform Servlet requests coming from the web server. Each worker is identified to the web server by the host on which it is located, the port where it listens, and the communication protocol used to exchange messages. In this configuration there is one worker for each Tomcat instance and one worker that will handle the load balancing (this is a specific worker with no host and no port number). All workers are defined in the worker.properties file.

NoteNote:
 

The JK Module can also be used for site partitioning.

42.2.1.2. Configure Apache

httpd.conf should load the following files:

  • AddModule mod_jk.c

  • Include $APACHE_HOME/conf/jk/tomcat_jk.conf

  • LoadModule jk_module libexec/mod_jk.so

42.2.1.3. Configure Tomcat

To configure Tomcat, perform the following configuration steps for each Tomcat server.

  1. Configure Tomcat for the connector AJP13. In the file installdir/conf/server.xml, add (if not already there):

    <!-- Define an AJP 1.3 Connector on port 9009 -->
    <Connector
        port="9009" minProcessors="5"
        maxProcessors="75"
        acceptCount="10" debug="20" protocol="AJP/1.3"/>

    Explanations:

    port

    The TCP port number on which this Connector will create a server socket and await incoming connections.

    minProcessor

    The minimum number of processors to start at intialization time. If not specified, this attribute is set to 5.

    maxProcessor

    The maximum number of processors allowed.

    acceptCount

    The maximum queue length for incoming connection requests when all possible requests processing threads are in use. Any requests received when the queue is full will be refused.

    debug

    The debugging detail level of log messages generated by this component, with higher numbers creating more detailed output.

    protocol

    This attribute must be AJP/1.3 to use the AJP handler.

    NoteNote
     

    Consult the AJP Connector documentation http://jakarta.apache.org/tomcat/tomcat-5.0-doc/condig/ajp.html.

  2. Define the jvmRoute.

    In the installdir/conf/server.xml file, add a unique route to the Catalina engine. Replace the line:

    <Engine name="Standalone" defaultHost="localhost"
        debug="0">

    with:

    <Engine jvmRoute="worker1" name="Standalone"
        defaultHost="localhost" debug="0">

    Explanations:

    name

    Logical name of this Engine, used in log and error messages.

    jvmRoute

    Uniquely identifies the Tomcat server to the Apache server. The name has been specified in the orkers.properties file.

    defaultHost

    Identifies the Host that will process requests directed to host names on this server.

    debug

    The level of debugging detail logged by this Engine.

    NoteNote
     

    The jvmRoute name should be the same as the name of the associated worker defined in worker.properties. This will ensure the Session affinity. Consult the Engine Container documentation http://jakarta.apache.org/tomcat/tomcat-5.0-doc/condig/engine.html.

42.2.2. Configuring JOnAS

In the JOnAS-specific deployment descriptor, add the tag shared for the entity beans involved and set it to true. When this flag is set to true, multiple instances of the same entity bean in different JOnAS servers can access a common database concurrently.

The following is an example of a deployment descriptor with the flag shared:

<jonas-ejb-jar>
  <jonas-entity>
    <ejb-name>Id_1</ejb-name>
    <jndi-name>clusterId_1</jndi-name>
    <shared>true</shared>
    <jdbc-mapping>
     
<jndi-name>jdbc_1</jndi-name>
     
<jdbc-table-name>clusterIdentityEC</jdbc-table-name>
      <cmp-field-jdbc-mapping>
       
<field-name>name</field-name>
       
<jdbc-field-name>c_name</jdbc-field-name>
      </cmp-field-jdbc-mapping>
      <cmp-field-jdbc-mapping>
       
<field-name>number</field-name>
       
<jdbc-field-name>c_number</jdbc-field-name>
      </cmp-field-jdbc-mapping>
      <finder-method-jdbc-mapping>
        <jonas-method>
         
<method-name>findByNumber</method-name>
        </jonas-method>
        <jdbc-where-clause>where
c_number = ?</jdbc-where-clause>
      </finder-method-jdbc-mapping>
      <finder-method-jdbc-mapping>
        <jonas-method>
         
<method-name>findAll</method-name>
        </jonas-method>
       
<jdbc-where-clause></jdbc-where-clause>
      </finder-method-jdbc-mapping>
    </jdbc-mapping>
  </jonas-entity>
</jonas-ejb-jar>

42.2.3. Running a Web Application

The web application is now ready to run:

  1. Start the jonas servers:

    service jonas start
  2. Restart Apache:

    /usr/local/apache2/bin/apachectl restart
  3. Use a browser to access the welcome page, usually index.html.

NoteNote
 

The order of launching is important, otherwise it will not work correctly.