負載均衡,這應該是一個永恆的話題,也是一個十分重要的話題。畢竟當網站成長到一定程度,訪問量自然也是會跟着增長,這個時候,
一般都會對其進行負載均衡等相應的調整。現如今最常見的應該就是使用Nginx來進行處理了吧。當然Jexus也可以達到一樣的效果。既然是
負載均衡,那就勢必有多台服務器,如果不對session進行處理,那么就會造成Session丟失的情況。有個高大上的名字叫做分布式Session。
舉個通俗易懂的例子,假設現在有3台服務器做了負載,用戶在登陸的時候是在a服務器上進行的,此時的session是寫在a服務器上的,那
么b和c兩台服務器是不存在這個session的,當這個用戶進行了一個操作是在b或c進行處理的,而且這個操作是要登錄后才可以的,那么就會
背景交待完畢,簡單的實踐一下。
相關技術 | 說明 |
ASP.NET Core | 演示的兩個站點所用的技術 |
Redis | 用做Session服務器 |
Nginx/Jexus | 用做反向代理服務器,演示主要用了Nginx,最后也介紹了Jexus的用法 |
IIS/Jexus | 用做應用服務器,演示用了本地的IIS,想用Jexus來部署可參考前面的相關文章 |
先來看看不進行Session處理的做法,看看Session丟失的情況,然后再在其基礎上進行改善。
在ASP.NET Core中,要使用session需要在Startup中的ConfigureServices添加 services.AddSession(); 以及在Configure中添加
app.UseSession(); 才能使用。在控制器中的用法就是 HttpContext.Session.XXX ,下面是演示控制器的具體代碼:
1 [HttpGet("/")] 2 [ResponseCache(NoStore =true)] 3 public IActionResult Index() 4 { 5 ViewBag.Site = "site 1"; 6 return View(); 7 } 8 [HttpPost("/")] 9 public IActionResult Index(string sessionName,string sessionValue) 10 { 11 //set the session 12 HttpContext.Session.Set(sessionName,System.Text.Encoding.UTF8.GetBytes(sessionValue)); 13 return Redirect("/about?sessionName="+sessionName); 14 } 15 16 [HttpGet("/about")] 17 [ResponseCache(NoStore = true)] 18 public IActionResult About(string sessionName) 19 { 20 byte[] bytes; 21 ViewBag.Site = "site 1"; 22 //get the session 23 if (HttpContext.Session.TryGetValue(sessionName, out bytes)) 24 { 25 ViewBag.Session = System.Text.Encoding.UTF8.GetString(bytes); 26 } 27 else 28 { 29 ViewBag.Session = "empty"; 30 } 31 return View(); 32 }
其中的ViewBag.Site是用來標識當前訪問的是那個負載的站點。這用就不用去查日記訪問了那個站點了,直接在頁面上就能看到了。從
Session的用法也看出了與之前的有所不同,Session的值是用byte存儲的。我們可以寫個擴展方法把它封裝一下,這樣就方便我們直接向之
視圖比較簡單,一個寫Session,一個讀Session。Index.cshtml用於填寫Session的信息,提交后跳轉到About.cshtml。

1 @{ 2 ViewData["Title"] = "Home Page"; 3 } 4 <div class="row"> 5 <div class="col-md-6"> 6 <form method="post" action="/"> 7 <div class="form-group"> 8 <label>session name</label> 9 <input type="text" name="sessionName" /> 10 </div> 11 <div class="form-group"> 12 <label>session value</label> 13 <input type="text" name="sessionValue" /> 14 </div> 15 <button type="submit">set session</button> 16 </form> 17 </div> 18 </div> 19 <div class="row"> 20 <div class="col-md-6"> 21 <p> 22 site: @ViewBag.Site 23 </p> 24 </div> 25 </div>

1 @{ 2 ViewData["Title"] = "About"; 3 } 4 <p>@ViewBag.Session </p> 5 <p>site:@ViewBag.Site</p>
到這里,我們是已經把我們要的“網站”給開發好了,下面是把這個“網站”部署到IIS上面。我們要在IIS上部署兩個站點,這兩個站點用於
這時可以參考dotNET Core的文檔,至於為什么沒有放到Linux下呢,畢竟是台老電腦了,開多個虛擬機電腦吃不消,雲服務器又還沒想好要
租那家的,所以只好放到本地的IIS上來演示了,想在Linux下部署ASP.NET Core可以參考我前面的博文,也是很簡單的喔。
這是部署到本地IIS上面的兩個站點,site1和site2。
Nginx的負載均衡模塊,更多的細節可以去它的官網看一下。這里就不做詳細的介紹,畢竟這些配置都十分的簡單。
Nginx的配置也配好了,接下來就是啟動我們的Nginx服務器,執行 /usr/local/nginx/sbin/nginx 即可,最后就是訪問我們Nginx這個
空殼站點http://192.168.198.128:8033(實際是訪問我們在IIS上的那2個站點),然后就可以看看效果了,建議把瀏覽器的緩存禁用掉,不然
輪詢的效果可能會出不來。
可以看到輪詢的效果已經出來了,訪問Linux下面的Nginx服務器,實際上是訪問IIS上的site1和site2。我們是在站點2 設置了session,
但是在站點2卻得不到這個session值,而是在站點1才能得到這個值。這是因為我們用的算法是Nginx默認的輪詢算法,也就是說它是一直這樣
循環訪問我們的站點1和站點2,站點1->站點2 ->站點1->站點2....,演示是在站點2設置Session並提交,但它是提交到了站點1去執行,執行
完成后Redirect到了站點2,所以會看到站點2上沒有session的信息而站點1上面有。
好了,警報提醒,Session丟失了,接下來我們就要想辦法處理了這個常見並且棘手的問題了, 本文的處理方法是用Redis做一台單獨的
Session服務器,用這台服務器來統一管理我們的Session,當然這台Redis服務器會做相應的持久化配置以及主從或Cluster集群,畢竟沒人能
保證這台服務器不出故障。思路圖如下:
在上面例子的基礎上,添加一個RedisSession類,用於處理Session,讓其繼承ISession接口
1 using Microsoft.AspNetCore.Http; 2 using System; 3 using System.Collections.Generic; 4 using System.Threading.Tasks; 5 6 namespace AutoCompleteDemo.Common 7 { 8 public class RedisSession : ISession 9 { 10 private IRedis _redis; 11 public RedisSession(IRedis redis) 12 { 13 _redis = redis; 14 } 15 16 public string Id 17 { 18 get 19 { 20 return Guid.NewGuid().ToString(); 21 } 22 } 23 24 public bool IsAvailable 25 { 26 get 27 { 28 throw new NotImplementedException(); 29 } 30 } 31 32 public IEnumerable<string> Keys 33 { 34 get 35 { 36 throw new NotImplementedException(); 37 } 38 } 39 40 public void Clear() 41 { 42 throw new NotImplementedException(); 43 } 44 45 public Task CommitAsync() 46 { 47 throw new NotImplementedException(); 48 } 49 50 public Task LoadAsync() 51 { 52 throw new NotImplementedException(); 53 } 54 55 public void Remove(string key) 56 { 57 _redis.Del(key); 58 } 59 60 public void Set(string key, byte[] value) 61 { 62 _redis.Set(key, System.Text.Encoding.UTF8.GetString(value),TimeSpan.FromSeconds(60)); 63 } 64 65 public bool TryGetValue(string key, out byte[] value) 66 { 67 68 string res = _redis.Get(key); 69 if (string.IsNullOrWhiteSpace(res)) 70 { 71 value = null; 72 return false; 73 } 74 else 75 { 76 value = System.Text.Encoding.UTF8.GetBytes(res); 77 return true; 78 } 79 } 80 } 81 }
會有一個過期的時間,這里默認給了60秒,真正實踐的時候可能要結合SessionOptions來進行修改這里的代碼。前面也提到寫個擴展方法,可以減少
調用的代碼量和方便我們的使用,所以還寫了一個對Session的擴展,方便在控制器中使用,這樣就不用每次都把要存的東西再處理成byte。
1 public static class SessionExtension 2 { 3 public static string GetExtension(this ISession session, string key) 4 { 5 string res = string.Empty; 6 byte[] bytes; 7 if (session.TryGetValue(key, out bytes)) 8 { 9 res = System.Text.Encoding.UTF8.GetString(bytes); 10 } 11 return res; 12 } 13 public static void SetExtension(this ISession session, string key,string value) 14 { 15 session.Set(key, System.Text.Encoding.UTF8.GetBytes(value)); 16 } 17 }
下面是修改之后控制器的代碼:
1 using AutoCompleteDemo.Common; 2 using Microsoft.AspNetCore.Http; 3 using Microsoft.AspNetCore.Mvc; 4 5 namespace AutoCompleteDemo.Controllers 6 { 7 public class SessionController : Controller 8 { 9 private ISession _session; 10 public SessionController(ISession session) 11 { 12 _session = session; 13 } 14 15 [HttpGet("/")] 16 [ResponseCache(NoStore =true)] 17 public IActionResult Index() 18 { 19 ViewBag.Site = "site 1"; 20 return View(); 21 } 22 [HttpPost("/")] 23 public IActionResult Index(string sessionName,string sessionValue) 24 { 25 //set the session 26 _session.SetExtension(sessionName, sessionValue); 27 return Redirect("/about?sessionName="+sessionName); 28 } 29 30 [HttpGet("/about")] 31 [ResponseCache(NoStore = true)] 32 public IActionResult About(string sessionName) 33 { 34 //get the session 35 ViewBag.Session = _session.GetExtension(sessionName); 36 ViewBag.Site = "site 1"; 37 return View(); 38 } 39 } 40 }
也整潔了不少。是直接用了自己寫的擴展方法。
視圖沒有變化。Nginx的配置也沒有變化。下面是對session進行一番簡單處理后的效果。
可以看到無論在那個站點,都能正常的讀取到session服務器里面的值。也就是說,經過簡單的初步處理,我們的Session在負載均衡下面已經
不會丟失了。當然這個只能說是一個雛形,還有更多的細節要去完善。
文中講到用Jexus也可以完成同樣的功能,下面就簡單說一下它的配置:

這樣就可以完成和上面演示中同樣的功能。
當然,對於分布式Session的管理,這只是其中一種解決方法--基於Redis的解決方案,還有許多前人總結出來的方案,好比孤獨俠客前輩的
這篇博客總結了6種方案:http://www.cnblogs.com/lonely7345/p/3796488.html,都是值得我們這些小輩去學習和研究的。
源碼已上傳到github:
https://github.com/hwqdt/Demos/tree/master/src/RedisDemo