Apache + Tomcat + mod_jk for Cluster service

Source: Internet
Author: User
Tags session id server port

The cluster principle in Tomcat is to find the nodes in a multicast manner and use a TCP connection for session replication.

Implementation effect: Distribute requests to the corresponding project in Tomcat with Apache

Environment Description:

Operating system: Windows XP

javasdk:1.7

apache:2.2.14 (Local installation path: D:\Apache2.2\)

tomcat:7.0.42 (http://tomcat.apache.org/download-70.cgi), if simulated on the same machine, download the zip version. 2 nodes are shown in the example

mod_jk:1.2.37: (http://tomcat.apache.org/download-connectors.cgi)

Installation steps:

1. Installing the JDK

2. Install Apache2.2, use the default settings, and do not space in the installation path.

3. Unzip Tomcat

4. Copy the mod_jk.so to the Apache installation path under the Modules folder

Configuration steps

To modify the Apache configuration:

About the changes involved in the file httpd.conf and workers.properties files can download a copy of the MOD_JK source package see

1. Modify the Apache configuration file httpd.conf (author path: D:\Apache2.2\conf\httpd.conf) and add it at the end of the last line:

Include "D:\Apache2.2\conf\mod_jk.conf"

2. Create a new mod_jk.conf file in the httpd.conf directory

#加载mod_jk Module LoadModule jk_module modules/mod_jk.so# Specify workers.properties file path Jkworkersfile conf/ workers.properties# specifies which requests are given to Tomcat processing, "controller" for the load allocation controller name specified in Workers.propertise jkmount/* Controllers

3. Create a new workers.properties file in the httpd.conf directory

 #这里可以配置任意多个Tomcat, 2 Tomat servers are configured here. #host和port根据自己实际配置. The instance is configured with native two Tomcat, using a different port. Avoid conflicts # If Tomcat is no longer on the same machine, There is no need to change the port. #server list WORKER.LIST=CONTROLLER,TOMCAT1,TOMCAT2 #========tomcat1========worker.tomcat1.port=9988 #ajp13 port number, in TOMC At the Server.xml configuration, the default 8009worker.tomcat1.host=localhost #tomcat的主机地址, if not for this machine, please fill in the IP address worker.tomcat1.type= Ajp13worker.tomcat1.lbfactor=1 #server的加权比重, the higher the value, the more requests are #========tomcat2========worker.tomcat2.port=9999 #ajp 13 port number, under Tomcat server.xml configuration, default 8009worker.tomcat2.host=localhost #tomcat的主机地址, if not native, please fill in the IP address worker.tomcat2.type= Ajp13worker.tomcat2.lbfactor=1 #server的加权比重, the higher the value, the more requests are #========controller, and the load Balancer controller ======== worker.controller.type=lb# Specifies the Tomcat application node that this load balancer is responsible for. WORKER.CONTROLLER.BALANCED_WORKERS=TOMCAT1,TOMCAT2 #指定分担请求的tomcat # HERE Specifies whether the cluster requires session replication, and if set to true, indicates that the session is sticky and not session copied. When a user's request is distributed to the #tomcat for the first time, subsequent requests are distributed to this Tomcat server for processing, and if set to false, the requirement session is replicated. Worker.controller.sticky_session=false #设为false, the requirement session replication is indicated. 

To modify the Tomcat configuration:

Note that if you modify the Tomcat configuration file, it is best to convert the file encoding to UTF-8 format.

Because we have defined 2 Tomcat processing distributions in the instance. So we'll copy the version of Tomcat in the ZIP format. Used to perform different distribution processing roles. Here because on a machine, so we use the zip version, Of course, if you have a single Tomcat server on a distribution processing machine, you can choose to install the version. This is recommended to use the decompression version of the. TOMCAT6 is configured in the same way as 7.

Take Tomcat-node1 as an example

1. Modify the distribution tomcat corresponding to the Service.xml file to ensure that Apache corresponds to the workers.properties in the AJP13 of the connector port.

<!--define a AJP 1.3 connection port of 9988, the default value is 8009, here we change to our own defined 9988 port--><connector port= "9988" protocol= "ajp/1.3" redirectport= "8443"/>

2. Increase the value of the Jvmroute to ensure consistency with the values configured in the Workers.properties

<!--add Jvmroute, which is the value in the list cluster node configured in Apache, defined as TOMCAT1 node--><engine name= "Catalina" defaulthost= "localhost "Jvmroute=" > "TOMCAT1"

3. Remove the default commented out cluster configuration

<!--uncomment the cluster node, the default value of the sentence is commented out, we need to configure the cluster so remove the comment, let it work--><cluster classname= " Org.apache.catalina.ha.tcp.SimpleTcpCluster "/>

If our tomcat nodes are distributed across different machines, then our cluster has been configured to do so. Remove the extra annotations and show the parts that have been modified

Before modification

<!--Define an AJP 1.3 Connector on port 8009--><connector port= "8009" protocol= "ajp/1.3" redirectport= "8443"/& gt;<!--should set Jvmroute to support load-balancing via AJP ie: <engine name= "Catalina" defaulthost= "localhost "Jvmroute=" jvm1 ">--><engine name=" Catalina "defaulthost=" localhost "><!--for clustering--><!- -<cluster classname= "Org.apache.catalina.ha.tcp.SimpleTcpCluster"/>-->

Modified Tomcat-node1

<!--define a AJP 1.3 connection port of 9988, the default value is 8009, here we change to our own defined 9988 port--><connector port= "9988" protocol= "ajp/1.3" Redirectport= "8443"/><!--added Jvmroute, the value is the value in the list cluster node configured in Apache, defined as TOMCAT1 node--><engine name= " Catalina "defaulthost=" localhost "  jvmroute=" TOMCAT1 "><!--Remove the cluster node-related comments, the default value of the sentence is commented out, we need to configure the cluster so remove the comment, let it work-- ><cluster classname= "Org.apache.catalina.ha.tcp.SimpleTcpCluster"/>

Modified Tomcat-node2

<!--Define an AJP 1.3 Connector on port 8009--><connector port= "9999" protocol= "ajp/1.3" redirectport= "8443"/& gt;<!--should set Jvmroute to support load-balancing via AJP ie: <engine name= "Catalina" defaulthost= "localhost "Jvmroute=" jvm1 ">--><engine name=" Catalina "defaulthost=" localhost "  jvmroute=" Tomcat2 "><!-- For Clustering--><cluster classname= "Org.apache.catalina.ha.tcp.SimpleTcpCluster"/>

Description: Here's protocol= "ajp/1.3", The connection and jvmroute need to be guaranteed to be consistent with the works.properties that we configured on the Apache server. It is best to set the encoding of the Service.xml file to the Utf-8 format when you are finished modifying it. Otherwise, Tomcat startup will be faulted.

4. Our two Tomcat nodes in the example are on the same machine, so there is also a need to ensure that the port of the protocol= "http/1.1" is inconsistent. Otherwise, the local two Tomcat will clash.

The following is a modification of port conflicts between multiple Tomcat servers on the same machine in the author instance.

Tomcat--node1

Tomcat-node2

<server port= "9995" shutdown= "shutdown" >......<connector port= "9990" protocol= "http/1.1               " connectiontimeout= "20000"               redirectport= "8443"/>

Description: There is no conflict between the relevant ports in the Protocol= "http/1.1" configuration, nor does it conflict with ports that are used by other applications in the native computer. This may cause an error.

Instance test

1. In the Web. xml file, add

<distributable/>

2. Writing test JSP code

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><%@ page import= " Java.text.SimpleDateFormat "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

3. Test load balancing and session distribution

Deploy the project to each cluster node, that is, Tomcat_node1 and Tomcat_node2 in the instance, move Apache and Tomcat servers in turn, and the boot order between the Tomcat servers is arbitrary. Here the Apache Port uses the default of 80.

Above is the effect of refreshing 3 executions after running the project in Firefox, and you can see the rotation load between the 2 Tomcat distribution nodes. and the session value on both servers is the same. Indicates that the session was distributed successfully.

Two Tomcat server logs print out the results:

Error resolution
Problem: Service-specific error occurred: 1

Right--My computer---management, System tools, time Viewer, application, and then the right side of the application can view the error log.

Author Error:

The Apache service named  reported the following error:>>> Httpd.exe:Syntax error on line 484 of d:/apache2.2 /conf/httpd.conf:syntax error on line 1 of d:/apache2.2/conf/mod_jk.conf:cannot load d:/apache2.2/modules/mod_jk.so int O Server: \xd5\xd2\xb2\xbb\xb5\xbd\xd6\xb8\xb6\xa8\xb5\xc4\xb3\xcc\xd0\xf2\xa1\xa3     

The reason for the error is that this file should correspond to the Apache version. My Apache version is 2.2, which was originally made for the tomcat-connectors-1.2.37-windows-i386-httpd-2.0.x version so wrong.

Startup after repair

Appendix:

The difference between Apache and Tomcat:

Apache is a Web server environment program that can be used as a Web server. However, only support static Web pages, such as (asp,php,cgi,jsp) and other dynamic Web pages, it seems powerless.

If you want your server to be able to handle dynamic pages, then Tomcat is required.

When dealing with static pages, Tomcat is not as fast as Apache. Tomcat is not as configurable as Apache (e.g., as a proxy server, Loadbalaner).

Tomcat is not as strong as Apache.

For these reasons, a real site uses Apache as a Web server to serve static page requests for the site, and uses the Tomcat server as a servlet/jsp plugin to display dynamic pages of the site.

Apache,tomcat load Balancing and clustering:

There are two different ways to handle requests: Load balancing, State replication (that is, clustering).

Load balancing: Each server is independent and only balances the requested load, not the state (SESSION).

State replication (Cluster): Load balancing first, and then replicating application state across servers.

Apache+tomcat Building Enterprise-class applications

1.Apache is mainly used to parse static text, such as Html,tomcat also has this function, but Apache can greatly improve the efficiency, for the large number of concurrent enterprise applications, can better show the efficiency of Apache;

2.Tomcat is used to parse jsp,servlet and so on, all customer requests will be sent to Apache first, if the request is static text is parsed by Apache, and the results are returned to the client, if it is a dynamic request, such as Jsp,apache will resolve the work to Tomcat , by Tomcat parsing (this first to both reality integration), after the Tomcat parsing is completed, the results are still returned to the client through Apache, so that we can achieve division of labor, load balancing, improve the performance of the system!

Some problems encountered in the construction process:

1. The following error may occur when Apache is in a non-root directory:

Httpd:syntax error on line The d:/wwwroot/apache24/conf/httpd.conf:serverroot must be a valid directory

This error simply modifies httpd.conf under define Srvroot to the directory where Apache resides.

#Define srvroot "/apache24" #指向对应Apache目录Define srvroot "D:/wwwroot/apache24"

2, if the cluster successfully built after the visit page is still in the Apache default page:

Check to see if virtual hosts is commented httpd.conf, Apache2.4 is on by default, and include conf/extra/httpd-vhosts.conf comments.

3. Apachemonitor start prompt The requested operation has failed!

View Apache24\logs\error.log under Error, or use the command line httpd.exe-w-n "Apache2.4 (Apache service name)"--Start debugging

Service Installation: Httpd.exe-k Install

    • This article is from: Linux Tutorial Network

Apache + Tomcat + mod_jk for Cluster service

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.