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; } }
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
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)
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"/>
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"%>Tomcat集群测试 <% out.print("["+request.getLocalAddr()+":" +request.getLocalPort()+"]" + "
session id:" + session.getId()); %>
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.