首页 > 代码库 > Nginx + tomcat + Memcached(session共享)

Nginx + tomcat + Memcached(session共享)

Nginx + tomcat + Memcached(session共享)


实验拓扑

技术分享














nginx   实现负载

tomcat   实现web功能

memcached 实现会话共享



安装Nginx

[root@localhost ~]# yum -y install pcre pcre-devel

[root@localhost ~]# tar -zxvf nginx-1.0.5.tar.gz

[root@localhost ~]# cd nginx-1.0.5

[root@localhost nginx-1.0.5]# ./configure --prefix=/usr/local/nginx --with-pcre

[root@localhost nginx-1.0.5]# make

[root@localhost nginx-1.0.5]# make install


配置Nginx

[root@svr1 ~]# ls -Z /var/ftp/rt.txt 

upstream tomcatgrp

      server 192.168.170.33:8080

      server 192.168.170.43:8080;

      } 

      server { 

            listen 80

            server_name nginx.tutu.com

            location / { 

                   root html; 

                   index index.html 

                   index.htm; 

                   proxy_pass http://tomcatgrp

                   } 

           }


启动Nginx

[root@svr1 ~]# /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

[root@svr1 ~]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf


安装jdk

[root@www ~]# chmod +x jdk-6u27-linux-i586.bin 

[root@www ~]# ./jdk-6u27-linux-i586.bin 

[root@www ~]# mv jdk1.6.0_27 /usr/local/jdk 

[root@www ~]# vim /etc/bashrc   //定义环境变量

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 

[root@www ~]# source /etc/bashrc //初始化环境变量


验证jdk

[root@www ~]# java -version

java version "1.6.0_27"

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

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

[root@www ~]#


启动tomcat

[root@www ~]# /usr/local/tomcat/bin/shutdown.sh //停止tomcat

[root@www ~]# /usr/local/tomcat/bin/startup.sh //启动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

[root@www ~]# netstat -utnalp | grep :8080  //监听端口

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

[root@www ~]#


配置验证用户

[root@www ~]# vim /usr/local/tomcat/conf/tomcat-users.xml

<tomcat-users>

<role rolename="manager"/>

<user username="admin" password="123456" roles="manager"/>  //用户身份:manager

</tomcat-users>

[root@www ~]# /usr/local/tomcat/bin/shutdown.sh

[root@www ~]# /usr/local/tomcat/bin/startup.sh


访问tomcat

技术分享


编写网页文件

[root@www ~]# vim /usr/local/tomcat/webapps/ROOT/test.jsp

<html>

<body bgcolor=“red”>         //指定网页背景颜色

<center>

<%= request.getSession().getId() %> //获取sessionID

<h1>tomcatA 33</h1>

</center>

</body>

</html>

[root@www ~]#


测试nginx负载均衡

http://192.168.170.33:8080/test.jsp 

每刷新一次就会访问到不同的tomcat上,但会话ID不一样,下面就解决会话ID不同的问题

技术分享

技术分享


在tomcat上部署msm

[root@localhost 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

[root@localhost session]# cp *.jar /usr/local/tomcat/lib/


连接memcached

[root@localhost ~]# 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“               //指定memB为备用服务器 

REQUESTuRILGNOREpATTERN=".*\(ico|png|gif|jpg|css|js)$" transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" /> </Context> 

[root@localhost ~]# /usr/local/tomcat/bin/shutdown.sh 

[root@localhost ~]# /usr/local/tomcat/bin/startup.sh


安装事件库

[root@plj ~]# tar –zxvf libevent-2.0.15-stable.tar.gz

[root@plj ~]# cd libevent-2.0.15-stable

[root@plj libevent-2.0.15-stable]# ./configure

[root@plj libevent-2.0.15-stable]# make

[root@plj libevent-2.0.15-stable]# make install

[root@plj ~]# echo "/usr/local/lib" > /etc/ld.so.conf.d/libevent.conf

[root@plj ~]# ldconfig    //更新链接库


安装memcached

[root@plj ~]# tar -zxvf memcached-1.4.5.tar.gz 

[root@plj ~]# cd memcached-1.4.5 

[root@plj memcached-1.4.5]# ./configure 

[root@plj memcached-1.4.5]# make 

[root@plj memcached-1.4.5]# make install 

[root@plj ~ ]# /usr/local/bin/memcached -l 192.168.170.44 -p 11211 -u root -m 200 -c 150 -n 10 -f 1.5 -vvv -d   //启动


测试memcached

如下图可见,实现负载的同时,会话ID也一样了,说明实现了会话共享

技术分享

技术分享


本文出自 “Dave-技术博客” 博客,请务必保留此出处http://davewang.blog.51cto.com/6974997/1857081

Nginx + tomcat + Memcached(session共享)