负载均衡+session共享(memcached-session-manager实现)

jopen 8年前

本文的形成参考了很多人的博客,最多的应该是青葱岁月兄的这篇博客,大家可以先去看下,熟悉一些内容,因为本文是直接实践,一些理论性的知识就需要大家自己去补充了。

本文是在我之前环境 LVS + keepalived + nginx + tomcat 实现主从热备 + 负载均衡 基础上进行的,所以很多环境的搭建本文就不涉及了,没看的朋友可以先去看下。

由于我大天朝对国外网站的限制,memcached的相关jar文件不太好下载,这里给上我的 memcached的网盘地址 ,为需要的朋友提供方便。

环境准备与配置

VIP(Virtual IP)为192.168.1.200,用户只需要访问这个IP地址即可获得网页服务

负载均衡主机为192.168.1.114(master) ----》keepalived

备机为 192.168.1.112(brucelee) ----》keepalived

Web服务器A为192.168.1.111(youzhibing) ----》realserver + nginx + tomcat + memcached

Web服务器B为 192.168.1.115(youzhibing03) ----》realserver + nginx + tomcat + memcached

目前差的就是memcached的安装和配置了。

1.memcached安装

1.1 依赖安装

memcached依赖libevent,我们需要先安装libevent,这里我就为了省事,直接用yum全依赖安装了:yum -y install libevent-devel(可能安装不上,提示找不到镜像站点,那么就用源码安装,源码包我的网盘中已共享了)。

1.2 源码安装

memcache的安装则采用源码方式安装,源码包也已分享

解压:tar -zxf memcached-1.4.25.tar.gz

configure:cd memcached-1.4.25,然后./configure --prefix=/usr/local/memcached (若libevent采用的是源码安装,那么则是./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent(libevent安装目录))

make && make install

没有异常的话,那么memcached已经安装成功了!

2.memcached-session-manager配置

2.1 tomcat集成

memcached-session-manager作为tomcat的拓展,那么只需要将相关的jar包copy到tomcat的lib下即可,不同版本的tomcat对应的jar包的版本有所不同,大家不要搞错了(我用的是tomcat7)。

将上图中的jar全部拷贝到自己的tomcat目录的lib下。

2.2 配置文件修改

本文实现的是memcached来管理黏非黏性session(黏性session的配置与非黏性session的配置只有些许差别),修改tomcat下conf中的context.xml配置文件,内容如下( 集群中的所有tomcat都是用同一个配置,context.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.168.1.111:11211,n2:192.168.1.115:11211"  <!-- ip需要改成自己tomcat服务器的ip -->          sticky="false"            sessionBackupAsync="false"            lockingMode="uriPattern:/path1|/path2"            requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"            transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"        />   </Context>

至此配置就完成了,别配漏了就行!

3.服务启动

启动顺序:tomcat --》 nginx --》memcached

realserver启动时机没要求,但是别忘记启动了!

keepalived启动。

效果展示

因为上传图片有大小限制,我录的gif图片太大,那么我也就将效果展示图放入的我的网盘共享中了。

同一个浏览器的sessionid相同,不论你开多少个(甚至是你全关了,再开);

不同的浏览器sessionid不同;

不同的电脑那肯定就不用说了,肯定不同!

总结

最终的效果达到了,配置的过程也出现了各种问题,最后都解决了。session共享一直是负载均衡、集群关注的一个重点,是各位小伙伴需要掌握的一个重点!

session共享还有其他的实现方式,希望各位小伙伴发散思维,多多查阅资料,有更好的见解,可以在评论区留言!

后话

很长时间没有更新博客了,这里表示抱歉了;这两天我会有所更新,各位小伙伴记得来看哦!

最后还是那句话:有些许的冲动就赶紧实践!

来自: http://www.cnblogs.com/youzhibing/p/5094460.html