Demo

In order to load-balance a GlassFish cluster via Apache, follow these steps:

1.  Create cluster and local instances

asadmin create-cluster cluster1
asadmin create-local-instance --cluster cluster1 in1
asadmin create-local-instance --cluster cluster1 in2asadmin create-local-instance --cluster cluster1 in3
asadmin start-local-instance instance1
asadmin start-local-instance instance2asadmin start-local-instance instance3

2.  Create mod_jk enabled network-listener

asadmin create-network-listener --jkenabled true --target cluster1 --protocol http-listener-1 --listenerport ${AJP_PORT} jk-listener

3.  Define the jvmRoute system property at the GlassFish cluster level. For example, in the case of a cluster named "cluster1", run these commands:

asadmin create-jvm-options --target cluster1 "-DjvmRoute=\${AJP_INSTANCE_NAME}"

4.  Configure the above system properties for each instance in the cluster. For example, for a cluster instance named "in1", run these commands:

asadmin create-system-properties --target instance1 AJP_INSTANCE_NAME=instance1
asadmin create-system-properties --target instance1 AJP_PORT=8009

asadmin create-system-properties --target instance2 AJP_INSTANCE_NAME=instance2
asadmin create-system-properties --target instance2 AJP_PORT=8010
asadmin create-system-properties --target instance3 AJP_INSTANCE_NAME=instance3
asadmin create-system-properties --target instance3 AJP_PORT=8011

Notice how the port number (8009) specified for the mod_jk connector on "instance1" matches the value of the corresponding worker.instance1.port in the sample workers.properties below.

5.  List each GlassFish instance, including the port number of its mod_jk connector, in Apache's workers.properties configuration file. Make sure that the name of each worker equals the value of the jvmRoute system property of the GlassFish instance to which the worker connects. This convention makes it possible for an HTTP session to remain sticky to the GlassFish instance on which the session was created, or on which the session was last resumed.

6.  The following sample workers.properties configuration file is used to load-balance a 2-instance GlassFish cluster, in which the instances are spread over three physical server machines: my.domain1.com, my.domain2.com, and my.domain3.com:

# Define 1 real worker using ajp13
worker.list=loadbalancer
#Set properties for instance1
worker.instance1.type=ajp13
worker.instance1.host=my.domain1.com
worker.instance1.port=8009
worker.instance1.lbfactor=50
worker.instance1.cachesize=10
worker.instance1.cache_timeout=600
worker.instance1.socket_keepalive=1
worker.instance1.socket_timeout=300
#Set properties for instance2
worker.instance2.type=ajp13
worker.instance2.host=my.domain2.com
worker.instance2.port=8010
worker.instance2.lbfactor=50
worker.instance2.cachesize=10
worker.instance2.cache_timeout=600
worker.instance2.socket_keepalive=1
worker.instance2.socket_timeout=300
Set properties for instance3
worker.instance3.type=ajp13
worker.instance3.host=my.domain3.com
worker.instance3.port=8011
worker.instance3.lbfactor=50
worker.instance3.cachesize=10
worker.instance3.cache_timeout=600
worker.instance3.socket_keepalive=1
worker.instance3.socket_timeout=300

worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=instance1,instance2,instance3

7.  Reference the loadbalancer worker specified in your workers.properties file from your httpd.conf. The following snippet from httpd.conf causes any JSP requests to be load-balanced over the GlassFish cluster configured in the above workers.properties file:

JkWorkersFile workers.properties
#Loadbalance all JSP requests over GlassFish cluster
JkMount /*.jsp loadbalancer

As soon as the cluster instance to which an HTTP session has been sticky has failed, the loadbalancer will route any subsequent requests for the same HTTP session to a different instance. This instance will be able to load and resume the requested session using the in-memory session replication feature that has been available since GlassFish V2. The in-memory session replication feature is enabled only for those web applications that have been marked as distributable in their web.xml deployment descriptor, and that have been deployed to the cluster with the --availabilityenabled option of the asadmin deploy command set to true (default is false*).*

Documentation

To Enable mod_jk http://download.oracle.com/docs/cd/E19798-01/821-1751/gixqw/index.html

To Load Balance Using mod_jk and GlassFish Server http://download.oracle.com/docs/cd/E19798-01/821-1751/gixpx/index.html

How to Loadbalance GlassFish Cluster with Apache Loadbalancerhttp://blogs.oracle.com/jluehe/entry/supporting_apache_loadbalancer_with_glassfish