In this tutorial, we will cover the second main concern of clustering, which is the ability to make several servers participate in the same service and do the same work. In other words, how to load-balance the number of requests across the available servers.
Historically, the JBoss AS has inherited the load-balancing libraries from his “cousin” Tomcat, which is itself a part of the application server modules. This module, mod_jk, is still available for use in the new application server era; however, new specific instruments are available for the AS 7. In particular, mod_cluster project, which is a part of the AS 7 modules, provides a level of intelligence and granularity unmatched in other load-balancing solutions.
Having made this a short premise, we will, at first, introduce the advantages of using a web server in front of your web applications, and then we will cover the following topics:
In real-world projects, it’s common to find Apache web server as a front door to your application server. The advantages of such an architecture are as follows:
Connecting Apache and JBoss AS can be done by means of several libraries: in the past, most projects have adopted either Tomcat’s mod_jk library or Apache’s mod_proxy libraries. Because the installation of either mod_jk or mod_proxy does not differ from earlier AS releases, we will just include a quick setup guide for your
If, however, you are planning to set up a high-performance, dynamic cluster of web servers, we suggest you migrate to the newer mod_cluster API, which is discussed in the second part of this chapter.
mod_jk is the most-used solution for fronting JBoss AS with Apache web server. All requests first come to the Apache web server. The Apache web server accepts and processes any static resource requests, such as requests for HTML pages or graphical images. Then, with the help of mod_jk, the Apache web server redirects requests for
any JSP or Servlet component to a JBoss Web server instance(s). Sending the request over the network using the AJP protocol performs this redirection.
In order to install mod_jk , at first, download the appropriate Tomcat Connector for your Apache web server installation from http://tomcat.apache.org/downloadconnectors.cgi.
Then, modify the Apache configuration file (APACHE_HOME/conf/httpd.conf) and add a single line at the end of the file:
Next, create a new file named APACHE_HOME/conf/mod-jk.conf, which will contain
the mod_jk configuration:
# Load mod_jk module
# Specify the filename of the mod_jk lib
LoadModule jk_module modules/mod_jk.so
# Where to find workers.properties
# Where to put jk logs
# Set the jk log level [debug/error/info]
# Mount your applications
# Send everything for context /myapp to worker1 (ajp13)
JkMount /myapp/* loadbalancer
Please note that the following two settings are most important:
Next, you need to configure the mod_jk workers file conf/workers.properties. This file specifies where the different web servers are located and, possibly, how calls should be load-balanced across them. For a one-node setup, the file could look like this:
# Define worker list using ajp13
# Set properties for worker1 (ajp13)
# Set properties for worker2 (ajp13)
In the workers.properties file, each node is defined using the worker.XXX naming convention, where XXX represents an arbitrary name you choose for each web server containers. For each worker, you must specify the hostname (or IP address) and the port number of the AJP13 connector running in the web server.
Configuring mod proxy
Since Apache 1.3 , there’s support for an optional module, named mod_proxy, that configures Apache to act as a proxy server. This can be used to forward requests for particular web applications such as Tomcat or JBoss, without having to configure a web connector such as mod_jk.
So, mod_proxy just requires including the following directives in your Apache’s httpd.conf file:
LoadModule proxy_module modules/mod_proxy.so
Then, include two directives in your httpd.conf file for each web application that you wish to forward to JBoss AS. For example, to forward an application at context path /myapp:
ProxyPass /myapp http://localhost:8080/myapp
ProxyPassReverse /myapp http://localhost:8080/myapp
This tells Apache to forward URLs of the form http://localhost/myapp/* to the
JBoss HTTP connector listening on port 8080.
As shown in the previous diagram, Apache’s mod_proxy is TCP-based and uses the HTTP, so you don’t need to add anything else in your JBoss configuration. By definition, this is the simplest way to put Apache in front of JBoss, but also the slowest way to do it.
In Apache 2.2 , there’s support for another module, named mod_proxy ajp, which can be used in much the same way as mod_proxy. However, it uses AJP protocol to proxy Apache requests to JBoss AS. In order to use it, add the following directive to your
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
Then, enable proxy pass to JBoss AS with this directive:
ProxyPass / ajp://localhost:8009/
ProxyPassReverse / ajp://localhost:8009/
Here, we are simply redirecting all traffic (“/”) to the web server listening on localhost
at port 8009.
Since mod_proxy_ajp runs on a dedicated port, you have to activate it on your JBoss
Get Updates on Tech posts, Interview & Certification questions and training schedules