如何在 ASP .Net Core 中支持 Redis Sentinel

问题描述

我需要支持 Redis 哨兵以获得高可用性。我创建了一个简单的 .NET Core 控制台应用程序来轻松测试 Redis 哨兵。我通过 ConnectionMultiplexer of StackExchange.Redis

执行此操作

我在控制台端做的是

var conn = ConnectionMultiplexer.Connect("127.0.0.1:26379," +
                                                     "serviceName=mymaster," +
                                                     "allowAdmin=true," +
                                                     "tiebreaker=\"\"," +
                                                     "abortConnect=false");
IDatabase db = conn.GetDatabase();

...

连接后,我试图杀死主服务器,然后 StackExchange 正确地移动到从服务器并使其成为主服务器,一切都很好。

但问题是当我通过 ASP .Net Core Application 应用相同的场景时,手动杀死 master 后,我无法发送与 Redis 相关的请求。

这是我在 Startup.cs 中所做的

var connectionMultiplexer = ConnectionMultiplexer.Connect($"127.0.0.1:26379," +
                                                                      "serviceName=mymaster," +
                                                                      "allowAdmin=true," +
                                                                      "tiebreaker=\"\"," +
                                                                      "abortConnect=false");

var database = connectionMultiplexer.GetDatabase(0);
services.AddScoped(_ => database);

services.AddMvc(options =>
{
    options.EnableEndpointRouting = false;
});

在杀死 master 之后,然后尝试发送一些有关 Redis 的请求,我收到以下错误

StackExchange.Redis.RedisConnectionException: No connection is available to service this operation: SADD 100001; An existing connection was forcibly closed by the remote host.; IOCP: (Busy=0,Free=1000,Min=16,Max=1000),WORKER: (Busy=2,Free=32765,Max=32767),Local-cpu: n/a
 ---> StackExchange.Redis.RedisConnectionException: SocketFailure (ReadSocketError/ConnectionReset,last-recv: 20) on 127.0.0.1:6379/Subscription,Idle/Faulted,last: PING,origin: ReadFromPipe,outstanding: 0,last-read: 13s ago,last-write: 13s ago,keep-alive: 60s,state: ConnectedEstablished,mgr: 8 of 10 available,in: 0,in-pipe: 0,out-pipe: 0,last-heartbeat: 0s ago,last-mbeat: 0s ago,global: 0s ago,v: 2.0.593.37019
 ---> Pipelines.sockets.Unofficial.ConnectionResetException: An existing connection was forcibly closed by the remote host.
 ---> System.Net.sockets.socketException (10054): An existing connection was forcibly closed by the remote host.
   at Pipelines.sockets.Unofficial.Internal.Throw.socket(Int32 errorCode) in C:\code\Pipelines.sockets.Unofficial\src\Pipelines.sockets.Unofficial\Internal\Throw.cs:line 59
   at Pipelines.sockets.Unofficial.socketAwaitableEventArgs.GetResult() in C:\code\Pipelines.sockets.Unofficial\src\Pipelines.sockets.Unofficial\SocketAwaitableEventArgs.cs:line 74
   at Pipelines.sockets.Unofficial.socketConnection.DoReceiveAsync() in C:\code\Pipelines.sockets.Unofficial\src\Pipelines.sockets.Unofficial\SocketConnection.Receive.cs:line 64

我期待的是在杀死主人后,它应该成功地成为主人,所以我不会出错。

有什么想法吗?我可能缺少什么?谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)