Apache + Tomcat server Load balancer configuration and tomcat server Load balancer

Source: Internet
Author: User

Apache + Tomcat server Load balancer configuration and tomcat server Load balancer

Apache + Tomcat server Load balancer ensures high availability and system performance, which is achieved through communication between Apache and Tomcat. There are three ways to communicate between Apache and Tomcat: Mod_Jk, HTTP_Proxy, and AJP_Proxy. Among them, Mod_Jk is the most popular, and the official documentation is also very comprehensive.

Mod_JK is a module of Apache. It communicates with Tomcat through the AJP protocol. Tomcat listens to the connection request of AJP by setting Connector.

 

Note: If you want apache to communicate with tomcat using the HTTP protocol, you need to use HTTP_Proxy. Please refer to the final section.

 

Install Mod_JK:

Slave

Tar zxvf tomcat-connectors-1.2.40-src.tar.gz

Cd tomcat-connectors-1.2.40-src/

Cd native/

./Configure -- with-apxs =/home/soft/httpd-2.4.10/bin/apxs -- prefix =/home/soft/tomcat-connectors-1.2.40
Make

Make install
Optional

Make clean

Make distclean

Note: The installation result does not seem to be installed in/tomcat-connectors-1.2.40, but will write files to the local apache httpd, for example, modules/mod_jk.so

 

Server Load balancer configuration steps:
1. Find httpd. conf In the conf directory under the apache installation directory, and add Include conf/mod_jk.conf at the end.
2. Add the mod_jk.so file to the Apache \ modules folder.
3. Create and configure the mod_jk.conf File
4. Create and configure the workers. properties and uriworkermap. properties files.
5. Modify the tomcat configuration file server. xml, which mainly corresponds to the port and sets the jvmRoute attribute of the engine.

 

Mod_jk.conf content:
# Load the mod_jk Module
LoadModule jk_module modules/mod_jk.so
# Workers. properties location
JkWorkersFile conf/mine_jk/workers. properties
# Loading workers requests to process allocated files
JkMountFile conf/mine_jk/uriworkermap. properties
# Log storage location
JkLogFile logs/mine_mod_jk.log
# Set the log level [debug/error/info]
JkLogLevel info
# Select the timestamp log format
JkLogStampFormat "[% a % B % d % H: % M: % S % Y]"

Workers. properties content:
# Define worker. The list name must be the same as the dispatcher name in the mod_jk.conf file.
Worker. list = dispatcher, status
# Define Node1
Worker. tomcat1.port = 8019
Worker. tomcat1.host = localhost
Worker. tomcat1.type = ajp13
Worker. tomcat1.lbfactor = 1
# Worker. tomcat1.connection _ pool_size = 10
# Worker. worker at1.connection _ pool_timeout = 600
# Worker. worker at1.socket _ connect_timeout = 300
# Define Node2
Worker. tomcat2.port = 8029
Worker. tomcat2.host = localhost
Worker. tomcat2.type = ajp13
Worker. tomcat2.lbfactor = 1
# Worker. tomcat2.connection _ pool_size = 10
# Worker. tomcat2.connection _ pool_timeout = 600
# Worker. worker at2.socket _ connect_timeout = 300

# Load-balancing behaviour
Worker. dispatcher. type = lb
Worker. dispatcher. balance_workers = worker at1, tomcat2
# Sticky_session session affinity settings
Worker. dispatcher. sticky_session = 0
Worker. dispatcher. sticky_session_force = 1
# Loadbalancer
Worker. status. type = status

Uriworkermap. properties:
# Specify the request to tomcat for processing. "dispatcher" is the specified Server Load balancer controller in workers. propertise.
/*. Jsp = dispatcher
/Jkstatus = status
! /*. Gif = dispatcher
! /*. Jpg = dispatcher
! /*. Png = dispatcher
! /*. Css = dispatcher
! /*. Js = dispatcher
! /*. Htm = dispatcher
! /*. Html = dispatcher

 

Tomcat settings:

Prepare two tomcat servers, set their router to tomcat1 and tomcat2, set their connector to ajp, and set ports to 8019 and 8029 respectively.

After the configuration is complete, start apache and tomcat and access http: // localhost: 8080/jkstatus to view the cluster status (provided that the status type worker is configured)

 

There can look at other people to write, very detailed: http://aokunsang.iteye.com/blog/1699914

 

Note: If you want apache to communicate with tomcat using the HTTP protocol, you need to use HTTP_Proxy:

Http_proxy

This is to use the mod_proxy module provided by Apache to connect to Tomcat Using proxy technology. Before configuration, make sure that the Apache server version 2.2.x is used. This module is rewritten in version 2.2.x, greatly enhancing its functionality and stability.

The http_proxy mode is an HTTP-based proxy. Therefore, Tomcat must provide HTTP services, that is, Tomcat's HTTP ctor must be enabled.

I use the linux source code to compile apache, mod_proxy.so and mod_proxy_http.so are already included, and other versions of apache should also exist. If not, install the library file separately.

############# Mod_proxy.so ##############
LoadModule proxy_module modules/mod_proxy.so
# LoadModule proxy_connect_module modules/mod_proxy_connect.so
# LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
############# Mod_proxy_http.so ##############
LoadModule proxy_http_module modules/mod_proxy_http.so
The above content is in the apache configuration file httpd. conf. You can use the library by releasing the annotations,

You also need to add the following at the end of the httpd. conf file:


############# Http_proxy ##############
# The http_proxy method needs to load mod_proxy.so and mod_proxy_http.so, and release the comments of the LoadModule corresponding to the above two rows.
ProxyPass/images!
ProxyPass/css!
ProxyPass/js!
ProxyPass/http: // 127.0.0.1: 8080/

In this configuration, We proxy all http: // 127.0.0.1 requests to http: // 127.0.0.1: 8080/, which is the Tomcat access address, except for images, css, and js directories. We can also use mod_proxy for load balancing, and then look at the following configuration

ProxyPass/images!

ProxyPass/css!

ProxyPass/js!

 

ProxyPass/balancer: // example/

<Proxy balancer: // example/>

BalancerMember http: // server1: 8080/

BalancerMember http: // server2: 8080/

Balancermemberhttp: // server3: 8080/

</Proxy>

Start apache and tomcat to test.

View comments

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.