Nginx+tomcat+memcache+session binding

Source: Internet
Author: User

Node information

Node 1 ip:172.16.30.1 nginx

Node 101 ip:172.16.30.101 Tomcat

Node 102 ip:172.16.30.102 Tomcat

650) this.width=650; "Src=" http://linuxu.qiniudn.com/wp-content/uploads/2015/02/ Wpid-fa15bef10782a1dbf0f85f29daa70511_104637390.png "style=" font-size:10.5pt;line-height:1.5; "border=" 0 "/>

Access back-end tomcat via the Nginx Proxy and implement memcache session binding, which is the same as the one accessed by the same browser.

1, Node 1 installation nginx

[email protected] ~]# Yum install-y nginx

1.1. Start Nginx Test

[[Email protected] ~]# service Nginx start

650) this.width=650; "Src=" http://linuxu.qiniudn.com/wp-content/uploads/2015/02/ Wpid-fa15bef10782a1dbf0f85f29daa70511_35f9dbe1-c046-45c8-85f6-0bd15004e46b.png "style=" FONT-SIZE:10.5PT; line-height:1.5; "height=" 145 "width=" 929 "/>"


2. Nodes 101 and 102 respectively install JDK and Tomcat

2.1. Install JDK, version 1.7.67

[Email protected] ~]# RPM-IVH jdk-7u67-linux-x64.rpmpreparing ... ######################################## # # # [100%] 1:jdk ########################################### [100%] unpacking JAR files ... js Se.jar ... charsets.jar ... tools.jar ... localedata.jar ..... Jfxrt.jar ...

Export environment variables

[Email protected] ~]# vim/etc/profile.d/jdk.shexport java_home=/usr/java/latest export path= $JAVE _home/bin: $PATH [[ Email protected] ~]#. /etc/profile.d/jdk.sh


Test whether the installation is correct and whether the variables are exported

[[email protected] lib]# Java-versionjava version "1.7.0_67" Java (TM) SE Runtime Environment (build 1.7.0_67-b01) Java Ho Tspot (TM) 64-bit Server VM (build 24.65-b04, Mixed mode) 2.2, install Tomcat[[email protected] ~]# tar XF Apache-tomcat-7.0.55.tar . gz-c/usr/local/[[email Protected] ~]# cd/usr/local/[[email protected] local]# ln-sv apache-tomcat-7.0.55/tomcat ' t Omcat ' apache-tomcat-7.0.55/'

Export environment variables

[Email protected] tomcat]# vim/etc/profile.d/tomcat.shexport Catalina_home=/usr/local/tomcat Export Path= $CATALINA _ Home/bin: $PATH [[email protected] tomcat]#. /etc/profile.d/tomcat.sh

Launch Tomcat test for successful installation and environment variables export

[[email protected] tomcat]# catalina.sh start[[email protected] tomcat]# catalina.sh startusing catalina_base:/usr/loca L/tomcat using Catalina_home:/usr/local/tomcat using Catalina_tmpdir:/usr/local/tomcat/temp using JRE_HOME:/US R/java/latest Using CLASSPATH:/usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Tomcat STA RTed.

Access test, note here the default port is 8080, access is to note

650) this.width=650; "Src=" http://linuxu.qiniudn.com/wp-content/uploads/2015/02/ Wpid-fa15bef10782a1dbf0f85f29daa70511_103166671.png "style=" font-size:10.5pt;line-height:1.5; "height=" 213 "width = "631"/>650) this.width=650; "Src=" http://linuxu.qiniudn.com/wp-content/uploads/2015/02/ Wpid-fa15bef10782a1dbf0f85f29daa70511_103184828.png "style=" font-size:10.5pt;line-height:1.5; "height=" 211 "width = "551"/>


Close Tomcat

[Email protected] lib]# catalina.sh stopusing catalina_base:/usr/local/tomcat Using catalina_home:/usr/local/tomcat Using Catalina_tmpdir:/usr/local/tomcat/temp using jre_home:/usr/java/latest using CLASSPATH:/usr/local/t Omcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar

3, installs the Memcache, here uses is compiles installs, the version 1.4.15

[email protected] memcached-1.4.15]# Yum install-y gcc libevent-devel[[email protected] memcached-1.4.15]#./configure --with-libevent--prefix=/usr/local/memcached [[email protected] memcached-1.4.15]# make && make install

Installation complete go to install directory test whether installation is successful

[Email protected] memcached-1.4.15]# Cd/usr/local/memcached/bin[[email protected] bin]#./memcached-i memcached 1.4.15 Copyright (c) 2003, Danga Interactive, Inc. All rights reserved. The following omit N more ....

Create a user running memcache, and connect

[Email protected] bin]# useradd-r memcache[[email protected] bin]#./memcached-d-v-p 12000-m 512-u memcache

Explanation:-D indicates that running memcached;-v as a daemon indicates output warnings and error messages;-p Specifies the port number to listen on,-m specifies the maximum memory that can be used, and the unit mb;-u specifies the account that runs memcached, not the root user.

Use # Ps-ef | grep memcached to view the process.

Command basic Options Description:

- p listening TCP port (default: 11211)

-D Run as daemon memcached

- u run memcached account, non-root user

- m maximum memory usage, in MB, default is MB

- c Soft connection number, default is 1024 (maximum number of concurrent connections)

-V Output warnings and error messages

-VV request and return information for the print client

-H Printing Help information

-I print copyright information for memcached and libevent

To verify that a service is available by using Telnet connection

[Email protected]]# yum install-y telnet[[email protected]]# telnet 172.16.30.101 12000 Trying 172.16.30.101 ... Connected to 172.16.30.101. Escape character is ' ^] '.

Use the stats command to view the status and show many current states

Statsstat PID 4838 stat uptime 6747 stat time 1422407090 stat version 1.4.15 stat libevent 1.4.13-stablestat pointer_size Rusage_user stat 1.142826 stat rusage_system 0.705892 stat curr_connections stat total_connections stat Connectio N_structures stat Reserved_fds cmd_get stat cmd_set 8 stat cmd_flush 0 Stat cmd_touch 0 Stat get_hits 1

Stop Memcache Service

[Email protected]]# killall memcached

4. Configure Memcache for Tomcat

Tomcat to support memcached management session, you need to call some jar library files:

Memcached-session-manager Project Address, http://code.google.com/p/memcached-session-manager/

Download the following jar file to the Lib directory in the Tomcat installation directory of each tomcat node, where ${version} is replaced with the version number you need, tc${6,7,8} to be the same version number as the Tomcat version.

Memcached-session-manager-${version}.jar

Memcached-session-manager-tc${6,7,8}-${version}.jar

Spymemcached-${version}.jar

Msm-javolution-serializer-${version}.jar

Javolution-${version}.jar

Different versions of the corresponding library files, you need to use the corresponding

The library file used in this experiment is shown below

-rw-r--r--1 root root 452748 Jan 07:43 javolution-5.4.3.1.jar-rw-r--r--1 root root 146932 Jan 07:42 Memcached-se ssion-manager-1.8.1.jar-rw-r--r--1 root root 11283 Jan 07:42 Memcached-session-manager-tc7-1.8.1.jar # #因为使用的tomcat版  This is 7.0.55, so it's tc7-rw-r--r--1 root root 71050 Jan 07:43 msm-javolution-serializer-1.8.1.jar-rw-r--r--1 root root 439107 Jan 07:42 Spymemcached-2.10.2.jar # #此文件无版本区别

Note: The configuration on Node 101 above must also be performed on node 102

5. Configure Tomcat to use the Memcache service

Define a context container for testing on a host on two Tomcat, and create a session manager in it, as follows:

# #可以使用默认host, Code insertion location

One need to note: The engine remains the default, no need to add Jvmroute

650) this.width=650; "Src=" http://linuxu.qiniudn.com/wp-content/uploads/2015/02/ Wpid-fa15bef10782a1dbf0f85f29daa70511_44415462-ff7d-4a42-91bc-ba215f6102af.png "style=" FONT-SIZE:10.5PT; line-height:1.5, "height=" "width=" 649 "/>650" this.width=650; "src=" http://linuxu.qiniudn.com/wp-content/ Uploads/2015/02/wpid-fa15bef10782a1dbf0f85f29daa70511_ff306fdf-6ac3-4c8f-b7a2-8d5f56deb021.png "Style=" font-size:10.5pt;line-height:1.5; "height=" 123 "width=" 984 "/>"

<context path= "/test"  docbase= "/usr/local/tomcat/webapps/test"  reloadable= "true" >               <manager classname= " De.javakaffee.web.msm.MemcachedBackupSessionManager "                 memcachednodes= "n1:172.16.100.9:11211,n2:172.16.100.10:11211"                  failovernodes= "N1"                  Requesturiignorepattern= ". *\. (ICO|PNG|GIF|JPG|CSS|JS) $ "                 transcoderfactoryclass= "De.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"               />      &Nbsp;       </context> 

Provides a test page for two context, respectively:

Node 101:

# mkdir-pv/usr/local/tomcat/webapps/test/web-inf/{classes,lib}# Vim/usr/local/tomcat/webapps/test/index.jsp

Add the following content :

<%@ page language= "Java"  %>

Node 102:

# mkdir-pv/usr/local/tomcat/webapps/test/web-inf/{classes,lib}# Vim/usr/local/tomcat/webapps/test/index.jsp

Add the following content:

<%@ page language= "Java"  %>

Access Test separately

650) this.width=650; "Src=" http://linuxu.qiniudn.com/wp-content/uploads/2015/02/ Wpid-fa15bef10782a1dbf0f85f29daa70511_103340046.png "style=" font-size:10.5pt;line-height:1.5; "height=" 181 "width = "385"/>650) this.width=650; "Src=" http://linuxu.qiniudn.com/wp-content/uploads/2015/02/ Wpid-fa15bef10782a1dbf0f85f29daa70511_2776217b-5f89-436a-94b9-d8b7e0192746.png "style=" FONT-SIZE:10.5PT; line-height:1.5; "height=" 182 "width=" 385 "/>"


If the N1 node 101 Memcache is closed at this time, the session information will be accessed again on the N2

Because the access is two nodes so the session information is not the same, if through the front-end proxy access will be the same session

The following will implement the Memcache session binding by setting Nginx to implement the front end proxy

Configure Nginx

[[email protected] ~]# vim/etc/nginx/nginx.conf add upstreamupstream Tomcat {server 172.16.30.101:80;server in the HTTP segment 172.16.30.102:80;} Add proxy location in location/{root Html;index index.jsp index.html index.htm;proxy_pass http://tomcat;}

You can change it by

650) this.width=650; "Src=" http://linuxu.qiniudn.com/wp-content/uploads/2015/02/ Wpid-fa15bef10782a1dbf0f85f29daa70511_8c0b6102-2819-4b9f-aad9-1b0b0e8cdeaa.png "style=" FONT-SIZE:10.5PT; line-height:1.5, "height=" width= "439"/>


Re-loading Nginx

[[Email protected] ~]# service Nginx reloadreloading nginx: [OK]

6. Access Test

650) this.width=650; "Src=" http://linuxu.qiniudn.com/wp-content/uploads/2015/02/ Wpid-fa15bef10782a1dbf0f85f29daa70511_924b8add-dad5-43ab-a645-ff265375cb8f.png "style=" FONT-SIZE:10.5PT; line-height:1.5; "height=" "width=" 385 "/>650" this.width=650; src= http://linuxu.qiniudn.com/wp-content/ Uploads/2015/02/wpid-fa15bef10782a1dbf0f85f29daa70511_af0bb00e-35b2-442b-a8be-83fb299ff4cb.png "Style=" font-size:10.5pt;line-height:1.5; "height=" 182 "width=" 384 "/>"


At this time regardless of the refresh will not change, we can change a browser to try, but also the same, so that the implementation of the session through the Memcache server, Nginx front-end agent session binding

650) this.width=650; "Src=" http://linuxu.qiniudn.com/wp-content/uploads/2015/02/ Wpid-fa15bef10782a1dbf0f85f29daa70511_1c49688d-2f4f-492e-8065-1895a71d78ac.png "style=" FONT-SIZE:10.5PT; line-height:1.5, "height=", "width=", "/>650" this.width=650; src= "http://linuxu.qiniudn.com/wp-content/" Uploads/2015/02/wpid-fa15bef10782a1dbf0f85f29daa70511_9419574f-ac7a-4723-8514-b1142ee82aff.png "Style=" font-size:10.5pt;line-height:1.5; "height=" 198 "width=" 441 "/>"


This article is from the "Linuxu" blog, make sure to keep this source http://linuxu.blog.51cto.com/9471357/1613530

Nginx+tomcat+memcache+session binding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.