Detailed explanation of Apache+Tomcat7 load balancing method under Windows

  • 2020-12-22 17:53:09
  • OfStack

The preparatory work

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

1. Install Apache2. 4.4

There is no need to pay attention during the installation process, 1 straight Next will do. After installation, Apache service will be enabled by default. You can type localhost or 127.0.0.1 into the browser and the word It works will appear

2. Java environment

There are many configurations available on Ubuntu that I won't go into, but remember to configure JAVA_HOME and PATH

3. Tomcat installation

Just unzip it in the directory, my directory is /opt/tomcat7/, and unzip it on both Ubuntu. You may run into a problem with insufficient permissions, and that's when you need to type commands. The most straightforward is the sudo chmod 777 directory. For security reasons, you may want to properly authorize it, but forget about it.

Start the configuration

Apache configuration

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


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, Apache1 was unable to start up

I looked at the log and found that 1 was reporting Failed to lookup provider 'shm' for' slotmem': is mod_slotmem_shm loaded? The & # 63; This error, I looked at the original Module did not open, remove the # sign will not report this error;

The other one is because the error (22)Invalid argument: AH01183:Cannot share balancer was reported. After opening this Module, the error will not be reported.

2. Open the comment on Virtual hosts in the conf/httpd file

Remove the pound sign before Include

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

Post my own configuration first


<VirtualHost *:80>
  ServerAdmin 502053382@qq.com
  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: Map the remote server to the URL space of the local server

balancer:// : Instruction type for the agent

stickysession: Equalizer adhesion session name. This value is often set to something like JSESSIONID or PHPSESSIONID, which depends on the back-end application server that supports the session. If the backend server uses a different cookie name or ID encoded by URL (like the servlet container), use | to separate them. Part 1 is for cookie, and part 2 is for paths.

nofailover: If set to 'On', the session breaks immediately when the unit of work is disabled or an error occurs. You can set this value to On if the backend server does not support session replication (Session replication).

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

BalancerMember: Balanced membership

loadfactor: Unit load factor. Used for BalancerMember. It is a number word between 1 and 100 that defines the normalized weight load applied to the unit of work.

route: Routing of units of work for use in load balancers. This path is one value that is attached to session ID.

smax: The maximum number of flexible links (Soft Maximum) can be created as needed, which is smax's link. Any link that exceeds the number of smax will specify a lifetime of ttl.

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

ttl: The lifetime of inactive links (Time To Live) beyond the number of smax connections, in seconds. Apache will close all links that have not been used during this time.

retry: Timeout in seconds for thread pool unit of work to retry. If the thread pool work unit state to the back-end server is an error, Apache will not submit any requests to that server until the timeout ends. This allows the backend server to be shut down for maintenance and later brought online. A value of 0 means that the unit of work that always retries the error state does not wait any time.

timeout: Link timeout in seconds. If not set, Apache 1 waits until a link location is available. This directive is often used with the max parameter 1 to restrict links to back-end servers.

That's about it. Restart the Apache service.

Note: when entering localhost or 127.0.0.1, the word It works will no longer appear, but 503 error will be reported, which is normal, because Apache has started the load balancing function, and AJP has been directed to Tomcat on IP, but Tomcat has not been started.

Tomcat configuration

Take the server of 192.168.10.98 as an example

Use Vim to open server.xml below conf. (If not Vim, please imagine.) The load balancing introduced here is the connection between Apache and Tomcat through the AJP protocol, so the port number configured in Apache is the port number of AJP.

In addition, jvmRoute needs to be configured in Engine


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

jvmRoute here corresponds to route in Apache. (I just took this out of the configuration and found it worked, which is amazing, but it should be configured.)

And then finally you have to


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

This comment turns on

At this point, the configuration is complete. The same is true for 192.168.10.137.

Write test.jsp for a small test


<%@ 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 1 balance folder under the webapps directory of Tomcat, place test.jsp under that folder, and start Tomcat

Finally in the browser input 192.168.10.212 / balance/test jsp
You will find that the value 1 of SESSION ID changes directly between two numbers

So that's load balancing. Of course, input 192.168.10.212 will not appear It works, but the classic Tomcat home page.

By the way, this is based on 3 servers

For more information on the Apache+Tomcat7 load balancing approach, click the relevant link below


Related articles: