架构(一共使用3台主机,其中nginx主机上运行2个memcached实例,端口分别是11211、11212)
环境配置说明:
软件:Centos6.7、tomcat8.0、memcached1.4.4、nginx1.4.7、jdk-8u65-linux-x64、
实现msm+kryo必须的包,一共9个:
(注意:不同版本的tomcat,必须下载对应的memcached-session-manager包)
spymemcached-2.11.4
reflectasm-1.01
msm-kryo-serializer-1.8.2
minlog-1.2
memcached-session-manager-tc8-1.8.2
memcached-session-manager-1.8.2
kryo-serializers-0.11
kryo-1.04
asm-3.2
nginx服务器 IP:192.168.61.161
tomcat223服务器IP :192.168.61.223
tomcat224服务器IP: 192.168.61.224
1、nginx服务器配置
(1)nginx安装请参考http://dengxi.blog.51cto.com/4804263/1710472
/etc/nginx/nginx.conf内容如下: worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream tomcatserver{ server 192.168.61.224:8080; server 192.168.61.223:8080; } server { listen 80; server_name 192.168.61.161; location / { root html; proxy_pass http://tomcatserver; proxy_set_header X-Real-IP $remote_addr; client_max_body_size 100m; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
(2)memcached安装
yum -y install memcached
启动两个memcached实例
memcached -d -p 11211
memcached -d -p 11212
2、tomcat223服务器安装
(1)安装JDK
rpm -ivh jdk-8u65-linux-x64.rpm
配置环境:
vim /etc/profile.d/java.sh
export JAVA_HOME=/usr/java/latest
export PATH=$JAVA_HOME/bin:$PATH
保存后source /etc/profile.d/java.sh立即生效
(2)安装tomcat
tar -xvzf apache-tomcat-8.0.28.tar -C/usr/local/
ln -sv /usr/local/apache-tomcat-8.0.28/usr/local/tomcat
配置环境变量:
vim /etc/profile.d/tomcat.sh
exportCATALINA_HOME="/usr/local/tomcat"
export PATH=$CATALINA_HOME/bin:$PATH
保存后source /etc/profile.d/tomcat.sh 立即生效
(3)测试主页添加与session共享配置
添加一个test应用和test.jsp
mkdir -pv/usr/local/tomcat/webapps/test/{WEB-INF,lib,class,META-INF}
vim /usr/local/tomcat/webapps/test/test.jsp
SessionID:<%=session.getId()%>
SessionIP:<%=request.getServerName()%>
SessionPort:<%=request.getServerPort()%>
<%
out.println("This is Tomcat Server 8080 8080 ");
%>
编辑/usr/local/tomcat/conf/server.xml 在Host段中添加以下Context内容
memcachedNodes="n1:192.168.61.161:11211,n2:192.168.61.161:11212"
failoverNodes="n1"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
重启tomcat
catalina.sh stop
catalina.sh start
3、tomcat224服务器安装
安装步骤同上,只是/usr/local/tomcat/conf/server.xml 添加的Context内容有点不一样
memcachedNodes="n1:192.168.61.161:11211,n2:192.168.61.161:11212"
failoverNodes="n2"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
4、测试
访问http://192.168.61.161/test/test.jsp
SessionID始终是不变的,可以测试把tomcat223和tomcat224服务器的tomcat服务停止一段时间再启动,再次访问,还是之前那个SessionID,由此可见,tomcat的Session是保存在memcached上的