尝试将redis sentinel与netcore2.1结合使用

kmbjn2e3  于 2021-06-10  发布在  Redis
关注(0)|答案(0)|浏览(220)

嗯,我在用 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'

启动应用程序时,一切正常,因此我关闭主节点以测试sentinel,并在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

好吧,如果我再次启动主节点,一切正常,但这不是预期的行为。我希望在关闭主节点后,sentinel将从节点升级为主节点,并且我可以继续使用redis而不会出错。
我错过什么了吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题