Thursday, February 16, 2012

Clustering and Load Balancing of JBoss 4.2

JBoss Clustering
As you all might be knowing how to cluster JBoss, by surfing various sites and blogs. This article doesn’t make difference in briefing you about JBoss clustering and Apache Load balancing.

Let me start of with explaining cluster.
Cluster: A cluster is a set of nodes. In Jboss each node is Jboss server instance. Multiple Jboss instances on same machine and different machine in same network forms a cluster.
There might be different clusters, differentiated by the name across same network.

To start the Jboss in cluster mode you need to use the following command

run –c “all”

We can specify each jboss instance to join a specific cluster by  specifying the name in /server/all/deploy/cluster-service.xml file. The default name will be of default partition.

When you run multiple instances of Jboss in same machine, then you might get the port conflict(like port already in use). Its not recommended to use multiple instance of Jboss in same machine. But if required we can run multiple instance of Jboss in same machine by resolving the port conflict either manually changing all the conflicting ports or by configuring the sample-binding.xml file.

You can get all the ports of Jboss which run independently, in the sample-binding.xml file. Here each set of ports will be defined under server name (port-default,port-01), you can use this server name in /server/all/conf/jboss-service.xml of each jboss server instance , by either changing the port number or using default port numbers in the sample-binding.xml file. Before that to enable this feature you need to uncomment the following piece of code in /server/all/conf/jboss-service.xml

   <mbean code="org.jboss.services.binding.ServiceBindingManager"
    >
     <attribute>ports-01</attribute>
     <attribute>${jboss.home.url}/docs/examples/binding-manager/sample-bindings.xml</attribute>
     <attribute>
       org.jboss.services.binding.XMLServicesStoreFactory
     </attribute>
   </mbean>

You can even change the name and location of sample-binding.xml file.

Now let us start two Jboss server instances in same machine, to form a cluster automatically. For example,
http://localhost:8080/
http://localhost:8180/
Apache Load Balancing
After clustering Jboss we need to balance the load, and we need direct request to a jboss server with less load. (Also we need to have a common URL , instead exposing the clustered Joss servers url) . Here comes in picture, Apache web server.

In architecture, the apache web server will be placed in front of the Jboss server. Like the one shown in figure,



Now to configure the apache server,  for the cluster which we have created with 2 nodes in same machine. First download and install apache2.2.x. It will get installed in
/apache software foundation/apache2.2.x/.

First open the httpd.conf file under conf directory in your apache installation path and uncomment the following, which acts as connector between your webserver and Jboss application server.

LoadModule proxy_module modules/mod_proxy.soLoadModule proxy_balancer_module modules/mod_proxy_balancer.soLoadModule proxy_http_module modules/mod_proxy_http.so
Next add the following lines in your httpd.conf file. Which says the webserver about the nodes that form the cluster and its url. And what is the URL to the webserver that will pass the request to application deployed in Jboss server instance.

<Proxy balancer://mycluster>       Order deny,allow       Allow from all       BalancerMember http://localhost:8080/test route=node1       BalancerMember http://localhost:8180/test route=node2</Proxy>      ProxyPass /jmx-console balancer://mycluster stickysession=JSESSIONID lbmethod=bytraffic nofailover=Off  ProxyPassReverse /test  http://localhost:8080/testProxyPassReverse /test  http://localhost:8180/test  You need to use the sticky session attribute to make the request available to both the Jboss server , to server request even during failover of other server. To be simple maintaining or duplication of session created in one server on the other server.

Whenever you need change your httpd.conf file, remember to restart your apache webserver service also.

Now to use Sticky session in Jboss make change to your Jboss instance server.xml file, like the following

<Engine defaultHost="localhost" jvmRoute="node1">..
..
..
</Engine>

Atlast we need to tell the Jboss web to add the jvm route to the incoming sessions, by making the usejk attribute to true, in jboss-service.xml under /deploy/jboss-webdeployer.war/META-INF

<attribute>true</attribute>
Now start both of your jboss server instances in same machine. And restart the Apache webserver.

Hope you find this article useful.