負載均衡+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共享還有其餘的實現方式,但願各位小夥伴發散思惟,多多查閱資料,有更好的看法,能夠在評論區留言!

後話

  很長時間沒有更新博客了,這裏表示抱歉了;這兩天我會有所更新,各位小夥伴記得來看哦!

  最後仍是那句話:有些許的衝動就趕忙實踐!

相關文章
相關標籤/搜索