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鏈接上的文檔也是沒有說明使用方法。大概的情況說完了,先說說我想要達到的結果:
如圖:

 

我的配置:

<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

---恢復內容結束---


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM