CentOS Apache-based Tomcat load balancing and clustering

Source: Internet
Author: User

CentOS Apache-based Tomcat load balancing and clustering


First, the background principle
1, Tomcat to do a Web server has its limitations, low processing power, low efficiency. withstands concurrency Small (around 1000). However, there are many websites or pages that are JSP. And the use of Tomcat as the web, so only on this basis to increase the optimization.
2, the current approach is the Apache + MOD_JK + tomcat to solve a part of the request, the user access to Apache, but there is a JSP page when the request Tomcat. If the amount of a large, then Tomcat can not withstand, then only do tomat cluster, Apache + MOD_JK is the load balancer (Apache+tomcat through AJP13 protocol to do the cluster, Apache and Tomcat with MOD_JK Connector communication).
3. MOD_JK2 Load balancer can forward different JSP requests to different Tomcat servers, and can also detect the server surviving. If there is a condition can make a ha for mod_jk2 because the pressure is on JK after the cluster is finished.



4. Reverse Proxy load balancer (APACHE+JK+TOMCAT)

A proxy server can be used to forward requests to internal Web servers, allowing the proxy server to transfer requests evenly to one of several internal Web servers for load balancing purposes. This proxy approach differs from the normal proxy approach, which is that clients use proxies to access multiple external Web servers, which are used by multiple customers to access internal Web servers and are also referred to as reverse proxy mode.


5. Why to configure Tomcat load balancer and cluster

Load balancing: Load Balancing can handle requests for high concurrency. Tomcat cluster: Tomcat cluster with load balancing can be a system to achieve high availability, that is, any one server is not available, automatically reconnect, can keep session data consistent, do not need to re-enter the password.


6. Introduction of simple principle

 The connection between 1.apatche  and tomcat  is done by jk , with the AJP protocol. Each tomcat  is listening on the AJP port. The default port for  AJP Connector  is  8009 .   2. Configure the  tomcat  between clusters to replicate  session through  tcp  protocol communication, and of course to include in the web.xml  file in the tomcat  application  <distributable/> or <context distributable= "true"  />&nbsp, which marks the session to be copied. The session  is replicated in order to be down when a server is being requested, and the request is forwarded by apache  to the other tomcat  server via jk ,session  Information is lost in one of the ways to keep session , there are other solutions, such as putting session  into the database. 3.jk  to assign a request to a tomcat  server follows a different principle, and here we use load balancing. A lot of worker&nbsp are defined in  workers.properties , and if worker  's type  is defined as  lb, it means this worker  is a load balancer worker&nbsp, and he knows how to provide polling to allocate request . Load balancing is also divided into a number of balanced ways, in which case the attribute Worker.balancer.method is determined. The following describes the different load balancing methods (the default is R):worker.balancer.method=request  (abbreviated r ): Load Balancing worker  will be based on each tomcat  The number of request  processed on the server to find the best tomcat  server. This approach is suitable for a large number of application . worker.balancer.method=session  (abbreviated  S ): Load Balancing worker  based on each Tomcat  on the number of  session to find the best tomcat&nbsp, but the load Balancer worker  and no state, so do not know the number of session , so there is no session  cookie  request or url  without encode sessionid  request as new session . worker.balancer.method= traffic  (abbreviated t ): Load balancer worker  based on jk  and tomcat  Direct network condition to find the best tomcat  server. worker.balancer.method= busyness  (abbreviated b ): Load balancer worker  Select the tomcat  with the least traffic based on tomcat  traffic.


7, the corresponding installation introduction

Apache server: Is the front-end Web server that is used to receive requests from clients. Mod_jk.so: Is Tomcat Connector, which is used to distribute Apache received requests to Tomcat for processing. JK should correspond to the Apache version. JK is divided into two versions 1,x and 2.x, of which 2.x is not the latest version, it is another branch of JK, after unknown why did not continue to develop, so the 2.x version is obsolete. Tomcat: It is also a Web server, but here it serves primarily as a servlet container to handle requests forwarded by JK. Install Apache, and mod_jk.2.0.55 extracted after the copy to the Apache installation directory (apache_home)/modules below, in fact, modules below is the various functional modules Apache, pluggable.



Second, the required package
jdk:jdk-7u45-linux-x64.rpm
Tomcat:apache-tomcat-7.0.47.tar.gz
Apache:httpd-2.2.27.tar.gz
Mod_jk:tomcat-connectors-1.2.39-src.tar.gz

Third, installation instructions
See:
CentOS-6.3 Installation Configuration JDK-7 HTTP://WWW.FLYBI.NET/ARTICLE/12
CentOS-6.3 Installation Configuration Tomcat7.0 HTTP://WWW.FLYBI.NET/ARTICLE/2
CentOS-6.3 Installation Configuration Apache2.2.27 HTTP://WWW.FLYBI.NET/ARTICLE/20
CentOS-6.3 Installation Configuration mod_jk1.2 HTTP://WWW.FLYBI.NET/ARTICLE/22

Third, the configuration
1. Configure Apache
Under Apache conf, use the VI Editor to open httpd.conf, add the following line at the end of the file (remember that/usr/local/apache/conf and/etc/httpd/conf httpd.conf are added)

Include/usr/local/apache/conf/mod_jk.conf


2. Configuring the Mod_jk.conf File
Build the mod_jk.conf file under/usr/local/apache/conf. Configuration content:

#加载mod_jk moduleloadmodule jk_module/usr/local/apache/modules/mod_jk.so# load workersjkworkersfile/usr/local/in a cluster apache/conf/workers.properties# load workers request processing allocation file jkmountfile/usr/local/apache/conf/uriworkermap.properties# Specify the log output file for JK jklogfile/usr/local/apache/logs/mod_jk.log# Specify the log level jkloglevel warn# Specify the format of the log Jklogstampformat "[%a%b%d%h:% m:%s%Y] "#指定哪些请求交给tomcat处理," Controller "is the name Jkmount/[i] for the load assigned in workers.propertise. [/I] Controller


Description
Jkmount: Set Apache Distributor,/ indicates that Apache distributes all files by the Distributor Lbcontroller, you can set them yourself . jsp,*.do, etc.;

3. Configuring the Workers.properties File
Build the Workers.properties file under/usr/local/apache/conf. Configuration content:

#server   List worker.list=controller,tomcat1,tomcat2#========tomcat1========worker.tomcat1.port=8008 # ajp13  port number, under Tomcat server.xml configuration, default 8009worker.tomcat1.host=localhost  #tomcat的主机地址, if not native, Please fill in the IP address worker.tomcat1.type=ajp13worker.tomcat1.lbfactor=1  #server的加权比重, the higher the value, the more requests to be divided #========tomcat2======= =worker.tomcat2.port=8009  #ajp13   Port number, server.xml configuration under Tomcat, default 8009worker.tomcat2.host=localhost # Tomcat host address, if not for this machine, please fill in the IP address worker.tomcat2.type=ajp13worker.tomcat2.lbfactor=1  #server的加权比重, the higher the value, the more requests to be divided #==== ====controller, Load Balancer controller ========worker.controller.type=lb  #server名为controller for load Balancing worker.retries=3 # Retry Count worker.controller.balance_workers=tomcat1,tomcat2# Sticky session (default is on)   when the attribute value =true (or 1), the session is sticky, That is, the same session is processed on the same node in the cluster, and the session does not cross the node. In a clustered environment, this value is typically set to falseworker.controller.sticky_session=false# settings for server load Balancing session can be shared   There are many articles saying that setting to 1 is possible, There is also a setting of 0 to #worker.controller.sticky_session=1#worker.controller.sticky_session_force=1#worker.status.type=status 


Description
1, note that the above Worker.tomcat1.port port is Tomcat Server.xml under the
<connector port= "8019" protocol= "ajp/1.3" redirectport= "8443"/>

4. Configuring the Uriworkermap.properties File
Build the Uriworkermap.properties file under/usr/local/apache/conf. Configuration content:

#所有请求都由controller这个server处理/*=controller# All included jkstatus requests are handled by the status this server #/jkstatus=status# here "!" It means "no".!/*.gif=controller!/*.jpg=controller!/*.png=controller!/*.css=controller!/*.js=controller!/*.htm= Controller!/*.html=controller


5. Modify Tomcat1_home\conf\server.xml Configuration
JK:AJP13 listening on/0.0.0.0:8009 the modified port must be consistent with worker.tomcat1.port=8009 in the Workers.propertise file.

<connector port= "8009" protocol= "ajp/1.3" redirectport= "8443"/>


The above is load balanced configuration complete! Next you can configure Tomcat cluster and session replication!

<engine name= "Catalina"  defaulthost= "localhost"  [b]jvmroute= "TOMCAT2" [/b]>      <!--tomcat1 will paste with tomcat2 session  here to specify the Paste object-->...<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" &NBSP;&NBSP;&NBSP;&NBSP;&NBsp;        frequency= "       "       droptime= " />        " <receiver classname= "Org.apache.catalina.tribes.transport.nio.NioReceiver"               address= "Auto"              port= "4000"               autobind= "             " 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>    <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/" & nbsp;         watchdir= "/tmp/war-listen/"           watchenabled= "false"  />    <ClusterListener  Classname= "Org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"  />     <clusterlistener classname= "Org.apache.catalina.ha.session.ClusterSessionListener"  /></ Cluster>...</engine>


Description: 228.0.0.4 is the default configuration cluster default address, do not change. The Server.xml configuration of the

Tomcat2 is also modified as this article tests the use of the same computer, so the corresponding micro-do modifies the port. Tomcat is distributed on different computers without changing the port number.

<engine name= "Catalina"  defaulthost= "localhost"  [b]jvmroute= "TOMCAT1" [/b]>      <!--tomcat2 will paste with TOMCAT1 session  here to specify the Paste object-->...<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" &NBSP;&NBSP;&NBSP;&NBSP;&NBsp;        frequency= "       "       droptime= " />        " <receiver classname= "Org.apache.catalina.tribes.transport.nio.NioReceiver"               address= "Auto"              port= "4001"               autobind= "             " 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>    <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/" & nbsp;         watchdir= "/tmp/war-listen/"           watchenabled= "false"  />    <ClusterListener  Classname= "Org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"  />     <clusterlistener classname= "Org.apache.catalina.ha.session.ClusterSessionListener"  /></ Cluster>...</engine>


6, finally to Tomcat support session replication, you must add a tag in Web. Xml. Add and corresponding tomcat_home\conf\web.xml in the Web. xml file of the published project.

<!--cluster indicates that you want to copy the session, usually on the last--><distributable/>


 Note: In the session paste (sharing) aspect, if the session is stored in the Javabean,javabean must implement the Serializable interface, if not implemented will report error Exception Thrown:class Java.lang.IllegalArgumentException.
7. Testing
Create the Test folder under the \webapps of TOMCAT1 and TOMCAT2 and create the test.jsp file

<%system.out.println ("==============");%>


Start Tomcat1, TOMCAT2, Apache. In the browser, enter: http://localhost/test/test.jsp. Constantly refresh the browser, you can see the ===== alternating output in the TOMCAT1 and 2 consoles.
Result: Requests are distributed evenly to Tomcat on different clients. Turn off one of the Tomcat 1,tomcat 1 The original processing request will be received by other tomcat2, if there is information in the session of TOMCA 1t that was previously closed, TOMCAT1 session information in TOMCAT2 is still present and appears as login--to MCAT down-the request is transferred to another tomcat and is still logged in.
8. Other Summary
Different types of Worker,worker.controller.type configurations:

AJP12 This type of worker knows to use the AJP12 protocol to turn a request to Tomcat and eventually it will be used by JK to interact with the Tomcat server. Ajp13 This type of worker knows to use the AJP13 protocol to turn a request to Tomcat and eventually it will be used by JK to interact with the Tomcat server. JNI This type of worker knows to use the JNI protocol to turn the request to Tomcat and eventually it will be used by JK to interact with the Tomcat server. LB This type of worker is a load balancer worker, and he knows how to provide polling to allocate requests. (common) Status This type of worker is responsible for managing load balancing.



Third, Apache Jkstatus JK Running State Management Authority
Add the following two lines to the Workers.properties configuration file in the configured Apache project

Worker.list = Add the following line to the statusworker.status.type=statusmod_jk.conf configuration file jkmount/jkstatus status


Access HTTP://IP address in the Address bar: Port number/jkstatus you can see it.


CentOS Apache-based Tomcat load balancing and clustering

Related Article

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.