Nginx+tomcat+memcached Cache Sharing Session
There are 4 official recommendations for the session's serialization scheme:
1. Java serialization
2. Msm-kryo-serializer
3. Msm-javolution-serializer
4. Msm-xstream-serializer
I'm using Msm-javolution-serializer here.
Basic architecture
System: CentOS 6.3
Front-end Nginx agent: 192.168.1.211
Backend tomcat+memcached Group: 192.168.1.212,192.168.1.213
One, Nginx installation configuration
1. Create a user who runs Nginx
[[Email protected] ~] #useradd-R nginx
2. Installing Nginx
First install dependent software pcre,openssl-devel, etc.
[[Email protected] ~] #tar-xf pcre-8.37.tar.gz-c/usr/local/#nginx编译需要的是pcre的源码
[[Email protected] ~] #yum-y install Openssl-devel
Compile and install Nginx
[[Email protected] ~] #tar-xf nginx-1.9.9.tar.gz
[[Email protected] ~] #cd nginx-1.9.9
[Email protected] ~]#./configure/configure--prefix=/usr/local/nginx--user=nginx--group=nginx--with-http_stub_ Status_module--with-http_ssl_module--with-http_gzip_static_module--with-http_flv_module--with-pcre=/usr/local/ pcre-8.37
[[Email protected] ~] #make; make install
3, the configuration of Nginx Agent
[[Email protected] ~] #vim/usr/local/nginx/conf/nginx.conf
User Nginx Nginx;
Worker_processes 1;
Events {
Worker_connections 1024;
}
HTTP {
Include Mime.types;
Default_type Application/octet-stream;
Sendfile on;
Keepalive_timeout 65;
server {
Listen 80;
server_name localhost;
Location/{
Proxy_pass Http://tomcat;
Proxy_set_headerHost$host;
Proxy_set_headerx-real-ip$remote _addr;
Proxy_set_headerx-forwarded-for$proxy _add_x_forwarded_for;
}
Error_page 502 503 504/50x.html;
Location =/50x.html {
root HTML;
}
}
Upstream Tomcat {
Server 192.168.1.212:8080;
Server 192.168.1.213:8080;
}
}
4. Start Nginx
[Email protected] ~]#/usr/local/nginx/sbin/nginx
Second, Tomcat installation configuration
1. Installation and configuration of JDK
[Email protected] ~]#./jdk-8u73-linux-x64.rpm
Find JDK Home Directory
[[Email protected] ~] #find/usr-type d-name "jdk*"
Configuring Java (JDK) environment variables
[[Email protected] ~] #vim/etc/profile.d/java.sh
Export java_home=/usr/java/jdk1.8.0_73
Export path= $PATH: $JAVA _home/bin
Reading variables
[Email protected] ~]#. /etc/profile.d/java.sh
Confirm that the JDK is active
[Email protected] ~]# java-version
Java Version "1.8.0_73"
Java (TM) SE Runtime Environment (build 1.8.0_73-b02)
Java HotSpot (TM) 64-bit Server VM (build 25.73-b02, Mixed mode)
2. Tomcat installation Configuration
[[Email protected] ~] #tar-xf apache-tomcat-7.0.68.tar.gz-c/usr/local/
[[Email protected] ~] #cd/usr/local/
[[Email protected] ~] #ln-SV apache-tomcat-7.0.68 Tomcat
Download the MSM library file
Using the Javolution serialization framework, you need to provide the following library files:
Https://memcached-session-manager.googlecode.com/files/memcached-session-manager-1.6.3.jar
Https://memcached-session-manager.googlecode.com/files/memcached-session-manager-tc7-1.6.3.jar
Https://memcached-session-manager.googlecode.com/files/msm-javolution-serializer-1.6.2.jar
Http://memcached-session-manager.googlecode.com/svn/maven/javolution/javolution/5.4.3.1/javolution-5.4.3.1.jar
Https://spymemcached.googlecode.com/files/spymemcached-2.8.4.jar
Put the above 5 library files into/usr/local/tomcat/lib/
Modify Tomcat's configuration file Context.xml to enable Tomcat session sharing
[[Email protected] ~] #vim/usr/local/tomcat/conf/context.xml #将下面配置复制到 <Context> and </Context>, notice the actual modification of IP
<manager classname= "De.javakaffee.web.msm.MemcachedBackupSessionManager"
Memcachednodes= "n1:192.168.1.212:11211,n2:192.168.1.213:11211"
Requesturiignorepattern= ". *\. (PNG|GIF|JPG|CSS|JS) $ "
Sessionbackupasync= "false"
sessionbackuptimeout= "100"
Transcoderfactoryclass= "De.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
Copycollectionsforserialization= "false"
/>
Three, the memcached installation configuration
[[Email protected] ~] #yum-y install memcached
Add a user who runs memcached memcache
[[Email protected] ~] #useradd-R memcache
Open memcached
[[Email protected] ~] #memcached-M 64-d-u memcache-p 11211-l 192.168.1.212-c 1000-p/tmp/memcached.pid
Parameter description
The-D option is to start a daemon process;
-M is the amount of memory allocated to Memcache, in megabytes, I am 64MB;
-U is the user running memcache;
-L is the server IP address of the listener, I specify the server's native IP address here;
-P is set memcache listening port, I set up here 11211, preferably more than 1024 port;
The-c option is the maximum number of concurrent connections to run, the default is 1024, I set the 1000 here, according to the load of your server to set,
-P is set to save memcache PID file, I am here to save in/tmp/memcached.pid
Four, start Tomcat, check the log to confirm whether the Memcachedsessionservice load successfully
[[Email protected] ~] #grep ' Memcachedsessionservice '/usr/local/tomcat/logs/catalina.2016-03-05.log
Info:memcachedsessionservice finished initialization, sticky true, operation timeout, with node IDs [N1] and Failove R node IDs [N2]
See above to complete the initialization of the information, indicating that the normal startup.
Five, 192.168.1.212 and 192.168.1.213 two units are respectively completed the second to fourth steps above.
VI. Testing
212 and 213 respectively edit the index.jsp file for testing
[[Email protected] ~] #vim index.jsp
<%@ page contenttype= "Text/html;charset=utf-8" iselignored= "false"%>
Sessionid:<%=session.getid ()%>
<BR>
Sessionip:<%=request.getservername ()%>
<BR>
Sessionport:<%=request.getserverport ()%>
<%
Out.println ("This is Tomcat Server 212");
%>
Empty/usr/local/tomcat/webapps/root Directory contents (note Backup first), put index.jsp into this directory
Browser Access 192.168.1.211
Appears as follows:
Sessionid:37aa312466ccbd3374f5c0842b2d3a0f-n1
sessionip:192.168.1.211
Sessionport:80 This is Tomcat Server 213
Continuous refresh access, if the SessionID is not changed, indicates a successful configuration.
Nginx+tomcat+memcached Cache Sharing Session