nginx+tomcat+memcached building server clusters and load balancing

Source: Internet
Author: User
Tags memcached

In actual projects, because of the large number of user visits, often need to open multiple servers at the same time to meet the actual needs. But how do you manage them when you start multiple services at the same time? How to achieve session sharing? Let's talk about how to use Tomcat+nginx to build a server cluster and how to implement session sharing.

Environment:

APACHE-TOMCAT-6.0.29+APACHE-TOMCAT-6.0.29+JDK1.6+WIN7 (the server system was not used because he had just made a demo,j)

1. First we prepare two Tomcat (TOMCAT1,TOMCAT2) and change the port to a different one. My tomcat1 is 8089,tomcat2 is 9089. Modify the location of the port in the Conf->server.xml file of the Tomcat file:

2. In order to distinguish between which Tomcat we are actually visiting, so here we modify the information of the Tomcat home page, under the Tomcat1 folder Webapps->root->index.html, at the front I added <span Style= "Color:red" >this is Tomcat One</span>, also add Tomcat2 <span under style= "color:red" >this is tomcat TW O</span>.

3. Next we begin to prepare nginx. Nginx is also a server, where we are mainly used to do proxy and load balancing. When we download the Nginx directly decompression can be used. To run Nginx can be directly click Nginx.exe, you can also use the command mode: Start Nginx start nginx,nginx-s stop Nginx.

4. After you start Nginx, we enter 127.0.0.1 in the browser, the following interface appears to represent the Nginx has been nearly started:

5. Next we configure Nginx to do proxy server and load balancer:

Open conf->nginx.conf file under Nginx file, add in Hettp

Upstream 127.0.0.1{//This 127.0.0.1 can take a name     127.0.0.1:8088 weight=1; #服务器A    127.0.0.1:9088 Weight=1; #服务器B}

Add the location in server:

Proxy_pass  http://127.0.0.1;  #访问js, pictures and other need to do agent

The others are now not configured, and the configuration is completed as follows:

Description

1) After the server IP, even if Tomcat ip,weight represents the weight, the larger the weight, the greater the probability that the corresponding server is accessed, if equal, several servers are accessed in turn;

2) The HTTP after Proxy_pass must be added;

This is when we start Tomcat1,tomcat2,nginx, enter 127.0.0.1 in the browser, then refresh, then we will see the TOMCAT1 and TOMCAT2 pages alternating display, which means that Nginx has loaded two tomcat, The request can be forwarded to a different tomcat.

6. Now we're going to solve the session problem.

The following jar packages need to be placed in the Tomcat Lib file

These jar packages I have uploaded,: Http://files.cnblogs.com/mouseIT/nginx%2Cmemcached%EF%BC%8Ctomcat%E9%9B%86%E7%BE%A4%E7%9A%84jar%E5%8C%85.rar

Then add the following data in Tomcat's context.xml:

<manager classname= "De.javakaffee.web.msm.MemcachedBackupSessionManager"                      memcachednodes= "N1 : 127.0.0.1:11211 "                      sticky=" false "                      sessionbackupasync=" false "                      Lockingmode = "Auto"                      requesturiignorepattern= ". *\." ( ICO|PNG|GIF|JPG|CSS|JS) $ "                      transcoderfactoryclass=" De.javakaffee.web.msm.JavaSerializationTranscoderFactory "                      />

As follows:

In order to achieve session sharing, here we also need to use the memcached.

will be down the memcached directly decompression, click Memcached.exe. This is only a window, nothing is seen, enter after the command stats, see the following indicates normal:

In order to test, we build a Web project, remember to add <distributable/> in the Web-app XML, so the server knows it is distributed.

At the same time we set up the default landing page, the content is as follows:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding= "UTF-8"%><%@ pageImport= "Java.util.*"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > Thisis Tomcat oneserver Info:<%String Path=Request.getcontextpath (); Out.println (Request.getlocaladdr ()+ ":" + request.getlocalport () + "<br>");%><%Out.println ("<br> ID" + session.getid () + "<br>"); //If there is a new Session property setString dataname = Request.getparameter ("Dataname"); if(Dataname! =NULL&& dataname.length () > 0) {String DataValue= Request.getparameter ("DataValue");  Session.setattribute (Dataname, DataValue); } out.println ("<b>session list </b><br>"); System.out.println ("============================"); Enumeration e=Session.getattributenames ();  while(E.hasmoreelements ()) {String name=(String) e.nextelement (); String value=Session.getattribute (name). toString (); OUT.PRINTLN (Name+ "=" + value+ "<br>"); SYSTEM.OUT.PRINTLN (Name+ " = " +value); }%> <form action= "<%=path%>/login.jsp" method= "POST" >Name:<input type= "text" size=20 name= "Dataname" > <br>values:<input type= "text" size=20 name= "DataValue" > <br> <input type= "Submit" > </form></body& Gt;

This is collected on the Internet, the specific principle I will not elaborate.

Then we start Memcached->tomcat->nginx in turn. Note that you must start memcached first, or you will get an error when you start Tomcat.

Then we visit the Web project name under 127.0.0.1/tomcat

After entering the main page, we enter the name and value, then click Submit (Note Tomcat window), I entered the abc,123, click Submit, TOMCAT1 appears as follows:

Then we refresh the browser and observe the Tomcat2 window, and the above data will appear:

This means that TOMCAT2 obtained the session data, Tomcat1,tomcat2 realized the session sharing.

These are just a few of the simple applications that have been summed up today, and a lot of detail has not been said. And that's what I learned. After what good things will continue to share with you, we discuss together!

nginx+tomcat+memcached building server clusters and load balancing

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.