Implementing the application cluster under Windows Apache+tomcat
1 Environmental dependencies: Windows, jdk1.6, Apache2.2, tomcat6.0
2 Jdk1.6 Installation
2.1 Slightly
3 Installing Apache (2.2.27)
3.1 Downloads: from Apache official website http://httpd.apache.org/download.cgi download Httpd-2.2.25-win32-x86-openssl-0.9.8y.msi;
3.2 Installation: Double-click the previous download of the installer, continue to the next step until completion, the Installation Wizard completed successfully, the left-hand corner of the tray will appear the Apache icon.
3.3 Test: Open the browser, enter http://localhost or http://127.0.0.1 in its address bar, open after you can see the word "It works", indicating that the installation was successful.
4 Configuring Apache
The Apache2.2 integrates the MOD_JK functionality, which eliminates the need for configuration of the worker.perproties in relation to version 1.3, making the configuration process much simpler.
4.1 Configuring the Boot auto-load proxy module (httpd.conf)
Open the ${apache_home}/conf/http.conf file with a text editor and remove the comment (#) before the text
LoadModule Proxy_module modules/mod_proxy.so
LoadModule Proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule Proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule Proxy_connect_module modules/mod_proxy_connect.so
LoadModule Proxy_ftp_module modules/mod_proxy_ftp.so
LoadModule Proxy_http_module modules/mod_proxy_http.so
4.2 Configuration Homepage
On the basis of the previous step, continue to find the words "DirectoryIndex index.html", after which additional index.jsp
4.3 Turning on the virtual node
On the basis of the previous step, continue to find the word "Include conf/extra/httpd-vhosts.conf" and remove the previous comment
4.4 Configuring virtual Nodes
Open the ${apache_home}/conf/extra/httpd-vhosts.conf file with a text editor and append the following to the last face
<virtualhost *:80>
ServerAdmin[email protected]# Admin Mailbox
ServerNamewww.domain.com# Default domain name, no IP can be used instead
Serveraliaslocalhost# aliases, separated by multiple spaces
Proxypass/Balancer://Cluster/Stickysession=jsessionid Nofailover=on
proxypassreverse/balancer://Cluster/
Errorlog "logs/Cluster_test-error.log"
Customlog "logs/Cluster_test-access.log"Common
</VirtualHost>
The above balancer://is to tell Apache to need a load balancer proxy, the following cluster is the cluster name, the name can be defined by itself, and then *log as the log engine, responsible for logging.
4.5 Configuring the Proxy
Go back to the ${apache_home}/conf/http.conf file and append the following format to the end
Proxyrequests OFF # using reverse proxy
<proxy balancer://Cluster>
Balancermember ajp://127.0.0.1:18009Loadfactor=1 route=jvm1
Balancermember ajp://127.0.0.1:28009Loadfactor=1 route=jvm2
</proxy>
The above cluster needs to be consistent with the cluster name in 4.4, Apache communicates with Tomcat through the AJP protocol, the IP address and port number determine the Tomcat,loadfactor as the load factor, and Apache forwards the request to tomcat by factor ratio, with the route tomcat In the Jvmroute, see chapter.
4.6 Testing
Restart the Apache service, Access http://localhost at this time, there will be a 503 error, indicating normal, because Tomcat has not been configured and started.
5 Installing Tomcat
5.1 Downloads
Slightly
5.2 Installation
Slightly
6 Configuring Tomcat
Because it is a tomcat cluster, this article takes two tomcat as an example, it is named Tomcat1, Tomcat2.
6.1 Configuring Server Port
Open ${tomcat_home_1}/conf/server.xml with a text editor,
Found: <server port= "8005" shutdown= "Shutdown" >
Modified to: <server port= "18005"Shutdown=" > "Shutdown"
6.2 Configuration Connector Port
On the basis of the previous step, continue looking down,
Found: <connector port= "8080" protocol= "http/1.1" connectiontimeout= "20000" redirectport= "8443"/>
Modified to: <connector port= "18080"Protocol=" http/1.1 "connectiontimeout=" 20000 "redirectport="18443"uriencoding=" UTF-8 "/>
Continue searching down
Found: <connector port= "8009" protocol= "ajp/1.3" redirectport= "8443"/>
Modified to: <connector port= "18009"Protocol=" ajp/1.3 "redirectport="18443"/>
Note: Port here is the port where Apache communicates with Tomcat via the APJ protocol to achieve load balancing, so ports 18009 should correspond to the 4.5 section.
6.3 Configuring the Engine
First off <engine name= "Catalina" defaulthost= "localhost" >
Then turn on <engine name= "Catalina" defaulthost= "localhost" jvmroute= "jvm1">
Note: The value of Jvmroute here jvm1 should correspond to the route in section 4.5.
6.4 Configuring Cluster
Continue looking down in the current file to find the <cluster classname= "Org.apache.catalina.ha.tcp.SimpleTcpCluster"/>, the default Cluster configuration is not turned on, Here we uncomment, make it open, the main role is to make each Tomcat sharing session, it is reported in the case of high pressure, the loss of the session, this paragraph <cluster/> is a shorthand for the code, its complete code is as follows:
# synchronous asynchronous mode is controlled by the channelsendoptions parameter, the default value is 8, the asynchronous mode, and 4 is the synchronous mode. In asynchronous mode, you can increase reliability by adding a copy acknowledgment (acknowledge), at which point the channelsendoptions is set to 10.
<cluster classname= "Org.apache.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= "/> <receiver classname=" Org.apache.catalina.tribes.transport.nio.NioReceiver "address=" Auto " port=" 15900 " autobind=" 100 " selectortimeout= " 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> # filter= ". *\.gif;. *\.js;. *\.jpg;. *\.png;. *\.htm;. *\.html;. *\.css;. *\.txt; " /> <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>
<valve classname= "Org.apache.catalina.ha.session.JvmRouteBinderValve"/> and <clusterlistener in the above code Classname= "Org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/> configuration is in use MOD_JK and is not used sticky Sessions or sticky sessions is not working when the solution, we do not use MOD_JK, there is a large pressure session replication failure, so we need to remove the above configuration of the two lines, and then replace the <cluster Classname= "Org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
6.5 Configure other Tomcat for the cluster, repeat the 6.1~6.4 operation to configure other Tomcat, mainly to change the port number (where the green logo is in the text)
7 Test Load Balancing
7.1 Test Project Preparation
Create a new Web project, named Cluster_demo;
Create a new index.jsp page in the project and add the following to the body of the page:
<% String SessionID = Session.getid (); System.out.println ("Current SessionID =" + SessionID);%>
7.2 Deployment and launch
Deploy the test project you established in the previous step to Tomcat for the cluster.
Mode one: Put the project under ${tomcat_home_*}/webapps;
Method Two: Add under the Host node in the ${tomcat_home_*}/conf/server.xml of TOMCAT for the cluster
<context path= "/cluster_demo" docbase= "D:/apache_tomcat/projects/cluster_demo" reloadable= "true" crossContext= " False "privileged=" true "></Context>
Then start tomcat in the cluster, and then start the Apache service
7.3 Concurrent Access
Use the AB in Apache for concurrent access simulation testing.
Open a command-line window and switch to ${apache_home}/bin
CD ${apache_home}/bin
Ab–n 20–c http://localhost/cluster_demo/index.jsp
You can see the output information in each Tomcat console and whether you received an average of 20 requests.
As shown in the running results, two Tomcat can be seen averaging 20 requests:
8 Test session Sharing
8.1 Shared Configuration
In the test project's Web. XML, add the <distributable/> element, which informs the Tomcat server that the current application needs to share the session across all nodes of the emergency population.
Important: All objects in the session should be serializable, implementing the Java.io.Serializable interface.
8.2 Test Preparation
Based on the 7.1 chapters, the contents of the modified index.jsp are
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><! doctype html public "-//w3c//dtd html 4.01 transitional//en" > 8.3 Validation Tests
After the redeployment starts, accesses the http://localhost/cluster_demo/index.jsp, adds some information to the session on the page, can see the following effect, explained session sharing, does not explain.
At this point, it's done!