1. 程式人生 > >負載均衡+session共享(memcached-session-manager實現)

負載均衡+session共享(memcached-session-manager實現)

前言

  先給大傢伙拜個年,祝大家:新的一年健健康康,平平安安!    

  本文的形成參考了很多人的部落格,最多的應該是青蔥歲月兄的這篇部落格,大家可以先去看下,熟悉一些內容,因為本文是直接實踐,一些理論性的知識就需要大家自己去補充了。

  本文是在我之前環境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共享還有其他的實現方式,希望各位小夥伴發散思維,多多查閱資料,有更好的見解,可以在評論區留言!

後話

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

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