Nginx + Tomcat + Memcached cluster

Source: Internet
Author: User
Tags nginx server

Nginx + Tomcat + Memcached cluster must take into account at least 2 aspects of the problem:

1. How to achieve load balancing?

2. How to achieve session replication and synchronization?

This article collects the related material on the net, the construction process of Nginx + Tomcat + Memcached cluster under window is summarized as follows.

First, the development environment

1. Nginx Version: 1.8.0

2. Tomcat version: 7.0.55

3. memcached Version: 1.4.13

4. Session replication synchronization using Memcache-session-manager Latest version: 1.6.3

5. Operating system: Win7


Second, Nginx + Tomcat to achieve load balancing

1. Installation of nginx and tomcat download

There is not much to say, see the download installation of Nginx and Tomcat specifically

2. Nginx Configuration

Open Nginx Main profile: D:\nginx-1.8.0\conf\nginx.conf, mainly to modify the proxy forwarding server IP and port, weight for server weight, the higher the number represents the greater chance of being accessed:

Upstream Local_tomcat {   server localhost:18080 weight=1;            Server localhost:18081 weight=1; }     Server {        listen       ;        Location/{            root   html;            Index  index.html index.htm;            Proxy_pass http://local_tomcat;         }    }

Third, Tomcat + memcached to achieve session replication and synchronization

The problem with Tomcat session replication and synchronization is usually the use of memcached, or Nginx_upstream_jvm_route, an Nginx extension module that implements the function of a Cookie-based session Sticky. If Tomcat too much does not recommend session synchronization, the server synchronization session is very resource-intensive, high concurrency environment prone to the session storm. Please adopt the session solution as appropriate for your application.

The following describes the use of memcached (Memcache-session-manager) to manage the session

1. memcached download installation (slightly)

2. Download the relevant JAR package

There are a lot of jar packages to download, the version of the jar can not be mistaken, I am here to provide a complete set of download of the jar package (has been tested available), need to download the click on the link to download.

3. Configure session Sharing

OK, put the above jar package into the Tomcat/lib directory, modify each tomcat configuration file Tomcat\conf\context.xml, in ... Add the following code between the tags:

A. Configuring Tomcat

Modify Tomcat\conf\server.xml, because I am the local win7 system installed under the 2 tomcat, so to avoid port conflicts, take tomcat-node1 as an example, mainly modify the following places: (Node2 and so on)

 
  

    
 
  

    
 
  

    
 
  
Modify the Tomcat\conf\context.xml, in ... Add the following code between the tags:
 
  
  
   n1:127.0.0.1:11211"  sticky=" false "  Sessi  lockingmode=" Uripattern:/path1|/path2  " Requesturiignorepattern= ". *\. (ICO|PNG|GIF|JPG|CSS|JS) $ "  transcoderfactoryclass=" De.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory "/>
 
  
(Note that the 2 Tomcat configurations are all memecachednodes n1:*)

B. Modifying the application JSP

I am using examples project with Tomcat, and replace the following JSP directly with the D:\nginx-1.8.0\Tomcat\tomcat-node1\webapps\examples\index.html

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><%@ page import= " Java.text.SimpleDateFormat "%>      Tomcat Cluster Testing        <%out.print ("[" +request.getlocaladdr () + ":" +request.getlocalport () + "]" + "
Session ID: "+ session.getid ()); %>

Iv. Testing

Start Nginx, memcached and 2 tomcat, use the browser to access the http://localhost/examples/, compare the Nginx server returned 2 pages, found 2 times the port is not the same but the session represents Nginx + The Tomcat + Memcached cluster was built successfully.


Reference article:

1. Http://www.tuicool.com/articles/I7ryYf

2. http://blog.csdn.net/liuzhigang1237/article/details/8880752

3. http://www.cnblogs.com/phirothing/archive/2013/12/05/3459814.html

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The above describes the Nginx + Tomcat + Memcached cluster, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.