Distributed share of Session

In a previous blog post we achieve by Nginx + IIS a simple load balancing, then the question arises is how multiple sites to share information when switching Session, this introduction implement distributed by Redis in .net Session sharing site, without modifying the original project Session read mode.

ASP.Net session storage

First, we look at the advantages and disadvantages asp.net storage and can be employed in the session. as follows:

  1. InProc mode (in-process mode), the default setting. The advantage of high performance without having to read the cross-process, the disadvantage is dependent on asp.net process, when the process crashes restart will be lost and can not be shared at multiple sites distributed situation.
  2. StateServer mode (state server mode). Session state is stored in a separate process called ASP.Net state service, which ensures that when you restart your Web application will keep session state, and let the session state can be used to network multiple Web servers.
  3. SQL Server mode. Session state is stored in a SQL Server database. This ensures that when you restart your Web application will keep session state, and let the session state can be used to network multiple Web servers.
  4. Custom mode. This mode allows you to specify custom storage provider, such as a distributed mode often used in custom site will be stored in Session in Redis or Memcached.
    Benpian i.e. Custom mode using a custom program stored in the provided storage Redis the Session.

Redis store Session

Custom modules need to be modified using the mode attribute of the sessionState node configuration file web.config website is Custom, In addition, we need to write a class that implements SessionStateStoreProviderBase abstract class, you can check for Session custom additions and deletions to change by the method of abstract class, currently Microsoft has Redis version for realizing the storage, the package can be directly nuget RedisSessionStateProvider mounted as follows:
RedisSessionStateProvider
asp.net session sharing
the sessionState will redis server configuration to Redis connection configuration, then we create a Session mvc view at a plurality of test sites and read shared. Controler code is as follows:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            if (this.HttpContext.Session["MySessionTest"] == null)
            {
                this.HttpContext.Session["MySessionTest"] = string.Format("站点{0}创建的Session", Request.ServerVariables.Get("Server_Port").ToString());
            }
            return View();
        }
    
        public JsonResult GetSession()
        {
            var result = this.HttpContext.Session["MySessionTest"].ToString();
            return Json(new { isok = true, result = result }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
       
    }

In the index action, we write a session key = MySessionTest of information, and write information to the station ports value, then the method provides an interface for reading and returning session [ "MySession"]. Then we look at the view page code:

<div class="jumbotron">
    <h1>ASP.NET</h1>
    <p class="lead">ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.</p>
    <h3>
        服务器地址:@Request.ServerVariables.Get("Server_Name").ToString()
    </h3>
    <h3>
        服务器端口:@Request.ServerVariables.Get("Server_Port").ToString()
    </h3>
    <div>
        <input id="sessiontxt" /> <button onclick="GetSession()">读取Session["MySessionTest"]</button>
    </div>
</div>
    function GetSession() {
        $.ajax({
            type: "GET",
            url: "/Home/GetSession",
            xhrFields: { withCredentials: true },
            success: function (data) {
                if (data.isok == true) {
                    $("#sessiontxt").val(data.result);
                }
            }
        });

    }

Function is very simple, display the address and port information of the current site, click on the button calls the interface to read session information and output to a text box. Now we do a test, we have three new sites 8010/8020/8030 pointing to this site on IIS, and then we were visited three sites to view the session to obtain the following results:
Session sharing under load balancing
Redis share SessIon
you can see our first visit in 8010 website, enter in session 8010 Web site when creating session, acquired in 8020 when accessing the website and click the button for session session 8010 is the same, so far we have achieved Session sharing across multiple sites. I speak with in the blog post load balancing to achieve the session sharing between the various web sites distributed systems.

Previous: Nginx + IIS load balancing

Published 12 original articles · won praise 27 · views 20000 +

Guess you like

Origin blog.csdn.net/Leaderxin/article/details/104076166