Apache + Tomcat Cluster load balancing

Source: Internet
Author: User

1. Version description

Apache: 2.2.21;

Tomcat: 6.0.x;

2. Download and install

Tomcat: http://tomcat.apache.org/download-60.cgi

Apahce: http://httpd.apache.org/download.cgi

Install Tomcat:

Decompress the downloaded Tomcat package to the Directory D: \ tomcat1 and D: \ tomcat2 respectively, enter the directory bin under the command line to install Tomcat, and run the command: service. bat install custom Tomcat name;

My services are service. Bat install tomcat1 and service. Bat install tomcat2;

Since the Tomcat name is defined at the time of installation, the tomc6w.exe file in the bindirectory cannot be started. Tomcat can be started only in the local service (which can be opened by the services. msc command). (If anyone has a good idea, share it with me );

Follow the Wizard to install Apache. After the installation is complete, start the http: // localhost test in the browser. If the installation is successful, an IT works prompt is displayed;

3. Environment Configuration

Apache configuration:

1. Go to the conf folder of the Apache installation directory, open the httpd. conf file with a text file, and remove the following lines of text before:

LoadModule proxy_module modules/mod_proxy.soLoadModule proxy_ajp_module modules/mod_proxy_ajp.soLoadModule proxy_balancer_module modules/mod_proxy_balancer.soLoadModule proxy_connect_module modules/mod_proxy_connect.soLoadModule proxy_ftp_module modules/mod_proxy_ftp.soLoadModule proxy_http_module modules/mod_proxy_http.so

2. Continue searching. Remove the # before the line of text and save it:

# Virtual hostsInclude conf/extra/httpd-vhosts.conf

3. Use the text document to open the conf/extra/httpd-vhosts.conf file, configure the Virtual Site, add the following content at the bottom:

<Virtualhost *: 80> serveradmin administrator email servername localhostserveralias localhostproxypass/balancer: // cluster/stickysession = jessionid nofailover = onproxypassreverse/balancer: // clusterproxyrequests offproxypreservehost onerrorlog "logs/test_error.log" customlog "logs/test_access.log" commmon </virtualhost>

Balancer: // indicates the Server Load balancer proxy for Apache. The cluster is the cluster name and can be retrieved at will. The errorlog of the two log engines records errors, customlog records all HTTP access and return statuses. You can retrieve the log name by yourself;
Httpd-vhosts.conf configuration is complete;

4. Open the httpd. conf file again and add the following content at the bottom:

ProxyRequests Off<proxy balancer://cluster>BalancerMember ajp://127.0.0.1:8009 loadfactor=1 route=jvm1BalancerMember ajp://127.0.0.1:9009 loadfactor=1 route=jvm2</proxy>

Proxyrequests off tells Apache to use a reverse proxy. <proxy balancer: // cluster> is used to configure all nodes in the Tomcat cluster, the "cluster" must be the same as the preceding cluster name. Apache communicates with Tomcat through the AJP protocol. The IP address and port uniquely determine the Tomcat node and the configured AJP accept port. Loadfactor is the load factor. Apache will forward requests to the backend Tomcat Node Based on the load factor. The larger the load factor, the more requests the corresponding Tomcat server will process, for example, if both Tomcat servers are 1, Apache forwards data in a ratio of. If the servers are 2 and 1, Apache forwards data in a ratio. The route parameter corresponds to the engine path (jvmroute) in the subsequent tomcat configuration ).

Restart the apache service. If you access http: // localhost/at this time, the error 503 will be returned. Open the configured Error Log logs/test-error.log and you will see the error because the background server has no response, at this time, tomcat has not been configured and started.

Tomcat configuration:

1. Modify the port to avoid conflicts when two Tomcat servers are started at the same time. Here, only the conf/server. xml file of Tomcat 2 is modified, and three ports need to be modified;

First, modify and close the port, find the following line of code, and change the port number 8005 to 9005;

<Server port="8005" shutdown="SHUTDOWN">

Second, modify the connector port, find the following line of code, and change the port number 8080 to 9080;

<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />

Third, modify the port AJP/1.3 connector. The following code changes the port number 8009 to 9009;

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

2. Modify jvmroute and find the <engine name = "Catalina" defaulthost = "localhost"> line of code to comment it out and remove the comments of the line above it, modify the value of jvmroute to "jvm2". Here, jvmroute corresponds to Apache httpd. the route parameter configured in balancermember in Conf. After modification, the effect is as follows:

<!-- You should set jvmRoute to support load-balancing via AJP ie :--> <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm2">         <!-- <Engine name="Catalina" defaultHost="localhost"> --> 

3. Configure the shared sessions among all Tomcat nodes in the cluster and find <cluster classname = "org. apache. catalina. ha. TCP. simpletcpcluster "/> This line of code removes its comment; server. xml configuration complete;

Now, the Apache + Tomcat cluster is configured and can be tested. for the project to be deployed, you must go to the project web. add the following content to the root node of the XML configuration file <web-app>:

<distributable />

Restart Apache and start tomcat1 and tomcat2 in the local service in sequence. Test and access the project deployed in the browser. If either tomcat1 or tomcat2 is in the running state, web access.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.