Set up a tomcat cluster in Linux

Source: Internet
Author: User
Tags gz file
1. expected goals

This article describes how to build a tomcat cluster in Linux and possible problems and solutions during the construction process. For simplicity, this article demonstrates that the cluster is composed of only two tomact nodes and one Apache node. The three nodes will be installed on the same machine:
Apache:
Installation path:/usr/local/apache2
Port: 90
Tomcat1:
Installation path:/usr/local/Apache-Tomcat-6.0.33-1
Port 6080
Tomcat2:
Installation path:/usr/local/Apache-Tomcat-6.0.33-2
Port 7080

2. List of Required Software

Apache

Latest Version: 2.2.21
This article uses the source code installation, source code package: http://labs.renren.com/apache-mirror//httpd/httpd-2.2.21.tar.gz
For other versions, see: http://httpd.apache.org/download.cgi

Tomcat 6

Latest Version: 6.0.33
This article uses self-decompressed tar.gz package for installation,: http://labs.renren.com/apache-mirror/tomcat/tomcat-6/v6.0.33/bin/apache-tomcat-6.0.33.tar.gz
For other versions, see: http://tomcat.apache.org/download-60.cgi

Apache Tomcat Connector (mod_jk)

Latest Version: JK-1.2.32
This article uses the source code installation, source code package: http://www.apache.org/dist/tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.32-src.tar.gz
For other versions, see: http://tomcat.apache.org/download-connectors.cgi

3. Software Installation

3.1 install Apache

1. Enter the directory where the downloaded httpd-2.2.21.tar.gz file is located and perform the decompression:
Tar-zxvf httpd-2.2.21.tar.gz

2. Go to the root directory of the decompressed Folder:
CD httpd-2.2.21

3. Configure compilation parameters:
./Configure -- prefix =/usr/local/apache2 -- enable-mod-shared = all
For more information about configure parameters, see:
Http://httpd.apache.org/docs/2.2/en/programs/configure.html#installationdirectories

4. Compile and install:
Make
Make install

5. Start and close Apache
After completing the preceding steps, Apache installation is complete. Run the following command to start and close Apache:
Start:
/Usr/local/apache2/bin/apachectl start
Close:
/Usr/local/apache2/bin/apachectl stop

3.2 solution to cannot use an external APR with the bundled Apr-util error during Apache installation

Sometimes, this error is reported when you install Apache and perform the./configure operation. The solution is to install the APR carried by apache2.2.x. The method is as follows:

1. Self-source installation directory httpd-2.2.21 into the installation directory of apache2.2.x with APR
CD srclib/APR

2. Install Apr. (install it in/usr/local/APR. If APR has been installed in the system, find the installation directory to overwrite it)
./Configure -- prefix =/usr/local/APR
Make
Make install

3. Install Apr-util
./Configure -- prefix =/usr/local/APR-util -- With-Apr =/usr/local/APR
Make
Make install

4. Use the newly installed Apr to re-compile and configure Apache.
Put back the installation root directory httpd-2.2.21 and execute:
./Configure -- prefix =/usr/local/apache2 \
-- Enable-mod-shared = all \
-- With-Apr =/usr/local/APR \
-- With-Apr-util =/usr/local/APR-util/bin

Supplement:

If you are installing APACHE-2.4 or later, the following differences exist:

1. You need to download APR and APR-util separately.

APR: http://mirror.bjtu.edu.cn/apache//apr/apr-1.4.6.tar.bz2

Apr-util: http://mirror.bjtu.edu.cn/apache//apr/apr-util-1.4.1.tar.bz2

2. You also need to install PCRE

Go to http://pcre.org/download pcre, decompress it into the source code directory for execution

./Configure -- prefix =/usr/local/PCRE

3. Configure configure when installing Apache.

./Configure -- prefix =/usr/local/apache2 \
-- Enable-mod-shared = all \
-- With-Apr =/usr/local/APR \
-- With-Apr-util =/usr/local/APR-util/bin

-- With-PCRE =/usr/local/PCRE

3.3 install Apache Tomcat Connector (mod_jk)

1.import and download the directory of the tomcat-connectors-1.2.32-src.tar.gz file, and decompress the file:
Tar-zxvf tomcat-connectors-1.2.32-src.tar.gz

2. Go to the native subdirectory under the decompressed Folder:
CD tomcat-connectors-1.2.32-src/native

3. Configure compilation parameters:
./Buildconf. Sh

./Configure -- With-apxs =/usr/local/apache2/bin/apxs \
-- With-Java-home = $ java_home -- With-Java-platform = 2 \
-- Enable-JNI

4. Compile and install:
Make
Make install

If the process ends successfully, you can find the mod_jk.so file under/usr/local/apache2/modules.

3.4 install Tomcat
Decompress the downloaded apache-tomcat-6.0.33.tar.gz file to/usr/local/and rename it APACHE-Tomcat-6.0.33-1 and APACHE-Tomcat-6.0.33-2 respectively.

4. cluster configuration

4.1 configure Tomcat

1. Modify the port
Tomcat configuration is mainly concentrated in the conf/server. xml file. First, because two Tomcat instances are running on the same machine. modify all ports configured in XML to avoid port occupation, A simple and secure modification method is to change all the port numbers in the file according to a simple rule. For example, add 1000 or minus 1000 to the original port number. Based on this principle, the connection port of APACHE-Tomcat-6.0.33-1 is changed to 6080, and the connection port of APACHE-Tomcat-6.0.33-2 is changed to 7080, other port configurations also follow this principle.

2. Enable Tomcat cluster support
In the conf/server. xml file, make two changes:

Change 1: Set jvmroute for <engine>

In fact. there is a piece of ready-made <engine> Configuration with jvmroute in XML. We can refer to this section to add a jvmroute for <engine/>. Pay special attention to the jvmroute values: the value must be in the workers of mod_jk mentioned later. the node names in the properties file are consistent! Because the nodes in the file are named tomcat1 and tomcat2, we must set the jvmroute value to one of these two values. This is also explicitly mentioned in the Tomcat official documentation http://tomcat.apache.org/connectors-doc/webserver_howto/apache.html on Apache Tomcat Connector. The jvmroute value appears in the session ID created by the node. For example, in a non-cluster environment, a sessionid may be in the "XXXXXXXXX" format, but in a cluster environment, if the jvmrtomat1oute of the current node is configured as limit 1, The sessionid generated by the node will be changed to "XXXXXXXXX. the tomat1 format, while mod_jk relies on the node suffix to implement sticky session, that is, all requests with the suffix tomat1 are sent to the tomat1 node for processing.

Change 2: add the cluster configuration in <engine/>.

In the Tomcat official documentation on Tomcat cluster configuration: http://tomcat.apache.org/tomcat-6.0-doc/cluster-howto.html, there is a default configuration:

        <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="3000"/>            <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"                      address="auto"                      port="4000"                      autoBind="100"                      selectorTimeout="5000"                      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>          <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>    

This default configuration can meet most application scenarios. Therefore, you only need to copy this configuration to <engine/>, and all Tomcat configurations are complete.

4.2 configure Apache
Open the conf/httpd. conf file in the Apache installation directory and append the following content at the end of the file:

# Load mod_jk moduleLoadModule jk_module modules/mod_jk.so# Specify jk log file.JkLogFile /var/log/mod_jk.log# Specify jk log level [debug/error/info]JkLogLevel info# Specify workers.properties, this file tell jk:# how many nodes and where they are.JkWorkersFile conf/workers.properties# Specify which requests should handled by which node.JkMount /* controller

For more information about mod_jk configuration items, see: http://tomcat.apache.org/connectors-doc/webserver_howto/apache.html
In the preceding configuration, jkworkersfile CONF/workers. properties indicates that a workers. properties file to describe the cluster node. Therefore, we need to create this workers. properties file, which is placed in the conf folder. The content of this file is as follows:

# List of all nodes. The controller is a logical node responsible for load balancing control, # If the URL in jkmount is specified to the Controller, the request is automatically hashed to a physical node. # Note: The Tomcat name (tomcat1, tomcat2) that is responsible for request processing must be consistent with the jvmrout attribute values configured in the conf/server. xml # file! Worker. list = controller, worker at1, tomcat2 #======= worker AT1 ======= worker. tomcat1.port = 6009 # ajp13 port number, in Tomcat server. xml configuration. The default value is 8009worker. tomcat1.host = localhost # Tomcat host address. If not, enter the IP address worker. tomcat1.type = ajp13worker. tomcat1.lbfactor = 1 # server's weighted weight. The higher the value, the more requests are allocated. #========= tomcat2 ======= worker. tomcat2.port = 7009 # ajp13 port number, in Tomcat server. xml configuration. The default value is 8009worker. tomcat2.host = localhost # Tomcat host address. If it is not a local machine, enter I P address worker. tomcat2.type = ajp13worker. tomcat2.lbfactor = 1 # server weighting ratio. The higher the value, the more requests are allocated. #======= controller, server Load balancer controller ======= worker. controller. type = lbworker. controller. balance_workers = worker at1, tomcat2 # specify the Tomcat that shares the request. The balanced_workers in the old version is no longer recommended! Worker. controller. sticky_session = 1 # sticky_session indicates 1. # When a session of a client is created, requests subsequently initiated by the client, that is, all requests of this session are always processed by the node that processes the request for the first time # (unless the node fails)

By now, all configurations have been completed. After starting two Tomcat and Apache, deploy an application to both Tomcat and access this application through Apache, observe the logs output in the Tomcat background and you will find that the requests are randomly allocated to two Tomcat instances for alternate execution.

Note: Application preparation for the cluster

1. Add the <distributable/> element to the Web. xml of the application.
2. Data (such as attribute) stored in the session must be serialized.

Reference resources:

Tomcat official documents on Tomcat cluster configuration:
Http://tomcat.apache.org/tomcat-6.0-doc/cluster-howto.html

Official Tomcat documentation on Apache Tomcat Connector
Http://tomcat.apache.org/connectors-doc/webserver_howto/apache.html

Apache installation documentation
Http://httpd.apache.org/docs/2.2/en/install.html

Apache official configure parameter documentation
Http://httpd.apache.org/docs/2.2/en/programs/configure.html#installationdirectories

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.