一般实现session共享,最常用的一种方法就是通过memcached-session-manager。下边我就说一下,怎么通过memcached-session-manager实现session共享。
首先建一个放软件的目录,进入此目录。
[root@node1 ~]# mkdir -p /taokey/tools [root@node1 ~]# cd /taokey/tools/
2.把下载好的apache-tomcat-7.0.57.tar.gz和jdk-7u71-linux-x64.gz软件包上传到服务器上。
[root@node1 tools]# ls apache-tomcat-7.0.57.tar.gz jdk-7u71-linux-x64.gz [root@node1 tools]# tar -zxf apache-tomcat-7.0.57.tar.gz [root@node1 tools]# tar -zxf jdk-7u71-linux-x64.gz [root@node1 tools]# ls apache-tomcat-7.0.57 jdk1.7.0_71
3.创建/data目录,并且把tomcat改名,把tomcat和jdk移到data目录下。
[root@node1 tools]# mv apache-tomcat-7.0.57 node1 [root@node1 tools]# mkdir /data [root@node1 tools]# cp -r jdk1.7.0_71 node1 /data
4.配置系统的jdk环境变量。
[root@node1 data]# tail -7 /etc/profile JAVA_HOME=/data/jdk1.7.0_71 JRE_HOME=/data/jdk1.7.0_71/jre PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin CLASSPATH=:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib export JAVA_HOME JRE_HOME PATH CLASSPATH export PATH=$PATH:JAVA_HOME/bin export JAVA_HOME [root@node1 data]# source /etc/profile
5.修改默认的项目路径。
[root@node1 data]# vim node1/conf/server.xml +125 <Host name="localhost" appBase="/data/" #默认的是webapps,我修改成了/data/目录。
6.给tomcat默认启动和关闭脚本添加执行权限。
[root@node1 data]# cd node1/bin/ [root@node1 bin]# chmod +x startup.sh [root@node1 bin]# chmod +x shutdown.sh
7.启动tomcat,看下是否成功运行。 [root@node1 bin]# ./startup.sh Using CATALINA_BASE: /data/node1 Using CATALINA_HOME: /data/node1 Using CATALINA_TMPDIR: /data/node1/temp Using JRE_HOME: /data/jdk1.7.0_71/jre Using CLASSPATH: /data/node1/bin/bootstrap.jar:/data/node1/bin/tomcat-juli.jar Tomcat started. [root@node1 bin]# ps -ef | grep java root 2036 1 83 12:41 pts/1 00:00:04 /data/jdk1.7.0_71/jre/bin/java -Djava.util.logging.config.file=/data/node1/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/data/node1/endorsed -classpath /data/node1/bin/bootstrap.jar:/data/node1/bin/tomcat-juli.jar -Dcatalina.base=/data/node1 -Dcatalina.home=/data/node1 -Djava.io.tmpdir=/data/node1/temp org.apache.catalina.startup.Bootstrap start root 2050 1907 0 12:42 pts/1 00:00:00 grep java
8.看下tomcat默认监听的端口。
[root@node1 bin]# netstat -anpt | grep java tcp 0 0 :::8080 :::* LISTEN 2036/java tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 2036/java tcp 0 0 :::8009 :::* LISTEN 2036/java
9.在项目路径下创建一个测试网页,测试下网页是否可以打开。
[root@node1 bin]# mkdir /data/ROOT [root@node1 bin]# touch /data/ROOT/test.html [root@node1 bin]# echo "hello world" >/data/ROOT/test.html [root@node1 bin]# cat /data/ROOT/test.html hello world [root@node1 ~]# curl -I 192.168.1.15:8080/test.html HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Accept-Ranges: bytes ETag: W/"12-1421038003000" Last-Modified: Mon, 12 Jan 2015 04:46:43 GMT Content-Type: text/html Content-Length: 12 Date: Mon, 12 Jan 2015 04:48:16 GMT [root@node1 ~]# wget 192.168.1.15:8080/test.html [root@node1 ~]# cat test.html hello world tomcat7.0.57搭建完毕。
第一个tomcat实例node1已经搭建完毕,tomcat实例node2也按照此步骤搭建即可。tomcat搭建比较简单,这里不再赘述。两个tomcat实例(node1、node2),都搭建好之后,下面结合memcache和msm实现 session共享。
memcahce已经安装好了,详细安装步骤请参加:
10.安装memcahce。
cd /taokey/tools/ curl -O http://www.monkey.org/~provos/libevent-2.0.13-stable.tar.gz tar -zxvf libevent-2.0.13-stable.tar.gz cd libevent-2.0.13-stable ./configure --prefix=/usr make && make install curl -O http://www.danga.com/memcached/dist/memcached-1.4.0.tar.gz tar -zxvf memcached-1.4.0.tar.gz cd memcached-1.4.0 ./configure --with-libevent=/usr/ make make install /usr/local/bin/memcached -d -m 200 -u root -c 256 -p 11211 -P /tmp/memcache.pid 测试node1和node2连接memcache是否正常。 [root@node1 ~]# telnet 192.168.1.84 11211 Trying 192.168.1.84... Connected to 192.168.1.84. Escape character is ‘^]‘. [root@node2 ~]# telnet 192.168.1.84 11211 Trying 192.168.1.84... Connected to 192.168.1.84. Escape character is ‘^]‘. 经过测试,memcache服务是没有问题。
要想实现session共享,需要上传tomcat7所需要的memcached-session-manager的jar包。
tomcat7实现session共享所需jar包下载地址:
http://down.51cto.com/data/1974783
11.这里列出了包括tomcat自带和实现共享session所需要的所有jar包:
[root@node1 ~]# cd /data/node1/lib/ [root@node1 lib]# ls annotations-api.jar jasper-el.jar minlog-1.2.jar tomcat-dbcp.jar catalina-ant.jar jasper.jar msm-kryo-serializer-1.6.5.jar tomcat-i18n-es.jar catalina-ha.jar javolution-5.4.3.1.jar reflectasm-0.9.jar tomcat-i18n-fr.jar catalina.jar jsp-api.jar servlet-api.jar tomcat-i18n-ja.jar catalina-tribes.jar kryo-1.03.jar spymemcached-2.10.2.jar tomcat-jdbc.jar couchbase-client-1.2.2.jar kryo-serializers-0.10.jar tomcat7-websocket.jar tomcat-util.jar ecj-4.4.jar memcached-session-manager-1.6.5.jar tomcat-api.jar websocket-api.jar el-api.jar memcached-session-manager-tc7-1.6.5.jar tomcat-coyote.jar [root@node1 lib]# du -sh . 8.2M . [root@node2 ~]# cd /data/node2/lib/ [root@node2 lib]# ls annotations-api.jar jasper-el.jar minlog-1.2.jar tomcat-dbcp.jar catalina-ant.jar jasper.jar msm-kryo-serializer-1.6.5.jar tomcat-i18n-es.jar catalina-ha.jar javolution-5.4.3.1.jar reflectasm-0.9.jar tomcat-i18n-fr.jar catalina.jar jsp-api.jar servlet-api.jar tomcat-i18n-ja.jar catalina-tribes.jar kryo-1.03.jar spymemcached-2.10.2.jar tomcat-jdbc.jar couchbase-client-1.2.2.jar kryo-serializers-0.10.jar tomcat7-websocket.jar tomcat-util.jar ecj-4.4.jar memcached-session-manager-1.6.5.jar tomcat-api.jar websocket-api.jar el-api.jar memcached-session-manager-tc7-1.6.5.jar tomcat-coyote.jar [root@node2 lib]# du -sh . 8.2M .
12.修改两个tomcat的配置文件 context.xml。
修改后的tomcat node1配置文件 [root@node1 ~]# cd /data/node1/conf/ [root@node1 conf]# tail context.xml <!-- <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" /> --> <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:192.168.1.84:11211" requestUriIgnorePattern=".*\.(png|gif|jpg|css|js|ico|jpeg|htm|html)$" sessionBackupTimeout="1800000" transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" /> </Context> 修改后的tomcat node2配置文件 [root@node2 ~]# cd /data/node2/conf/ [root@node2 conf]# tail context.xml <!-- <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" /> --> <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:192.168.1.84:11211" requestUriIgnorePattern=".*\.(png|gif|jpg|css|js|ico|jpeg|htm|html)$" sessionBackupTimeout="1800000" transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" /> </Context>
13.修改之后,创建一个index.jsp测试session共享的文档,重启两个tomcat实例。
重启实例node1。
[root@node1 ROOT]# cat index.jsp <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>Tomcat+memcached共享session测试</title> </head> <body> SessionID:<%=session.getId()%> <BR> SessionIP:<%=request.getServerName()%> <BR> SessionPort:<%=request.getServerPort()%> <BR> <% out.println("This is Tomcat Server 8081."); %> %</body> %</html> /data/node1/bin/shutdown.sh [root@node1 ~]# ps -ef | grep java root 9063 1907 0 17:20 pts/1 00:00:00 grep java /data/node1/bin/startup.sh [root@node1 ~]# netstat -anpt | grep 8080 tcp 0 0 :::8080 :::* LISTEN 9072/java tcp 0 0 ::ffff:127.0.0.1:8080 ::ffff:127.0.0.1:40496 TIME_WAIT -
14.重启实例tomcat node2。
[root@node2 ROOT]# cat index.jsp <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>Tomcat+memcached共享session测试</title> </head> <body> SessionID:<%=session.getId()%> <BR> SessionIP:<%=request.getServerName()%> <BR> SessionPort:<%=request.getServerPort()%> <BR> <% out.println("This is Tomcat Server 8081."); %> %%</body> %%</html> [root@node2 ~]# /data/node2/bin/shutdown.sh [root@node2 ~]# ps -ef | grep java root 2830 2081 0 17:22 pts/0 00:00:00 grep java [root@node2 ~]# /data/node2/bin/startup.sh [root@node2 ~]# netstat -anpt | grep 8080 tcp 0 0 :::8080 :::* LISTEN 2840/java
15.访问nginx代理的服务器IP,192.168.1.15
16.把node1停掉,然后再测试192.168.1.15
[root@node1 ~]# /data/node1/bin/shutdown.sh Using CATALINA_BASE: /data/node1 Using CATALINA_HOME: /data/node1 Using CATALINA_TMPDIR: /data/node1/temp Using JRE_HOME: /data/jdk1.7.0_71/jre Using CLASSPATH: /data/node1/bin/bootstrap.jar:/data/node1/bin/tomcat-juli.jar [root@node1 ~]# ps -ef | grep java root 9132 1907 0 17:29 pts/1 00:00:00 grep java
17.session信息还是原来的,好现在咱们开启node1,关闭node2,在测试一下,session信息、
[root@node1 ~]# /data/node1/bin/startup.sh Using CATALINA_BASE: /data/node1 Using CATALINA_HOME: /data/node1 Using CATALINA_TMPDIR: /data/node1/temp Using JRE_HOME: /data/jdk1.7.0_71/jre Using CLASSPATH: /data/node1/bin/bootstrap.jar:/data/node1/bin/tomcat-juli.jar Tomcat started. [root@node1 ~]# netstat -anpt | grep java tcp 0 0 :::8080 :::* LISTEN 9144/java tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 9144/java tcp 0 0 :::8009 :::* LISTEN 9144/java tcp 0 0 ::ffff:192.168.1.15:58421 ::ffff:192.168.1.84:11211 ESTABLISHED 9144/java tcp 0 0 ::ffff:192.168.1.15:58418 ::ffff:192.168.1.84:11211 ESTABLISHED 9144/java tcp 0 0 ::ffff:192.168.1.15:58419 ::ffff:192.168.1.84:11211 ESTABLISHED 9144/java tcp 0 0 ::ffff:192.168.1.15:58420 ::ffff:192.168.1.84:11211 ESTABLISHED 9144/java [root@node2 ~]# /data/node2/bin/shutdown.sh Using CATALINA_BASE: /data/node2 Using CATALINA_HOME: /data/node2 Using CATALINA_TMPDIR: /data/node2/temp Using JRE_HOME: /data/jdk1.7.0_71/jre Using CLASSPATH: /data/node2/bin/bootstrap.jar:/data/node2/bin/tomcat-juli.jar [root@node2 ~]# ps -ef | grep java root 2896 2081 0 17:31 pts/0 00:00:00 grep java
session共享成功实现。
tomcat7实现session共享所需jar包下载地址:
http://down.51cto.com/data/1974783
本文出自 “岁月在流逝,光辉依然在” 博客,请务必保留此出处http://taokey.blog.51cto.com/4633273/1615551
Centos6.5系统搭建tomcat7.0.57通过msm实现session共享
原文地址:http://taokey.blog.51cto.com/4633273/1615551