Let us say we have 2 machines in the same network. Node A and Node B
nodeA 192.168.0.1
nodeB 192.168.0.2
Now for every installation of JBoss go under the folder deploy\jboss-web.deployer (jBoss 4.X) or under the folder deploy\jbossweb.sar (jBoss 5.X and later) and pick up the file server.xml line.
Configure on the 192.168.0.1 machine
<Engine name=”jboss.web” defaultHost=”localhost” jvmRoute=”nodeA”>
and on the machine 192.168.0.2
<Engine name=”jboss.web” defaultHost=”localhost” jvmRoute=”nodeB”>
Simple Helloworld java servlet that throws the session information.
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(“text/html”);
String dummy = (String)request.getSession().getAttribute(“dummy”);
PrintWriter out = response.getWriter();
System.out.print(“Hello from Node “+ InetAddress.getLocalHost().toString());
if (dummy == null) {
out.println(“Dummy value not found. Inserting it….”);
request.getSession().setAttribute(“dummy”, “dummyOk”);
} else {
out.println(“Dummy is ” +dummy);
}
out.flush();
out.close();
}
Web.xml
<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app version=”2.4″
<distributable/>
</web-app>
he easiest way to deploy an application into the cluster is to use the farming service. That is to hot-deploy the application archive file (e.g., the EAR, WAR or SAR file) in the all/farm/ directory of any of the cluster member and the application is automatically duplicated across all nodes in the same cluster.
Using Apache Web Server will be used for the Load Balancer infront of the JBOSS App servers.
The first thing we need to have is redirecting calls to the mod_JK load balancer: this is done in file mod_jk.properties:
# Where to find workers.properties
JkWorkersFile conf/workers.properties
# Where to put jk logs
JkLogFile logs/mod_jk.log
JkMount /HelloWorld/* loadbalancer
Here HelloWorld is the Web Context of our sample application. Now let’s move to workers.properties file.
worker.list=loadbalancer,status
worker.nodeA.port=8009
worker.nodeA.host=192.168.0.1
worker.nodeA.type=ajp13
worker.nodeA.lbfactor=1
worker.nodeB.port=8009
worker.nodeB.host=192.168.0.2
worker.nodeB.type=ajp13
worker.nodeB.lbfactor=1
worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=nodeA,nodeB
# Status worker for managing load balancer
worker.status.type=status
As you can see we have the two nodes (nodeA and nodeB) with relative port and IP address.
Now launch the two instances of JBoss using the command run -c all. The first instance started will display this information when the second instance kicks in:
In order to invoke our Servlet simply point to our Apache Web Server domain address
You’ll see that the balancer load balances calls and, once it has created the session, it will replicate the session to the secondary server. You can do experiments to test your cluster, switching off one instance of the cluster and verifying that the session stays alive on the other server.
Simply verify that the dummy values stays in the Session when you switch off the current instance of JBoss.
Dummy is dummyOk
MOD_JK APACHE MODULE:
mod_jk is an Apache module used to connect the Tomcat servlet container with web servers such as Apache, iPlanet, Sun ONE (formerly Netscape) and even IIS using the Apache JServ Protocol.
In a nutshell, a web server is waiting for client HTTP requests. When these requests arrive the server does whatever is needed to serve the requests by providing the necessary content.
Adding a servlet container may somewhat change this behavior. Now the web server needs also to perform the following:
Load the servlet container adapter library and initialize it (prior to serving requests).
When a request arrives, it needs to check and see if a certain request belongs to a servlet, if so it needs to let the adapter take the request and handle it.
The adapter on the other hand needs to know what requests it is going to serve, usually based on some pattern in the request URL, and to where to direct these requests.
Things are even more complex when the user wants to set a configuration that uses virtual hosts, or when they want multiple developers to work on the same web server but on different servlet container JVMs.