asp.net使用redis存储session(RedisSessionStateProvider)
2017-03-11 15:23
561 查看
近日一直在尝试建立一个真正高可用的负载平衡的方式,可以大大缩减因程序版本更新/服务器维护/突发异常等等导致的服务停止时间.要让web站点实现真正的高可用,势必要进行session共享,防止某一服务器down掉时session也跟着丢失.
通过不断的Google,最终我选择使用微软提供的RedisSessionStateProvider进行session的托管.
使用这个方式的优点:
1.无需自己扩展session provider,对自己代码能力不够自信的小伙伴们放心选择~
2.无需修改现有代码,只需透过web.config配置即可.
这个方式使用起来非常简单,通过nuget安装RedisSessionStateProvider组件,然后修改web.config即可.
1.通过nuget安装RedisSessionStateProvider
2.修改web.config配置的Host与port节点,并将ssl改成false.
3.测试
页面上取得的sessionid与redis存储的一致,证明我们的session数据已经可以顺利存到redis啦.
页面上取到的session Id
redis上的数据
通过不断的Google,最终我选择使用微软提供的RedisSessionStateProvider进行session的托管.
使用这个方式的优点:
1.无需自己扩展session provider,对自己代码能力不够自信的小伙伴们放心选择~
2.无需修改现有代码,只需透过web.config配置即可.
这个方式使用起来非常简单,通过nuget安装RedisSessionStateProvider组件,然后修改web.config即可.
1.通过nuget安装RedisSessionStateProvider
2.修改web.config配置的Host与port节点,并将ssl改成false.
<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="192.168.0.97" port="6379" accessKey="" ssl="false" /> </providers> </sessionState>
3.测试
页面上取得的sessionid与redis存储的一致,证明我们的session数据已经可以顺利存到redis啦.
页面上取到的session Id
redis上的数据
相关文章推荐
- Asp.net Session保存到Redis: 使用 RedisSessionStateProvider
- Haproxy+asp.net +RedisSessionStateProvider 完美实现负载均衡,并且session保持
- [转]Asp.net Core 使用Redis存储Session
- Asp.net Core 使用Redis存储Session
- 分布式系统状态下redis存储asp.net session使用第三方Providers驱动
- Asp.net Core 使用Redis存储Session
- 详解Asp.net Core 使用Redis存储Session
- ASP.NET Core 使用Redis存储Session
- ASP.NET Core 使用Redis存储Session
- Asp.net Core 使用Redis存储Session
- ASP.NET 使用mode=”InProc”方式保存Session老是丢失,无奈改成StateServer 模式。
- (WebSite----Asp.Net Configuration----->无法连接到SQL Server数据库------>选择数据存储区---->应用程序当前被配置为使用提供程序:AspNetSqlProvider)解决方案
- 通过ASP.NET StateServer实现进程外Session存储
- ASP.NET下跨应用共享Session和使用Redis进行Session托管
- ASP.NET Core 使用 Redis 和 Protobuf 进行 Session 缓存
- ASP.NET 使用mode=”InProc”方式保存Session老是丢失,无奈改成StateServer 模式。
- 使用ASP.NET State Service保存Session
- asp.net SessionState模式的配置及使用
- ASP.NET MVC 使用Redis共享Session
- Asp.Net使用Memcached存储Session并实现二级域名共享(mysql数据库)