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