Nginx + tomcat + Memcached (session sharing)

Source: Internet
Author: User
Tags memcached nginx load balancing

Nginx + tomcat + Memcached (session sharing)


Experimental topology

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/88/1F/wKioL1fqPmSibPNSAABpY5R6flw286.png "title=" 1.png " Style= "float:left;width:600px;height:280px;" width= "height=" 280 "border=" 0 "hspace=" 0 "vspace=" 0 "alt=" Wkiol1fqpmsibpnsaabpy5r6flw286.png "/>














Nginx Implementation Load

Tomcat implements Web functionality

Memcached Implementing session Sharing



Installing Nginx

[Email protected] ~]# yum-y install pcre pcre-devel

[Email protected] ~]# TAR-ZXVF nginx-1.0.5.tar.gz

[Email protected] ~]# CD nginx-1.0.5

[Email protected] nginx-1.0.5]#/configure--prefix=/usr/local/nginx--with-pcre

[[email protected] nginx-1.0.5]# make

[[email protected] nginx-1.0.5]# make install


Configure Nginx

[Email protected] ~]# ls-z/var/ftp/rt.txt

Upstream tomcatgrp {

Server 192.168.170.33:8080;

Server 192.168.170.43:8080;

}

server {

Listen ;

server_name nginx.tutu.com;

Location/{

root HTML;

Index index.html

index.htm;

Proxy_pass http://tomcatgrp;

}

}


Start Nginx

[Email protected] ~]# /usr/local/nginx/sbin/nginx–t

Nginx: .../usr/local/nginx/conf/nginx.conf syntax is OK

Nginx: .../usr/local/nginx/conf/nginx.conf test is successful

[Email protected] ~]# /usr/local/nginx/sbin/nginx-c/usr/local/nginx/conf/nginx.conf


Installing the JDK

[Email protected] ~]# chmod +x jdk-6u27-linux-i586.bin

[Email protected] ~]#./jdk-6u27-linux-i586.bin

[Email protected] ~]# MV JDK1.6.0_27/USR/LOCAL/JDK

[[email protected] ~]# vim /ETC/BASHRC //define environment variables

Export JAVA_HOME=/USR/LOCAL/JDK

Export Java_bin=/usr/local/jdk/bin

Export path= $JAVA _home/bin: $PATH

Export classpath=.: $JAVA _home/lib/dt.jar: $JAVA _home/lib/tools.jar

Export Java_home java_bin PATH CLASSPATH

[[email protected] ~]# SOURCE/ETC/BASHRC //Initialize environment variable


Verifying the JDK

[Email protected] ~]# java-version

Java Version "1.6.0_27"

Java (TM) SE Runtime Environment (build 1.6.0_27-b07)

Java HotSpot (TM) Client VMs (build 20.2-b06, mixed mode, sharing)

[Email protected] ~]#


Start Tomcat

[[email protected] ~]# /usr/local/tomcat/bin/shutdown.sh //Stop Tomcat

[[email protected] ~]# /usr/local/tomcat/bin/startup.sh //start Tomcat

Using catalina_base:/usr/local/tomcat

Using Catalina_home:/usr/local/tomcat

Using Catalina_tmpdir:/usr/local/tomcat/temp

Using Jre_home:/USR/LOCAL/JDK

Using CLASSPATH:/usr/local/tomcat/bin/bootstrap.jar

[Email protected] ~]# Netstat-utnalp | grep:8080 //listening port

TCP 0 0::: 8080:::* LISTEN 3181/java

[Email protected] ~]#


Configure Authenticated Users

[Email protected] ~]# Vim/usr/local/tomcat/conf/tomcat-users.xml

<tomcat-users>

<role rolename= "Manager"/>

<user username= "admin" password= "123456" roles= "manager"/> //user identity: Manager

</tomcat-users>

[Email protected] ~]#/usr/local/tomcat/bin/shutdown.sh

[Email protected] ~]#/usr/local/tomcat/bin/startup.sh


Access Tomcat

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/88/23/wKiom1fqPvzAmME-AAG5-S0BCmU528.png "title=" 2.png " Width= "height=" 289 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" WIDTH:700PX;HEIGHT:289PX; "alt=" Wkiom1fqpvzamme-aag5-s0bcmu528.png "/>


Writing Web page files

[Email protected] ~]# vim/usr/local/tomcat/webapps/root/test.jsp

<body bgcolor= "Red" > //Specify page background color

<center>

<%= request.getsession (). GetId ()%> //Get SessionID

</center>

</body>

[Email protected] ~]#


Test Nginx Load Balancing

http://192.168.170.33:8080/test.jsp

Each refresh is accessed on a different tomcat, but the session ID is different, and the following resolves the issue with the session ID

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M02/88/20/wKioL1fqP8XiHmzZAAChab6UJVo017.png-wh_500x0-wm_3 -wmp_4-s_3926322995.png "title=" 4.png "alt=" Wkiol1fqp8xihmzzaachab6ujvo017.png-wh_50 "/>

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M00/88/23/wKiom1fqQCaQwLFmAACXINhpuxA574.png-wh_500x0-wm_3 -wmp_4-s_2631254618.png "title=" 5.png "alt=" Wkiom1fqqcaqwlfmaacxinhpuxa574.png-wh_50 "/>


Deploying MSM on Tomcat

[[email protected] session]# ls

Kryo-1.04.jar Memcached-session-manager-1.5.1.jar Msm-kryo-serializer-1.5.1.jar

Kryo-serializers-0.9.jar Memcached-session-manager-tc6-1.5.1.jar Reflectasm-1.01.jar

Memcached-2.5.jar Minlog-1.2.jar

[Email protected] session]# CP *.jar/usr/local/tomcat/lib/


Connection memcached

[Email protected] ~]# vim/usr/local/tomcat/conf/context.xml

</Context>

<manager classname= "De.javakaffee.web.msm.MemcachedBackupSessionManager" memcachednodes= "MemA : 192.168.170.44:11211,memb:192.168.170.49:11211"failovernodes=" MemB " //Specify MemB as standby server

Requesturilgnorepattern= ". *\ (ICO|PNG|GIF|JPG|CSS|JS) $" transcoderfactoryclass= " De.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory "/> </Context>

[Email protected] ~]#/usr/local/tomcat/bin/shutdown.sh

[Email protected] ~]#/usr/local/tomcat/bin/startup.sh


Installing the Event Library

[Email protected] ~]# TAR–ZXVF libevent-2.0.15-stable.tar.gz

[Email protected] ~]# CD libevent-2.0.15-stable

[Email protected] libevent-2.0.15-stable]#./configure

[[email protected] libevent-2.0.15-stable]# make

[[email protected] libevent-2.0.15-stable]# make install

[Email protected] ~]# echo "/usr/local/lib" >/etc/ld.so.conf.d/libevent.conf

[[email protected] ~]# ldconfig //Update link library


Installing memcached

[Roo[email protected] ~]# tar-zxvf memcached-1.4.5.tar.gz

[Email protected] ~]# CD memcached-1.4.5

[Email protected] memcached-1.4.5]#./configure

[[email protected] memcached-1.4.5]# make

[[email protected] memcached-1.4.5]# make install

[Email protected] ~]#/usr/local/bin/memcached-l 192.168.170.44-p 11211-u root-m 200-c 150-n 10-f 1.5-vvv-d //Start


Test memcached

As can be seen, the implementation of the load while the session ID is the same, the implementation of the session sharing

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M01/88/20/wKioL1fqQR7x1UyjAABloikziHM494.png-wh_500x0-wm_3 -wmp_4-s_2160737607.png "title=" 6.png "alt=" Wkiol1fqqr7x1uyjaabloikzihm494.png-wh_50 "/>

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M01/88/23/wKiom1fqQTeCSSMHAACitUvckIY025.png-wh_500x0-wm_3 -wmp_4-s_1187248127.png "title=" 7.png "alt=" Wkiom1fqqtecssmhaacituvckiy025.png-wh_50 "/>


This article is from the "Dave-Technology blog" blog, please be sure to keep this source http://davewang.blog.51cto.com/6974997/1857081

Nginx + tomcat + Memcached (session sharing)

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.