asp.net使用redis存储session(RedisSessionStateProvider)

本文介绍了一种通过Redis实现Web应用高可用性的方法,重点在于使用RedisSessionStateProvider进行session托管,无需修改现有代码,仅需配置web.config即可实现session共享,确保服务器故障时session不丢失。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

近日一直在尝试建立一个真正高可用的负载平衡的方式,可以大大缩减因程序版本更新/服务器维护/突发异常等等导致的服务停止时间.要让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.

<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上的数据
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值