【问题标题】:Trying to use Redis Sentinel with Net Core 2.1尝试将 Redis Sentinel 与 Net Core 2.1 一起使用
【发布时间】:2020-10-15 01:10:02
【问题描述】:

好吧,我正在使用 Net Core 2.1 和 lib Microsoft.Extensions.Caching.Redis ver 2.1.2,当我只使用一个 Redis 节点时,一切正常。

使用单个节点:

services.AddDistributedRedisCache(ops =>
            {
                ops.Configuration = "localhost:6379";
                ops.InstanceName = "master";
            });

但现在,我正在尝试将 Redis 与 Sentinel 一起使用,因此我将配置更改为(遵循本指南 https://stackexchange.github.io/StackExchange.Redis/Configuration.html):

 services.AddDistributedRedisCache(ops =>
            {
                ops.Configuration = "localhost,serviceName:mymaster";
                ops.InstanceName = "master";
            });

所以,我有一个 docker-compose.yml 来使用 sentinel 启动 redis 节点:

version: '2'
services:
  redis-master:
    image: bitnami/redis:5.0
    environment:
      ALLOW_EMPTY_PASSWORD: 'yes'
      REDIS_REPLICATION_MODE: 'master'
    ports:
      - '6379:6379'
  redis-slave1:
    image: bitnami/redis:5.0
    environment:
      ALLOW_EMPTY_PASSWORD: 'yes'
      REDIS_REPLICATION_MODE: 'slave'
      REDIS_MASTER_HOST: redis-master
    depends_on:
      - redis-master
    ports:
      - '6380:6380'
  redis-slave2:
    image: bitnami/redis:5.0
    environment:
      ALLOW_EMPTY_PASSWORD: 'yes'
      REDIS_REPLICATION_MODE: 'slave'
      REDIS_MASTER_HOST: redis-master
    depends_on:
      - redis-master
    ports:
      - '6381:6381'
  redis-sentinel:
    image: bitnami/redis-sentinel:5.0
    environment:
      REDIS_MASTER_HOST: redis-master
      REDIS_SENTINEL_DOWN_AFTER_MILLISECONDS: 10000
    ports:
      - '26379:26379'

当我启动我的应用程序时一切正常,所以我关闭了主节点以测试哨兵并在 Asp Net Core 中得到以下错误:

RedisConnectionException: SocketClosed on localhost:6379/Subscription, origin: ProcessReadBytes, input-buffer: 0, outstanding: 0, last-read: 14s ago, last-write: 14s ago, unanswered-write: 1998113s ago, keep-alive: 60s, pending: 0, state: ConnectedEstablished, in: 0, ar: 0, last-heartbeat: 0s ago, last-mbeat: 0s ago, global: 0s ago
Unknown location

RedisConnectionException: No connection is available to service this operation: EVAL; SocketClosed on localhost:6379/Subscription, origin: ProcessReadBytes, input-buffer: 0, outstanding: 0, last-read: 14s ago, last-write: 14s ago, unanswered-write: 1998113s ago, keep-alive: 60s, pending: 0, state: ConnectedEstablished, in: 0, ar: 0, last-heartbeat: 0s ago, last-mbeat: 0s ago, global: 0s ago
StackExchange.Redis.ConnectionMultiplexer.ThrowFailed<T>(TaskCompletionSource<T> source, Exception unthrownException) in ConnectionMultiplexer.cs, line 2000

好吧,如果我再次启动主节点,一切正常,但这不是预期的行为。我希望在关闭主节点后,哨兵将一个从属提升为主,我可以继续使用redis而不会出错。

我错过了什么吗?

【问题讨论】:

    标签: asp.net-core .net-core redis stackexchange.redis


    【解决方案1】:

    很抱歉回复晚了,但我们只是自己解决这个问题。连接 Sentinel 时,需要在连接字符串中指定端口:

    services.AddDistributedRedisCache(ops =>
    {
        ops.Configuration = "localhost:26379,serviceName:mymaster";
        ops.InstanceName = "master";
    });
    

    一旦 ConnectionMultiplexer 连接到 Sentinel 节点,它就会获取主节点并使用它。另外,我发现要完成这项工作需要几个设置:

    redisOptions.TieBreaker = "";
    redisOptions.AbortOnConnectFail = false;
    redisOptions.AllowAdmin = true;
    

    我们还在 .NET Core 3.1 上使用 StackExchange.Redis v2.2.4

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-20
      • 1970-01-01
      • 2017-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-28
      相关资源
      最近更新 更多