Apache Tomcat AJP to achieve load balancing __apache

Source: Internet
Author: User
Tags session id sessions tomcat server apache tomcat

Most people who start to touch Web servers may be as surprised as I am with Apache and Tomcat servers (they're all developed by Apache), but they're not redundant servers, but they all have their own focus, although they can provide a Web server. Here's a brief introduction.

1. Apache is the world's number one Web server, and is always the first to support the Apache Foundation. and open source Apache Tomcat is also very concerned about, because of open source, the user most support.

2. Apache support static page, Tomcat support dynamic page (servlet, JSP); Generally in the use of Apache + Tomcat configuration, Apache is the implementation of JSP forwarding, to Tomcat to deal with. Apache can support PHP, but if you want to support Java, you need Tomcat to handle it.

3. Apache is a Web server, Tomcat is an application (Java) server, it is only a servlet container, is an Apache extension, but can run independently of Apache, if run independently, the function is equivalent to Apache, However, the processing of static pages is not ideal.

Through the above analysis, you can know:

1. Apache can have a good static page processing capabilities, if paired with Tomcat can support both static and dynamic pages

2. Tomcat can assume low performance requirements for Web applications. Load Balancing

In order to improve the usability and performance of the whole system, load balancing is often used, while Apache and Tomcat are often combined to achieve load balancing technology.

Host Apache allocates the user as a front load balancing server, and the backend's different Tomcat server ultimately processes the request, which can be configured in two different ways depending on the focus.

1. Increase system availability

Ensure the synchronization of sessions between multiple Tomcat servers to ensure that any one machine does not affect the operation of the system, thereby increasing the availability of the system.

2. Improve system performance

Record the session ID of each request (this ID is assigned by Tomcat) on the Apache host that is responsible for load balancing, and the Tomcat server response to the session, and determine each session ID when the next request arrives. If an identity connection has been established, then go to the corresponding server; be responsible for the new connection, assign a server based on the status of each background Tomcat server, and record the session ID.

The following figure:



Apache + Tomcat Configuration

As seen above, the Apache +tomcat can be configured to achieve a higher Web server while also achieving higher availability and system performance. These are done through the communication between Apache and Tomcat. There are basically three ways of communicating between Apache and Tomcat:

1. mod_jk

2. Http_proxy

3. Ajp_proxy

This article mainly introduces the MOD_JK way, it is also the most popular way, and the official document is also very complete. Mod_jk

MOD_JK is a module of Apache, which realizes the communication between Apache and Tomcat through the AJP protocol, while Tomcat listens to the AJP connector's 8009 ports to accept the AJP connection request; Typically these requests come from the Apache server on the front end.

Since MOD_JK has been designed as a module of Apache, it is possible to communicate between Apache and Tomcat with some relatively simple configurations, as well as load balancing. Configuration Step Installation

1. Install Apache HTTP server, download from the official Apache website; I installed the Apache2.0.64.

2. Install Apache Tomcat, you can install the same machine in Apache, also can be in other machines, version to match with MOD_JK

3. Download MOD_JK; Note here to match Apache Tomcat. Configuration Plus in MOD_JK

MOD_JK is loaded on the Apache HTTP server, you need to place the downloaded mod_jk.so in the Apache/modules directory and modify httpd.conf to add the following line:

Include conf/mod_jk.conf

Use the include command to include Mod_jk.conf, which is the configuration file we added to ourselves about MOD_JK.

Create a new mod_jk.conf and add the following:

#Load mod_jk module.

LoadModule Jk_module modules/mod_jk.so

#Where to find Workers.properties

Jkworkersfile conf/workers2.properties

Jkmountfile conf/urimap.properties

#Where to put JK logs

Jklogfile Logs/mod_jk.log

#Set the JK Log Level[debug/error/info]

Jkloglevel Info

#Select the log format

Jklogstampformat "[%a%b%d%h:%m:%s%Y]"

#JkRequestLogFormat set the request format

Jkrequestlogformat "%w%V%T"

Jkmount/* LoadBalancer

It contains two properties files, namely Workers2.properties and urimap.properties.

Workers2.properties is used to describe the load balancing configuration method for Tomcat servers, as follows: #worker. list=loadbalancer

WORKER.LIST=LOADBALANCER,TOMCAT1,TOMCAT2 #server List

#define The

worker.tomcat1.port=8009

worker.tomcat1.host=10.224.70.57

Worker.tomcat1.type=ajp13

Worker.tomcat1.lbfactor=1

#define The

worker.tomcat2.port=8009

worker.tomcat2.host=10.224.70.57

Worker.tomcat2.type=ajp13

Worker.tomcat2.lbfactor=1

#Now We define the load-balancing behavior

Worker.loadbalancer.type=lb

WORKER.LOADBALANCER.BALANCE_WORKERS=TOMCAT1, TOMCAT2

Worker.loadbalancer.sticky_session=true

Worker.loadbalancer.sticky_session_force=true

In mod_jk.conf there is jkmount/* loadbalancer, which corresponds to worker.list on the LoadBalancer line above.

In addition TOMCAT1 and TOMCAT2 are configured background tomcat servers, of course there is only one server, so all two worker are the same address. Lbfactor represents the weights used in load-balancing algorithms.

In addition, the Urimap.properties file indicates which requests are forwarded to the Tomcat server, and the configuration entry is:

!/*.htm=loadbalancer

Note that the request for *.htm here is to LoadBalancer; this loadbalancer is the loadbalancer above.

So far the load balancing between Apache and Tomcat has been configured.

Run-time observation

The main observation here is how the stickiness of the session is implemented, and how the Apache server has learned about the session ID-related information.
In fact, for load balancing, if you have two tomcat configured in the above example, if you turn off sticky_session, each tomcat server will receive the request in turn.

In fact, the session ID was found to be created by a Tomcat server assignment at the time of the experiment, and the Apache server was used only to manage its relationship with the Tomcat server; the Apache server needs to check the session ID in each request. This ID is assigned by Tomcat, and Tomcat generates jsessionid for each dynamic page that is first requested, and Jessionid is not generated for static pages.

Specific operation process:

1. The Apache server receives a request from the client to check whether the HTTP request contains SessionID

2. If not included, use the load balancing algorithm to select a Tomcat server

3. The Apache server forwards this client request to the 8009 port of the selected Tomcat server, in the format of the packet AJP protocol

4. tomcat resolves the AJP protocol, generates the session ID and gets the response, sends the results to the Apache server, which is sent back through the previous 8009 links.

5. The Apache server resolves the AJP package and gets the session ID information and associates it with the corresponding server (and sessions).

6. Apache forwarded session ID to client

7. Client saves the session ID to the cookie for the next use.

Packet No

Apache

tomcat:8009

1

2:req:get/index.jsp Http/1.1à

2

ß2:rsp:send headers:200 OK

3

ß2:rsp:send Body CHUNK

4

ß2:rsp:end RESPONSE

Usually this connection is established and will not break for a short time, but it can be configured with parameters.

During the package interaction above: The 2nd package contains information about the session ID, and the content resembles the following:

Set-cookie.. 3jsessionid=955aae2aa22b074bd1824395b6e4835a; path=/... Content-type...text/html ... Content-length ... 141.AB ..... AJP (Apache jserver Protocol)

The AJPV13 protocol is package oriented. Web servers and Servlet containers interact through TCP connections; To save the high cost of socket creation, the Web server attempts to maintain a persistent TCP connection to the Servlet container, and reuse the connection during multiple request and response cycles.

Once a connection is assigned to a particular request, the connection cannot be reused until the request processing cycle is completed, in other words, the request is not connected to a multiple worker. This encoding is simpler.

Once the connection is assigned to a specific request, the basic Request information (HTTP header) is compressed (usually the string is encoded as an integer), and if there is a request entity, it is sent immediately thereafter.

At this point, the servlet container is ready to process the request and send the West message back to the Web server:

1. Send_headers: Send back to the browser's response header

2. Send_body_chunk: Send entity to browser

3. Get_body_chunk: Get more request data, this data has not been sent.

4. End_response: Indicates the end of the request processing cycle.

The Web server requests sent to the servlet container include:

1. Forwarding Request start

2. Close

3. Ping

4. cping

Format of Ajp13_forward_request

Ajp13_forward_request: =

Magic: (Word)

Length: (Word)

Prefix_code (byte) 0x02 = Jk_ajp13_forward_request

Method (Byte) &n

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.