The Tomcat cluster session synchronization scheme is available in several ways:
- With the cluster method of Tomcat, it is easy to configure the session information automatically in real-time with multiple tomcat. However, this scheme is inefficient and does not perform well in large concurrency. Principle: http://zyycaesar.iteye.com/blog/296606
- Using Nginx's IP-based hash routing strategy, it is easier to ensure that IP access is always routed to the same tomcat. But if the application is a large number of users of a local area network logged in at the same time, so load balancing does not work.
- Using Nginx plugin to achieve Tomcat cluster and session synchronization, Nginx-upstream-jvm-route-0.1.tar.gz, is an nginx extension module, used to implement the function of Cookie-based session Sticky, can be obtained through http://code.google.com/p/nginx-upstream-jvm-route/downloads/list.
- Using memcached to centralize the session of multiple tomcat, the front-end is separated by the Nginx load balancing and dynamic static resources, which can guarantee the high performance while the system level is extended.
The following fourth scenario uses the cluster environment:
1. Nginx Latest Version: 1.5.7
2. Tomcat version: 6.0.37
3. memcached Latest Version: 1.4.15
4. Session replication synchronization using Memcache-session-manager Latest version: 1.6.5
5. System: CentOS6.3
First, Nginx installation
-centos6.3 does not install gcc-c++ by default and GCC is installed first:
Yum-y Install gcc-c++
Remember to reboot the system when you finish loading.
-CD to installation directory
Cd/usr/lcoal/src
-Install Pcre Library
Cd/usr/local/srcwget FTP://FTP.CSX.CAM.AC.UK/PUB/SOFTWARE/PROGRAMMING/PCRE/PCRE-8.21.TAR.GZTAR-ZXVF PCRE-8.21.TAR.GZCD Pcre-8.21./configuremakemake Install
If wget download, go to the official website to download pcre-8.12.tar.gz package copy to SRC.
-Install zlib Library
CD/USR/LOCAL/SRC wget http://zlib.net/zlib-1.2.8.tar.gztar-zxvf zlib-1.2.8.tar.gzcd zlib-1.2.8./configuremakemake Install
-Install SSL
Cd/usr/local/srcwget HTTP://WWW.OPENSSL.ORG/SOURCE/OPENSSL-1.0.1C.TAR.GZTAR-ZXVF openssl-1.0.1c.tar.gz
-Installation of Nginx
CD nginx-1.5.7./configure--prefix=/usr/local/nginx/nginx--with-http_ssl_module--with-pcre=/usr/local/src/ pcre-8.12--with-zlib=/usr/local/src/zlib-1.2.8--with-openssl=/usr/local/src/openssl-1.0.1c Makemake Install
Installation successful, cd/usr/local/nginx/conf/nginx.conf, modify configuration file:
HTTP {... Upstream localhost { server localhost:8081; Server localhost:8082; Server localhost:8083; } ...} Location/{ root html; Index index.html index.htm; Proxy_pass http://localhost; Proxy_redirect off; Proxy_set_header Host $host; Proxy_set_header x-real-ip $remote _addr; Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for; Client_max_body_size 10m; Client_body_buffer_size 128k; Proxy_connect_timeout ; Proxy_send_timeout ; Proxy_read_timeout ; Proxy_buffer_size 4k; Proxy_buffers 4 32k; Proxy_busy_buffers_size 64k; Proxy_temp_file_write_size 64k; }
Second, memcached installation
-memcached installation is simple, you need to libevent the library first:
Install libevent libevent-develwget http://www.danga.com/memcached/dist/memcached-1.4.15.tar.gz tar zxf memcached-1.4. tar. GZCD memcached-1.4. ./Configure makeinstall
Installation succeeded (installed by default under bin), boot:
#-VV Console output #-d background run/USR/LOCAL/BIN/MEMCACHED-VV
After startup, you can telnet to look at the status:
127.0.11211stats
Three, Tomcat configuration
1. Modify Server.xml
<EngineName= "Catalina"Defaulthost= "localhost"Jvmroute= "JVM1">...<ContextPath=""DocBase= "/demo/appserver/app/cluster"Debug= "0"Reloadable= "true"Crosscontext= "true"><Manager
ClassName= "De.javakaffee.web.msm.MemcachedBackupSessionManager"memcachednodes= "n1:192.168.2.43:11211" requesturiignorepattern= ". *\." ( png|gif|jpg|css|js|ico|jpeg|htm|html) $ " Sessionbackupasync=" false " sessionbackuptimeout=" 1800000 " copycollectionsforserialization=" false " transcoderfactoryclass=" De.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory " /></Context>
Nginx+tomcat+memcached cluster