Using Nginx to do load balancing memcached session sharing
Environment Windows 7 X64
Java:jdk-7windows-x64.rar
Nginx:http://nginx.org/en/download.html, here we recommend download stable version (stable versions), this article uses nginx-1.8.0
tomcat:apache-tomcat-7.0.63
Configure multiple Tomcat on the same computer (this time with two Tomcat to demonstrate), modify the three configurations in the Conf/server.xml
Install memcached unzip Open, open cmd into memcached extracted to the path D:\memcached
Memcached.exe–d Install enter the Windows service
Input: Memcached.exe–p 11211–d Start enter memcached service-P indicates port
Unzip nginx-1.8.0 to D Drive
Modify the Conf/nginx.conf file
#user nobody;worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid;events {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; #tcp_nopush on; #keepalive_timeout 0; Keepalive_timeout 65; #gzip on; #设定负载均衡的服务器列表 upstream 127.0.0.1 {#weigth参数表示权值, the higher the weight, the greater the probability of being assigned to the server 127.0.0. 1:8080 weight=1; Server 127.0.0.1:8081 weight=1; } server {Listen 80; server_name localhost; #charset Koi8-r; #access_log Logs/host.access.log Main; Location/{root HTML; Index index.html index.htm; Proxy_pass http://127.0.0.1; } #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; } # Proxy The PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ {# ProX Y_pass http://127.0.0.1; #} # Pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root HTML; # Fastcgi_pass 127.0.0.1:9000; # Fastcgi_index index.php; # Fastcgi_param Script_filename/scripts$fastcgi_script_name; # include Fastcgi_params; #} # Deny access to. htaccess files, if Apache ' s document Root # concurs with Nginx ' s one # #l Ocation ~/\.ht { # Deny All; #}} # Another virtual host using mix of ip-, name-, and port-based configuration # #server {# listen 8000; # Listen somename:8080; # server_name somename alias Another.alias; # location/{# root HTML; # index index.html index.htm; #} #} # HTTPS Server # #server {# listen 443 SSL; # server_name localhost; # ssl_certificate Cert.pem; # Ssl_certificate_key Cert.key; # Ssl_session_cache shared:ssl:1m; # ssl_session_timeout 5m; # ssl_ciphers high:!anull:! MD5; # ssl_prefer_server_ciphers on; # location/{# root HTML; # index index.html index.htm; # } #}}
Session sharing
Jar Packages added under TOMCAT7:
Installation method: Put these bags into two tomcat7 Lib
PS: Use Memcache to save Tomcat session
Modify the Conf/context.xml under two Tomcat
<?xml version= ' 1.0 ' encoding= ' utf-8 '?><!--Licensed to the Apache software Foundation (ASF) under one or more C Ontributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); You are not a use of this file except in compliance with the License. Obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 unless required by applicable or agreed to writing, software distributed under the License are distributed on a "as is" BASIS, without warranties or CONDITIONS of any KIND, either express or implied. See the License for the specific language governing permissions and limitations under the license.--><!--the Conte NTS of this file is loaded for each Web application--><context> <!--Default set of monitored Resourc ES--<watchedresource>web-inf/web.xml</watchedresource> <!--Uncomment this to disable session persistence across Tomcat restarts--> ; <!--<manager pathname= ""/>--><manager classname= "De.javakaffee.web.msm.MemcachedBackupSessionMana Ger "memcachednodes=" n1:127.0.1.1:11211 "requesturiignorepattern=". *\. (PNG|GIF|JPG|CSS|JS) $ "sessionbackupasync=" false "sessionbackuptimeout=" Transcoderf Actoryclass= "De.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory" Copycollectionsforserialization= "false"/> <!--uncomment this to enable Comet connection tacking (provides even TS on session expiration as well as WebApp lifecycle)-<!--<valve Classname= "Org.apache.catalin A.valves.cometconnectionmanagervalve "/>--></context>
Because support memcached distributed if more than one memcached in memcachednodes= "Nx:IP:port" can be
Note: The port number here is the same as that of the boot setting
Open cmd into nginx unzip directory input nginx start restart Nginx start two tomcat in turn
Then add a e:\javanv\apache-tomcat-71\webapps\root,e:\javanv\apache-tomcat-72\webapps\root under the
session.jsp
<%@ page contenttype= "text/html;charset=utf-8"%> <%@ page import= "java.util.*"%>