.Net Core添加分布式Session
一、Session
HTTP是一個無狀態協議,Web服務器將每一個請求都視為獨立請求。并且不保存之前請求中用戶的值。
Session 狀態是ASP.NET Core提供的一個功能,它可以在用戶通應用訪問網絡服務器的時候保存和存儲用戶數據。ASP.NET Core通過包含Session ID的Cookie來維護會話狀態,每個請求都會攜帶此Session ID。
實現分布式Session方法官方提供有Redis、Sql Server等。但是Sql Server效率對于這種以key/value獲取值的方式遠遠不及Redis效率高,所以這里選用Redis來作示例實現分布式Session。
二、安裝Redis(Docker方式)
2.1、新建一個Dockerfile和一個redis.conf
Dockerfile內容:
FROM redis COPY redis.conf /usr/local/etc/redis/redis.conf CMD [ "redis-server", "/usr/local/etc/redis/redis.conf" ]redis.conf內容:主要是啟用密碼登陸redis
protected-mode yes port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize no supervised no pidfile /var/run/redis_6379.pid loglevel notice logfile "" databases 16 always-show-logo yes save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb dir ./ slave-serve-stale-data yes slave-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100 requirepass wangjun1234 lazyfree-lazy-eviction no lazyfree-lazy-expire no lazyfree-lazy-server-del no slave-lazy-flush no appendonly no appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes aof-use-rdb-preamble no lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-size -2 list-compress-depth 0 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit slave 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 aof-rewrite-incremental-fsync yes2.1、構建docker鏡像(docker build -t redis_test .)
2.1、運行容器(docker run -d --restart=always -p 6379:6379 --name redis_container redis_test)
三、開始建立Core Demo
3.1、新建2個MVC項目
?dotnet new mvc --name Session-demo-1
?dotnet new mvc --name Session-demo-2
?
3.2、2個項目都添加(Microsoft.AspNetCore.DataProtection.Redis和Microsoft.Extensions.Caching.Redis)nuget包
dotnet add package Microsoft.Extensions.Caching.Redis
dotnet add package Microsoft.AspNetCore.DataProtection.Redis
3.3、修改Startup.cs
在ConfigureServices中添加:
public void ConfigureServices(IServiceCollection services){//分布式sessionvar redis = ConnectionMultiplexer.Connect("47.107.30.29:6379,password=f***,defaultdatabase=7");services.AddDataProtection().SetApplicationName("Test").PersistKeysToRedis(redis, "Test-Keys");services.AddDistributedRedisCache(options => {options.Configuration = "47.107.30.29:6379,password=***,defaultdatabase=7";options.InstanceName = "session";});services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);services.AddSession(options =>{options.IdleTimeout = TimeSpan.FromHours(2);});}在Configure中添加
app.UseSession();只配置Session-demo-1,同時啟動2個項目
配置Session-demo-2(startup.cs的配置一摸一樣),同時啟動項目
源碼:https://github.com/WangJunZzz/Core-Session.git
轉載于:https://www.cnblogs.com/WJ--NET/p/10341702.html
總結
以上是生活随笔為你收集整理的.Net Core添加分布式Session的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 算法 记忆化搜索
- 下一篇: python测试开发django-1.开