<%@ page language= "java" contenttype= "text/html; Charset=utf-8 " pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
Step two: Start the Memcached service
#/opt/bin/memcached-d-M 5-u root-l 192.168.74.129-p 12000-c 256-p/tmp/memcached.pid ,:
The third step:Nginx as a proxy server, customer service side of the request server side, the use of load balancing to deal with, so that the average customer service request to distribute to every day server, so as to reduce the server-side pressure. Realize static and dynamic separation, configure the nginx.conf file under Nginx .
#vi/usr/local/nginx/conf/nginx.conf
<span style= "color: #333333;" > #user nobody;worker_processes 1;error_log logs/error.log;pid logs/nginx.pid;events {use epoll; Worker_connections 1024;} HTTP {include mime.types; Default_type Application/octet-stream; Log_format Main ' $remote _addr-$remote _user [$time _local] "$request" "$status $body _bytes_sent" $ Http_referer "" "$http _user_agent" "$http _x_forwarded_for"; Access_log Logs/access.log Main; Sendfile on;keepalive_timeout 65;gzip on; Gzip_min_length 1k; Gzip_buffers 4 16k; Gzip_http_version 1.0; Gzip_comp_level 2; Gzip_types text/plain application/x-javascript text/css application/xml; Gzip_vary on; </span><span style= "color: #ff0000;" >upstream Localhost_server {ip_hash; Server 192.168.74.129:8081; Server 192.168.74.129:8082; }</span><span style= "color: #333333;" > server {Listen default; SeRver_name localhost; </span><span style= "color: #ff0000;" > Location ~. *\. (HTML|HTM|GIF|JPG|JPEG|BMP|PNG|ICO|TXT|JS|CSS) $//Nginx handle static page </span><span style= "color: #333333;" > {root/usr/tomcat/apache-tomcat-8081/webapps/root; Expires 30d; Cache to client 30 days} Error_page 404/404.html; #redirect Server error pages to the static page/50x.html Error_page 502 503 504/50x.html; Location =/50x.html {root html; } </span><span style= "color: #ff6666;" > Location ~ \. (Jsp|do) $ {//All JSP dynamic requests are given to Tomcat for processing </span><span style= "color: #333333;" > </span><span style= "color: #c0c0c0;" >proxy_pass Http://localhost_server; A request from a JSP or do suffix is given to Tomcat for processing </span><span style= "color: #333333;" > Proxy_redirect off; Proxy_set_header Host $host; The backend Web server can beOver x-forwarded-for obtain the user real IP proxy_set_header x-real-ip $remote _addr; Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for; Client_max_body_size 10m; The maximum number of single-file bytes allowed to be requested by the client client_body_buffer_size 128k; Buffer proxy buffers The maximum number of bytes requested by the client Proxy_connect_timeout 90; Nginx and Backend server connection time-out proxy_read_timeout 90; After the connection is successful, the back-end server response time Proxy_buffer_size 4k; Set the proxy server (Nginx) to save the user header information buffer size Proxy_buffers 6 32k; Proxy_buffers buffer, Web page average below 32k, so set proxy_busy_buffers_size 64k;//under high load buffer size (proxy_buffers*2) proxy_te Mp_file_write_size 64k; Set the cache folder size, greater than this value, will be passed from the upstream server}}}</span>
Fourth step: Modify the conf/context.xml under apache-tomcat-8081 and apache-tomcat-8082 files, modifying Session storage mode.
Add in the <Context></Context> tab
<manager classname= "De.javakaffee.web.msm.MemcachedBackupSessionManager" memcachednodes= "<span style=" color : #ff0000; " >n1:192.168.74.129:12000</span> "sessionbackupasync=" false "sessionbackuptimeout=" 100 " Transcoderfactoryclass= "De.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory" Copycollectionsforserialization= "false" requesturiignorepattern= ". *\. (PNG|GIF|JPG|CSS|JS) $ "/>
Description
1.requestUriIgnorePatter: Filter images and other static files to trigger Session Backup to Memcached.
2.sessionBackupAsync: Specifies whether the Session should be asynchronously saved to Memcached .
3.backupThreadCount: The number of threads used to asynchronously save the Session.
4.sessionBackupTimeout : The default is up to a millisecond, and the save fails after the operation has elapsed.
Fifth step: To introduce the jar package required by Memcached-session-manager
If it is not introduced under Tomcat/lib, the corresponding class will not be found:
The jar required
Memcached-session-manager-1.6.3.jar
Javolution-5.5.1.jar
Spymemcached-2.7.1.jar
Memcached-session-manager-tc6-1.6.3.jar
Msm-javolution-serializer-1.6.3.jar
Msm-xstream-serializer-1.6.3.jar
Download the required jar here
1. It is important to note that when we introduced this Memcached-2.6.jar package, instead of Spymemcached-2.7.1.jar , we reported this error:
2.memcached-session-manager-tc6-1.6.3.jar This package is for tomcat6
Sixth step: Start Nginx,Tomcat service
1. start Tomcat
#./bin/startup.sh;tail-f./logs/catalina.out, this way you can see how to modify the save session and see if the configuration is correct :
2. start Nginx
#cd/usr/local/nginx
#sbin/nginx
Seventh step: Visit index.jsp
1. Let's try not to go through the Nginx Reverse proxy, we directly access Tomcat to access the JSP, and test that we write the JSP is correct, And SessionID can display normally,:
2. we visited Nginx Reverse proxy, we deployed apache-tomcat-8081 and apache-tomcat-8082 Two clusters, we see if we can share a session, We print out the page, to see sessionId is the same?
The request address is 192.168.74.129/index.jsp, we brush several times, the port of the page will change, we look at call apache-tomcat-8081 index.jsp and call The SessionID of apache-tomcat-8082 's index.jsp will change, :
apache-tomcat-8081 's index.jsp and shows SessionID
apache-tomcat-8082 's index.jsp and shows SessionID
At this point, it is found that the index.jsp calling apache-tomcat-8081 is identical to the SessionID of the index.jsp that calls apache-tomcat-8082, indicating that the same session is shared.
Nginx+tomcat+memcached session sharing when implementing cluster deployment