ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Tomcat7 Clustering using Apache HTTP server & Microsoft IIS server

Updated on December 21, 2013

1. Creating multiple tomcat instances and configuring it to enable clustering

Download tomcat 7 server from the link below.

Download Tomcat 7

Unzip the folder and name it as Tomcat1, create an exact copy of the folder and name it as tomcat2.

Now edit the server.xml file present in the conf directory of both tomcat instances to enable clustering. Server shutdown port, HTTP 1.1 connector port, AJP 1.3 connector port and jvmRoute values should be distinct in each tomcat instance.

Configurations for tomcat 1

Configuring different attributes

Attribute
Tomcat 1
Tomcat 2
Shutdown port
8105
8205
HTTP 1.1 connector port
8081
8082
AJP 1.3 connector port
8109
8209
jvmRoute
node1
node2

Now the most important step is to uncomment cluster tag in server.xml files of both the tomcat instances. And configure it with clustering options. A sample configuration is given below.

This cluster element should be inside engine or host element. Now clustering is enabled between both the tomcat instances.

Sample server.xml

<Cluster className="org.<span id="vnlmx9_5" class="vnlmx9"><span id="irmdol_5" class="irmdol">apache</span></span>.catalina.ha.tcp.SimpleTcpCluster"

channelSendOptions="8">

<Manager className="org.apache.catalina.ha.session.DeltaManager"

expireSessionsOnShutdown="false"

notifyListenersOnReplication="true"/>

<Channel className="org.apache.catalina.tribes.group.GroupChannel">

<Membership className="org.apache.catalina.tribes.membership.McastService"

address="228.0.0.4"

port="45564"

frequency="500"

dropTime="3000"/>

<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"

address="auto"

port="4000"

autoBind="100"

selectorTimeout="5000"

maxThreads="6"/>

<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">

<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>

</Sender>

<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>

<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>

</Channel>

<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"

filter=""/>

<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>

<Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"

tempDir="/tmp/war-temp/"

deployDir="/tmp/war-deploy/"

watchDir="/tmp/war-listen/"

watchEnabled="false"/>

<ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>

<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>

</Cluster>



## Reference : http://tomcat.apache.org/tomcat-7.0-doc/cluster-howto.html

Now deploy the application in both the tomcat instances’ webapps directory. And for clustering to work, <distributable /> tag should be inserted inside web.xml of the application.


Configurations for tomcat instances are over. Now we can move to configurations of webservers for load balancing and managing requests to tomcat instances.

Conf Folder
Conf Folder

2. Configurations for Apache HTTP Server

We will use mod_jk for load balancing. For this first download mod_jk.so file and place it in the modules folder under Apache home directory. We can either get the native source file which can be compiled to create the .so file or directly use a precompiled mod_jk.so file. Please download this file from net. Now create two files namely, mod-jk.conf and workers.properties inside the conf folder of apache home directory.


Now include the line highlighted in the picture to httpd.conf file

We can also set the server root and listener port attributes of the server. If we copy the server folder from installed location to a new location, change the server root attribute to new location. Similarly, by default the server listener port is 80. If port 80 is already being used by another process we can change this port to another port, say 8080, in the httpd.conf file.

1) mod-jk.conf file

Here the last line JKMount /examples/TestApp/*.jsp loadbalancer indicates that request to any jsp file in the path /examples/TestApp of tomcat instance’s webapps directory will be load balanced by the worker ‘loadbalancer’. This load balancer worker is defined in the worker.properties file.

2) Workers.properties file

Here loadbalancer is the worker that balances load between two tomcat instances. Node1 is the worker corresponding to tomcat1 and node2 is the worker corresponding to tomcat2. This should match with the value we set for jvmRoute attribute set in server.xml of corresponding tomcat instance and also the worker ports should match with that of ajp connector port in server .xml. Enabling sticky session ensures that once a request is being served by a tomcat instance the subsequent requests from same session are forwarded to the same tomcat instance until there is a server failure. Now we have successfully configured the settings for tomcat clustering and load balancing with apache http server.

3. Configurations for IIS 7 server

In IIS we use isapi_redirect.dll file for connecting to tomcat instances. A working version of this can be downloaded from the following url.

First we need to configure the jk 1.2 Connector which will allow IIS to effectively act as a proxy and forward requests on to Tomcat. For this we require isapi_redirect.dll file and isapi_redirect.properties file. The isapi_redirect.properties file tells the connector where to find its configuration files and also where the isapi_redirect.dll file is located. The folder structure I used is given.


isapi_redirect.dll and isapi_redirect.properties files are kept in ISAPI folder. Logs folder is for keeping log files. The workers.properties and uniworkermap.properties files are kept in conf folder.

1) isapi_redirect.properties file

2) workers.properties file

Here wlb is the load balancer worker and node1 and node2 corresponds to tomcat workers. The configurations are same as in the case of apache Http server.

3) uriworkermap.properties file

Configuring IIS

Now we need to configure IIS. Go to run and type inetmgr or directly access IIS manager. For this you need to have admin right on the machine. The start page looks like as shown below.

Now we will create a new site for our application. For this right click on the ‘Site’ in the left pane. And select Add Web site.



The window will appear as shown. Select different fields as applicable.


Now create a virtual directory by right clicking on the site name and enter the fields.


Here the Alias name should match with the extension_uri line of our isapi_redirect.properties file.

Next we need to configure the virtual directory to have execute permission. For this click on the jakarta virtual directory on the left pane and then double-click Handler Mappings as shown below.

Within the Handler Mappings feature click Edit Feature Permissions in the Actions Pane.

Check ‘Execute’ in the Edit Feature Permissions dialog box and click OK. Now the isapi dll files will be enabled in handler mappings feature.

Next we need to add an ISAPI filter on the website. To do this click on the website and double click ISAPI Filters feature.

In the Actions pane click Add. In the Add ISAPI Filter dialog box enter a name and the path to the isapi_redirect.dll file and click OK.

The final step is to configure the ISAPI and CGI Restrictions feature in IIS. Go to the Server Home and then double click on the ISAPI and CGI Restrictions feature.

In the ISAPI and CGI Restrictions feature click Add on the Actions pane. In the Add ISAPI or CGI Restriction dialog box enter a name and the path to the isapi_redirect.dll file, tick the Allow extension path to execute option and click OK.

The configurations are completed now and now we need to test the functionality.

4. Testing the functionality

I have created a counter.jsp file as shown below.

Here counter is a session variable and it is set to 1 when a session starts. For each subsequent visit the counter value increases for the single session and is again reset to 1 when a new session begins. Session Id and creation time is also displayed. The first line indicates which tomcat server serves the request. Now create a copy of this jsp and Change the first line to

‘Tomcat 2’. Create a folder named TestApp inside the “webapps/examples” folder in both the tomcat instances. And paste the corresponding jsp file inside this folders.

Now start the webserver(either Apache or IIS) and both tomcat instances.

Run http://localhost:8081/examples/TestApp/counter.jsp in the browser. It will show the request served by first tomcat instance.

On refreshing the page the counter will increase and it will be shown as an existing session.

Next open a new browser and check http://localhost:8082/examples/TestApp/counter.jsp. This will show the request served by second tomcat instance.

Now open new browser and run http://localhost/examples/TestApp/counter.jsp. This request is handled by the webserver and it redirects the request to any of the tomcat instance.

Let the request be now served by Tomcat 1.

Now shut down Tomcat 1. And run the above url, now the request will be redirected to Tomcat 2 and the session will be retained.

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: "https://hubpages.com/privacy-policy#gdpr"

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)