Detailed explanation of Apache+Tomcat7 load balancing configuration method under Windows

Detailed explanation of Apache+Tomcat7 load balancing configuration method under Windows

Preparation

Windows Server 2008 R2 Enterprise (2.40GH, 8GB, 64Bit, 192.168.10.212)
2 Ubuntu 10.04.4 (192.168.10.98, 192.168.10.137)
JDK1.7.80
Tomcat7.0.68
Apache 2.4.4

1. Install Apache 2.4.4

There is nothing to note during the installation process, just keep clicking Next. After the installation is complete, the Apache service is enabled by default. You can enter localhost or 127.0.0.1 in the browser, and the words "It works" will appear.

2. Java environment

There are many configurations on Ubuntu on the Internet, so I won’t go into details, but remember to configure JAVA_HOME and PATH

3.Tomcat installation

Just unzip it in the directory. My directory is /opt/tomcat7/. I unzipped it on both Ubuntu. You may encounter the problem of insufficient permissions. At this time, you need to type commands. The simplest and most brutal one is sudo chmod 777 directory. Due to security issues, you should properly authorize, so I won’t mention it here.

Start configuration

Apache Configuration

1. First, open some necessary Module comments (just remove the # sign in front), in the conf/httpd file

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule slotmem_shm_module modules/mod_slotmem_shm.so
LoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.so

Here I mention the last two. I only opened the first few, and Apache couldn't start.

I checked the log and found that it kept reporting the error "Failed to lookup provider 'shm' for 'slotmem': is mod_slotmem_shm loaded??". I checked and found that the module was not enabled. If I removed the # sign, the error would not be reported.

Another reason is that the error (22) Invalid argument: AH01183: Cannot share balancer is reported. After opening this module, the error will not be reported.

2. Uncomment Virtual hosts in the conf/httpd file

Remove the # before Include

3. Add configuration in conf/extra/httpd-vhosts.conf

Let me post my own configuration first.

<VirtualHost *:80>
  ServerAdmin [email protected]
  ProxyPass / balancer://cluster/ stickysession=JSESSIONID|jsessionid nofailover=On
  ProxyPassReverse / balancer://cluster/
  ServerName 192.168.10.212
  ErrorLog "logs/error.log"
  CustomLog "logs/access.log" common
</VirtualHost>
ProxyRequests Off
<proxy balancer://cluster>
  BalancerMember ajp://192.168.10.98:8009 loadfactor=1 route=jvm1 smax=5 max=20 ttl=120 retry=300 timeout=15
  BalancerMember ajp://192.168.10.137:8009 loadfactor=1 route=jvm2 smax=5 max=20 ttl=120 retry=300 timeout=15
</proxy>

ProxyPass: Maps the remote server to the URL space of the local server

balancer:// : proxy instruction type

stickysession: Balancer sticky session name. This value is often set to something like JSESSIONID or PHPSESSIONID, depending on the backend application server that supports sessions. If the backend server uses different cookie names or URL-encoded IDs (like a servlet container), use | to separate them. The first part is for the cookie, the second for the path.

nofailover : If set to 'On', the session will be terminated immediately when the worker is disabled or an error occurs. You can set this value to On if the backend server does not support session replication.

ProxyRequests: When using the ProxyPass directive, the ProxyRequests directive should usually be turned off.

BalancerMember: Balancer member

loadfactor: Work unit load factor. Used for BalancerMember. It is a number between 1 and 100 that defines the normalized weight load applied to this worker.

route : The route of the worker, used in the load balancer. The route is a value appended to the session ID.

smax: Create up to the maximum number of links (Soft Maximum, or smax) as needed. Any link that exceeds the smax number will be assigned a lifetime, or ttl.

max: The default value is the number of threads per process in the current MPM. In the Prefork MPM, this value is always 1, and in the Worker MPM, this value is controlled by ThreadsPerChild.

ttl: Time To Live (TTL) for inactive links exceeding the smax number of connections, in seconds. Apache will close any connections that have not been used during this time.

retry: The timeout for thread pool worker retry, in seconds. If the thread pool worker status to the backend server is an error, Apache will not submit any requests to the server until the timeout expires. This allows the backend server to be taken down for maintenance and brought back online later. A value of 0 means always retrying the worker on error without waiting any time.

timeout: Link timeout, in seconds. If not set, Apache will wait until a connection is available. This directive is often used together with the max parameter to limit connections to the backend server.

This is almost the configuration, just restart the Apache service.

Note: When you enter localhost or 127.0.0.1, the words "It works" will no longer appear. Instead, a 503 error will be reported. This is normal because Apache has now enabled the load balancing function and has directed the target IP to Tomcat through AJP, but Tomcat has not been started yet.

Tomcat Configuration

Take the server 192.168.10.98 as an example

Use Vim to open the server.xml under conf (if you don’t know Vim, please use your imagination ╮(╯▽╰)╭). The load balancing introduced here is that Apache connects with Tomcat through the AJP protocol, so the port number configured in Apache is the port number of AJP in Tomcat.

In addition, you need to configure jvmRoute in Engine

 <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">

The jvmRoute here corresponds to the route in Apache. (I just removed this and found that it still works. I think it's amazing, but it should be configured.)

Finally,

 <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>

This note opens

At this point, the configuration is complete. Similarly, configure it on 192.168.10.137.

Write a small test.jsp for testing

<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="java.util.*"%>
<html>
  <head>
    <title>ApacheTomcatTest</title>
  </head>
  <body>
    <%
      out.println("<br> SESSION ID:" + session.getId() + "</br>");
    %>
  </body>
</html>

Create a balance folder under Tomcat's webapps directory, put test.jsp in the folder, and start Tomcat.

Finally, enter 192.168.10.212/balance/test.jsp in the browser
You will find that the value of SESSION ID has been changing between two numbers

This is the embodiment of load balancing. Of course, when you enter 192.168.10.212, it will not show "It works", but the classic Tomcat homepage.

By the way, the above is based on three servers

For more information about Apache+Tomcat7 load balancing configuration, please click the following related links

You may also be interested in:
  • Apache, wsgi, django program deployment configuration method detailed explanation
  • Detailed explanation of Apache website service configuration based on Linux
  • Detailed explanation of the correct way to configure SSL (https certificate) in Apache on Ubuntu
  • How to configure two or more sites using Apache Web server
  • Installation, activation and configuration of ModSecurity under Apache
  • Apache Web Server Installation and Configuration Tutorial in CentOS 7
  • Alibaba Cloud Server Apache configures SSL certificate to successfully enable Https (records various pitfalls)
  • Solution to localhost not being available after win10 apache configures virtual host
  • How to configure multiple virtual hosts locally via Apache

<<:  Analyzing the troublesome Aborted warning in MySQL through case studies

>>:  This article helps you understand PReact10.5.13 source code

Recommend

The difference between br and br/ in HTML

answer from stackflow: Simply <br> is suffic...

Description of the hr tag in various browsers

Generally, we rarely meet HR, but once we do, it c...

Native js to achieve accordion effect

In actual web page development, accordions also a...

Solution to 1045 error in mysql database

How to solve the problem of 1045 when the local d...

Explore JavaScript prototype data sharing and method sharing implementation

Data Sharing What kind of data needs to be writte...

Detailed steps to download Tomcat and put it on Linux

If you have just come into contact with Linux, th...

Detailed explanation of using JavaScript WeakMap

A WeakMap object is a collection of key/value pai...

CSS3 realizes the mask barrage function

Recently I saw a barrage effect on B station call...

Detailed explanation of the definition and function of delimiter in MySQL

When you first learn MySQL, you may not understan...

React Fragment Introduction and Detailed Usage

Table of contents Preface Motivation for Fragment...

How to test network speed with JavaScript

Table of contents Preface Summary of the principl...