<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Web2.0 Tutorials &#187; jvmroute</title>
	<atom:link href="http://www.zulutown.com/blog/tag/jvmroute/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.zulutown.com/blog</link>
	<description>All the Guides You Need to Become a Web2.0 Expert</description>
	<lastBuildDate>Mon, 13 Jul 2009 07:50:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Java EE Load Balancing with Tomcat and Apache</title>
		<link>http://www.zulutown.com/blog/2009/02/16/java-ee-load-balancing-with-tomcat-and-apache/</link>
		<comments>http://www.zulutown.com/blog/2009/02/16/java-ee-load-balancing-with-tomcat-and-apache/#comments</comments>
		<pubDate>Mon, 16 Feb 2009 21:16:45 +0000</pubDate>
		<dc:creator>Zulutown Webmaster</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[Java EE]]></category>
		<category><![CDATA[Tomcat]]></category>
		<category><![CDATA[ajp]]></category>
		<category><![CDATA[balancermember]]></category>
		<category><![CDATA[cluster]]></category>
		<category><![CDATA[connector]]></category>
		<category><![CDATA[jsessionid]]></category>
		<category><![CDATA[jvmroute]]></category>
		<category><![CDATA[load balancer]]></category>
		<category><![CDATA[load balancing]]></category>
		<category><![CDATA[mod_proxy]]></category>
		<category><![CDATA[mod_proxy_ajp]]></category>
		<category><![CDATA[mod_proxy_balancer]]></category>
		<category><![CDATA[stickysession]]></category>

		<guid isPermaLink="false">http://www.zulutown.com/blog/?p=78</guid>
		<description><![CDATA[This tutorial explains how to configure an Apache HTTPD server to map a specific path on a series of load-balanced Apache Tomcat.
The first step is to define the Virtual Host in the Apache configuration files.
In this case the root directory (on file system) of the site is located in /path/to/your/site/, the name of the site [...]]]></description>
			<content:encoded><![CDATA[<p>This tutorial explains how to configure an <em>Apache HTTPD</em> server to map a specific path on a series of load-balanced <em>Apache Tomcat</em>.<br />
The first step is to define the <em>Virtual Host</em> in the Apache configuration files.<br />
In this case the root directory (on file system) of the site is located in <code>/path/to/your/site/</code>, the name of the site is <code>www.yoursite.com</code> and the path where the Tomcat servers may be reached is <code>/javaee</code>.</p>
<p>In few words, an URL like <code>http://www.yoursite.com/home.html</code> is mapped on the file <code>/path/to/your/site/home.html</code>.</p>
<p>An URL like <code>http://www.yoursite.com/javaee/hello.jsp</code> is mapped to the <code>hello.jsp</code> file contained in <code>javaee.war</code> application deployed on all the Tomcat servers defined in the <em>load balanced cluster</em>.</p>
<p>The configuration of the Apache virtual host:</p>
<pre>&lt;VirtualHost *&gt;
	ServerAdmin webmaster@localhost
	ServerName www.yoursite.com
	DocumentRoot /path/to/your/site/
	&lt;Directory /path/to/your/site/&gt;
		Options MultiViews
		AllowOverride All
		Order allow,deny
		allow from all
	&lt;/Directory&gt;

	ErrorLog /var/log/yoursite-error.log

	LogLevel warn

	CustomLog /var/log/yoursite-access.log combined

    &lt;Proxy balancer://tomcatservers&gt;
	BalancerMember ajp://tomcatserver.yoursite.com:8009 route=tomcatA retry=60
        BalancerMember ajp://tomcatserver.yoursite.com:8010 route=tomcatB retry=60
	BalancerMember ajp://tomcatserver.yoursite.com:8011 route=tomcatC retry=60
    &lt;/Proxy&gt;

    &lt;Location /javaee&gt;
	Allow From All
        ProxyPass balancer://tomcatservers/javaee stickysession=JSESSIONID nofailover=off
    &lt;/Location&gt;

&lt;/VirtualHost&gt;</pre>
<p>The most important settings are <code>Proxy</code> and <code>Location</code>.<br />
In <code>Proxy</code> it&#8217;s defined a <em>load balancer</em> made with 3 tomcat servers and an URL is assigned to the balancer, in this case <code>balancer://tomcatservers</code>.</p>
<p>The balancer has three members, everyone with its own URL based on the <code>ajp</code> protocol. In this case Apache will connect to the Tomcat servers on their <em>AJP connectors</em> (an alternative would be to use their <em>HTTP connectors</em>).</p>
<p>The Tomcat servers run on the <code>tomcatserver.yoursite.com</code> hostname and each of them opens its own AJP connector on a different port: the first on <code>8009</code> (the default one), the second on <code>8010</code>, the third on <code>8011</code> (obviously if they run on the same hostname/IP they must bind to different ports).</p>
<p>Each Tomcat is identified by a route name: <code>tomcatA</code>, <code>tomcatB</code> and <code>tomcatC</code>. The importance of it will be explained later.</p>
<p>In the <code>Location</code> section, a specific path <code>/javaee</code> of the virtual host is mapped on the previously defined balancer <code>balancer://tomcatservers/javaee</code>. So when someone asks for <code>http://www.yoursite.com/javaee/hello.jsp</code> the virtual host will request that JSP to a randomly chosen Tomcat in the balancer members.</p>
<p>What&#8217;s the <code>stickysession</code> attribute? It&#8217;s a very useful configuration parameter used in conjunction with the <code>route</code> attributes, defined before.</p>
<p>As probably every Java EE (or Web) developer should know, while browsing on a server, it keeps trace of some data about the browsing session in a server-side <em>HttpSession</em> object. For example an ecommerce web application needs to store somewhere the information about the shopping cart of non registered users.</p>
<p>How the server can associate the remote session data with the specific navigation session? This is done through a cookie (or via a GET parameter in the URL) that gives to the server the <em>session ID</em> value.</p>
<p>In Java EE applications, the cookie name to identify the sessions is <code>JSESSIONID</code>.</p>
<p>This is closely related to the management of the load balancing between the Tomcat servers.</p>
<p>If Apache picked randomly one of the Tomcat to handle a single request and if the next request from the same user/browser was forwarded by the balancer <em>to another Tomcat</em> in that battery, things wouldn&#8217;t work correctly.</p>
<p>Each Tomcat doesn&#8217;t know anything of the existence of other Tomcat in that balancer configuration and especially a single Tomcat server cannot access the information of <em>http sessions</em> handled by another Tomcat.</p>
<p>In few words, when a Tomcat is chosen to handle the first request from a user/browser, it&#8217;s absolutely required that, to keep valid session data, the same Tomcat must be used to handle the following requests coming from that browser/user.</p>
<p>If not, on each request, the session data would be lost and simple tasks, such as building a shopping cart would result impossible.</p>
<p>So, it&#8217;s required to tell to Apache what is the session cookie name: <code>JSESSIONID</code> and which is the identifier of the routes to each single tomcat Server: <code>tomcatA</code>, <code>tomcatB</code>, <code>tomcatC</code>.</p>
<p>In this way, Apache will append to the end of the cookie value the information about the route to the specific Tomcat.</p>
<div id="attachment_83" class="wp-caption aligncenter" style="width: 490px"><img class="size-large wp-image-83" title="Java EE Tomcat Load Balancing" src="http://www.zulutown.com/blog/wp-content/uploads/2009/02/javaee-tomcat-load-balancing-480x320.png" alt="Java EE Tomcat Load Balancing" width="480" height="320" /><p class="wp-caption-text">Java EE Tomcat Load Balancing</p></div>
<p>Finally, the last thing to set-up Apache, is obviously to add to it the  modules required by the previous configuration:</p>
<ul>
<li>proxy</li>
<li>proxy_balancer</li>
<li>proxy_ajp</li>
</ul>
<p>About Tomcat configuration, there are just few changes to apply to the default configuration, in the <code>Engine</code> section it&#8217;s required to add the <code>jvmRoute</code> attribute.</p>
<pre>&lt;Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcatA"&gt;</pre>
<p>This is related to the <code>route</code> parameter defined in the Apache load balancer. So, <code>tomcatA</code> should be set in the configuration of the first Tomcat server, <code>tomcatB</code> in the second and <code>tomcatC</code> in the third one.</p>
<p>Then, the port where <em>AJP connector</em> listens, has to be set accordingly to the apache configuration, so <code>8009</code>, <code>8010</code>, <code>8011</code> respectively on the first, second and third Tomcat.</p>
<p>The following is the the configuration of the <em>AJP connector</em>:</p>
<pre>&lt;Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /&gt;</pre>
<p>It&#8217;s not directly related to the setup of the load-balancer, but since they run on the same host, each Tomcat should have its own set of ports.</p>
<p>To prevent any conflict you should change the following settings on the second and third servers: <code>Server port="8005"</code> and <code>Connector port="8080"</code>.</p>
<p>I hope this tutorial has given a complete overview about every step required to setup <em>Apache</em> and <em>Tomcat</em> to create a simple load-balanced cluster.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zulutown.com/blog/2009/02/16/java-ee-load-balancing-with-tomcat-and-apache/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
