查看: 258|回复: 0

[.NET开发] asp.net 分布式探讨之Session共享问题

发表于 7 天前
句号论坛

---恢复内容开始---

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. <sessionState mode="Custom" customProvider="MySessionStateStore">
  2. <providers>
  3. <!-- For more details check https://github.com/Azure/aspnet-redis-providers/wiki -->
  4. <!-- Either use 'connectionString' OR 'settingsClassName' and 'settingsMethodName' OR use 'host','port','accessKey','ssl','connectionTimeoutInMilliseconds' and 'operationTimeoutInMilliseconds'. -->
  5. <!-- 'throwOnError','retryTimeoutInMilliseconds','databaseId' and 'applicationName' can be used with both options. -->
  6. <!--
  7. <add name="MySessionStateStore"
  8. host = "127.0.0.1" [String]
  9. port = "" [number]
  10. accessKey = "" [String]
  11. ssl = "false" [true|false]
  12. throwOnError = "true" [true|false]
  13. retryTimeoutInMilliseconds = "5000" [number]
  14. databaseId = "0" [number]
  15. applicationName = "" [String]
  16. connectionTimeoutInMilliseconds = "5000" [number]
  17. operationTimeoutInMilliseconds = "1000" [number]
  18. connectionString = "<Valid StackExchange.Redis connection string>" [String]
  19. settingsClassName = "<Assembly qualified class name that contains settings method specified below. Which basically return 'connectionString' value>" [String]
  20. 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]
  21. loggingClassName = "<Assembly qualified class name that contains logging method specified below>" [String]
  22. 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]
  23. redisSerializerType = "<Assembly qualified class name that implements Microsoft.Web.Redis.ISerializer>" [String]
  24. />
  25. -->
  26. <add name="MySessionStateStore" type="Microsoft.Web.Redis.RedisSessionStateProvider" host="服务器IP地址" port ="6379" accessKey="" ssl="false" />
  27. </providers>
  28. </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

---恢复内容结束---



太阳http代理AD
回复

使用道具 举报

关闭

站长推荐上一条 /1 下一条