32.2. Load Balancing at the Web Level with mod_jk

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

Figure 32-2. Load balancing at web level with mod_jk

32.2.1. Configuring the JK Module (mod_jk)

32.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.

32.2.1.2. Configure Apache

  • httpd.conf

    Create a file named tomcat_jk.conf, which must be included in $APACHE_HOME/conf/httpd.conf. This file loads the module mod_jk:

    LoadModule jk_module libexec/mod_jk.so 
    AddModule mod_jk.c

    Next, configure mod_jk:

    # Location of the worker file
    JkWorkersFile "/etc/httpd/conf/jk/workers.properties"
    # Location of the log file
    JkLogFile "/etc/httpd/jk/logs/mod_jk.log"
    # Log level : debug, info, error or emerg
    JkLogLevel emerg
    # Assign specific URL to Tomcat workers
    JkMount /admin loadbalancer
    JkMount /admin/* loadbalancer
    JkMount /examples loadbalancer
    JkMount /examples/* loadbalancer
  • worker.properties

    This file should contain the list of workers first:

    worker.list=a_comma-separated_list_of_worker_names

    then the properties of each worker:

    worker.worker name.property;=property_value

    The following is an example of a worker.properties file:

    # List the workers name
    worker.list=worker1,worker2,loadbalancer
    # ----------------
    # First worker
    # ----------------
    worker.worker1.port=8009
    worker.worker1.host=server1
    worker.worker1.type=ajp13
    # Load balance factor
    worker.worker1.lbfactor=1
    # ----------------
    # Second worker
    # ----------------
    worker.worker2.port=8009
    worker.worker2.host=server2
    worker.worker2.type=ajp13
    worker.worker2.lbfactor=1
    # ----------------------
    # Load Balancer worker
    # ----------------------
    worker.loadbalancer.type=lb
    worker.loadbalancer.balanced_workers=worker1,worker2

32.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 conf/server.xml of the JOnAS installation directory, add (if not already there):

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector className="org.apache.ajp.tomcat4.Ajp13Connector"
     port="8009" minProcessors="5" maxProcessors="75"
     acceptCount="10" debug="20"/>
  2. Define the jvmRoute.

    In the file conf/server.xml of the JOnAS installation directory, 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">

    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.

32.2.2. Configuring JOnAS

In the JOnAS-specific deployment descriptor, add the tag shared for the Entity Beans involved and set it to true (line 5 in the following example). 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>

32.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.