Apache load balancing for Tomcat Example (with Wamp server apache module (Apache2.2.xx))

This is continued from the previous post in here.

Scenario: You have application running on two tomcat servers. Example is for localhost. Replcae it with relevant IP addresses which the tomcat servers are runing.

1. http://localhost:8080/app/
2. http://localhost:8090/app/

You need to load balance between two tomcat servers through apache server as well as to hide the actualURLs from outside.

Here is the Step by Step instructions.

1. First follow the previous post and configure apache proxy.
2. Let's write a balancer configuration named "mybalancer" with two members who are two tomcat servers. The load should be balanced between those two members (1:1). Add the following segment to httpd.conf file.

<PROXY balancer://mybalancer>
BalancerMember http://localhost:8080
BalancerMember http://localhost:8090
</PROXY>

3. Now change the ProxyPass statement to refer the balancing cluster. For that replace line

ProxyPass /proxydemo/ http://localhost:8080/app/

with

ProxyPass /proxydemo/ balancer://mybalancer


4. Then replace the ProxyPassReverse statement line

ProxyPassReverse /proxydemo/http://localhost:8080/app
with

ProxyPassReverse /proxydemo/ balancer://mybalancer

What you did was to place the load balancer behind the apache server. So the requests come to http://localhost:9090/proxydemo (assuming Apache runs on port 9090,replace with your own port)will be redirected to either of the balancing members. But the actual URL is hidden to outside.

If you want to access one of the resource hosted on tomcat servers such as

http://localhost:8080/app/service.jsp

You can access it through apache load balancer through

http://localhost:9090/proxydemo/app/service.jsp

when someone access the above URL requests will be balanced to one of the members, in this case 1:1. You can shut down one tomcat server and  try the above URL. Then it will work as another server is still working. But if you have shut down both servers it will display as server temporaly not available.

Ths example was assuming there are two tomcat servers running on the same machine (port 8080 and port 8090) and apache server is running on port 9090. You can try this on distributed servers and replace localhost with IP addresses.

Comments

  1. Good and straight forward article.

    One thing to keep in mind is that many j2ee apps use sessions and without the use of the stickysession attribute the client can and will be at some point forwarded to the tomcat that doesn't have the session. Ref. http://httpd.apache.org/docs/2.2/mod/mod_proxy_balancer.html#stickyness_implementation

    ReplyDelete
  2. Sure I will look at it! Thanks for the comment!

    ReplyDelete
  3. We have the setup with stickysessioness working it can be configured, the Problem that seems to remain is that the mod_proxy does not delegate users on a broken node to a working one.

    ReplyDelete
  4. How about setup in different server different machine with different IP), not different port (tomcat instance) in same machine.

    ReplyDelete

Post a Comment

Popular posts from this blog

PHP-SOAP web service with out a WSDL

Boomi Mapping - Removing special chars from an input

How to add Maven dependency from a relative path referencing a local jar