Nginx + Tomcat + Memcached cluster

WBOY
Release: 2016-07-30 13:30:56
Original
996 people have browsed it


Nginx + Tomcat + Memcached cluster must consider at least 2 aspects:

1. How to achieve load balancing?

2. How to implement session replication and synchronization?

This article collects relevant materials from the Internet and summarizes the process of building an Nginx + Tomcat + Memcached cluster under window as follows.

1. Development environment

1. nginx version: 1.8.0

2. tomcat version: 7.0.55

3. memcached version: 1.4.13

4. Session replication synchronization uses memcache-session- The latest version of manager: 1.6.3

5. Operating system: Win7


2. Nginx + Tomcat to achieve load balancing

1. Download and install nginx and tomcat

I won’t go into details here. For details, see nginx and Download and install tomcat

2. nginx configuration

Open the main configuration file of nginx: D:nginx-1.8.0confnginx.conf, mainly modify the proxy forwarding server IP and port, weight is the server weight, the higher the number, the higher the number means it is accessed The greater the chance:

	upstream local_tomcat {
	   server localhost:18080 weight=1; 
           server localhost:18081 weight=1; 
	} 
	
    server {
        listen       80;

        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://local_tomcat; 
        }
    }
Copy after login

3. Tomcat + memcached implements session replication and synchronization

Tomcat session replication and synchronization problems are usually solved by using memcached or nginx_upstream_jvm_route, which is an Nginx extension module used to implement Cookie-based Session Sticky functionality. If there are too many tomcats, session synchronization is not recommended. Synchronizing sessions between servers consumes resources, and high concurrency environments can easily cause session storms. Please adopt the session solution appropriately according to your application situation.

The following introduces the use of memcached (memcache-session-manager) to manage sessions

1. Download and install memcached (omitted)

2. Download related jar packages

There are many jar packages that need to be downloaded, each jar The version cannot be mistaken. I have provided a complete set of jar packages that need to be downloaded (already tested and available). If you need to download, please click Link Download.

3. Configure session sharing

Okay, put the above jar package in the tomcat/lib directory, modify each tomcat configuration file tomcatconfcontext.xml, between the ... tag Add the following code:

a. Configure tomcat

Modify tomcatconfserver.xml. Since I have two tomcats downloaded from the local win7 system, I want to avoid port conflicts. Taking tomcat-node1 as an example, the main modifications are as follows: (Node2 and so on)

<Server port="8005" shutdown="SHUTDOWN">
Copy after login

    <Connector port="18080" protocol="HTTP/1.1"
               c
               redirectPort="8443" />
Copy after login

    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
Copy after login

    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
Copy after login
Modify tomcatconfcontext.xml and add the following code between the ... tags:
	<Manager  
		className="de.javakaffee.web.msm.MemcachedBackupSessionManager"  
		memcachedNodes="<span style="color:#FF0000;"><strong>n1:127.0.0.1:11211</strong></span>"  
		sticky="false"  
		sessi  
		lockingMode="uriPattern:/path1|/path2"  
		requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"  
		transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"/>
Copy after login
(note that memecachedNodes in the two tomcat configurations are all n1:* )

b. Modify the application jsp

I am using the examples project that comes with tomcat, and directly replace the following jsp with D:nginx-1.8.0Tomcattomcat-node1webappsexamplesindex.html

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.text.SimpleDateFormat"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Tomcat集群测试</title>
  </head>
  <body>
    <%
		out.print("["+request.getLocalAddr()+":" +request.getLocalPort()+"]" + "<br/>session id:" + session.getId()); 
    %>
  </body>
</html>
Copy after login

Four. Test

Start nginx, memcached and Two tomcats, use the browser to access http://localhost/examples/, compare the two pages returned by the nginx server, and find that the two ports are different but the sessions are the same, which means that the Nginx + Tomcat + Memcached cluster is successfully established.


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 Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

The above introduces the Nginx + Tomcat + Memcached cluster, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template