Redis套接字故障

zujrkrfu  于 2022-10-31  发布在  Redis
关注(0)|答案(1)|浏览(245)

我是Redis的新手。我实现了一个类,它有GET, SET, DELETE and ISKEYPRESENT的方法。下面是我的类的代码:

static ConnectionMultiplexer redisConnection = ConnectionMultiplexer.Connect("connectionstring");
static readonly IDatabaseAsync dbConnection = redisConnection.GetDatabase();

        public static async Task SetRecordAsync<T>(string recordId, T data, TimeSpan? timeSpan = null)//default 30 mins
        {
            var jsonData = JsonSerializer.Serialize(data);

            if (timeSpan.HasValue)
            {
                await dbConnection.StringSetAsync(recordId, jsonData, timeSpan);
            }
            else
            {
                timeSpan = TimeSpan.FromMinutes(30);
                await dbConnection.StringSetAsync(recordId, jsonData, timeSpan);
            }
        }

        public static async Task<T> GetValueAsync<T>(string recordId)
        {
            var data = await dbConnection.StringGetAsync(recordId);
            var jsonData = data.ToString();

            if (jsonData is null)
            {
                return default(T);
            }

            return JsonSerializer.Deserialize<T>(jsonData);
        }

        public static async Task DeleteValueAsync(string recordId)
        {
            await dbConnection.KeyDeleteAsync(recordId);
        }

        public static async Task<bool> IsKeyNull(string recordId)
        {
            var value = await dbConnection.StringGetAsync(recordId);
            var isNull = value.IsNullOrEmpty ? true : false;
            return isNull;
        }

但我不断得到这个错误,我不能理解它是什么意思:

StackExchange.Redis.RedisConnectionException
  HResult=0x80131500
  Message=No connection is active/available to service this operation: GET Order; A blocking operation was interrupted by a call to WSACancelBlockingCall, mc: 1/1/0, mgr: 10 of 10 available, clientName: ASR-PC, IOCP: (Busy=2,Free=998,Min=4,Max=1000), WORKER: (Busy=1,Free=8190,Min=4,Max=8191), v: 2.1x.58.3xyz
  Source=StackExchange.Redis
  StackTrace:
   at StackExchange.Redis.ConnectionMultiplexer.ThrowFailed[T](TaskCompletionSource`1 source, Exception unthrownException) in /_/src/StackExchange.Redis/ConnectionMultiplexer.cs:line 2777
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at RedisCache.DistributedCacheExtensions.<IsKeyNull>d__4.MoveNext() in RedisCache\DistributedCacheExtensions.cs:line 67
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at BusinessLogic.Services.Order.OrderService.GetOnlineOrderCount(Int32 storeId) in OrderService.cs:line 1846
   at GetOnlineOrderCount(Int32 storeId) in Api\Controllers\Order\OrderController.cs:line 443
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)

Inner Exception 1:
RedisConnectionException: SocketFailure on windows.net:6380/Subscription, Initializing/NotStarted, last: NONE, origin: ConnectedAsync, outstanding: 0, last-read: 10s ago, last-write: 10s ago, keep-alive: 60s, state: Connecting, mgr: 10 of 10 available, last-heartbeat: never, global: 10s ago, v: 2.x.58.3xyz

Inner Exception 2:
IOException: Unable to read data from the transport connection: A blocking operation was interrupted by a call to WSACancelBlockingCall.

Inner Exception 3:
SocketException: A blocking operation was interrupted by a call to WSACancelBlockingCall

我的redis连接字符串如下所示:
Windows. net:6380,密码=xyz=,ssl=真,中止连接=假,连接超时=10000,连接重试=8,同步超时=3000
Redis连接日志

redis.redis.cache.windows.net:6380,syncTimeout=100000000,connectTimeout=100000000,password=*****,ssl=True,connectRetry=8

Connecting redis.cache.windows.net:6380/Interactive...
BeginConnect: redis.cache.windows.net:6380
1 unique nodes specified
Requesting tie-break from redis.cache.windows.net:6380 > __Booksleeve_TieBreak...
Allowing endpoints 1.03:46:40 to respond...
Awaiting task completion, IOCP: (Busy=0,Free=1000,Min=4,Max=1000), WORKER: (Busy=1,Free=8190,Min=4,Max=8191)
Configuring TLS
connection failed: redis.cache.windows.net:6380 (Subscription, SocketFailure): SocketFailure on redis.cache.windows.net:6380/Subscription, Initializing/NotStarted, last: NONE, origin: ConnectedAsync, outstanding: 0, last-read: 30s ago, last-write: 30s ago, keep-alive: 60s, state: Connecting, mgr: 10 of 10 available, last-heartbeat: never, global: 30s ago, v: 2.1.58.34321
> Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
> An existing connection was forcibly closed by the remote host
connection failed: redis.cache.windows.net:6380 (Interactive, SocketFailure): SocketFailure on redis.cache.windows.net:6380/Interactive, Initializing/NotStarted, last: NONE, origin: ConnectedAsync, outstanding: 0, last-read: 30s ago, last-write: 30s ago, keep-alive: 60s, state: Connecting, mgr: 10 of 10 available, last-heartbeat: never, global: 30s ago, v: 2.1.58.34321
> Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
> An existing connection was forcibly closed by the remote host
Finished awaiting tasks, IOCP: (Busy=0,Free=1000,Min=4,Max=1000), WORKER: (Busy=2,Free=8189,Min=4,Max=8191)
redis.cache.windows.net:6380 faulted: SocketFailure on redis.cache.windows.net:6380/Interactive, Initializing/NotStarted, last: NONE, origin: ConnectedAsync, outstanding: 0, last-read: 30s ago, last-write: 30s ago, keep-alive: 60s, state: Connecting, mgr: 10 of 10 available, last-heartbeat: never, global: 30s ago, v: 2.1.58.34321
Waiting for tiebreakers...
All tasks are already complete
redis.cache.windows.net:6380 failed to nominate (Faulted)
> SocketFailure on redis.cache.windows.net:6380/Interactive, Initializing/NotStarted, last: NONE, origin: ConnectedAsync, outstanding: 0, last-read: 30s ago, last-write: 30s ago, keep-alive: 60s, state: Connecting, mgr: 10 of 10 available, last-heartbeat: never, global: 30s ago, v: 2.1.58.34321
No masters detected
redis.cache.windows.net:6380: Standalone v3.0.0, master; keep-alive: 00:01:00; int: Connecting; sub: Connecting; not in use: DidNotRespond
redis.cache.windows.net:6380: int ops=0, qu=0, qs=0, qc=0, wr=0, socks=2; sub ops=0, qu=0, qs=0, qc=0, wr=0, socks=2
Circular op-count snapshot; int: 0 (0.00 ops/s; spans 10s); sub: 0 (0.00 ops/s; spans 10s)
Sync timeouts: 0; async timeouts: 0; fire and forget: 0; last heartbeat: -1s ago
resetting failing connections to retry...
retrying; attempts left: 7...
1 unique nodes specified
Requesting tie-break from redis.cache.windows.net:6380 > __Booksleeve_TieBreak...
Allowing endpoints 1.03:46:40 to respond...
Awaiting task completion, IOCP: (Busy=2,Free=998,Min=4,Max=1000), WORKER: (Busy=2,Free=8189,Min=4,Max=8191)
Finished awaiting tasks, IOCP: (Busy=0,Free=1000,Min=4,Max=1000), WORKER: (Busy=2,Free=8189,Min=4,Max=8191)
redis.cache.windows.net:6380 faulted: UnableToConnect on redis.cache.windows.net:6380/Interactive, Initializing/NotStarted, last: NONE, origin: BeginConnectAsync, outstanding: 0, last-read: 0s ago, last-write: 0s ago, keep-alive: 60s, state: Connecting, mgr: 10 of 10 available, last-heartbeat: never, global: 30s ago, v: 2.1.58.34321
Waiting for tiebreakers...
All tasks are already complete
redis-red
3ks5zfa0

3ks5zfa01#

这可能是TLS的问题

假设你的连接字符串是windows.net:6380,password=xyz=,ssl=True,abortConnect=False,connectTimeout=10000,connectRetry=8,syncTimeout=3000,我会在黑暗中尝试一下,说你最可能的问题是TLS的问题。最有可能的是,你的redis-server没有启用TLS。有趣的是,通过显式地将abortConnect设置为false,连接本身并不会在一开始就大声失败。

尝试禁用TLS

让代码正常工作的最短路径可能是从连接字符串中删除ssl=True位,这将阻止多路复用器尝试与redis进行tls协商。

如果您真的需要TLS

如果您需要TLS,则需要确保:
1.正确配置您的服务器(启用TLS不是默认设置,也不是Redis的默认版本)
1.正确配置多路复用器

配置服务器

在redis中启用tls有两种方法。首先你可以使用第三方redis提供者,tls是Redis提供者的标准特性。或者,你可以从源代码构建Redis,设置tls标志,然后用你的certs / ca文件加载tls。redis.io上有一个很好的文章

配置客户端

如果在Redis服务器上启用了TLS,你需要验证你的证书和CA是否都能被客户端接受,有几种方法可以做到这一点。你可以在你的机器的证书库中加载CA/cert,或者你可以直接将它们加载到客户端,这里有一篇关于如何configure TLS in the multiplexer的文章,这个例子展示了如何通过TLS连接到Redis Cloud示例。但这些步骤适用于您正在使用的任何证书/ca

相关问题