Currently, Server Load balancer is commonly used. 1. DNS-Based Server Load balancer implements Server Load balancer through random name resolution in the DNS service. In the DNS server, you can configure the same name for multiple different addresses, and the client that finally queries the name will get an address when parsing the name. Therefore, for the same name, different customers get different addresses and they access the Web servers at different addresses, so as to achieve the purpose of load balancing 2. reverse proxy load balancing (such as apache + JK2 + Tom
Currently, the most common load balancing technologies are:
1. DNS-Based Load Balancing
Server Load balancer is achieved through random name resolution in the DNS service. In the DNS server, you can configure the same name for multiple different addresses, the client that finally queries the name will get an address when parsing the name. Therefore, for the same name, different customers get different addresses, and they also access the Web servers at different addresses to achieve load balancing.
2. Reverse proxy Server Load balancer (for example, apache + JK2 + Tomcat, apache + proxy http or apache + proxy + ajp)
The proxy server can forward requests to the internal Web server so that the proxy server can evenly distribute requests to one of multiple internal Web servers to achieve load balancing. This proxy method is different from the common proxy method. The standard proxy method is that the customer uses the proxy to access multiple external Web servers. This proxy method is used by multiple customers to access internal Web servers, therefore, it is also called reverse proxy mode.
3. Network Address Translation (NAT)-based load balancing technology (such as Linux Virtual Server (LVS)
The network address is converted between the internal address and the external address so that computers with the internal address can access the external network, when a computer in an external network accesses an external address owned by the address translation gateway, the address translation gateway can forward it to a mapped internal address. Therefore, if the address translation gateway can evenly convert each connection to a different internal server address, then the computers in the external network will communicate with the server on the address they have obtained, this achieves load balancing.
The proxy mode in this article is apache + JK2 + Tomcat.
1. Environment
Apache + 2 tomcat + memcached servers
2. install apache, tomcat, and memcached on Ubuntu
A. Install apache2
Sudo apt-get install apache2
You can use a browser to test whether the installation is successful.Http: // localhost/
B. Download the official tar compression version tomcat7 (7.0.29 is used in this case) and decompress tar-zxvf into two folders with different names.
C. install and configure apache2 mod jk
Sudo apt-get install libapache2-mod-jk
Edit workers. properties and add the following content:
Sudo gedit/etc/apache2/workers. properties
Worker. list = controller, worker at1, tomcat2
Worker. tomcat1.type = ajp13
Worker. tomcat1.host = localhost
Worker. tomcat1.port = 8009
Worker. tomcat1.lbfactor = 1
Worker. tomcat2.type = ajp13
Worker. tomcat2.host = localhost
Worker. tomcat2.port = 9009
Worker. tomcat2.lbfactor = 1
#======= Controller, Server Load balancer controller ========
Worker. controller. type = lb
Worker. controller. balanced_workers = worker at1, tomcat2 # specify tomcat that shares the request
Worker. controller. sticky_session = false
And replaceSudo gedit/etc/apache2/mod-available/jk. conf
MediumJkWorkersFile attribute value is/Etc/apache2/workers. properties
Finally, edit the VM File
Sudo gedit/etc/apache2/sites-enabled/default
< span>
*:80
>
..............................................................................JkMount /* controller
<> *:80>
3. Modify tomcat configuration
Conf/server. xml of the First tomcat
Conf/server. xml of the second tomcat
Note that the Connector port configuration must be consistent with the workers. properties port in step 3. In addition, jvmRoute must be consistent.
Of course, make sure that the ports contained in the conf/server. xml file are not the same. Just change the second tomcat.
ModifyTomcat context. xml
- <Manager ClassName="De. javakaffee. web. msm. MemcachedBackupSessionManager"
- MemcachedNodes="N1: localhost: 11211"
- RequestUriIgnorePattern=". */. (Png | gif | jpg | css | js) $"
- SessionBackupAsync="False"
- SessionBackupTimeout="100"
- TranscoderFactoryClass="De. javakaffee. web. msm. serializer. javolution. JavolutionTranscoderFactory"
- CopyCollectionsForSerialization="False"/>
Jar package:
Memcached-2.6.jar (Https://code.google.com/p/spymemcached/downloads/detail? Name = spymemcached-2.8.4.jar)
Javolution-5.4.3.1.jar
Memcached-session-manager-1.5.1.jar
Memcached-session-manager-tc7-1.5.1.jar
Msm-javolution-serializer-1.5.1.jar
Msm-kryo-serializer-1.5.1.jar
Msm-xstream-serializer-1.5.1.jar
The above jar packages can be found at http://code.google.com/p/memcached-session-manager/downloads/list. Put it in the tomcat/lib directory
4,Install the memcached server, refer to the http://www.linuxidc.com/Linux/2012-08/69313.htm
5. Test code and test results
Create a webapps/balance/test. jsp file
- <% @ PageContentType="Text/html; charset = UTF-8"%>
- <% @ PageImport="Java. util .*"%>
- <Html><Head><Title>Cluster App Test Title> Head>
- <Body>
- Server Info:
- <%
- Out. println (request. getLocalAddr () + ":" + request. getLocalPort () +"<Br>"); %>
- <%
- Out. println ("<Br>ID "+ session. getId () +"<Br>");
- // If a new Session attribute is set
- StringDataName=Request. GetParameter ("dataName ");
- If (dataName! = Null & dataName. length ()>0 ){
- StringDataValue=Request. GetParameter ("dataValue ");
- Session. setAttribute (dataName, dataValue );
- }
- Out. print ("<B>Session list B>");
- EnumerationE=Session. GetAttributeNames ();
- While (e. hasMoreElements ()){
- StringName= (String) e. nextElement ();
- StringValue=Session. GetAttribute (name). toString ();
- Out. println (name + "=" + value +"<Br>");
- System. out. println (name + "=" + value );
- }
- %>
- <Form Action="Test. jsp" Method="POST">
- Name:<Input Type=Text Size=20 Name="DataName">
- <Br>
- Value:<Input Type=Text Size=20 Name="DataValue">
- <Br>
- <Input Type=Submit>
- Form>
- Body>
- Html>
Start apache and two tomcat services. Do not forget to check the normal startup of memcache Service (telnet 127.0.0.1 11211 & stats)
Enter localhost/balance/test. jsp in the browser
Enter key value and submit
Refresh page