NET Distributed System two: the CentOS system to build the Nginx load balancer (bottom)

Source: Internet
Author: User
Tags httpcontext

The previous article introduced the VMWare12 virtual machine, Linux (CentOS7) system installation, deployment Nginx1.6.3 Proxy service to do load balancing. The next step is to distribute the request through Nginx to each Web application processing service.

First, Web application development

  1. ASP. MVC5 Development

(1) Create a new MVC5 project, create a new controller, in the index method to save the current time to session["MySession"], and write cookies["mycookies" Storage host name and the current time.

Public ActionResult Index ()        {            if (this. httpcontext.session["mysession"] = = null)            {this                . httpcontext.session["mysession"] = DateTime.Now.ToString ("Yyyy-mm-dd hh:mm:ss");            }            This. HTTPCONTEXT.RESPONSE.COOKIES.ADD (New HttpCookie ("Mycookies")                {                    Expires = DateTime.Now.AddDays (1),                    Value = HttpContext.Server.MachineName + "| |" + DateTime.Now.ToString ()                });            return View ();        }

(2) Add a second method getsession to the controller, showing the value of the session and the cookie.

Public ActionResult getsession ()        {            if (this. httpcontext.session["MySession"]! = null)            {                viewbag.dd = this. httpcontext.session["MySession"]. ToString ();                Viewbag.scode = this. httpcontext.session["MySession"]. GetHashCode (). ToString ();                Viewbag.sid = this. HttpContext.Session.SessionID;            }            Viewbag.cval = system.web.httpcontext.current.request.cookies["Mycookies"]. Value;            Viewbag.cid = system.web.httpcontext.current.request.cookies["Mycookies"]. Name;            VIEWBAG.CDO = system.web.httpcontext.current.request.cookies["Mycookies"]. Domain;            return View ();        }

(3) The session and the cookie information are displayed on the page, the GetSession View code is as follows:

@{    viewbag.title = "GetSession";} 

The above implementation session and the cookie read and write, in order to verify the load balance, each request processing is consistent, the next important content, how to do load balancing how to keep the session consistent, for the ASP. NET Technology session principle here do not introduce, online search under a lot.

  2. Session Sharing Technology

. NET platform to support several session storage modes:

(1) InProc mode

Session stored in the current site in the same process, modify the Web. config or bin file updates, will cause the session to be lost. This mode is the default mode.

(2) ASPNET State mode

The ASPNET state stores the session in the status service and needs to start the ASP. Aspnet_state.exe to see the process. You also need to configure this mode in Web. config.

(3) SQL Server mode

This mode requires SQL Server configuration-related information, starts the proxy service, database accounts, and tables, and points the Web. config to the database.

(4) Third-party expansion mode

This framework uses this mode to store sessions in other storage, such as: Memcached, Redis Cache, to achieve the purpose of sharing the session. You can extend this abstract class by implementing Sessionstatestoreproviderbase in ASP. This system uses the session stored in the Redis cache, by introducing the Redissessionstateprovider component.

Install-package Microsoft.Web.RedisSessionStateProvider

  3, Nginx Service status situation

Enter the command service Nginx status at the CentOS terminal to view the situation and ensure that the service is running properly.

4. Web site Deployment

Deploy two sites for:

Site A: port is 8081,

Site B: Port is 8082,

  

Second, the function effect shows

(1) The browser accesses the index method, Http://192.168.119.128/demo, as shown below:

(2) Browser access GetSession method, Http://192.168.119.128/demo/getsession, shown as follows:

Through the above verification, the session and the cookies obtained are consistent.

Author: Andon
Source: Http://www.cnblogs.com/Andon_liu
About the focus on Microsoft Platform Project architecture, management. Familiar with design patterns, domain drivers, architecture design, agile development and project management. is mainly engaged in ASP, Wcf/web API, SOA, MSSQL, Redis aspects of project development, architecture, management work. If you have questions or suggestions, please learn to discuss together!
This article is copyrighted by the author and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link.
If you have questions, you can email: [Email protected] contact me, thank you.

The second of the

Net Distributed system: The CentOS system builds nginx load balancer (bottom)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.