【问题标题】:Configure ASP.NET Redis Session State Provider for Sentinel Configuration为 Sentinel 配置配置 ASP.NET Redis 会话状态提供程序
【发布时间】:2015-11-28 00:34:29
【问题描述】:

一段时间以来,我一直在尝试在我的应用中配置 ASP.NET Redis Session State provider。多亏了这篇文章,我终于能够成功地直接连接到主服务器并设置/获取密钥:Can't connect to Redis server using ASP.NET Session State Provider

现在,我的下一个问题...让它与 Sentinel 配置一起使用。

我熟悉SENTINEL get-master-addr-by-name master-dev-sessionstate命令判断master。这个提供者有这个内置的吗?根据上面链接的博客文章中的 cmets(这也是我能找到的唯一文档),看来我应该能够使用 connectionString 属性来传递多个主机。不过,我不确定这些多台主机是否打算成为 Sentinel。

<connectionStrings>
  <add name="RedisConnection" connectionString="1.2.3.4:5,6.7.8.9:10,abortConnect=false,ssl=false,password=XXXXXX,operationTimeoutInMilliseconds=5000"/>
</connectionStrings>

<sessionState mode="Custom" customProvider="MySessionStateStore">
  <providers>
    <clear/>
    <add name="MySessionStateStore" type="Microsoft.Web.Redis.RedisSessionStateProvider" connectionString="RedisConnection"/>
  </providers>
</sessionState>

像这样配置我的连接时,我收到此错误:

附加信息:无法连接到 redis 服务器;要创建断开连接的多路复用器,请禁用 AbortOnConnectFail。

即使我的连接字符串中只有主 IP,我也会收到此错误。正如您在上面看到的,我的连接字符串中有 abortConnect="false",这是它指示我执行的操作。无论连接字符串中是否存在此错误都会发生相同的错误。

考虑到这一点,这是我的问题......

  1. 此提供程序是否支持 Sentinel 配置?
  2. 如果是,连接字符串的正确格式是什么?
  3. 是否有人为此提供任何其他好的文档资源?除了那篇博文之外,我什至无法在 Microsoft 网站上找到任何内容。

编辑:我应该注意,这是一个自定义的本地 Redis 安装。我们没有通过 Azure 运行。

编辑:我最近尝试将我的工作配置指向 Sentinel,但我收到“没有可用于服务此操作的连接:EVAL。”这使我相信该提供商没有 Sentinel 支持。谁能证实这一点?

【问题讨论】:

    标签: c# asp.net session redis redis-sentinel


    【解决方案1】:

    这是安装 nuget 包时通常添加到 web.config 中的内容;

    sessionState mode="Custom" customProvider="MySessionStateStore">
      <providers>
        <!--
          <add name="MySessionStateStore" 
            host = "127.0.0.1" [String]
            port = "" [number]
            accessKey = "" [String]
            ssl = "false" [true|false]
            throwOnError = "true" [true|false]
            retryTimeoutInMilliseconds = "0" [number]
            databaseId = "0" [number]
            applicationName = "" [String]
            connectionTimeoutInMilliseconds = "5000" [number]
            operationTimeoutInMilliseconds = "5000" [number]
          />
        -->
        <add name="MySessionStateStore" 
             type="Microsoft.Web.Redis.RedisSessionStateProvider"
             host="127.0.0.1" 
             accessKey="" 
             ssl="false" />
      </providers>
    </sessionState>
    

    这是我们与 Azure 缓存服务器一起使用的一个。

    <sessionState mode="Custom" customProvider="DefaultSessionProvider">
      <providers>
        <add name="DefaultSessionProvider"
              type="Microsoft.Web.Redis.RedisSessionStateProvider"
              port="6380"
              host="xxxxxxxx.redis.cache.windows.net"
              accessKey="vCG........We0n="
              ssl="true"
              connectionTimeoutInMilliseconds = "5000"
              operationTimeoutInMilliseconds = "1000"
              retryTimeoutInMilliseconds="3000" />
      </providers>
    </sessionState>
    

    我们将重试超时设置为 3 秒,操作超时设置为 1 秒,这允许 3 (1000/3000=3) 次尝试后放弃。

    【讨论】:

    • 我没有在您提供的示例中看到您正在传递多个主机并尝试确定主机。我们在本地服务器上使用自定义 Redis 安装,我们没有通过 Azure 运行。
    • MS Redis 会话状态提供程序仅支持 Redis(并且仅支持一个连接)。 Sentinel 是管理一组主/从 Redis 服务器的单独层形式,需要您支持它的客户端代码(redis.io/topics/… - 第 4 点)您可以在 git 项目中进一步讨论它github.com/Azure/aspnet-redis-providers
    • 另外,web.config 部分不是 Azure 特定的。
    【解决方案2】:

    我正在使用此提供程序进行私有 redis 安装。据我了解,此提供程序使用包 StackExchange.Redis.Strongname 和 ConnectionMultiplexer 进行配置的文档。有了这个库,就可以使用所描述的configuration options。此外,此文档指出当前未实施哨兵支持 (serviceName)。

    尽管如此,我想知道为什么您需要与哨兵通信,ConnectionMultiplexer 能够解决主从设置,请参阅文档。此外,我通过关闭 redis 实例来测试这种行为并查看网络流量。请查看 ConnectionMultiplexer 文档:

    更复杂的场景可能涉及主/从设置;对于这种用法,只需指定构成该逻辑 redis 层的所有所需节点(它将自动识别主节点): ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("server1:6379,server2:6379");

    此外,我的配置设置如下所示:

     <add   name="MySessionStateStore" 
        type="Microsoft.Web.Redis.RedisSessionStateProvider"
        connectionString="XXXXXX:6379,XXXXXX:6379,XXXXXX:6379"  
        applicationName="myFancyApp"ssl="false"/>
    

    关于 MS.RedisSessionState Provider,我在 site 旁边使用了以下教程。

    【讨论】:

    • 我已经查看了此提供程序的源代码,并且当它使用 ConnectionMultiplexer 时,它不包含对 Sentinel 的支持。我已经编写了这个功能并提交了一个拉取请求。
    • 不错!因此,通过您的更改,可以在连接字符串中添加哨兵 IP 以启用当前主服务器的自我发现?
    • 是的。我希望在接下来的几周内接受这个拉取请求。
    猜你喜欢
    • 1970-01-01
    • 2015-01-14
    • 1970-01-01
    • 2012-08-23
    • 2015-03-10
    • 2014-12-24
    • 2013-05-27
    • 2011-04-27
    • 1970-01-01
    相关资源
    最近更新 更多