asp.net 分布式探讨之Session共享问题
---恢复内容开始---
Session共享是分布式架构设计中的一大难点,尽管session共享的解决方案不少,但是.net 下的解决方案还是比较少,而且说明文档也很少。
之前尝试用memcached缓存session,以解决session共享问题,后来发现实在是没有解决方案,github上有一个MemcachedSessionProvider,但是我并没有成功,还在博客园的博问上说了这个问题,但是至今无人回我,链接 asp.net MemcachedSessionProvider 如何实现session共享?
于是我尝试了redis来解决session共享问题,我才用的解决方案是微软提供的redissessionstateprovider,github地址如下: https://github.com/Azure/aspnet-redis-providers/blob/master/README.md
一开始我就查了很多关于redissessionstateprovider 的资料,但是都是很浅的说明,并没有达到我想要的结果,github链接上的文档也是没有说明使用方法。大概的情况说完了,先说说我想要达到的结果:
如图:
我的配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <sessionState mode= "Custom" customProvider= "MySessionStateStore" > <providers> <!-- For more details check https: //github.com/Azure/aspnet-redis-providers/wiki --> <!-- Either use 'connectionString' OR 'settingsClassName' and 'settingsMethodName' OR use 'host' , 'port' , 'accessKey' , 'ssl' , 'connectionTimeoutInMilliseconds' and 'operationTimeoutInMilliseconds' . --> <!-- 'throwOnError' , 'retryTimeoutInMilliseconds' , 'databaseId' and 'applicationName' can be used with both options. --> <!-- <add name= "MySessionStateStore" host = "127.0.0.1" [String] port = "" [number] accessKey = "" [String] ssl = "false" [ true | false ] throwOnError = "true" [ true | false ] retryTimeoutInMilliseconds = "5000" [number] databaseId = "0" [number] applicationName = "" [String] connectionTimeoutInMilliseconds = "5000" [number] operationTimeoutInMilliseconds = "1000" [number] connectionString = "<Valid StackExchange.Redis connection string>" [String] settingsClassName = "<Assembly qualified class name that contains settings method specified below. Which basically return 'connectionString' value>" [String] settingsMethodName = "<Settings method should be defined in settingsClass. It should be public, static, does not take any parameters and should have a return type of 'String', which is basically 'connectionString' value.>" [String] loggingClassName = "<Assembly qualified class name that contains logging method specified below>" [String] loggingMethodName = "<Logging method should be defined in loggingClass. It should be public, static, does not take any parameters and should have a return type of System.IO.TextWriter.>" [String] redisSerializerType = "<Assembly qualified class name that implements Microsoft.Web.Redis.ISerializer>" [String] /> --> <add name= "MySessionStateStore" type= "Microsoft.Web.Redis.RedisSessionStateProvider" host= "服务器IP地址" port = "6379" accessKey= "" ssl= "false" /> </providers> </sessionState> |
原本是打算这么设计的,后来发现服务器不够(我只有一个linux和一个windows服务器)于是改变了方案,直接在本地上测试,本地iis配置两个不同的ip,不同的端口给两个网站,但是在redissessionstateprovider的配置文件中给出的redis地址,我配置的是远程windows的公网ip,一次来测试,看看测试结果:
嗯,失败了,如果是IP相同但是端口不同呢,我寻思着能够成功,测试:
嗯,成功了。那么问题来了,在真正的开发中,我们使用Nginx来做负载均衡,肯定是使用的不同ip的服务器,那么此时如何共享session, 本质问题还是没有解决啊!难道是我的理解有误?还是说我打开redissessionstateprovider的方式不对?随后一直查资料,愣是没有找到解决方法。而且,redis中并没有写入session,但是同一IP下的session却写入成功了,这就有意思了。
之所以写这篇文章,一方面,分享下asp.net下的session共享解决方案,另一方面希望大家帮忙解决下我的疑惑:如何在不同的IP下实现session共享
参考资料:
https://blogs.msdn.microsoft.com/webdev/2014/05/12/announcing-asp-net-session-state-provider-for-redis-preview-release/
http://www.cnblogs.com/newP/p/6518918.html
http://www.cnblogs.com/sunyj/p/5413495.html
https://www.cnblogs.com/weiweictgu/p/5776124.html
---恢复内容结束---
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 推荐几款开源且免费的 .NET MAUI 组件库
· 实操Deepseek接入个人知识库
· 易语言 —— 开山篇
· 【全网最全教程】使用最强DeepSeekR1+联网的火山引擎,没有生成长度限制,DeepSeek本体