利用memcached缓存保持session一致性

本文的设置的总体的框架如下图

第一步:安装tengine并设置反向代理,导向node3和node4的8080端口。


第二步:安装memcached,可以安装在另外一个服务器上,这里安装在其中一个Tomcat服务器上

安装:  yum -y install memcached

启动:  service memcached start

第三步:将必要的jar包拷贝到Tomcat的lib目录中


第四步:在node3_tomcat的配置中添加内容使其与memcached关联

<ManagerclassName="de.javakaffee.web.msm.MemcachedBackupSessionManager"

         memcachedNodes="n1:192.168.204.130:11211"

   sticky="false"

   lockingMode="auto"

   sessionBackupAsync="false"

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

   sessionBackupTimeout="1000"transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" />

第五步: 修改相关配置,便于后续查看session是哪个服务器创建的

在node3_tomcat的server.xml中配置jvmRoute

<Engine name="Catalina" defaultHost="localhost" jvmRoute="client3">

在node4_tomcat的server.xml中配置jvmRoute

<Engine name="Catalina" defaultHost="localhost" jvmRoute="client4">

第六步:修改Tomcat的index.jsp内容


第七步:启动

启动nginx: service nginx start

启动memcached:service memcached start

启动两个Tomcat: /opt/apache-tomcat-7.0.61/bin/startup.sh

第八步:测试

打开浏览器,输入192.168.204.128,显示如下:


刷新,可以看到如下图:


由此可知,红框中的session以及产生session的tomcat并没有改变,改变的只是服务器,这就说明用户多次访问会显示相同的session即保持了session的一致性。

此时清除缓存或重新打开另外的浏览器访问,会产生不同的session,如下图



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
1,在tomcat中找到文件apache-tomcat-6.0.37\conf\context.xml 加入内部 <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:192.168.1.65:11211" requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$" sessionBackupAsync="false" sessionBackupTimeout="100" transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory" copyCollectionsForSerialization="false" /> 加入之后的content.xml的内容为 <?xml version='1.0' encoding='utf-8'?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <!-- The contents of this file will be loaded for each web application --> <Context> <!-- Default set of monitored resources --> <WatchedResource>WEB-INF/web.xml</WatchedResource> <!-- Uncomment this to disable session persistence across Tomcat restarts --> <!-- <Manager pathname="" /> --> <!-- Uncomment this to enable Comet connection tacking (provides events on session expiration as well as webapp lifecycle) --> <!-- <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" /> <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:192.16

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值