简体   繁体   English

如何使用C#连接到Redis服务器?

[英]How to connect to Redis server using C#?

I am only trying to test connection with the redis server?我只是想测试与 redis 服务器的连接? If all the connection configuration are correct and if I am able to establish a connection with the redis server using C#. Here is the code that I used:如果所有连接配置都正确,并且我能够使用 C# 与 redis 服务器建立连接。这是我使用的代码:

class Program
{
    static readonly ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(
        new ConfigurationOptions
        {
            EndPoints = { "******windows.net", "6380" },
            Password = "****",
            Ssl = true,
            AbortOnConnectFail = false,
            AllowAdmin = true,
            ConnectTimeout = 30000,
            SyncTimeout = 30000
        });
        
    static async Task Main(string[] args)
    {
        ThreadPool.SetMinThreads(10, 10);
        var db = redis.GetDatabase();
        var pong = await db.PingAsync();
        Console.WriteLine(pong);
    }
}

Here is the error I am getting:这是我收到的错误:

StackExchange.Redis.RedisTimeoutException:
'The timeout was reached before the message could be written to the output buffer, 
and it was not sent, command=PING,
timeout: 30000,
inst: 0,
qu: 0,
qs: 0,
aw: False,
bw: CheckingForTimeout,
rs: NotStarted,
ws: Initializing,
in: 0,
last-in: 0,
cur-in: 0,
serverEndpoint: 0.0.24.236:6380,
mc: 1/1/0,
mgr: 10 of 10 available,
clientName: SJAIN(SE.Redis-v2.6.80.25426),
IOCP: (Busy=0,Free=1000,Min=10,Max=1000),
WORKER: (Busy=2,Free=32765,Min=10,Max=32767),
POOL: (Threads=13,QueuedItems=0,CompletedItems=699),
v: 2.6.80.25426 
(Please take a look at this article for some common client-side issues
that can cause timeouts:
https://stackexchange.github.io/StackExchange.Redis/Timeouts)'

What am I missing here with this code?这段代码我在这里错过了什么?

Here are complete logs:以下是完整日志:

11:20:43.7290: Connecting (sync) on .NET Core 3.1.31 (StackExchange.Redis: v2.6.80.25426)
11:20:43.8989: endpoint.windows.net,0.0.24.236,syncTimeout=30000,allowAdmin=True,connectTimeout=30000,password=*****,ssl=True,abortConnect=False

11:20:43.9637: endpoint.windows.net:6380/Interactive: Connecting...
11:20:44.0600: endpoint.windows.net:6380: BeginConnectAsync
11:20:44.1316: 0.0.24.236:6380/Interactive: Connecting...
11:20:44.1318: 0.0.24.236:6380: BeginConnectAsync
11:20:44.1503: 2 unique nodes specified (with tiebreaker)
11:20:44.1519: endpoint.windows.net:6380: OnConnectedAsync init (State=Connecting)
11:20:44.1521: 0.0.24.236:6380: OnConnectedAsync init (State=Connecting)
11:20:44.1538: Allowing 2 endpoint(s) 00:00:30 to respond...
11:20:44.2065: Awaiting 2 available task completion(s) for 30000ms, IOCP: (Busy=2,Free=998,Min=10,Max=1000), WORKER: (Busy=0,Free=32767,Min=10,Max=32767), POOL: (Threads=6,QueuedItems=0,CompletedItems=9)
11:20:44.4260: 0.0.24.236:6380: OnConnectedAsync completed (Disconnected)
11:20:44.5679: Connection failed: 0.0.24.236:6380 (Subscription, UnableToConnect): UnableToConnect on 0.0.24.236:6380/Subscription, 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: 0s ago, v: 2.6.80.25426
11:20:44.5679: Connection failed: 0.0.24.236:6380 (Interactive, UnableToConnect): UnableToConnect on 0.0.24.236: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: 0s ago, v: 2.6.80.25426
11:20:44.5679: Connection failed: 0.0.24.236:6380 (Subscription, UnableToConnect): UnableToConnect on 0.0.24.236:6380/Subscription, 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: 0s ago, v: 2.6.80.25426
11:20:44.5679: Connection failed: 0.0.24.236:6380 (Interactive, UnableToConnect): UnableToConnect on 0.0.24.236: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: 0s ago, v: 2.6.80.25426
11:20:47.5071: endpoint.windows.net:6380: OnConnectedAsync completed (Disconnected)
11:20:47.5183: Connection failed: endpoint.windows.net:6380 (Interactive, UnableToConnect): UnableToConnect on endpoint.windows.net:6380/Interactive, Initializing/NotStarted, last: NONE, origin: BeginConnectAsync, outstanding: 0, last-read: 3s ago, last-write: 3s ago, keep-alive: 60s, state: Connecting, mgr: 10 of 10 available, last-heartbeat: never, global: 3s ago, v: 2.6.80.25426
11:20:47.5183: Connection failed: endpoint.windows.net:6380 (Subscription, UnableToConnect): UnableToConnect on endpoint.windows.net:6380/Subscription, Initializing/NotStarted, last: NONE, origin: BeginConnectAsync, outstanding: 0, last-read: 3s ago, last-write: 3s ago, keep-alive: 60s, state: Connecting, mgr: 10 of 10 available, last-heartbeat: never, global: 3s ago, v: 2.6.80.25426
11:20:47.5427: All 2 available tasks completed cleanly, IOCP: (Busy=0,Free=1000,Min=10,Max=1000), WORKER: (Busy=3,Free=32764,Min=10,Max=32767), POOL: (Threads=13,QueuedItems=0,CompletedItems=29)
11:20:47.5559: Endpoint summary:
11:20:47.5571:   endpoint.windows.net:6380: Endpoint is (Interactive: Connecting, Subscription: Connecting)
11:20:47.5571:   0.0.24.236:6380: Endpoint is (Interactive: Disconnected, Subscription: Disconnected)
11:20:47.5571: Task summary:
11:20:47.5574:   endpoint.windows.net:6380: Returned, but incorrectly
11:20:47.5574:   0.0.24.236:6380: Returned, but incorrectly
11:20:47.5883: Election summary:
11:20:47.5955:   Election: endpoint.windows.net:6380 had no tiebreaker set
11:20:47.5955:   Election: 0.0.24.236:6380 had no tiebreaker set
11:20:47.5955:   Election: No primaries detected
11:20:47.6068: Endpoint Summary:
11:20:47.6079:   endpoint.windows.net:6380: Standalone v4.0.0, primary; keep-alive: 00:01:00; int: Connecting; sub: Disconnected; not in use: DidNotRespond
11:20:47.6609:   endpoint.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
11:20:47.6626:   endpoint.windows.net:6380: Circular op-count snapshot; int: 0 (0.00 ops/s; spans 10s); sub: 0 (0.00 ops/s; spans 10s)
11:20:47.6626:   0.0.24.236:6380: Standalone v4.0.0, primary; keep-alive: 00:01:00; int: Disconnected; sub: Disconnected; not in use: DidNotRespond
11:20:47.6645:   0.0.24.236: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
11:20:47.6645:   0.0.24.236:6380: Circular op-count snapshot; int: 0 (0.00 ops/s; spans 10s); sub: 0 (0.00 ops/s; spans 10s)
11:20:47.6646: Sync timeouts: 0; async timeouts: 0; fire and forget: 0; last heartbeat: -1s ago
11:20:47.6648: Resetting failing connections to retry...
11:20:47.6765:   Retrying - attempts left: 2...
11:20:47.6765: 2 unique nodes specified (with tiebreaker)
11:20:47.6766: endpoint.windows.net:6380: OnConnectedAsync init (State=Connecting)
11:20:47.6766: 0.0.24.236:6380: OnConnectedAsync init (State=Connecting)
11:20:47.6766: Allowing 2 endpoint(s) 00:00:30 to respond...
11:20:47.6906: Awaiting 2 available task completion(s) for 30000ms, IOCP: (Busy=1,Free=999,Min=10,Max=1000), WORKER: (Busy=4,Free=32763,Min=10,Max=32767), POOL: (Threads=13,QueuedItems=0,CompletedItems=35)
11:20:47.7117: 0.0.24.236:6380: OnConnectedAsync completed (Disconnected)
11:20:47.7527: endpoint.windows.net:6380: OnConnectedAsync completed (Disconnected)
11:20:47.7888: All 2 available tasks completed cleanly, IOCP: (Busy=0,Free=1000,Min=10,Max=1000), WORKER: (Busy=3,Free=32764,Min=10,Max=32767), POOL: (Threads=13,QueuedItems=0,CompletedItems=42)
11:20:47.7888: Endpoint summary:
11:20:47.7888:   endpoint.windows.net:6380: Endpoint is (Interactive: Disconnected, Subscription: Disconnected)
11:20:47.7889:   0.0.24.236:6380: Endpoint is (Interactive: Disconnected, Subscription: Disconnected)
11:20:47.7889: Task summary:
11:20:47.7889:   endpoint.windows.net:6380: Returned, but incorrectly
11:20:47.7889:   0.0.24.236:6380: Returned, but incorrectly
11:20:47.7889: Election summary:
11:20:47.7889:   Election: endpoint.windows.net:6380 had no tiebreaker set
11:20:47.7889:   Election: 0.0.24.236:6380 had no tiebreaker set
11:20:47.7889:   Election: No primaries detected
11:20:47.7889: Endpoint Summary:
11:20:47.7889:   endpoint.windows.net:6380: Standalone v4.0.0, primary; keep-alive: 00:01:00; int: Disconnected; sub: Disconnected; not in use: DidNotRespond
11:20:47.7889:   endpoint.windows.net:6380: int ops=0, qu=0, qs=0, qc=0, wr=0, socks=3; sub ops=0, qu=0, qs=0, qc=0, wr=0, socks=3
11:20:47.7890:   endpoint.windows.net:6380: Circular op-count snapshot; int: 0 (0.00 ops/s; spans 10s); sub: 0 (0.00 ops/s; spans 10s)
11:20:47.7890:   0.0.24.236:6380: Standalone v4.0.0, primary; keep-alive: 00:01:00; int: Disconnected; sub: Disconnected; not in use: DidNotRespond
11:20:47.7890:   0.0.24.236:6380: int ops=0, qu=0, qs=0, qc=0, wr=0, socks=3; sub ops=0, qu=0, qs=0, qc=0, wr=0, socks=3
11:20:47.7890:   0.0.24.236:6380: Circular op-count snapshot; int: 0 (0.00 ops/s; spans 10s); sub: 0 (0.00 ops/s; spans 10s)
11:20:47.7890: Sync timeouts: 0; async timeouts: 0; fire and forget: 0; last heartbeat: -1s ago
11:20:47.7890: Resetting failing connections to retry...
11:20:47.8632:   Retrying - attempts left: 1...
11:20:47.8632: 2 unique nodes specified (with tiebreaker)
11:20:47.8632: endpoint.windows.net:6380: OnConnectedAsync init (State=Connecting)
11:20:47.8632: 0.0.24.236:6380: OnConnectedAsync init (State=Disconnected)
11:20:47.8632: Allowing 2 endpoint(s) 00:00:30 to respond...
11:20:47.9120: endpoint.windows.net:6380: OnConnectedAsync completed (Disconnected)
11:20:47.9120: Awaiting 2 available task completion(s) for 30000ms, IOCP: (Busy=0,Free=1000,Min=10,Max=1000), WORKER: (Busy=5,Free=32762,Min=10,Max=32767), POOL: (Threads=13,QueuedItems=0,CompletedItems=47)
11:21:17.8775: Not all available tasks completed cleanly (from ReconfigureAsync#1292, timeout 30000ms), IOCP: (Busy=0,Free=1000,Min=10,Max=1000), WORKER: (Busy=1,Free=32766,Min=10,Max=32767), POOL: (Threads=12,QueuedItems=0,CompletedItems=86)
11:21:17.8866:   Server[0] (endpoint.windows.net:6380) Status: RanToCompletion (inst: 0, qs: 0, in: -1, qu: 0, aw: False, in-pipe: -1, out-pipe: -1, bw: Inactive, rs: NA. ws: NA)
11:21:17.8873:   Server[1] (0.0.24.236:6380) Status: WaitingForActivation (inst: 0, qs: 0, in: -1, qu: 0, aw: False, in-pipe: -1, out-pipe: -1, bw: Inactive, rs: NA. ws: NA)
11:21:17.8873: Endpoint summary:
11:21:17.8873:   endpoint.windows.net:6380: Endpoint is (Interactive: Disconnected, Subscription: Disconnected)
11:21:17.8873:   0.0.24.236:6380: Endpoint is (Interactive: Disconnected, Subscription: Disconnected)
11:21:17.8873: Task summary:
11:21:17.8873:   endpoint.windows.net:6380: Returned, but incorrectly
11:21:17.8874:   0.0.24.236:6380: Did not respond (Task.Status: WaitingForActivation)
11:21:17.8874: Election summary:
11:21:17.8874:   Election: endpoint.windows.net:6380 had no tiebreaker set
11:21:17.8874:   Election: 0.0.24.236:6380 had no tiebreaker set
11:21:17.8875:   Election: No primaries detected
11:21:17.8876: Endpoint Summary:
11:21:17.8885:   endpoint.windows.net:6380: Standalone v4.0.0, primary; keep-alive: 00:01:00; int: Disconnected; sub: Disconnected; not in use: DidNotRespond
11:21:17.8890:   endpoint.windows.net:6380: int ops=0, qu=0, qs=0, qc=0, wr=0, socks=4; sub ops=0, qu=0, qs=0, qc=0, wr=0, socks=4
11:21:17.8897:   endpoint.windows.net:6380: Circular op-count snapshot; int: 0 (0.00 ops/s; spans 10s); sub: 0 (0.00 ops/s; spans 10s)
11:21:17.8898:   0.0.24.236:6380: Standalone v4.0.0, primary; keep-alive: 00:01:00; int: Disconnected; sub: Disconnected; not in use: DidNotRespond
11:21:17.8898:   0.0.24.236:6380: int ops=0, qu=0, qs=0, qc=0, wr=0, socks=4; sub ops=0, qu=0, qs=0, qc=0, wr=0, socks=4
11:21:17.8898:   0.0.24.236:6380: Circular op-count snapshot; int: 0 (0.00 ops/s; spans 10s); sub: 0 (0.00 ops/s; spans 10s)
11:21:17.8898: Sync timeouts: 0; async timeouts: 0; fire and forget: 0; last heartbeat: -1s ago
11:21:17.8905: Starting heartbeat...
11:21:18.9710: 0.0.24.236:6380: OnConnectedAsync completed (Disconnected)
11:21:48.4544: Encountered exception: StackExchange.Redis.RedisTimeoutException: The timeout was reached before the message could be written to the output buffer, and it was not sent, command=SUBSCRIBE, timeout: 30000, inst: 0, qu: 0, qs: 0, aw: False, bw: CheckingForTimeout, last-in: 0, cur-in: 0, serverEndpoint: 0.0.24.236:6380, mc: 1/1/0, mgr: 10 of 10 available, clientName: SJAIN(SE.Redis-v2.6.80.25426), IOCP: (Busy=0,Free=1000,Min=10,Max=1000), WORKER: (Busy=0,Free=32767,Min=10,Max=32767), POOL: (Threads=14,QueuedItems=0,CompletedItems=294), v: 2.6.80.25426 (Please take a look at this article for some common client-side issues that can cause timeouts: https://stackexchange.github.io/StackExchange.Redis/Timeouts)
   at StackExchange.Redis.Maintenance.AzureMaintenanceEvent.AddListenerAsync(ConnectionMul

Updated Logs After Marc's answer马克回答后更新日志

    EndPoints = { "******windows.net", "6380" },

should be应该

    EndPoints = { { "******windows.net", 6380 } },

or perhaps more simply:或者更简单地说:

    EndPoints = { "******windows.net:6380" },

Right now, you're connecting to "******windows.net" on 6379, and (separately, as a different endpoint) "6380" on 6379.现在,您正在连接到 6379 上的"******windows.net"和 6379 上的(分别作为不同端点) "6380"


Note that you will need to be able to reach the machine - it must resolve by DNS (or be specified as an IP address), be routable by you, and the designated port(s) must be open.请注意,您将需要能够访问该机器 - 它必须通过 DNS(或指定为 IP 地址)解析,可由您路由,并且指定的端口必须打开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM