---
title: "[web architect] nginx1.2.6+ tomcat7+memcached1.2.6"
author: "marshan"
date: "2012-12-25T17:40:37Z"
tags: ["Tomcat", "cluster", "memcached", "nginx"]
categories: ["Web前端"]
slug: "web-architect-nginx1-2-6-tomcat7-memcached1-2-6"
draft: false
lastmod: "2013-01-03T22:25:40Z"
id: 1753983
url: "https://feuyeux.iteye.com/blog/1753983"
---

Load Balancer (Nginx)

          /           \\

      Cluster1      Cluster2

      /     \\        /     \\

  Tomcat1 Tomcat2  Tomcat3 Tomcat4

# memcached

下载：

Ubuntu:

sudo apt-get install memcached

Windows:

[http://www.kuaipan.cn/file/id\_18237169873244609.htm](http://www.kuaipan.cn/file/id_18237169873244609.htm)

 

启动服务器：

memcached -p 11211 -u memcached -m 64 -M -vv

 

 

客户端访问：

telnet localhost 11211

 

   Command Description Example get Reads a value get mykey set Set a key unconditionally set mykey 0 60 5 add Add a new key add newkey 0 60 5 replace Overwrite existing key replace key 0 60 5 append Append data to existing key append key 0 60 15 prepend Prepend data to existing key prepend key 0 60 15 incr Increments numerical key value by given number incr mykey 2 decr Decrements numerical key value by given number decr mykey 5 delete Deletes an existing key delete mykey flush\_all Invalidate specific items immediately flush\_all Invalidate all items in n seconds flush\_all 900 stats Prints general statistics stats Prints memory statistics stats slabs Prints memory statistics stats malloc Print higher level allocation statistics stats items stats detail stats sizes Resets statistics stats reset version Prints server version. version verbosity Increases log level verbosity quit Terminate telnet session quit

 

[http://code.google.com/p/memcached-session-manager/](http://code.google.com/p/memcached-session-manager/)

 

[https://github.com/magro/msm-sample-webapp](https://github.com/magro/msm-sample-webapp)

 

# Nginx

 

```
http {
    server {
        location / {
            root   html;
            index  index.html index.htm;
	    proxy_pass  http://127.0.0.1;
        } 
	upstream 127.0.0.1 {
		server 127.0.0.1:8080 weight=1; 
		server 127.0.0.1:8081 weight=2; 
	} 
}

```

 

# tomcat

server.xml

 

 

D:\\tomcat-7.0.34\_1

port:8005

D:\\tomcat-7.0.34\_2

port:8006

 

```
<Server port="8005" shutdown="SHUTDOWN">
```

 

 

 

D:\\tomcat-7.0.34\_1

port:8080

D:\\tomcat-7.0.34\_2

port:8081

 

```
 <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
```

 

 

D:\\tomcat-7.0.34\_1

port:8009

D:\\tomcat-7.0.34\_2

port:9009

 

```
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
```

 

D:\\tomcat-7.0.34\_1

jvmRoute:tomcat7-1

D:\\tomcat-7.0.34\_2

jvmRoute:tomcat7-2 

 

```
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat7-1">
```

 

D:\\tomcat-7.0.34\_1

port:4000

D:\\tomcat-7.0.34\_2

port:4001

 

```
 <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="8">
	<Manager className="org.apache.catalina.ha.session.DeltaManager" expireSessionsOnShutdown="false" notifyListenersOnReplication="true" />
	<Channel className="org.apache.catalina.tribes.group.GroupChannel">
	   <Membership className="org.apache.catalina.tribes.membership.McastService" address="228.0.0.4" port="45564" frequency="500" dropTime="3000" />
	   <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" address="auto" port="4000" autoBind="100" selectorTimeout="5000" maxThreads="6" />
	   <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
		  <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender" />
	   </Sender>
	   <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector" />
	   <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor" />
	</Channel>
	<Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter="" />
	<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve" />
	<Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer" tempDir="/tmp/war-temp/" deployDir="/tmp/war-deploy/" watchDir="/tmp/war-listen/" watchEnabled="false" />
	<ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener" />
	<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener" />
 </Cluster>
```

 

 

D:\\tomcat-7.0.34\_1

docBase:D:/tomcat-7.0.34\_1/webapps/test

D:\\tomcat-7.0.34\_2

docBase:D:/tomcat-7.0.34\_2/webapps/test

 

```
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Context docBase="D:/tomcat-7.0.34_1/webapps/test" path="" reloadable="true">
	<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"/>
</Context>
</Host>
```

 

D:\\tomcat-7.0.34\_1\\webapps\\test\\index.jsp

 

```
<%@ page contentType="text/html; charset=GBK" %> 
<%@ page import="java.util.*" %> 
<html>
   <head>
      <title>Cluster Test</title>
   </head>
   <body> 
    <% 
    System.out.println(session.getId()); 
    out.println("<br> SESSION ID:" + session.getId()+"<br>");   
    %> 
   </body>
</html>
```

 

D:\\tomcat-7.0.34\_1\\webapps\\test\\WEB-INF\\web.xml

 

```
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>test</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <distributable/>
</web-app>
```