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

It was not possible to connect to the redis server(s); to create a disconnected multiplexer

我有以下代码连接到 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;
            }
        }
    }

我也是这样使用的

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;
            }
        }

但是我每天遇到 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

问题是如果缓存连接失败,我如何重构这段代码以转到数据库?

您遇到的错误通常表示您没有在连接字符串中设置 abortConnect=falseabortConnect 的默认值是 true,这使得 StackExchange.Redis 不会'在某些情况下不会自动重新连接到服务器。我们强烈建议您在连接字符串中设置 abortConnect=false 以便 SE.Redis 将在后台自动重新连接出现光点。

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

你的情况:

无法连接到 redis 服务器;要创建断开连接的多路复用器,请禁用 AbortOnConnectFail。 PING 上的 UnableToResolvePhysicalConnection

我的情况:

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

对于深入研究他人代码的初学者来说,会遇到这个问题:

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

此问题已在新版本 1.2.6 中解决 - 您可以在 Here

中看到

对于那些维护旧代码库的人,您可以 运行 进入 "It was not possible to connect to the redis server(s); to create a disconnected multiplexer, disable AbortOnConnectFail. UnableToResolvePhysicalConnection on PING"

升级到更新的 nuget 包后,错误仍然存​​在,但我得到了更多错误信息:"The client and server cannot communicate, because they do not possess a common algorithm"。

一旦我应用了提到的注册表项 here I was ok. For those that don't wish to make that global change I believe there has been a PR 进行设置。

我通过将连接字符串从 localhost:6379 更改为 127.0.0.1:6379

来解决此问题

对我来说,连接字符串不正确。添加正确的连接字符串详细信息适用于 stackexchange.redis 2.1.58

你可以这样使用它。

  public class RedisService
    {
        private readonly string _host;
        private readonly int _port;
        private ConnectionMultiplexer _connectionMultiplexer;

        public RedisService(string host, int port)
        {
            _host = host;
            _port = port;
        }

        public void Connect() => _connectionMultiplexer = ConnectionMultiplexer.Connect($"{_host}:{_port}");
}

enter image description here

我通过更改解决了这个问题:

CacheSettings:ConnectionString=basketdb:6379

CacheSettings__ConnectionString=basketdb:6379

docker-compose.override.yml

请确保您已经设置了 Redis。以下是相应 OS:

的说明链接

Mac OS: https://gist.github.com/tomysmile/1b8a321e7c58499ef9f9441b2faa0aa8

Windows: https://dev.to/divshekhar/how-to-install-redis-on-windows-10-3e99