Enterprise grade Java.
You'll read about Conferences, Java User Groups, Java, Integration, Reactive, Microservices and other technologies.

Thursday, October 29, 2009

Loadbalancing with fail-over through Apache 2.2 and GlassFish v2.1.1

12:33 Thursday, October 29, 2009 Posted by Markus Eisele
, , , , , ,
One of the top posts on my blog is about installing and configuring the BEA/Oracle Apache Plugins for WebLogic Server. This inspired me to write a similar howto about how to achieve the same effect with Apache and GlassFish Server. To be honest, I recently came across some projects working with a clustered GlassFish domain behind an Apache Webserver. And there was definitely a need to understand, what is going on ;)

Let's start.

- GlassFish v2.1.1
- Apache Webserver 2.2.14

Setup GlassFish Domain,Nodeagent,Cluster and Nodes:
1) Download and install GlassFish (java -Xmx256m -jar glassfish-installer-v2.1.1-b31g-windows.jar)
2) Build a Cluster domain ( $GLASSFISH_ROOT/lib/ant/bin/ant -f setup-cluster.xml)
3) Start GlassFish ($GLASSFISH_ROOT/bin/asadmin start-domain)
4) Create a Nodeagent ($GLASSFISH_ROOT/bin/asadmin create-node-agent --host localhost --port 4848 nodeagent1
5) Start the Nodeagent ($GLASSFISH_ROOT/bin/asadmin start-node-agent nodeagent1)
6) Login to your admin server (http://localhost:4848/)
7) Create a cluster (name it, whatever you like, I like cluster1)
8) Create two nodes (same here, I like instance1, instance2)
9) Start the cluster
10) Add a property to the clusterwide jvm configuration (-DjvmRoute=${INSTANCE_ROUTE} )
11) Add an instance specific property to instance1 (INSTANCE_ROUTE = w1)
12) Add an instance specific property to instance2 (INSTANCE_ROUTE = w2)
13) deploy clusterjsp example app ($GLASSFISH_ROOT/glassfish/samples/quickstart/clusterjsp)

Setup Apache, mod_proxy, mod_status and mod_proxy_balancer:
1) Download and install Apache HTTPD Server (e.g apache_2.2.14-win32-x86-no_ssl.msi for windows )
2) Edit the httpd.conf:
Change Listen Port to whatever you like. If you run on a single maschine, you should change it to anything but the already blocked GF ports. I use Listen 6666 in this example
3) Uncomment the modules:
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule status_module modules/mod_status.so
4) Add the rewriting configuration to the end of the file:

# Enable the rewriting engine
RewriteEngine On

# Enable Reverse Proxy functions
ProxyRequests Off

# Preserve the proxy host-name
ProxyPreserveHost On

# Set access permissions to anyone
<Proxy *>
Order deny,allow
Allow from all

# Enable balance-manager (http://localhost:port/balancer-manager)
<Location /balancer-manager>
SetHandler balancer-manager

# balancer configuration with two nodes
<Proxy balancer://mycluster>
BalancerMember route=w1
BalancerMember route=w2
ProxySet lbmethod=byrequests stickysession=JSESSIONID|jsessionid nofailover=Off

# define the proxy rules for your application

ProxyPassMatch ^(.*\.do)$ balancer://mycluster$1
ProxyPassMatch ^(.*\.jsp)$ balancer://mycluster$1
ProxyPassMatch ^(.*\/clusterjsp\/.*)$ balancer://mycluster$1

# define the reverse proxy rule
ProxyPassReverse / balancer://mycluster/

# Enable extended server status
ExtendedStatus On

# enable server-status location (http://localhost:port/server-status)
<Location /server-status>
SetHandler server-status

# Set access permissions to anyone (not suitable for production !!!)
Allow from all

5) Test the configuration and start the apache instance
6) Access your application (http://localhost:6666/clusterjsp/)

Thats all. Finaly, this installation behaves like the oracle apache plugins. You can define dispatching rules für dynamic content or whole directories.

Not needed for the cluster funcionality is the server-status. But this is quite helpful if you like to know, what your apache is doing. Give it a shot and have a look at http://localhost:6666/server-status. Don't forget to disable this in production environment.
Another great thing is the balancer-manager. You can define weight based routings and see the status of the server nodes and the LoadBalancer status in general.