Apache2.2+Tomcat6 configuration cluster, load balancing, session sharing under Windows
1Download the software
Apache tomcat
Apache: apache_2.2.21 1
Tomcat: apache-tomcat-6.0.29 (zip version) 2
mod_jk: mod_jk.so 1个 (tomcat-connectors-1.2.32-windows-i386-httpd-2.2.x)
mod_jk, note that it matches the apache version
1.1apache software download
1.1.1The difference between the two types of apache software no_ssl and openssl
The download address of Apache is: http://httpd.apache.org/download.cgi
There are two types of the same version of apache software: no_ssl and openssl:
openssl has an additional ssl security authentication mode. Its protocol is HTTPS instead of HTTP. This is the difference between a server with SSL and a general web server.
In general, we download the no_ssl version.
1.2mod_jk download
address:
http://www.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/windows/
2Apache software installation
2. Port 180 is occupied
http://www.blogjava.net/rabbit/archive/2008/03/12/185559.html
2.2 Successful installation
Browser address bar access: http://localhost
appear:
3 Part 1: Load Balancing
Load balancing means that apache distributes customer requests in a balanced manner to tomcat1, tomcat2.... to process
3.1 Install apche, tomcat
My installation path:
D:\apache2.2
D:\webServer\tomcat_1
D:\webServer\tomcat_2
3.2 Install mod_jk.so
Copy mod_jk.so to the D:\Apache2.2\modules directory.
3.3 Modify the Apache configuration file D:\Apache2.2\conf\httpd.conf
Just add the following sentence at the end of the file
Include conf/mod_jk.conf
3.4 httpd.conf Create a new mod_jk.conf file in the same directory with the following contents:
#Load mod_jk Module
LoadModule jk_module modules/mod_jk.so
#Specify the worker.properties file path
JkWorkersFile conf/workers.properties
#Load worker's request processing allocation file
JkMountFile conf/uriworkermap.properties
#Specify the log output file of jk
JkLogFile logs/mod_jk.log
#Specify the log level
JkLogLevel warn
3.5 Create a new workers.properties file in the same directory as http.conf with the following contents
worker.list = controller,tomcat1,tomcat2 #server 列表
#========tomcat1========
worker.tomcat1.port=8009 #ajp13 Port number, configured in server.xml under tomcat, default 8009
worker.tomcat1.host=localhost #host address of tomcat, if not this machine, please fill in the ip address
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor = 1 #The weighted weight of the server, the higher the value, the more requests it gets
#========tomcat2========
worker.tomcat2.port=9009 #ajp13 Port number, configured in server.xml under tomcat, default 8009
worker.tomcat2.host=localhost #host address of tomcat, if not this machine, please fill in the ip address
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor = 1 #The weighted weight of the server, the higher the value, the more requests it gets
#========controller, load balancing controller========
worker.controller.type=lb
worker.controller.balance_workers=tomcat1,tomcat2 #Specify the tomcat that shares the request
worker.controller.sticky_session=false
3.6 Create a new uriworkermap.properties file in the same directory as http.conf, the content is as follows
#All requests are handled by the controller server
/*=controller
#All requests that contain jkstatus are processed by the status server
/jkmanager/*=jkstatus
#All requests ending with .gif are not processed by the controller server, the following are the same meaning
!/*.gif=controller
!/*.jpg=controller
!/*.png=controller
!/*.bmp=controller
!/*.css=controller
!/*.js=controller
!/*.htm=controller
!/*.html=controller
!/*.swf=controller
3.7 Modify the tomcat configuration file server.xml
If you install tomcat on different computers, the number of tomcat installations is one, you do not need to modify the tomcat configuration file
I am here to install two tomcats on the same computer, so I need to change the settings of one of the port configurations that need to be modified:
(1)<Server port="8005" shutdown="SHUTDOWN">
change to
<Server port="9005" shutdown="SHUTDOWN">
(2)<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000"
redirectPort="8443" />
change to
<Connector port="9080" protocol="HTTP/1.1" connectionTimeout="20000"
redirectPort="8443" />
(3)<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
change to
<Connector port="9009" protocol="AJP/1.3" redirectPort="8443" />
3.8 Writing test jsp
(1) Create a directory test. Create a new test.jsp in it, the content is
<%
System.out.println("===========================");
System.out.println(session.getAttribute("test"));
session.setAttribute("test","Session");
%>
<html>
<body bgcolor="red">
<center>
<h1>Tomcat 1</h1>
</body>
</html>
Put the test under the webapps of tomcat1
(2) Create a directory test. Create a new test.jsp in it, the content is
<%
System.out.println("===========================");
System.out.println(session.getAttribute("test"));
session.setAttribute("test","Session");
%>
<html>
<body bgcolor="blue">
<center>
<h1>Tomcat 2</h1>
</body>
</html>
Put the test under the webapps of tomcat2
3.9 Start apache, tomcat1, tomcat2 for testing
Access via http://localhost/test/test.jsp,
If a red page is displayed, it indicates the processing result of tomcat1;
If the blue page is displayed, it indicates the processing result of tomcat2.
After refreshing several times, I found that the pages of the two colors appeared in turn, which achieved load balancing.
Looking at the window of tomcat1, you can see that a line of "==========" is printed
Refresh again, tomcat2 also prints one, and then refresh, you can see that the request will be processed by tomcat1 and tomcat2 in turn, realizing load balancing
At this point, the load balancing setting is complete. It is not enough to only configure load balancing, but also session replication, that is to say, the added session of any one of the tomcats is to be replicated to other tomcats synchronously, and the tomcats in the cluster have the same session.
4 The second part, configure the cluster
4.1 Modify the server.xml of tomcat1 and tomcat2
(1) About line 102, there is <Engine name="Catalina" defaultHost="localhost">
Modify it, add jvmRoute="tomcat1",
tomcat1 改完后:<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
tomcat2 改完后:<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">
(2) About line 109, <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/> is commented out.
After this comment, add the following:
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="8">
<!--
<Manager className="org.apache.catalina.ha.session.BackupManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"
mapSendOptions="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"<!-- tomcat2 modify it, port="4002" -->
autoBind="100"
selectorTimeout="5000"
maxThreads="6"/>
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender" timeout="60000" keepAliveCount="120000"/>
</Sender>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>
</Channel>
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=""/>
<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
<!-- This paragraph can be omitted, Tomcat does not support it yet, only the interface, if configured, will report an error (serious: FarmWarDeployer can only work as host cluster subelement!).
<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>
4.2 Modify the test project test (both test.jsp can be changed to the same)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page import="java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Cluster App Test</title>
</head>
<body >
Server Info:
<%
out.println(request.getLocalAddr() + " : " + request.getLocalPort() + "<br>");
%>
<%
out.println("<br> ID " + session.getId() + "<br>");
// If there is a new Session property set
String dataName = request.getParameter("dataName");
if (dataName != null && dataName.length() > 0) {
String dataValue = request.getParameter("dataValue");
session.setAttribute(dataName, dataValue);
}
out.print("<b>Session 列表</b>");
Enumeration e = session.getAttributeNames();
while (e.hasMoreElements()) {
String name = (String) e.nextElement();
String value = session.getAttribute(name).toString();
out.println(name + " = " + value + "<br>");
System.out.println(name + " = " + value);
}
%>
<form action="test.jsp" method="POST">名称:<input type=text
size=20 name="dataName"> <br>
值:<input type=text size=20 name="dataValue"> <br>
<input type=submit></form>
</body>
</html>
4.3 Then create a new WEB-INF directory in test, and create a new web.xml under WEB-INF, the content is as follows
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>test</display-name>
<distributable/>
</web-app>
4.4 Copy the test to the webapps of tomcat1, tomcat2, restart apache, tomcat1, tomcat2
Enter the URL http://localhost/test/test.jsp to test.