memcached+session共享
目标:客户端通过nginx代理访问tomcat服务器,两台tomcat服务器共用memcached服务器,访问的sessionid存放在memcached服务器中,一台做主服务器,一台做备用服务器,当主服务器宕机,启用备用备用服务器
环境准备:
nginx服务器:192.168.1.1
两台基于tomcat的服务器:192.168.1.2 192.168.1.3
两台memcached服务器:192.168.1.4 192.168.1.5
安装gcc*开发环境
1.配置memcached服务(另一台做相同的配置)
首先安装依赖库
下载libevent-2.0版本的和memcached-1.4版本的
[root@www memceached-package]# tar -zxf libevent-2.0.15-stable.tar.gz
[root@www memceached-package]# cd libevent-2.0.15-stable
[root@www libevent-2.0.15-stable]# ./configure && make && make install
安装memcached源码包
[root@www memceached-package]# tar -zxf memcached-1.4.5.tar.gz
[root@www memceached-package]# cd memcached-1.4.5
[root@www memcached-1.4.5]# ./configure && make && make install
由于依赖包安装的路径不能被系统读取到,所以写一个配置文件让系统能够识别安装的路径
[root@www memcached-1.4.5]# vim /etc/ld.so.conf.d/libevent.conf
/usr/local/lib/
重新加载配置文件
[root@www memcached-1.4.5]# ldconfig /etc/ld.so.conf.d/libevent.conf
查看memcached帮助
[root@www memcached-1.4.5]# /usr/local/bin/memcached -h
启动服务
[root@www memcached-1.4.5]# /usr/local/bin/memcached -u root -m 100M -n 10 -f 2 -d -vvv
查看端口号是否启动成功
[root@www memcached-1.4.5]# netstat -anput | grep 11211
本机测试能否存取
[root@www memcached-1.4.5]# telnet 192.168.1.4 11211
Trying 192.168.1.4...
Connected to 192.168.1.4 (192.168.1.4).
Escape character is '^]'.
set name 0 180 3 //将key(name) value(jim)存入
jim
STORED
get name // 取出name的值
VALUE name 0 3
jim
END
quit
Connection closed by foreign host.
服务能实现正常的存取功能,结束!
2.配置tomcat服务器(另一个做相同的配置,jsp改变一下以示区别)
下载 jdk-6u27-linux-i586.bin apache-tomcat-6.0.32.tar.gz
首先安装JDK
[root@localhost memcached+session-package]# chmod +x jdk-6u27-linux-i586.bin
[root@localhost memcached+session-package]# ./jdk-6u27-linux-i586.bin
[root@localhost memcached+session-package]# cp jdk1.6.0_27/ /usr/local/jdk
导入环境变量
[root@localhost memcached+session-package]# vim /etc/bashrc
文件末尾添加
export JRE_HOME=/usr/local/jdk
export JAVA_BIN=/usr/local/jdk/bin
export PATH=$JRE_HOME/bin:$PATH
export CLASSPATH=.:$JRE_HOME/lib/dt.jar:$JRE_HOME/lib/tools.jar
export JRE_HOME JAVA_BIN PATH CLASSPATH
重新加载配置文件
[root@localhost memcached+session-package]# source /etc/bashrc
[root@localhost ~]# java -version
java version "1.6.0_27"
安装tomcat
[root@localhost memcached+session-package]# tar -zxf apache-tomcat-6.0.32.tar.gz
[root@localhost memcached+session-package]# cp apache-tomcat-6.0.32 /usr/local/tomcat
[root@localhost memcached+session-package]# /usr/local/tomcat/bin/startup.sh
查看端口是否启动
[root@localhost memcached+session-package]# netstat -anput | grep 8080
tcp 0 0 :::8080 :::* LISTEN 18162/java
访问主页
[root@localhost ~]# elinks --dump 192.168.1.2:8080
添加一个jsp的动态网页
[root@localhost ~]# vim /usr/local/tomcat/webapps/ROOT/test.jsp
<html>
<body bgcolor="green">
<center>
<%= request.getSession().getId() %>
<h1>tomcatA 43</h1>
</center>
</body>
</html>
[root@localhost ~]# elinks --dump 192.168.1.2:8080/test.jsp
8CD662E4983B212941DEFE18F0907EE4-memA
tomcatB 43
tomcat服务器已经能成功访问!
3.添加tomcat连接memcached的接口
下载以下包
kryo-1.04.jar memcached-session-manager-tc6-1.5.1.jar
kryo-serializers-0.9.jar minlog-1.2.jar
memcached-2.5.jar msm-kryo-serializer-1.5.1.jar
memcached-session-manager-1.5.1.jar reflectasm-1.01.jar
将以上所有包复制到tomcat的库目录中
[root@localhost session]# cp * /usr/local/tomcat/lib/
修改tomcat的配置文件
[root@localhost session]# vim /usr/local/tomcat/conf/context.xml
在<Context> </Context>添加指明memcached两个主机的IP地址及端口号和备用memcached服务器
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="memA:192.168.1.4:11211 memB:192.168.1.5:11211"
failoverNodes="memB"
requestUrilgnorePattern=".*\(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
重启tomcat使配置生效
[root@localhost session]# /usr/local/tomcat/bin/shutdown.sh
[root@localhost session]# /usr/local/tomcat/bin/startup.sh
4.配置nginx服务器
修改配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream "webgroup" {
server 192.168.1.2:8080;
server 192.168.1.3:8080;
}
server {
listen 80;
server_name localhost;
location / {
# root html;
# index index.html index.htm;
proxy_pass http://webgroup;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
重新启动nginx服务
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s stop
[root@localhost ~]# /usr/local/nginx/sbin/nginx
5.客户端测试
浏览器测试
http://192.168.1.1/test.jsp
刷新网页发现sessionid不变,证明memcached已经生效
将主memcached服务器关闭,测试备用服务器
[root@www ~]# ifconfig eth0 | head -2
eth0 Link encap:Ethernet HWaddr 00:0C:29:FD:5B:B1
inet addr:192.168.118.3 Bcast:192.168.118.255 Mask:255.255.255.0
[root@www ~]# netstat -anput | grep 11211
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 7775/memcached
[root@www ~]# pkill -9 memcached
[root@www ~]# netstat -anput | grep 11211
客户端再测试
http://192.168.1.1/test.jsp
刷新网页发现sessionid不变,证明备用memcached已经生效