Server Load balancer software Apache and tomcat integration, apachetomcat
Apache can be used not only as a WEB server, but also as a server Load balancer or reverse proxy server.
Some text and content come from the Internet.
First, Apache is not designed as a server Load balancer server. To use the Server Load balancer function of Apache, you must manually enable the mod_proxy module of Apache.
Using Apache for load balancing, there are some basic configurations
1). basic configuration
Assume that a website with the domain name www.a.com uses apache as the Server Load balancer device. You must first enable several required modules of Apache:
Httpd. conf code
LoadModule proxy_modulemodules/mod_proxy.so
LoadModule proxy_balancer_modulemodules/mod_proxy_balancer.so
LoadModule proxy_http_modulemodules/mod_proxy_http.so
Mod_proxy provides the proxy server function. mod_proxy_balancer provides the Server Load balancer function. mod_proxy_http enables the proxy server to support the HTTP protocol. If you change mod_proxy_http to another protocol module (such as mod_proxy_ftp), load balancing for other protocols is supported.
Then add the following Configuration:
Httpd. conf code
ProxyRequests Off
<Proxy balancer: // mycluster>
BalancerMemberhttp: // The node-a.myserver.com is: 8080
BalancerMemberhttp: // The node-b.myserver.com is: 8080
</Proxy>
ProxyPass/balancer: // mycluster/
# Warning: The following configurations are only used for debugging. Do not add them to the production environment !!!
<Location/balancer-manager>
SetHandler balancer-manager
Order Deny, Allow
Deny from all
Allow from localhost
</Location>
Note: node-a.myserver.com, node-b.myserver.com is the domain name of the other two servers, not the domain name of the current server
From the above ProxyRequests Off, we can see that the Server Load balancer is actually a reverse proxy, but its proxy forwarding address is not a specific server, but a balancer: // protocol:
ProxyPass/balancer: // The mycluster Protocol address can be defined at will. Then, set the content of the balancer protocol in the <Proxy> section. The BalancerMember command can be used to add real server addresses in the Server Load balancer group.
The following <Location/balancer-manager> is used to monitor the working conditions of Server Load balancer. It can be added during debugging (unavailable in the production environment !), Access http: // localhost/balancer-manager/to view the working status of the Server Load balancer.
OK. Restart the server and access the address of your Apache server (www.a.com) to see the effect of Server Load balancer.
Error prompt:
The webpage prompts Internal Serveral Error. Check the error. log file.
Error. log Code
[Warn] proxy: No protocol handler wasvalid for the URL/admin/login_form. If you are using a DSO version ofmod_proxy, make sure the proxy submodules are encoded in the configurationusing LoadModule.
The reason is that the configuration: # ProxyPass/balancer: // mycluster may be missing one/
2). Load Ratio allocation
On the balancer-manager page, you can see that requests are evenly distributed.
What should I do if I don't want an average allocation? Add the loadfactor parameter to BalancerMember. The value range is 1-100. For example, if you have three servers and the load distribution ratio is, you only need to set them as follows:
Httpd. conf code
Copy the Code as follows:
ProxyRequests Off
<Proxy balancer: // mycluster>
BalancerMemberhttp: // node-a.myserver.com: 8080 loadfactor = 7
BalancerMemberhttp: // node-b.myserver.com: 8080 loadfactor = 2
BalancerMemberhttp: // node-c.myserver.com: 8080 loadfactor = 1
</Proxy>
ProxyPass/balancer: // mycluster
3). Load Distribution Algorithm
By default, Server Load balancer tries its best to make the number of requests received by each server meet the predefined ratio. If you want to change the algorithm, you can use the lbmethod attribute. For example:
Httpd. conf code
ProxyRequests Off
<Proxy balancer: // mycluster>
BalancerMemberhttp: // node-a.myserver.com: 8080 loadfactor = 7
BalancerMemberhttp: // node-b.myserver.com: 8080 loadfactor = 2
BalancerMember http://node-c.myserver.com: 8080 loadfactor = 1
</Proxy>
ProxyPass/balancer: // mycluster
ProxySet lbmethod = bytraffic
Possible values of lbmethod include:
Lbmethod = byrequests balanced by the number of requests (default)
Lbmethod = bytraffic balanced by traffic
Lbmethod = bybusyness balanced by the degree of busy (always allocated to the server with the least number of active requests)
For the principles of various algorithms, see the Apache documentation.
2. Hot Standby)
Hot Backup is easy to implement. You only need to add the status = + H attribute to specify a server as a backup server:
Httpd. conf code
ProxyRequests Off
<Proxy balancer: // mycluster>
BalancerMemberhttp: // The node-a.myserver.com is: 8080
BalancerMemberhttp: // node-b.myserver.com: 8080 status = + H
</Proxy>
ProxyPass/balancer: // mycluster
From the balancer-manager interface, we can see that requests always flow to node-a. Once node-a fails, Apache will detect errors and distribute requests to node-B. Apache checks the status of node-a every few minutes. If node-a recovers, it continues to use node-.
Server Load balancer group for Tomcat and Apache
Below are two tomcat servers and one Apache Server Load balancer group for your reference.
Preparations:
1. Use the free installation version of tomcat and place it in different directories. tomcat uses different ports 。
2. Install JDK;
3. Download installation apache_2.2.11-win32-x86-no_ssl.msi
Apache uses a apache_2.2.11-win32-x86-no_ssl.msi. If versions earlier than 2.2Apache Server Load balancer are configured differently, since 2.2.11 and 2.2.8 are integrated with load balancing tools such as jk2, the configuration is much easier 。
Install Apache as a Windows Service Running on port 80. After successful installation, you can see the Apache2.2 service in the System Service list. After the service is started, enter http: // localhost in the browser for testing, if you can see an "Itworks! The page indicates that Apache is working properly 。
Find the conf/httpd. conf file in the Apache installation directory and remove the annotator (#) before the following text so that Apache can automatically load the proxy module at startup 。
LoadModuleproxy_module modules/mod_proxy.so
LoadModuleproxy_ajp_module modules/mod_proxy_ajp.so
LoadModuleproxy_balancer_module modules/mod_proxy_balancer.so
LoadModuleproxy_connect_module modules/mod_proxy_connect.so
LoadModuleproxy_ftp_module modules/mod_proxy_ftp.so
LoadModuleproxy_http_module modules/mod_proxy_http.so
Pull down the document to find the <IfModuledir_module> node, and add index after directoryindexindex.html. jsp, this step is just to wait for the configuration of tomcat can see the kitten home page, you can not do. Continue to pull down the document to find Include conf/extra/httpd-vhosts.conf, remove the previous annotator 。
Open the conf/extra/httpd-vhosts.conf, configure the virtual site, and add
<VirtualHost *: 80>
ServerAdmin administrator email
ServerNamelocalhost
ServerAliaslocalhost
ProxyPass/balancer: // sy/stickysession = jsessionid nofailover = On
ProxyPassReverse/balancer: // sy/
ErrorLog "log/sy-error.log"
CustomLog "logs/sy-access.log" common
</VirtualHost>
Return to httpd. conf and add
ProxyRequestsOff
<Proxybalancer: // sy>
BalancerMemberajp: // 127.0.0.1: 8009 loadfactor = 1 route = jvm1
BalancerMemberajp: // 127.0.0.1: 9009 loadfactor = 1 route = jvm2
</Proxy>
ProxyRequestsOff tells Apache to use a reverse proxy. The IP address and port uniquely determine the tomcat node and the configured ajp accept port. loadfactor is the load factor, apache will forward requests to the backend tomcat node according to the load factor. The larger the load factor, the more requests the tomcat server will process. For example, if both tomcat servers are 1, apache forwards data in a ratio of. If it is 2 and 1, it forwards data in a ratio of. In this way, you can make the configuration more flexible. For example, you can increase the proportion of processing work for servers with good performance, if multiple servers are used, you only need to modify the IP address and port. The route parameter corresponds to the engine path (jvmRoute) in the subsequent tomcat Load Balancing configuration)