首页 技术 正文
技术 2022年11月18日
0 收藏 502 点赞 2,852 浏览 2963 个字

—恢复内容开始—

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链接上的文档也是没有说明使用方法。大概的情况说完了,先说说我想要达到的结果:
如图:

asp.net 分布式探讨之Session共享问题

asp.net 分布式探讨之Session共享问题

我的配置:

<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相同但是端口不同呢,我寻思着能够成功,测试:
asp.net 分布式探讨之Session共享问题

asp.net 分布式探讨之Session共享问题

嗯,成功了。那么问题来了,在真正的开发中,我们使用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

—恢复内容结束—

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,946
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,472
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,285
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,101
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,733
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,768