Showing posts with label clustering and load balancing. Show all posts
Showing posts with label clustering and load balancing. Show all posts

Sunday, October 14, 2012

Enable buddy replication in JBOSS


To enable buddy replication you need to edit a configuration file in the nodes. Go to following directory and open the jboss-cache-manager-jboss-beans.xml file.

{JBOSS_HOME}\server\node3\deploy\cluster\jboss-cache-manager.sar\META-INF\jboss-cache-manager-jboss-beans.xml

Then you need to edit buddy replication mbean. Find the <property name="buddyReplicationConfig"> tag and set following tag to true in all the nodes within the cluster.

<property name="enabled">true</property>

Session Replication in JBOSS


We need to edit some configuration files to achieve session replication.
First go to the application web.xml file then add <distributable/> tag inside the <web-app> tag.
Then open the jboss-web.xml file and add following tag inside the <jboss-web> tag.


<replication-config>
        <replication-trigger>SET_AND_NON_PRIMITIVE_GET</replication-trigger>
        <replication-granularity>SESSION</replication-granularity>
        <replication-field-batch-mode>true</replication-field-batch-mode>
    </replication-config>

Then we need to edit some configuration files in the server. So we need to add jvmroute to server.xml file. Open the server.xml file located in {JBOSS_HOME}\server\node1\deploy\jbossweb.sar\server.xml. 
Find following tag.

<Engine name="jboss.web" defaultHost="localhost">

Then change it as follows.

<Engine name="jboss.web" defaultHost="localhost" jvmRoute="node1">

Then uncomment following tag in the server.xml file.

<Valve className="org.jboss.web.tomcat.service.sso.ClusteredSingleSignOn" />

Do these changes for all the nodes of the cluster. 
Now we completed session replication part also.. 
Cheers.. :)

Farming in JBOSS


We need not to copy the same ear file to all the nodes. There is a method call farming. So we can deploy to 
only one server and  automatically deploy to other nodes also. Go to a node and open farm folder inside “all” 
directory. Then paste into that folder. Then that project automatically deploy to other nodes. Remember, we 
we need to start all the servers to achieve this. Once start the servers, we can copy our ear file in to one 
"farm" folder inside one server. 

Cheerss..!!!


Testing cluster with three computers in JBOSS


First we have to create a cluster with three nodes. One node means one machine. That all three computers should be jboss installed. Then we need to start jboss with all configuration and bind with computer IP address. Therefore open the command prompt and go to the jboss bin directory. Then run following command.
Run.bat –c all –b 192.168.1.140 –Djboss.messaging.ServerPeerID=1
This serverPeerId should be unique within a cluster. Since we have three computers in our cluster, we need to start jboss with different peerIDs and corresponding IP’s by running above code.
Now we have created a cluster. Then we need to create a load balancer. So we need to install apache http server on one of above three machine or separate machine. We need not to install apache on every computers.
Go to the following link to download apache http server. Then download and install win32 MSI installer.  It is better not to install inside program files. Because the is a space between “Program file” directory. So in could be a problem. So create a new folder called apache inside C drive and install into that folder.


We are going to use mod_jk.so connecter for load balancing. We have to download mod_jk.so file from following link.


you need to download tomcat-connectors-1.2.37-windows-i386-httpd-2.2.x.zip. Because we used apache 2.2.22. 
Then unzip the downloaded file and copy that mod_jk.so file into modules folder inside apache installed directory.
Then go to the apache conf folder. Now create a file called mod_jk.conf and add following code to that file.

LoadModule jk_module modules/mod_jk.so
JkWorkersFile conf/workers.properties
JkShmFile logs/mod_jk.shm
JkLogFile logs/mod_jk.log
JkLogLevel info
JkMount /FarmTest-war/* loadbalancer

Most important part of above code is jkMount part. /FarmTest-war/* is the URL pattern of the clustered resource. So you need to specify your clustered resource here.
Then create another file called workers.properties inside the conf folder. This is the file that we specify cluster details. Add following code to that file.

# Define list of workers that will be used
     # for mapping requests
       worker.list=loadbalancer,status
     # Define Node1
     # modify the host as your host IP or DNS name.
       worker.node1.port=8009
    worker.node1.host=192.168.1.140
      worker.node1.type=ajp13
      worker.node1.lbfactor=1
       worker.node1.cachesize=10
     # Define Node2
     # modify the host as your host IP or DNS name.
       worker.node2.port=8009
       worker.node2.host= 192.168.1.141
       worker.node2.type=ajp13
       worker.node2.lbfactor=1
       worker.node2.cachesize=10
 #define node3
worker.node3.port=8009
       worker.node3.host=192.168.1.142
       worker.node3.type=ajp13
       worker.node3.lbfactor=1
      #worker.node1.local_worker=1 (1)
       worker.node3.cachesize=10
           
     # Load-balancing behavior
       worker.loadbalancer.type=lb
       worker.loadbalancer.balance_workers=node1,node2,node3
       worker.loadbalancer.sticky_session=1

Then open the httpd.conf file which located inside the conf folder. Then add following line of code to the bottom of that file.

Include conf/mod_jk.conf

Now restart the apache server.
Then start all jboss nodes.
We have created a load balancer and cluster with three nodes. Now we need to deploy an application to the cluster.  Create a small enterprise application and deploy it with one machine. Then go to the deploy folder inside the “all” folder. Now copy the application “war” file and paste it to the farm folder inside the “all” folder.
Now we deployed an application to the cluster. Now open a web browser and go to following link.
This IP address is the IP address of apache installed computer. 



Understanding Load balancing


Imagine you have a retail web application running on a single application server
instance and that you average 10,000 customers a month. Your company decides to
run a TV advertisement and predicts that you may start getting 100,000 customers a
month. You’re tasked with making sure that the application can support this number
of users. In addition, your application needs to be highly available. To accomplish
these goals, you have to deploy multiple application server instances and balance
requests across them.
Load balancing is a way of balancing incoming load, or concurrent requests, across
multiple application server instances, making your applications scalable and highly
available. Scalability is a term used to describe the ability to make your application
handle more user load by adding hardware and/or creating redundant instances of
your application without having to change code. Load balancing can help scale your
application because you can add more servers for the load balancer to balance the
load across, increasing the amount of traffic your application can handle. High availability
is the ability to continue processing requests in the face of server failure.

The load balancer acts as a single point of entry into an application environment
as well as a traffic director for requests.




TYPES OF LOAD BALANCERS
Load balancing can be done in two primary ways: with a hardware load balancer or
with a software load balancer. Hardware load balancers are typically more expensive
but are also more reliable


Load balancers typically make a single IP address for a cluster visible to clients. The
load balancer maintains a map of internal (or virtual) IP addresses for each machine
in the cluster. When the load balancer receives a request, it rewrites the header to
point to a particular machine in the cluster. If a machine in the cluster is removed or fails, the
hardware load balancer has the ability to recognize the failure and avoid
routing requests to it.



Understanding JBOSS Clustering


Clustering is the act of running the same application on multiple application server
instances simultaneously with each application server being aware of the others in the
cluster. An application server that’s part of a cluster is known as a node. But just having
nodes aware of other nodes in a cluster isn’t interesting. The nodes in the cluster must
be able to communicate with each other to do something useful, such as replicating
state or providing failover capabilities. Before we dive into the specifics of the clustering
features that JBoss offers, let’s examine some of the fundamental concepts behind
clustering. And, because you can’t seem to talk about clustering without talking about
load balancing, let’s start with that.

Introduction to jboss clustering and load balancing


A single JBoss server can handle several hundred concurrent requests; but, if your
application has to scale to support multiple thousands of concurrent requests or
multiple millions of requests a day, then a single application server probably won’t
do the trick. JBoss enables you to simultaneously run your application on multiple
application servers. Requests going to your application can then be balanced across
these servers, and your application can also withstand individual server failures.
This deployment architecture allows you to achieve maximum scalability with minimal
downtime. Clients need not know that their different requests may be handled
by different servers.
Java EE doesn’t specify any standards for how clustering services should work.
Every application server implements clustering differently and provides a different
set of clustering capabilities and services. Red Hat has set out to make cluster setup a simple task. As you’ll learn in this chapter, clusters are easy to create in JBoss and
require minimal configuration. Adding nodes requires no administrative management
because nodes detect each other automatically over network protocols. JBoss
also provides a sophisticated distributed cache that allows stateful components to replicate
their states across multiple nodes in a cluster, enabling you to easily develop
fault-tolerant applications, with very little code.