Loadbalancing based on URIs
Overview
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.
Pre-requisites
Setup the Apache, mod_jk using this WIKI UsingMod_jk1.2WithJBoss
Test it. Ensure everything is working fine!
Now configure multiple instance of JBoss. Use the sample-bindings.xml from docs\examples\binding-manager
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 workers.properties and populate it as follows:
worker.list=loadbalancer1,loadbalancer2,loadbalancer3,status # ------------------------ # First tomcat server # ------------------------ worker.TOM1.port=8009 worker.TOM1.host=localhost worker.TOM1.type=ajp13 worker.TOM1.lbfactor=1 worker.TOM1.local_worker=1 worker.TOM1.cachesize=10 # ------------------------ # 2nd tomcat server # ------------------------ worker.TOM2.port=8109 worker.TOM2.host=localhost worker.TOM2.type=ajp13 worker.TOM2.lbfactor=1 worker.TOM2.local_worker=1 worker.TOM2.cachesize=10 #--------------------------------------------- # Define load Balancing behavior #--------------------------------------------- worker.loadbalancer1.type=lb worker.loadbalancer1.balanced_workers=TOM1 worker.loadbalancer1.sticky_session=1 worker.loadbalancer2.type=lb worker.loadbalancer2.balanced_workers=TOM2 worker.loadbalancer2.sticky_session=1 worker.loadbalancer3.type=lb worker.loadbalancer3.balanced_workers=TOM1,TOM2 worker.loadbalancer3.sticky_session=0 worker.status.type=status
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/uriworkermap.properties
Step 3: Add the uri mappings
Under APACHE_HOME/conf, create uriworkermap.properties and add the following lines:
/uribalance/one=loadbalancer1 /uribalance/one/*=loadbalancer1 /uribalance/two=loadbalancer2 /uribalance/two/*=loadbalancer2 /uribalance/common=loadbalancer3 /uribalance/common/*=loadbalancer3 /*=loadbalancer3
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
Checkout the url http://localhost/uribalance/one, the TOM1's console will display "Servlet One called...."
Refreshing the above url will result in always TOM1 getting the hit and it never goes to TOM2.
Checkout the url http://localhost/uribalance/two, the TOM2's console will display "Servlet Two called...."
Refreshing the above url will result in always TOM2 getting the hit and it never goes to TOM1.
Checkout the url http://localhost/uribalance/three, the TOM1's console will display "Servlet Three called...."
Refresh the above url, the TOM2's console will display "Servlet Three called...."
Keep refreshing this url and you can notice it will load balance in a round robin fashion between TOM1 and TOM2.
Comments