[MVC學習筆記]6. 使用Memcache+Cookie解決分布式系統共用登入狀態

來源:互聯網
上載者:User

標籤:

      為瞭解決單機處理的瓶頸,增強軟體的可用性,我們需要將軟體部署在多台伺服器上啟用多個二級子網域名稱以頻道化的方式,根據業務功能將網站分布部署在獨立的伺服器上,或通過負載平衡技術(如:DNS輪詢、Radware、F5、LVS等)讓多個頻道共用一組伺服器。當我們將網站程式分部到多台伺服器上後,由於Session受實現原理的局限,無法跨伺服器同步更新Session,使得登入狀態難以通過Session共用。

      我們使用MemCache+Cookie方案來解決分布式系統共用登入狀態的問題。

      Memcache伺服器本身就是一個Socket服務端,內部資料採用索引值對的形式儲存在伺服器的記憶體中,本質就是一個大型的雜湊表。資料的刪除採用惰性刪除機制。雖然Memcache並沒有提供叢集功能,但是通過用戶端的驅動程式很容易就可以實現Memcache的叢集配置。

     先簡單介紹一下Memcache的用法

1. 下載安裝Memcache(Windows平台)

    (1)將程式解壓到磁碟任意位置

    (2)進入cmd視窗,運行Memcached.exe -d install安裝服務,安裝後開啟服務視窗查看服務是否安裝成功。

    (3)直接在服務管理中啟動服務,或者使用cmd命令 net start "Memcache Server"

    (4)使用Telnet串連到Memcache控制台,驗證服務是否正常 telnet 127.0.0.1 11211

            使用stats指令查看當前Memcache伺服器狀態

2. 程式中的用法

    (1)在程式中添加 Memcached.ClientLibrary.dll 的引用

    (2)C#中操作Memcache的程式碼範例

String[] serverlist = { "192.168.1.100:11211","192.168.1.101:11211"  };// initialize the pool for memcache serversSockIOPool pool = SockIOPool.GetInstance("test");pool.SetServers(serverlist);pool.Initialize();mc = new MemcacheClient();mc.PoolName = "test";mc.EnableCompression = false;pool.Shutdown();//關閉串連池

下面我們做方案的具體實現

1. 首先在Common層中引入Memcached.ClientLibrary.dll,並封裝Memcache的協助類,MemcacheHelper

using Memcached.ClientLibrary;using System;namespace PMS.Common{   public class MemcacheHelper    {       private static readonly MemcachedClient Mc = null;       static MemcacheHelper()       {           //最好放在設定檔中           string[] serverlist = { "127.0.0.1:11211", "10.0.0.132:11211" };           //初始化池           var pool = SockIOPool.GetInstance();           pool.SetServers(serverlist);           pool.InitConnections = 3;           pool.MinConnections = 3;           pool.MaxConnections = 5;           pool.SocketConnectTimeout = 1000;           pool.SocketTimeout = 3000;           pool.MaintenanceSleep = 30;           pool.Failover = true;           pool.Nagle = false;           pool.Initialize();           // 獲得用戶端執行個體           Mc = new MemcachedClient {EnableCompression = false};       }       /// <summary>       /// 儲存資料       /// </summary>       /// <param name="key"></param>       /// <param name="value"></param>       /// <returns></returns>       public static bool Set(string key,object value)       {          return Mc.Set(key, value);       }       public static bool Set(string key, object value,DateTime time)       {           return Mc.Set(key, value,time);       }       /// <summary>       /// 擷取資料       /// </summary>       /// <param name="key"></param>       /// <returns></returns>       public static object Get(string key)       {           return Mc.Get(key);       }       /// <summary>       /// 刪除       /// </summary>       /// <param name="key"></param>       /// <returns></returns>       public static bool Delete(string key)       {           return Mc.KeyExists(key) && Mc.Delete(key);       }    }}

2. 改變使用者登入方法UserLogin,使用者登入成功後產生GUID,將此GUID存入Cookie並以GUID為鍵將登入使用者資訊序列化存入Memcache伺服器。

public ActionResult UserLogin(){    #region 驗證碼校正    var validateCode = Session["validateCode"] != null ? Session["validateCode"].ToString() : string.Empty;    if (string.IsNullOrEmpty(validateCode))        return Content("no:驗證碼錯誤!!");    Session["validateCode"] = null;    var txtCode = Request["ValidateCode"];    if (!validateCode.Equals(txtCode, StringComparison.InvariantCultureIgnoreCase))        return Content("no:驗證碼錯誤!!");    #endregion    var userName = Request["UserName"];    var userPwd = Request["PassWord"];    //查詢使用者是否存在    var user = UserService.LoadEntities(u => u.UserName == userName && u.PassWord == userPwd).FirstOrDefault();    if (user == null) return Content("no:登入失敗");    //產生一個GUID值作為Memache的鍵.    var sessionId = Guid.NewGuid().ToString();    //將登入使用者資訊儲存到Memcache中。    MemcacheHelper.Set(sessionId, SerializeHelper.SerializeToString(user), DateTime.Now.AddMinutes(20));    //將Memcache的key以Cookie的形式返回給瀏覽器。    Response.Cookies["sessionId"].Value = sessionId;    return Content("ok:登入成功");}

3. 改變登入校正控制器FilterController的OnActionExecuting方法,使其校正方式改為從Memcache伺服器中讀取Cookie中值為鍵的對象:

protected override void OnActionExecuting(ActionExecutingContext filterContext){    base.OnActionExecuting(filterContext);    //if (Session["user"] == null)    if (Request.Cookies["sessionId"] != null)    {        var sessionId = Request.Cookies["sessionId"].Value;        //根據該值查Memcache.        var obj = MemcacheHelper.Get(sessionId);        if (obj == null)        {            filterContext.Result = Redirect("/Login/Index");            return;        }        var user = SerializeHelper.DeserializeToObject<User>(obj.ToString());        LoginUser = user;        //類比出滑動到期時間.        MemcacheHelper.Set(sessionId, obj, DateTime.Now.AddMinutes(20));     }    else        filterContext.Result = Redirect("/Login/Index");}

 

     

     

[MVC學習筆記]6. 使用Memcache+Cookie解決分布式系統共用登入狀態

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.