We will cover Three Steps
1. Running Multiple Tomcat Servers and One Apache as a Request Dispatcher to the available server.
2. Having a sticky session maintenance.
3. Making Clusters of Tomcat Servers so that they can share session also.
Step: 1 Running Multiple Tomcat Servers and One Apache as a Request Dispatcher to the available server.
it will follow 6 steps
1. Install Apache2
2. Load mod_jk2
3. Install Tomcat5 (Same Version) Instances
4. Configure JK2
5. Make a sample application
6. Test Scenario
1. Install Apache2
First of all Install Apache2 and run it.
After installation To Configure Apache2 go to the …/Apache2/conf/httpd.conf File and read it carefully to update server settings. It has some sections in it , Go to the
Code: Select all
[Section 1: Global Environment]
e.g Listen localhost:8083
If you will have installed Apache2 as a service you will find Service Monitor running near time bar. Otherwise you can install Apache2 as a service
Code: Select all
Apache –k install
Till now it is assumed that Apache2 is successfully running on the machine. Test it from browser.
2. Load mod_jk2
Download mode_jk2 binaries (jakarta-tomcat-connectors-jk2.0.4-win32-apache2.0.49) on your local drive and extract it you will find a modules directory copy mod_jk2 module from modules directory and place it in Apache2/modules directory. And write the following line in Apache2/cong/http2.conf File
Code: Select all
LoadModule jk2_module modules/mod_jk2.so
3. Install Tomcat5 Instances
Install 2 or 3 (Tomcat1, Tomcat2, Tomcat3) instances of Tomcat5 on different ports. To change the required ports and to do other settings follow these steps.
a.Go to Tomcat1/conf/server.xml
b.This port should be different from all so arrange any 3 ports like 8005, 8006, 8007 for Tomcat1, Tomcat2 and Tomcat3 respectively.
Code: Select all
<Server port="8005" shutdown="SHUTDOWN">
Code: Select all
<Connector port="8091" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" />
Code: Select all
<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8009" enableLookups="false"
redirectPort="8443" protocol="AJP/1.3" />
Code: Select all
<!-- You should set jvmRoute to support load-balancing via AJP
ie :-->
<Engine name="Standalone" defaultHost="localhost"
jvmRoute="server1">
4. Configure JK2
Create a new file workers2.properties in Apache2/conf directory and write the following contents in it…
Code: Select all
[shm:]
info=Shared memory
file=anonymous
[channel.socket:server1]
host=127.0.0.1
port=8009
tomcatId=server1
ver=0
[channel.socket:server2]
host=127.0.0.1
port=8010
tomcatId=server2
ver=0
[channel.socket:server3]
host=127.0.0.1
port=8011
tomcatId=server3
ver=0
[lb:lb]
info=Load balancer
ver=1
sticky=0
[uri:/test/*]
info=Examples Web application
group=lb:lb
ver=1
[status:status]
info=Status worker, displays runtime informations
[uri:/jkstatus/*]
info=Display status information and checks the config file for changes.
group=status:status
[channel.socket:server1]
host=127.0.0.1
port=8009
tomcatId=server1
ver=0
is used to add server. server1 is the jvmRoute that we have defined in server.xml file. 8009 port is the port that we have defined for AJP 1.3
ver we will discuss later if required…
[lb:lb]
info=Load balancer
ver=0
sticky=0
We define the load balancer by this property lb:loadbalancername
We will discuss sticky later
[uri:/test/*]
info=Examples Web application
group=lb:lb
ver=1
Using this property we define the context uri in our tomcat servers to have random requests.
Other two are not necessary to be explained now.
5. Make A Sample Application
As we have defined in context uri make a test application having only one servlet in it ActionServlet ..
Code: Select all
ackage com.jmobiles.loadbalancing;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class ActionServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
System.out.println("From Server 1");
out.println("<html>");
out.println("<head><title>ActionServlet</title></head>");
out.println("<body bgcolor=\"#ffffff\">");
if(session.getAttribute("USER") == null){
out.print("<h2>Session Not Set</h2><br/>");
session.setAttribute("USER", "hello");
}else{
out.print("<h2>Session Set ["+session.getAttribute("USER")+"] </h2><br/>");
}
out.println("<p>I am from Server 1 ....................</p>");
out.println("</body></html>");
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}
Code: Select all
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>com.jmobiles.loadbalancing.ActionServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>/ac</url-pattern>
</servlet-mapping>
http://localhost:8091/test/ac
it will display messages for Server 1 on console of the server and also on the browser.
6. Test Scenario
Start Apache2 service and all three tomcat servers Tomcat1, Tomcat2, Tomcat3. And access the url
http://localhost:8083/test/ac
You will see it will first show messages for Server1 then for Server2 and then for Server3 and it will continue in a round robin fashion on consecutive requests.
Step: 2 Having sticky session maintenanace
JK2 has two methods for request dispatching
1.Round Robin Fashion
2.Sticky Fashion
We have seen the Round Robin Fashion. First of all we see that what is a sticky fashion?
Sticky fashion means Apache once found an available server will redirect all the requests to that server during that session. So the session can be maintained in this way but if that server is down then Apache will find the other available server and will stick to that until it is dead. But switching the server the session will be lost.
To achieve this we have to change the load balancer property from workers2.properties as by adding the variable sticky=1 by default the value of sticky is 1.
Step: 3 Making Clusters of Tomcat Servers so that they can share session als
Making Clusters of the servers it is possible to share the session anyway if one goes down other is up and vise versa running at least one at a time.
To enable this behaviour follow the steps defined below
1.Go to the server.xml and uncomment the Cluster tag and keep in mind to have different Reciever tcpListenPort like in this case start from 4001 –to- 4003 for all three servers.
2.Now go to the web.xml of the test application and add
<distributable /> after the display name tag.
3.Restart the servers you will see on console that they will find each other to have a shared session
4.Now again test by accessing the URL
http://localhost:8083/test/ac
Test it by stopping restarting different servers it will never loose the session
value until all the servers are dead.
Note: It is necessary to have serialized objects in session because they have to travel on network in this case. [/url]