Tomcat_apache achieves multi-server network load balancing

Source: Internet
Author: User

When developing large network applications, sometimes a server cannot meet the requirements of the client. In this case, you can only add servers to solve the problem, that is, use multiple servers to balance the network load. By using network load balancing, multiple servers are published to the network with one IP address, which is similar to a server, if one or more servers in the network load balancing are unavailable or busy, the service will not be interrupted, and the client communication can be quickly assigned to the remaining servers, it helps you provide uninterrupted services for key business programs.
For different types of applications, the network load balancing methods are also different. In Windows Server 2003, it is easy to manage network load balancing (this is its built-in component). After configuring load balancing, all services that support load balancing can automatically achieve load balancing, such: internet Information Service (IIS), ISA Server 2000 firewall and proxy server, VPN virtual private network, Terminal Server, Windows Media Services (Windows VOD, video broadcast) and other services.
If you want to set up services other than the above, you need to support the service itself for network load balancing. The following describes how to use apathe SERVER + Tomcat to achieve network load balancing.
There are two ways to achieve network load balancing for Tomcat. The first is to rely on Windows Server 2003. The principle of this method is to install Tomcat servers with the same content on multiple servers, the configuration file is used to associate it with the heartbeat of Windows Server 2003, so that multiple Tomcat servers can be synchronized and shared by the heartbeat of Windows Server 2003. This method is relatively simple, but it has many disadvantages. It needs to constantly read the balance processing information of Windows Server 2003, which is inefficient and cannot be used across platforms.
The ideal way is not to rely on OS, so Tomcat itself has the ability to manage clusters. To achieve this, follow these steps.
I. Preparation
Apache server
: Http://httpd.apache.org
Apache HTTP Server (hereinafter referred to as Apache server or Apache) is an open source software provided by the Apache Software organization. It is a very good professional web server, provides network administrators with a variety of Web management functions, including Directory Index, directory alias, content negotiation, configurable HTTP Error Report, setuid function of CGI program, sub-process resource management, server-side image ing, URL rewriting, URL spelling check and online manual. The Apache server does not provide Servlet/jsp containers. Therefore, in practical applications, the integration of Tomcat and Apache server can establish a practical and commercial web platform.
JK plug-in (mod_jk.so)
: Http://tomcat.apache.org/download-connectors.cgi
JK plug-in is used to connect the Apache server to Tomcat. The version of this plug-in must be consistent with that of the Apache server. Otherwise, the plug-in cannot be loaded (I use version 2.0.63. Please note that I have been tortured ).
Tomcat5.5 is not limited.
Ii. installation and configuration
1. install Tomcat on the two servers. Assume that the IP addresses are 192.168.0.2 and 192.168.0.3, the ports are both 8080, and the network is smooth.
2. Install apache2.0.63 on one or the third machine. You only need to install apache2.0.63 according to the default settings. If the installation is successful, the Apache HTTP service is automatically added to windows and the default port 80 is used as the http port. Make sure that port 80 is not occupied before installation, or use other ports during installation. <Apache_home>/conf has a configuration file httpd. conf. Find listen 80. You can modify the port number here.
3. Copy mod_jk.so to the <apache_home>/modules directory.
4. Create the following ATS. properties file in the <apache_home>/conf directory. The content is as follows:
Worker. List = worker at1, tomcat2, loadbalancer # indicates the list of Connected Services.
Worker. tomcat1.port = 8009 # Connection Management port in Tomcat. This port is used by default in Tomcat. You can change it to another port in Tomcat/CONF/server. xml.
Worker. tomcat1.host = 192.168.0.2 # Tomcat server address
Worker. tomcat1.type = ajp13 # type
Worker. tomcat1.lbfactor = 100 # Load Balancing Factor

Worker. tomcat2.port = 8009
Worker. tomcat2.host = 192.168.0.2
Worker. tomcat2.type = ajp13 # type
Worker. tomcat2.lbfactor = 100 # Load Balancing Factor

Worker. loadbalancer. type = LB
Worker. loadbalancer. balanced_workers = worker at1, tomcat2 # balance between worker AT1 and worker at2
Worker. loadbalancer. sticky_session = 1 #0 indicates that the session is not locked, and 1 indicates that the session is locked.
Worker. loadbalancer. sticky_session_force = false
5. Add the following to the end of the <apache_home>/CONF/httpd. conf file:
Loadmodule jk_module modules/mod_jk.so
Jkworkersfile CONF/initats. Properties
Jklogfile logs/mod_jk.log
Jkloglevel debug # Log Level debug, info, error, war
Jkmount/*. jsp loadbalancer # when accessing *. jsp, loadbalancer is started and automatically balanced to a specific comcat server through this service.
Iii. Running
Create a test. jsp file under the webapps/root of the two Tomcat servers, respectively.
Restart the apahe server and the two Tomcat servers. in the address bar, enter http: // Apache server address/test. jsp. When worker. loadbalancer. when sticky_session = 0, the result may be different for each refresh, proving that it has been balanced to different Tomcat servers, in the same session, it is forcibly bound to the same tomcat.


 

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.