无法连接到redis服务器; 创建断开的多路复用器

Lui*_*cia 29 c# azure azure-redis-cache

我有以下代码连接到azure redis缓存.

   public class CacheConnectionHelper
    {
        private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
        {
            return ConnectionMultiplexer.Connect(SettingsHelper.AzureRedisCache);
        });

        public static ConnectionMultiplexer Connection
        {
            get
            {
                return lazyConnection.Value;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

我用这种方式使用它

public static List<Models.Module> GetModules()
        {
            IDatabase cache = CacheConnectionHelper.Connection.GetDatabase();
            List<Models.Module> listOfModules = new List<Models.Module>();
            listOfModules = (List<Models.Module>)cache.Get("ApplicationModules");
            if (listOfModules == null)
            {
                listOfModules = dbApp.Modulos.ToList();
                cache.Set("ApplicationModules", listOfModules, TimeSpan.FromMinutes(SettingsHelper.CacheModuleNames));
                return listOfModules;
            }
            else {
                return listOfModules;
            }
        }
Run Code Online (Sandbox Code Playgroud)

但是每天1或2次我得到这个例外:

 Additional information: It was not possible to connect to the redis server(s); to create a disconnected multiplexer, disable AbortOnConnectFail. UnableToResolvePhysicalConnection on PING
Run Code Online (Sandbox Code Playgroud)

问题是如何在缓存连接失败的情况下重构此代码以转到数据库?

Jon*_*ole 51

您获得的错误通常表明您没有在连接字符串中设置abortConnect = false.为默认值abortConnect,这使得它使StackExchange.Redis不会在某些情况下重新连接到服务器自动.我们强烈建议您在连接字符串中设置abortConnect = false,以便SE.Redis在后台发生网络闪烁时自动重新连接.

  • 谢谢,这个连接字符串对我有用 172.17.0.2:12000,abortConnect=false (3认同)

dan*_*man 13

对于潜入其他代码的初学者来说,面对这个问题:

if (RedisConn == null)
        { 
            ConfigurationOptions option = new ConfigurationOptions
            {
                AbortOnConnectFail = false,
                EndPoints = { redisEndpoint }
            };
            RedisConn = ConnectionMultiplexer.Connect(option);
        }
Run Code Online (Sandbox Code Playgroud)

  • 找到了,是ConnectRetry的ConfigurationOption设置为重试次数。不确定默认设置为什么。 (2认同)
  • ConnectRetry 的默认值为 3 - https://stackexchange.github.io/StackExchange.Redis/Configuration.html (2认同)

Zé *_*los 6

您还应该注意错误消息的最后部分,因为它似乎提供了有关连接失败原因的非常有用的详细信息。

在你的情况下:

无法连接到 Redis 服务器;要创建断开连接的多路复用器,请禁用 AbortOnConnectFail。PING 上无法解析物理连接

我的情况:

无法连接到 Redis 服务器;要创建断开连接的多路复用器,请禁用 AbortOnConnectFail。暂停