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

前言

  先给你们伙拜个年,祝你们:新的一年健健康康,平平安安!    css

  本文的造成参考了不少人的博客,最多的应该是青葱岁月兄的这篇博客,你们能够先去看下,熟悉一些内容,由于本文是直接实践,一些理论性的知识就须要你们本身去补充了。html

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

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

环境准备与配置

  VIP(Virtual IP)为192.168.1.200,用户只须要访问这个IP地址便可得到网页服务web

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

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

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

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

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

  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已经安装成功了!

  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>

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

  服务启动

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

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

    keepalived启动。

效果展现

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

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

  不一样的浏览器sessionid不一样;

  不一样的电脑那确定就不用说了,确定不一样!

总结

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

  session共享还有其余的实现方式,但愿各位小伙伴发散思惟,多多查阅资料,有更好的看法,能够在评论区留言!

后话

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

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

相关文章
相关标签/搜索