Index. jsp of Apache-tomcat-8082
<%@ 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 2: 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 ,:
Step 3: Nginx acts as a proxy server. When the client requests the server, it uses Server Load balancer to process the requests, so that the server can evenly distribute the requests to the server every day, this reduces the pressure on the server. Implement Dynamic and Static separation and 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 $ response" $ 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 1 k; gzip_buffers 4 16 k; 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 {lis Ten 80 default; server_name localhost; </span> <span style = "color: # ff0000;"> location ~. *\. (Html | htm | gif | jpg | jpeg | bmp | png | ico | txt | js | css) $ // process static pages by nginx </span> <span style = "color: #333333; "> {root/usr/tomcat/apache-tomcat-8081/webapps/ROOT; expires 30d; // cache to the client for 30 days} error_page 404/404 .html; # redirect server error pages to the static page/50x.html error_page 500 502 503 504/50 x.html; location =/50x.html {root html ;}</span> <span style = "color: # ff6666; "> location ~ \. (Jsp | do) $ {// All jsp dynamic requests are sent to Tomcat for processing </span> <span style = "color: #333333; "> </span> <span style =" color: # c0c0c0; "> proxy_pass http: // localhost_server; // send a request with the Suffix from jsp or do to tomcat for processing </span> <span style = "color: #333333;"> proxy_redirect off; proxy_set_header Host $ host; // The backend Web server can use X-Forwarded-For to obtain the user's Real IP proxy_set_header X-Real-IP $ remote_addr; proxy_set_header X-Forwarded-For $ scheme; client_max_body_size 10 m; // maximum number of single-file bytes allowed for client requests client_body_buffer_size 128 k; // maximum number of bytes cached by the buffer proxy for client requests proxy_connect_timeout 90; // nginx and backend server connection timeout time: proxy_read_timeout 90; // after the connection is successful, the backend server response time is proxy_buffer_size 4 k; // set Proxy Server (nginx) the buffer size proxy_buffers 6 32 k that stores user header information; // The proxy_buffers buffer. If the average webpage size is below 32 k, set proxy_busy_buffers_size 64 k; // buffer size under high load (proxy_buffers * 2) proxy_temp_file_write_size 64 k; // sets the cache folder size. If it is greater than this value, it will be uploaded from the upstream server }}</span>
Step 4: Modify the conf/context. xml file under Apache-tomcat-8081 and Apache-tomcat-8082, and modify the session storage mode.
Add the <Context> </Context> label
<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)$"/>
Note:
1. requestUriIgnorePatter: filters static files such as images to trigger Session backup to Memcached.
2. sessionBackupAsync: Specifies whether the Session should be asynchronously saved to Memcached.
3. backupThreadCount: Number of threads used to asynchronously Save the Session.
4. sessionBackupTimeout: The default value is 100 milliseconds. If the operation exceeds the time limit, saving fails.
Step 5: Introduce the jar package required by memcached-session-manager.
If it is not introduced in tomcat/lib, the corresponding class cannot be found ,:
Required jar
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
1. It should be noted that we introduced this memcached-2.6.jar package at that time, instead of reporting this error when it was reflected into the spymemcached-2.7.1.jar ,:
2. memcached-session-manager-tc6-1.6.3.jar this bag hour hand to tomcat6
Step 6: Start the Nginx and Tomcat services
1. Start Tomcat
#./Bin/startup. sh; tail-f./logs/catalina. out. You can modify the method of saving the session and check whether the configuration is correct ,:
2. Start Nginx
# Cd/usr/local/nginx
# Sbin/nginx
Step 7: access index. jsp
1. First, let's try to see if tomcat can access jsp directly without Nginx reverse proxy, and test whether the JSP we wrote is correct, and the sessionId can be properly displayed ,:
2. we visited the Nginx reverse proxy. We deployed two clusters, Apache-tomcat-8081 and Apache-tomcat-8082. Let's check whether a session can be shared. We printed them on the page, check whether sessionId is the same?
The request addresses are 192.168.74.129/index. jsp, we refresh the page several times, and the page port will change. Let's check the index that calls Apache-tomcat-8081. jsp and the index that calls Apache-tomcat-8082. will the sessionId of jsp change ,:
Index. jsp of Apache-tomcat-8081 and display SessionId
Index. jsp of Apache-tomcat-8082 and display SessionId
The index. jsp that calls Apache-tomcat-8081 is the same as the SessionId of index. jsp that calls Apache-tomcat-8082.