Session服务器之Memcached

Posted cxm123123form

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Session服务器之Memcached相关的知识,希望对你有一定的参考价值。

 

 

材料:两台Tomcat(接Session复制一起做)

第一台Tomcat:IP为130

[root@localhost ~]# yum install libevent memcached -y                                   //安装memcached依赖包

[root@localhost ~]# memcached -u root -m 512M -n 10 -f2 -d -vvv -c 512       //用memcached启动服务

[root@localhost ~]# netstat -anpt | grep :11211                                                  //memcached端口为11211
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 63317/memcached
tcp6 0 0 :::11211 :::* LISTEN 63317/memcached

测试memcached能否存储数据(用telnet小软件进行测试)

[root@localhost ~]# yum install telnet -y

 

[root@localhost ~]# telnet 192.168.200.130 11211                   //用telnete远程连接本机IP
Trying 192.168.200.130...
<30 new auto-negotiating client connection
Connected to 192.168.200.130.
Escape character is ‘^]‘.

 

set username 0 0 8        //set表示定义变量username 0 0 8   第一个0是标识符,第二个0是过期时间,8是长度

30: going from conn_new_cmd to conn_waiting
30: going from conn_waiting to conn_read
30: going from conn_read to conn_parse_cmd
30: Client using the ascii protocol
<30 set username 0 0 8
30: going from conn_parse_cmd to conn_nread
zhangsan
> NOT FOUND username
>30 STORED
30: going from conn_nread to conn_write
30: going from conn_write to conn_new_cmd
30: going from conn_new_cmd to conn_waiting
30: going from conn_waiting to conn_read
STORED                          //STORED表示存储成功

get username                     //get表示获取
30: going from conn_read to conn_parse_cmd
<30 get username
> FOUND KEY username
>30 sending key username
>30 END
30: going from conn_parse_cmd to conn_mwrite
30: going from conn_mwrite to conn_new_cmd
30: going from conn_new_cmd to conn_waiting
30: going from conn_waiting to conn_read
VALUE username 0 8
zhangsan
END

quit                                 //quit退出
30: going from conn_read to conn_parse_cmd
<30 quit
30: going from conn_parse_cmd to conn_closing
<30 connection closed.
Connection closed by foreign host.

最后让Tomcat主机一,Tomcat主机二通过(msm)连接到Memcached

将session包中的".jar"复制到/usr/local/tomcat/lib/下面

[root@localhost ~]# mkdir session
[root@localhost ~]# cd session/

[root@localhost session]# rz -E          //导入Tomcat7-Memcached JAR包.zip

[root@localhost session]# ls
javolution-5.5.1.jar   memcached-session-manager-1.5.1.jar   msm-kryo-serializer-1.6.4.jar
kryo-1.03.jar   memcached-session-manager-tc7-1.5.1.jar    reflectasm-0.9.jar
kryo-serializers-0.10.jar   minlog-1.2.jar   spymemcached-2.7.3.jar
memcached-2.5.jar            msm-javolution-serializer-1.5.1.jar

[root@localhost ~]# cp session/*.jar /usr/local/tomcat8/lib/             //将session下的jar全部移到lib下

编辑tomcat配置文件连接指定的

[root@localhost ~]# vim /usr/local/tomcat8/conf/context.xml

 

在末尾添加如下命令

<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="memA:192.168.200.130:11211 memB:192.168.200.122:11211"
requestUrilgnorePattern=".*\(ico|png|gif|jpg|css|js)$"
transcodeFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscodeFactory"/>

 

 

 

第二台Tomcat:IP为122

[root@localhost ~]# yum install libevent memcached -y                                   //安装memcached依赖包

[root@localhost ~]# vim /usr/local/tomcat8/conf/context.xml

在末尾添加如下命令

<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="memA:192.168.200.130:11211 memB:192.168.200.122:11211"
requestUrilgnorePattern=".*\(ico|png|gif|jpg|css|js)$"
transcodeFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscodeFactory"/>

 

 

以上是关于Session服务器之Memcached的主要内容,如果未能解决你的问题,请参考以下文章

PHP 之 SESSION 详解

Java 之 Session

会话技术之Session

原创Java基础之Session机制

Java Web之会话管理二:Session

浅析Django之session与cookie