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、 Memcached的Tomcat配置(session同步)
首先,将之前下载的JAR包全部拷贝到Tomcat的lib下
接着,配置两个Tomcat的context.xml的文件:在Context标签内加入如下内容:
<ManagerclassName="de.javakaffee.web.msm.MemcachedBackupSessionManager"
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的标签</Host> 上面添加 <Contextpath="" docBase="../webapps/test1" debug="0"reloadable="true" />
index.jsp内容:
<%@ page language="java"contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<body>
SessionID:<%=session.getId()%>
<BR>
SessionIP:<%=request.getServerName()%>
<BR>
SessionPort:<%=request.getServerPort()%>
<%
out.println("Itis test1");
%>
<%=(String)session.getAttribute("user")%>
</body>
</html>
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