Apache + Tomcat Cluster configuration detailed (1)
first, software preparation
Apache 2.2:http://httpd.apache.org/download.cgi, download MSI installer, select No SSL version
Tomcat 6.0:http://tomcat.apache.org/download-60.cgi, download tomcat 6.0.18 zip file
Note: Because the Apache and Tomcat project-related modules are in continuous development and optimization, the author does not guarantee that the configuration method in this article applies to all Apache and Tomcat versions.
second, software installation
Install Apache as a Windows service running on port 80 and you will see the Apache2.2 service in the list of system services after successful installation. For machines that have IIS installed, you must first stop the IIS Admin service before starting the Apache service, or it will not start because of a port conflict. After the service is started, enter http://localhost in the browser to test if you can see an "It works!" The page will indicate that Apache is working properly.
Unzip the tomcat zip file to two folders, T1 and T2 respectively, with the following T1 and T2 representing two tomcat servers. Configure the Java_home and CLASSPATH system environment variables, start T1 and T2 respectively, make sure Tomcat is available, and then turn off Tomcat.
This article only explains the configuration process, both Apache and Tomcat work on the same machine. There is no limit to the actual deployment, and Apache and individual tomcat can be deployed on separate servers.
three, Apache configuration
Apache 2.2 is a mod_jk feature that, compared to the 1.3 version, does not require cumbersome worker.properties configuration and greatly simplifies the configuration process.
First, locate the conf/httpd.conf file in the Apache installation directory and open it as a text editor.
Remove the comment (#) before the following text to allow Apache to automatically load the proxy module at startup. Java code LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_ajp_module modules/mod_proxy_ajp.so loadmod Ule 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_htt P.so
Pull the document down to find the node, after DirectoryIndex index.html add index.jsp, this step is only to be configured after Tomcat can see the kitten home, you can not do.
Continue to the drop-down document to find the include conf/extra/httpd-vhosts.conf, removing the previous comment.
Open the conf/extra/httpd-vhosts.conf with a text editor, configure the virtual site, and at the bottom, add
Java code <virtualhost *:80> ServerAdmin Administrator mailbox ServerName domain name (no IP address available) Serveralias Lo Calhost Proxypass/balancer://cluster/stickysession=jsessionid Nofailover=on Proxypassreverse/bala Ncer://cluster/errorlog "Logs/lbtest-error.log" Customlog "Logs/lbtest-access.log" Common </Vi Rtualhost>
Here balancer://is to tell Apache need to load balance agent, the following cluster is the cluster name, can be arbitrary, two log engine errorlog responsible for logging errors, Customlog is responsible for recording all HTTP access and return status, The log name can be taken by itself, the author takes it as Lbtest. Httpd-vhosts.conf configuration, back to httpd.conf, at the bottom of the document to add
Java code proxyrequests Off <proxy balancer://cluster> balancermember ajp://127.0.0.1:8009 LOADFA Ctor=1 route=jvm1 balancermember ajp://127.0.0.1:9009 loadfactor=1 route=jvm2 </proxy>
Proxyrequests Off is to tell Apache to use reverse proxy (using Apache to load balance must use reverse proxy, about more load balancer and reverse proxy details can refer to the author another blog/http/ zyycaesar.javaeye.com/admin/blogs/293839), used to configure all nodes working in the Tomcat cluster, where the "cluster" must be consistent with the cluster name above.
Apache communicates with Tomcat via the AJP protocol, and the IP address and port uniquely identify the Tomcat node and the configured AJP accept port. Loadfactor is a load factor, Apache forwards requests to the Tomcat node in proportion to the load factor, and the larger the load factor, the more requests will be processed by the corresponding Tomcat server, such as two Tomcat is 1,apache on a 1:1 scale. If it is 2 and 1, it is forwarded at a 2:1 ratio. The route parameter corresponds to the engine path (Jvmroute) in the subsequent Tomcat configuration.
To restart the Apache service, if accessing http://localhost/at this time will return a 503 error, open the error log just configured logs/ Lbtest-error.log, you can see that the reason for the error is because the background server is not responding because Tomcat is not configured and started at this time.
Apache + Tomcat Cluster configuration detailed (2)
Four, tomcat configuration
If you just want to configure an available cluster, the configuration of Tomcat will be very simple. Open the Server.xml configuration file for T1 and T2 respectively, and for T1, use the default settings as much as possible, and make large changes to T2 to avoid conflicts with T1. If T2 and T1 are not running on the same server, there is no need to change the port.
The first is to configure the shutdown port, find, T1 unchanged, change the T2 to 9005.
Configure the Connector port below, locate the Non-ssl http/1.1 Connector, which is the default Connector when Tomcat works alone, leave the T1 default configuration, listen on port 8080, and set the T2 to listen on port 9080.
Down to AJP 1.3 Connector, this is the port that Tomcat uses to receive AJP connection requests from Apache, leaving the T1 default setting to change the T2 port to 9009. Note that the port here corresponds to the AJP connection port configured in Balancermember in Apache httpd.conf.
Continue to configure the engine downward, find, remove the paragraph or change to a comment, remove the comment immediately above, for T2, remove the comment and change the jvm1 to jvm2. The jvmroute here corresponds to the route parameters configured in Balancermember in Apache httpd.conf.
Down, open the note, which is configured so that sessions can be shared among all tomcat nodes in the cluster. If only to obtain a usable tomcat cluster, cluster only need to be so configured, for more cluster configuration I will explain the other.
The configuration of the Server.xml is complete and the next step is to configure the specific application. Create a new test directory under the WebApps directory and create a new test.jsp file in the test directory with the following code:
Java code <%@ page contenttype= "TEXT/HTML;&NBSP;CHARSET=GBK" %> <%@ page import= "java.util.*" %>