(9) Net Core project uses Session and Redis for distributed

1. Introduction


1. Because Net Core does not start the Session function by default, if you need to use it, you need to open it through code.

2. This article explains that if the default Session implementation is enabled, the Session is stored in memory.

3. This article expands on how to use Redis as a Sessoin server to achieve distributed.

 

2. Turn on the default Session function


1. Write an api, first write the Session, and then read the Session to return.

2. Startup does not start the Session function, check the effect, and report an error.

3. Add two places to enable Session code in Startup, check the effect, normal. There are knowledge points here, UseSession needs to be written before UseMvc, otherwise an error will be reported.

4. UseSession is written in one of UseMvc, and then check the effect and report an error.

 

api code

    public class OneController : Controller
    {
        public string GetString(string id)
        {
            HttpContext.Session.SetString(id, Guid.NewGuid().ToString());
            return HttpContext.Session.GetString(id);
        }
    }

 

Session is not enabled, check the effect

 

 

Enable Session, the code is simple, just type it yourself.

 

View the effect

 

 

 UseSession to put it later

 

 Check the results, wrong again

 

 3. Use Redis as Session service


 1. Net Core has already implemented the Redis implementation of Session by default, and it only needs to be turned on.

2. First download and install Redis, if not, just use the landlord's, connect it in the code, just for testing. Redis does not require a username and password by default.

3. Once again, you need to download a Redis client, I use RedisDesktopManager

4. Add Redis configuration information in Startup.

5. View the running effect: the difference between the Redis database before and after the session is created.

 

Startup configuration code

        public void ConfigureServices(IServiceCollection services)
        {
            // Just this line of code will do. 
            services.AddDistributedRedisCache(option => option.Configuration = " bitdao.cn:1012,abortConnect=false,connectRetry=3,connectTimeout=3000,defaultDatabase=1,syncTimeout=3000,version=3.2.1,responseTimeout=3000 " );
            services.AddSession();
            services.AddMvc();
        }

 

Before Session is created

 

 Access API to create Session

 

After creating Session

 

 

Fourth, the significance of Redis as a Session service


1. The principle of Session is to store a SessionId in Cookies.

2. During distributed deployment, when server A writes a session, it only exists in the memory of server A. When the user requests for the second time, the server B may be accessed, and only when the SessionId is used to read the Session is a null value.

3. If Redis is used as the Session server, no matter which server the user access is assigned to, the SessionId will be used to retrieve the Session from Redis, and the value can be retrieved.

See picture below

V. Summary


 1. Net Core does not start Sessoin by default, and needs to be properly configured to start Session

2. In distributed deployment, Redis can be used as the session server, Net Core has been implemented, and only one line of configuration code is required.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325255575&siteId=291194637