Theoretical Basis:
User-> Web-> nginx-> worker AT1-> *. jsp
80 8080
Middleware-> Tomcat 2
HTML-> tomcat3
When a user accesses a webpage through the Web, port 80 is accessed by default, that is, nginx access. Nginx only supports static Web pages. If a user accesses a dynamic web page similar to *. jsp, it must be processed through the Application Server Tomcat first. Tomcat is equivalent to a media. After processing the dynamic web page, it is handed over to nginx and finally returned to the user.
Nginx backend Tomcat can achieve load balancing, but in order to prevent users from accessing the process similar to the following situations:
When you access the logon page, after entering the account and password, click Next and a logon page will pop up immediately.
Add nginxStickyModuleSo that requests from the same user are always sent to the same Tomcat for processing.
192.168.0.1-> 255.at1
192.168.0.2-> tomcat2Load Balancing by IP
To prevent a user from accessing the registration page, when the user enters half of the registration information, the server goes down. After the user presses "Next", all the information previously submitted by the user disappears, memcached must be used together to solve this problem.
Nginx
|
T1 T2
| \/|
| \/|
|/\ |
|/\ |
M1 m2
Cross-type storage perfectly solves single point of failure problems
Machine 1 has two services T1 and M1.
Machine 2 has two services: T2 and M2.
By default, T1 data is stored in m2, and T2 data is stored in M1 to prevent spof.
When the T1 service fails, it is taken over by T2. T2 obtains the data on M2 existing in T1 from m2, and then submits the data to M2.(Note: if another request is enabled and taken over by T2, data is stored on M1 by default, instead of M2);
Then, if m2 is connected, but T2 works normally, T2 will take out all the data on M2 and store it in M1, so that the data submitted by the user still exists, no loss.
When the first part is down, data on M1 on T2 will be lost.
Configuration:
System Environment: Rhel6 x86_64 iptables and SELinux disabled
Host: N1: 192.168.122.185: nginx Tomcat memcached server85.example.com
N2: 192.168.122.117: Tomcat memcached server17.example.com
Related URLs: Rpm.pbone.net tomcat.apache.org
Required packages:Memcache-2.2.5.tgz memcached-1.4.15.tar.gz libevent-devel-1.4.13-4.el6.x86_64.rpm libevent-doc-1.4.13-4.el6.noarch.rpm libevent-headers-1.4.13-4.el6.noarch.rpm webbench-1.5.tar.gzApache-tomcat-7.0.37.tar.gzNginx-sticky-module-1.0.tar.gz asm-3.2.jar kryo-1.04.jar kryo-serializers-0.10.jar memcached-session-manager-1.6.3.jar memcached-session-manager-tc7-1.6.3.jar minlog-1.2.jar msm-kryo-serializer-1.6.3.jar reflectasm-1.01.jar
1.InstallMemcached
[[Email protected] kernel] # tar zxf memcache-2.2.5.tgz
[[Email protected] kernel] # cd memcache-2.2.5
[[Email protected] memcache-2.2.5] # phpize
[[Email protected] memcache-2.2.5] #./configure
[[Email protected] memcache-2.2.5] # Make & make install
[[Email protected] memcache-2.2.5] # Vim/usr/local/lnmp/PHP/etc/PHP. ini
Extension = memcache. So
[[Email protected] memcache-2.2.5] #/etc/init. d/PHP-FPM reload
[[Email protected] kernel] # tar zxf memcached-1.4.15.tar.gz
[[Email protected] kernel] # cd memcached-1.4.15
[[Email protected] memcached-1.4.15] #./configure
The following error occurs:
Checking for libevent directory... configure: Error: libevent is required. You can get it fromhttp: // www.monkey.org /~ Provos/libevent/
The solution is as follows::
[[Email protected] memcached-1.4.15] # Yum install libevent
[[Email protected] kernel] # rpm-IVH libevent -*
Note: libevent-* contains: libevent-devel-1.4.13-4.el6.x86_64.rpm libevent-doc-1.4.13-4.el6.noarch.rpm libevent-headers-1.4.13-4.el6.noarch.rpm
[[Email protected] memcached-1.4.15] # Make & make install
[[Email protected] ~] # Memcached-M 50 m-D-u root (if no size is specified, the default value is 64 m, and the maximum value of each time cannot exceed 2g)
[[Email protected] ~] # Ps ax
6106? SSL 0: 00 memcached-M 50 m-D-u Root
[[Email protected] ~] # Netstat-antple
TCP 0 0 0.0.0.0: 11211 0.0.0.0: * Listen 0 12791 6106/memcached
The above content indicates that the installation is successful.
[[Email protected] kernel] # cd memcache-2.2.5/
[[Email protected] memcache-2.2.5] # cp memcache. php/usr/local/lnmp/nginx/html/
[[Email protected] memcache-2.2.5] # Vim/usr/local/lnmp/nginx/html/memcache. php
Define ('admin _ password', 'westos '); // admin password
# $ Memcache_servers [] = 'mymemcache-server1: 8080 ';
$ Memcache_servers [] = '192. 168.122.185: 661 ';
[[Email protected] memcache-2.2.5] # nginx-s reload
Access server85.example.com/memcache.php:
Perform stress testing:
[Root @ server85 kernel] # tar zxf webbench-1.5.tar.gz
[Root @ server85 kernel] # cd webbench-1.5
[Root @ server85 webbench-1.5] # Make
The following error occurs:
/Bin/sh: ctags: Command not found
The solution is as follows::
[Root @ server85 webbench-1.5] # Yum install ctags-y
[Root @ server85 webbench-1.5] # Make clean
[Root @ server85 webbench-1.5] # make install
[Root @ server85 ~] # Websockets-C 500-T 10 http: // 192.168.122.185/memcache. php
Webbench-simple web benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL open source software.
Benchmarking: Get http://server85.example.com/memcache.php
500 clients, running 10 sec.
Speed = 20442 pages/min, 102477 Bytes/sec.
Requests: 3407 susceed, 0 failed.
Or access server85.example.com/status:
2.InstallTomcat
[Root @ server85 kernel] # chmod + x jdk-6u32-linux-x64.bin
[Root @ server85 kernel] #./jdk-6u32-linux-x64.bin
[Root @ server85 kernel] # mv jdk1.6.0 _ 32 // usr/local/JDK
[Root @ server85 kernel] # Vim/etc/profile
Export java_home =/usr/local/JDK
Export classpath =: $ java_home/lib
Export Path = $ path: $ java_home/bin
[Root @ server85 kernel] # source/etc/profile
Run echo $ path to check whether/usr/local/JDK/bin has taken effect.
[Root @ server85 ~] # Vim test. Java
Public class test {
Public static void main (string [] agre ){
System. Out. println ("Hello world! ");
}
}
[Root @ server85 ~] # Javac test. Java
[Root @ server85 ~] # Java Test
Print Hello world! The installation is successful!
[Root @ server85 kernel] # tar zxf apache-tomcat-7.0.37.tar.gz-C/usr/local/
[Root @ server85 kernel] # cd/usr/local/
[Root @ server85 local] # mv APACHE-Tomcat-7.0.37/tomcat
[Root @ server85 local] # cd tomcat/bin/
[Root @ server85 bin] #./startup. Sh
[Root @ server85 bin] # netstat-antple
TCP 0 0: 8080: * Listen 0 57982 7465/Java
Port 8080 is successfully started.
When you access server85.example.com: 8080, the following page appears, indicating that Tomcat is normal.
[Root @ server85 bin] # cd/usr/local/tomcat/webapps/root/
[Root @ server85 root] # Vim test. jsp
The time is: <% = new java. util. Date () %>
Access server85.example.com: 8080/test. jsp. The following page appears as normal.
[Root @ server85 root] # Vim/usr/local/lnmp/nginx/CONF/nginx. conf
Location ~ \. Jsp $ {
Proxy_pass http: // 127.0.0.1: 8080;
} (Port 8080 is used to access *. jsp)
[Root @ server85 root] # nginx-s reload
Follow these steps:Server85Implementation:
[Root @ server85 kernel] # tar zxf nginx-sticky-module-1.0.tar.gz
[Root @ server85 kernel] # tar zxf nginx-1.2.7.tar.gz
[Root @ server85 kernel] # cd nginx-1.2.7
[Root @ server85 nginx-1.2.7] # Vim src/CORE/nginx. h
# Define nginx_ver "nginx"
[Root @ server85 nginx-1.2.7] # Vim Auto/CC/GCC
# Debug
# Cflags = "$ cflags-G"
[Root @ server85 nginx-1.2.7] #./configure -- prefix =/usr/local/lnmp/nginx/-- with-http_ssl_module -- with-http_stub_status_module-add-module =/root/kernel/nginx-sticky-module-1.0
[Root @ server85 nginx-1.2.7] # Make & make install
[Root @ server85 nginx-1.2.7] # Vim/usr/local/lnmp/nginx/CONF/nginx. conf
HTTP {
Upstream Koen {
Sticky;
Server 192.168.122.185: 8080;
Server 192.168.122.117: 8080;
}
Server {
Listen 80;
SERVER_NAME server85.example.com;
Location ~ \. Jsp $ {
Proxy_pass http: // Koen;
}
}
}
[Root @ server85 nginx-1.2.7] # nginx-s stop
[Root @ server85 nginx-1.2.7] # nginx
[Root @ server85 nginx-1.2.7] # memcached-D-u Root
[Root @ server85 nginx-1.2.7] # cd/usr/local/tomcat/webapps/root/
[Root @ server85 root] # Vim test. jsp
<% @ Page contenttype = "text/html; charset = GBK" %>
<% @ Page import = "Java. util. *" %>
<HTML>
<Body>
Server info:
<%
Out. println (request. getlocaladdr () + ":" + request. getlocalport () + "<br>"); %>
<%
Out. println ("<br> ID" + session. GETID () + "<br> ");
String dataname = request. getparameter ("dataname ");
If (dataname! = NULL & dataname. Length ()> 0 ){
String datavalue = request. getparameter ("datavalue ");
Session. setattribute (dataname, datavalue );
}
Out. Print ("<B> session List </B> ");
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 = "test. jsp" method = "Post">
Name: <input type = text size = 20 name = "dataname">
<Br>
Key: <input type = text size = 20 name = "datavalue">
<Br>
<Input type = submit>
</Form>
</Body>
</Html>
[Root @ server85 root] # cd/usr/local/tomcat/lib/
Place the following software packages in this directory:
Asm-3.2.jar
Kryo-1.04.jar
Kryo-serializers-0.10.jar
Memcached-session-manager-1.6.3.jar
Memcached-session-manager-tc7-1.6.3.jar
Minlog-1.2.jar
Msm-kryo-serializer-1.6.3.jar
Reflectasm-1.01.jar
Spymemcached-2.7.3.jar
[Root @ server85 lib] # Vim/usr/local/tomcat/CONF/context. xml
<Context>
<Manager classname = "De. javakaffee. Web. MSM. memcachedbackupsessionmanager"
Memcachednodes = "N1: 192.168.122.185: 11211, N2: 192.168.122.117: 11211"
Failovernodes = "N1"
Requesturiignorepattern = ". * \. (ICO | PNG | GIF | JPG | CSS | JS) $" transcoderfactoryclass = "De. javakaffee. Web. MSM. serializer. kryo. kryotranscoderfactory"
/>
</Context>
[Root @ server85 lib] #/usr/local/tomcat/bin/shutdown. Sh
[Root @ server85 lib] #/usr/local/tomcat/bin/startup. Sh
Follow these steps:Server17Implementation:
[Root @ server17 ~] # Memcached-D-u Root
[Root @ server17 ~] # Cd/usr/local/tomcat/webapps/root/
[Root @ server17 root] # Vim test. jsp
<% @ Page contenttype = "text/html; charset = GBK" %>
<% @ Page import = "Java. util. *" %>
<HTML>
<Body>
Server info:
<%
Out. println (request. getlocaladdr () + ":" + request. getlocalport () + "<br>"); %>
<%
Out. println ("<br> ID" + session. GETID () + "<br> ");
String dataname = request. getparameter ("dataname ");
If (dataname! = NULL & dataname. Length ()> 0 ){
String datavalue = request. getparameter ("datavalue ");
Session. setattribute (dataname, datavalue );
}
Out. Print ("<B> session List </B> ");
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 = "test. jsp" method = "Post">
Name: <input type = text size = 20 name = "dataname">
<Br>
Key: <input type = text size = 20 name = "datavalue">
<Br>
<Input type = submit>
</Form>
</Body>
</Html>
[Root @ server17 root] # cd/usr/local/tomcat/lib/
Place the following software packages in this directory:
Asm-3.2.jar
Kryo-1.04.jar
Kryo-serializers-0.10.jar
Memcached-session-manager-1.6.3.jar
Memcached-session-manager-tc7-1.6.3.jar
Minlog-1.2.jar
Msm-kryo-serializer-1.6.3.jar
Reflectasm-1.01.jar
Spymemcached-2.7.3.jar
[Root @ server17 lib] # Vim/usr/local/tomcat/CONF/context. xml
<Context>
<Manager classname = "De. javakaffee. Web. MSM. memcachedbackupsessionmanager"
Memcachednodes = "N1: 192.168.122.185: 11211, N2: 192.168.122.117: 11211"
Failovernodes = "N2"
Requesturiignorepattern = ". * \. (ICO | PNG | GIF | JPG | CSS | JS) $" transcoderfactoryclass = "De. javakaffee. Web. MSM. serializer. kryo. kryotranscoderfactory"
/>
</Context>
[Root @ server17 bin] #/usr/local/tomcat/bin/shutdown. Sh
[Root @ server17 bin] #/usr/local/tomcat/bin/startup. Sh
Test:
1.Store Data:
2.SetServer85OnM1Go down and saveUser3
3.ResetServer85OnM1, SaveUser4
4.SetServer17OnM2Down, saveUser5
5.ResetServer17OnM2,StorageUser6
6.SetServer17OnTomcatClose, saveUser7
Test Complete !!!
Rhel6-tomcat + nginx + memcached Configuration documentation