Apache + multiple Tomcat + Memcached shared session

Source: Internet
Author: User
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

  1. <Manager ClassName="De. javakaffee. web. msm. MemcachedBackupSessionManager"
  2. MemcachedNodes="N1: localhost: 11211"
  3. RequestUriIgnorePattern=". */. (Png | gif | jpg | css | js) $"
  4. SessionBackupAsync="False"
  5. SessionBackupTimeout="100"
  6. TranscoderFactoryClass="De. javakaffee. web. msm. serializer. javolution. JavolutionTranscoderFactory"
  7. 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

  1. <% @ PageContentType="Text/html; charset = UTF-8"%>
  2. <% @ PageImport="Java. util .*"%>
  3. <Html><Head><Title>Cluster App Test Title> Head>
  4. <Body>
  5. Server Info:
  6. <%
  7. Out. println (request. getLocalAddr () + ":" + request. getLocalPort () +"<Br>"); %>
  8. <%
  9. Out. println ("<Br>ID "+ session. getId () +"<Br>");
  10. // If a new Session attribute is set
  11. StringDataName=Request. GetParameter ("dataName ");
  12. If (dataName! = Null & dataName. length ()>0 ){
  13. StringDataValue=Request. GetParameter ("dataValue ");
  14. Session. setAttribute (dataName, dataValue );
  15. }
  16. Out. print ("<B>Session list B>");
  17. EnumerationE=Session. GetAttributeNames ();
  18. While (e. hasMoreElements ()){
  19. StringName= (String) e. nextElement ();
  20. StringValue=Session. GetAttribute (name). toString ();
  21. Out. println (name + "=" + value +"<Br>");
  22. System. out. println (name + "=" + value );
  23. }
  24. %>
  25. <Form Action="Test. jsp" Method="POST">
  26. Name:<Input Type=Text Size=20 Name="DataName">
  27. <Br>
  28. Value:<Input Type=Text Size=20 Name="DataValue">
  29. <Br>
  30. <Input Type=Submit>
  31. Form>
  32. Body>
  33. 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

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.