Version 4

    Loadbalancing based on URIs




    So you have a requirement wherein you need to setup a multiple node cluster of JBoss/Tomcat and you also want to route specific URLs to a specific set of nodes in that cluster. How to do that? This WIKI explains exactly that, the process and the configuration required to do URI based load-balancing. It explains taking an example web application that has three servlets with three different url mappings. The source and binary are attached to the bottom of this page.




    1. Setup the Apache, mod_jk using this WIKI UsingMod_jk1.2WithJBoss

    2. Test it. Ensure everything is working fine!

    3. Now configure multiple instance of JBoss. Use the sample-bindings.xml from docs\examples\binding-manager

    4. Now test the cluster of these two nodes.


    Load Balance


    The sample application attached here contains 3 servlets TestServlet1, TestServlet2 and TestServlet3 each are mapped to three different urls uribalance/one, uribalance/two and uribalance/common respectively. Now what we are going to do is that we are going to map the uribalance/one to be balanced by a loadbalancer1 worker and uribalance/two by loadbalancer2 worker. As of now we are configuring only one instance per loadbalancer1 and loadbalancer2 each, you can add as many instances as you need. The uribalance/common will be load balanced across all available Tomcat/JBoss server instances.


    Step 1: Configuring workers


    Under APACHE_HOME/conf, create and populate it as follows:

              # ------------------------
              # First tomcat server
              # ------------------------
              # ------------------------
              # 2nd tomcat server
              # ------------------------
              # Define load Balancing behavior


    Step 2:  Modify the modjk config


    Under APACHE_HOME/conf, modify mod-jk.conf and modify it as follows:

                   # Mount your applications
                   #JkMount /application/* loadbalancer ---> comment this line or remove it
                   # You can use external file for mount points.
                   # It will be checked for updates each 60 seconds.
                   # The format of the file is: /url=worker
                   # /examples/*=loadbalancer
                   JkMountFile conf/               



    Step 3:  Add the uri mappings


    Under APACHE_HOME/conf, create and add the following lines:



    Step 4: Restart JBoss nodes one at a time and let them form the cluster



    Step 5: Restart Apache



    Step 6: Deploy the war file



    Step 7: Testing


    1. Checkout the url http://localhost/uribalance/one, the TOM1's console will display "Servlet One called...."

    2. Refreshing the above url will result in always TOM1 getting the hit and it never goes to TOM2.

    3. Checkout the url http://localhost/uribalance/two, the TOM2's console will display "Servlet Two called...."

    4. Refreshing the above url will result in always TOM2 getting the hit and it never goes to TOM1.

    5. Checkout the url http://localhost/uribalance/three, the TOM1's console will display "Servlet Three called...."

    6. Refresh the above url, the TOM2's console will display "Servlet Three called...."

    7. Keep refreshing this url and you can notice it will load balance in a round robin fashion between TOM1 and TOM2.