Installation and implementation of apache load balancing

  • 2020-05-06 12:07:29
  • OfStack

In fact, whether it is distributed, data cache, or load balancing, is nothing more than to improve the performance of the website bottleneck, in the site source code does not do optimization, load balancing can be said to be the most direct means. Actually put aside the word, let go of the said, is to want users to be able to, that is to say, all the user's access pressure across multiple servers, can also be distributed to multiple tomcat, if more than one server install tomcat, so even if the load balancing, performance can improve too much, but can improve the stability, the fault tolerance & # 65377; When one of the main tomcat crashes, the other tomcat can be replaced, because Session sharing is implemented between tomcat. When the tomcat server is started again after the repair, it will automatically copy all session data and then join the cluster. This allows uninterrupted service to be provided. To really substantially improve performance, you have to distribute across multiple servers. The same can be done with tomcat. The relevant information on the Internet is more, can be very convenient to look up, but the quality is not high. I hope this essay can provide a systematic summary 。 In this paper, the

The example is to run two tomcat on the same server and do load balancing between two tomcat. It is also possible to configure one tomcat for multiple servers, and in that case, you can use the installed version of tomcat instead of the install-free tomcat shown below, and the tomcat port configuration will not need to be modified. Also mentioned below 。

Load balancing of tomcat requires the addition of apache server to realize. Please uninstall the installed tomcat before proceeding with the configuration, and then check the version of apache. I used the free installation version of apache-tomcat-6.0.18 for this configuration. After my personal test, I concluded that the installed version of tomcat would not be able to start more than two on the same machine, which may be because the installed version of tomcat invaded the system, resulting in conflicts even if the configuration was modified in server.xml. So I used tomcat install-free version 。

apache USES apache_2.11-win32-x86-no_ssl.msi 。 The configuration is different if the version is lower than 2.2Apache, because the 2.2.11 and 2.2.8 versions incorporate load balancing tools such as jk2, so the configuration is much simpler. I didn't test the other version, but it remains to be seen. The two programs can be downloaded from the official website.

Install Apache as Windows service running on port 80. After successful installation, you can see Apache2.2 service 。 in the system service list. After the service starts, type http://localhost in the browser to test if you can see a "It works!" Is that Apache is working properly. Unzip tomcat to any directory, assign a value of another name 。 Name and path have no effect on configuration 。 However, to ensure that the ports do not conflict, if the user with Oracle or IIS needs to modify or close the relevant interface of the service. Of course, jdk configuration is also required, which I won't overstate.

To achieve load balancing, first, find the conf/ httpd.conf file in the Apache installation directory, remove the following text before the comment (#) so that Apache automatically load the agent (proxy) module 。 at startup;

 
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 proxy_ftp_module modules/mod_proxy_ftp.so 
LoadModule proxy_http_module modules/mod_proxy_http.so 


Pull down the document to find < IfModule dir_module > Node, after DirectoryIndex index.html add index.jsp, this step is only for the kitten home page after the configuration of tomcat, can not do 。 Continue the drop down to Include conf/extra/ httpd-vhosts.conf, remove the previous comment 。

Then open conf/extra/ httpd-vhosts.conf, configure the virtual site, and add
at the bottom
 
<VirtualHost *:80> 
ServerAdmin  Administrator mailbox  
ServerName localhost 
ServerAlias localhost 
ProxyPass / balancer://sy/ stickysession=jsessionid nofailover=On 
ProxyPassReverse / balancer://sy/ 
ErrorLog "logs/sy-error.log" 
CustomLog "logs/sy-access.log" common 
</VirtualHost> 

Then go back to httpd.conf and add
at the bottom of the document
 
ProxyRequests Off 
<proxy balancer://sy> 
BalancerMember ajp://127.0.0.1:8009 loadfactor=1 route=jvm1 
BalancerMember ajp://127.0.0.1:9009 loadfactor=1 route=jvm2 
</proxy> 

ProxyRequests Off is to tell Apache to use a reverse proxy,ip address and port only determine the tomcat node and configuration of ajp accept port 。 loadfactor is the load factor,Apache will forward the request to the back-end tomcat node in proportion to the load factor. The larger the load factor is, the more requests the corresponding tomcat server will handle. This makes the configuration more flexible. For example, you can increase the proportion of processing work for good servers. If you have multiple servers, you only need to change the ip address and port. The route parameter corresponds to the engine path (jvmRoute)

in the subsequent tomcat load balancing configuration

Related articles: