1、  安装依赖包

yum install libevent libevent-devel

2、  安装memcached

tar zxf memcached-1.4.24.tar.gz

cd memcached-1.4.24

./configure

make

 make install

查看安装是否成功:ls-al /usr/local/bin/mem*

3、  启动及关闭memcached

/usr/local/bin/memcached -d -m 10 -u root -l localhost -p 11211-c 256 -P /tmp/memcached.pid

 

测试启动是否成功:
telnet localhost 11211

 

关闭memcached:

kill `cat /tmp/memcached.pid`

 

4、  MemcachedTomcat配置(session同步)

首先,将之前下载的JAR包全部拷贝到Tomcatlib

接着,配置两个Tomcatcontext.xml的文件:在Context标签内加入如下内容:

       memcachedNodes="n1:localhost:11211"

       requestUriIgnorePattern=".*/.(png|gif|jpg|css|js)$"

        sessionBackupAsync="false"

       sessionBackupTimeout="100"

transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"

       copyCollectionsForSerialization="false"/>

 

5、  修改tomcat默认目录及主页:

server.xml的标签  上面添加

 

index.jsp内容:
  <%@ page language="java"contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>

SessionID:<%=session.getId()%>


SessionIP:<%=request.getServerName()%>


SessionPort:<%=request.getServerPort()%>

<%

out.println("Itis test1");           

%>

<%=(String)session.getAttribute("user")%>

 


6、  Memcached自启动:

vi /etc/rc.d/rc.local

在最后添加:

/usr/local/bin/memcached -d -m 10 -u root -l localhost -p11211 -c 256 -P /tmp/memcached.pid