我在 WSL2 实例上的 docker 容器中运行 redis-stack:
mark@L-R910LPKW:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b6530cb6eecf docker.io/redis/redis-stack:latest /entrypoint.sh 17 hours ago Up 17 hours ago 0.0.0.0:6379->6379/tcp, 0.0.0.0:8001->8001/tcp redis-stack
mark@L-R910LPKW:~$
在 Windows 主机本身上,我可以导航到
http://localhost:8001
并访问在同一容器中运行的 Redis Insights GUI。它显示的数据很好。
在 WSL2 中我可以毫无问题地访问 redis 服务器。我没有 redis-cli 客户端(容器中可用的除外),所以我将使用
nc
命令来运行 redis PING 命令:
mark@L-R910LPKW:~$ nc localhost 6379
ping
+PONG
^C
mark@L-R910LPKW:~$
在Windows主机上我没有redis客户端,也没有nc工具。但我有 git-bash 并且这有效:
/c/dayforce/aida$ exec 3<>/dev/tcp/localhost/6379
/c/dayforce/aida$ echo -e "PING\r\n" >&3
/c/dayforce/aida$ cat <&3
+PONG
(谢谢https://www.baeldung.com/linux/redis-connection-without-redis-cli#bashs-built-in-devtcp-file)
我还尝试了 git bash(在 Windows 主机上运行)中的 INFO 命令,它工作正常。
现在我将尝试使用 StackExchange.Redis NuGet 包运行命令。为此,我进行了这个小型 xUnit 测试(大部分代码将 redis 日志记录配置到 xUnit):
using StackExchange.Redis;
using System.Diagnostics.CodeAnalysis;
using System.Text;
namespace AidaApi.Tests.Connectors;
public class XUnitTextWriter(ITestOutputHelper testOutputHelper) : TextWriter
{
public override void WriteLine(string value)
{
testOutputHelper.WriteLine(value);
}
public override void WriteLine([StringSyntax("CompositeFormat")] string format, params object[] arg)
{
testOutputHelper.WriteLine(format, arg);
}
public override Encoding Encoding => Encoding.UTF8;
}
public class LocalRedis
{
private readonly IConnectionMultiplexer sut;
public LocalRedis(ITestOutputHelper testOutputHelper)
{
if (Environment.GetEnvironmentVariable("TF_BUILD") == null)
{
sut = ConnectionMultiplexer.Connect("localhost,abortConnect=false", new XUnitTextWriter(testOutputHelper));
}
}
[SkippableFact]
public void Ping()
{
Skip.If(sut == null, $"{GetType().Name} tests can only run locally.");
var db = sut.GetDatabase();
db.Ping();
}
}
这件事失败了:
C:\xyz\aida\aida-api\tests\AidaApi.Tests\bin\Debug\net8.0> vstest.console.exe AidaApi.Tests.dll /Tests:Ping
VSTest version 17.11.1 (x64)
Starting test discovery, please wait...
[xUnit.net 00:00:00.00] xUnit.net VSTest Adapter v2.8.1+ce9211e970 (64-bit .NET 8.0.8)
[xUnit.net 00:00:00.09] Discovering: AidaApi.Tests
[xUnit.net 00:00:00.16] Discovered: AidaApi.Tests
A total of 1 test files matched the specified pattern.
[xUnit.net 00:00:00.00] xUnit.net VSTest Adapter v2.8.1+ce9211e970 (64-bit .NET 8.0.8)
[xUnit.net 00:00:00.08] Starting: AidaApi.Tests
[xUnit.net 00:00:20.20] AidaApi.Tests.Connectors.LocalRedis.Ping [FAIL]
[xUnit.net 00:00:20.21] StackExchange.Redis.RedisConnectionException : The message timed out in the backlog attempting to send because no connection became available (5000ms) - Last Connection Exception: It was not possible to connect to the redis server(s). ConnectTimeout, command=PING, timeout: 5000, inst: 0, qu: 0, qs: 0, aw: False, bw: SpinningDown, rs: NotStarted, ws: Idle, in: 0, last-in: 0, cur-in: 0, sync-ops: 1, async-ops: 0, serverEndpoint: localhost:6379, conn-sec: n/a, aoc: 0, mc: 1/1/0, mgr: 10 of 10 available, clientName: L-R910LPKW(SE.Redis-v2.8.0.27420), IOCP: (Busy=0,Free=1000,Min=1,Max=1000), WORKER: (Busy=2,Free=32765,Min=12,Max=32767), POOL: (Threads=10,QueuedItems=0,CompletedItems=80,Timers=6), v: 2.8.0.27420 (Please take a look at this article for some common client-side issues that can cause timeouts: https://stackexchange.github.io/StackExchange.Redis/Timeouts)
[xUnit.net 00:00:20.21] ---- StackExchange.Redis.RedisConnectionException : It was not possible to connect to the redis server(s). ConnectTimeout
[xUnit.net 00:00:20.21] Stack Trace:
[xUnit.net 00:00:20.21] /_/src/StackExchange.Redis/ConnectionMultiplexer.cs(2105,0): at StackExchange.Redis.ConnectionMultiplexer.ExecuteSyncImpl[T](Message message, ResultProcessor`1 processor, ServerEndPoint server, T defaultValue)
[xUnit.net 00:00:20.21] /_/src/StackExchange.Redis/RedisBase.cs(62,0): at StackExchange.Redis.RedisBase.ExecuteSync[T](Message message, ResultProcessor`1 processor, ServerEndPoint server, T defaultValue)
[xUnit.net 00:00:20.21] /_/src/StackExchange.Redis/RedisBase.cs(24,0): at StackExchange.Redis.RedisBase.Ping(CommandFlags flags)
[xUnit.net 00:00:20.21] C:\xyz\aida\aida-api\tests\AidaApi.Tests\Connectors\LocalRedis.cs(38,0): at AidaApi.Tests.Connectors.LocalRedis.Ping()
[xUnit.net 00:00:20.21] at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
[xUnit.net 00:00:20.21] at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
[xUnit.net 00:00:20.21] ----- Inner Stack Trace -----
[xUnit.net 00:00:20.21]
[xUnit.net 00:00:20.21] Output:
[xUnit.net 00:00:20.21] Connecting (sync) on .NET 8.0.8 (StackExchange.Redis: v2.8.0.27420)
[xUnit.net 00:00:20.21] localhost,abortConnect=False
[xUnit.net 00:00:20.21] localhost:6379/Interactive: Connecting...
[xUnit.net 00:00:20.21] localhost:6379: BeginConnectAsync
[xUnit.net 00:00:20.21] 1 unique nodes specified (with tiebreaker)
[xUnit.net 00:00:20.21] localhost:6379: OnConnectedAsync init (State=Connecting)
[xUnit.net 00:00:20.21] Allowing 1 endpoint(s) 00:00:05 to respond...
[xUnit.net 00:00:20.21] Awaiting 1 available task completion(s) for 5000ms, IOCP: (Busy=0,Free=1000,Min=1,Max=1000), WORKER: (Busy=2,Free=32765,Min=12,Max=32767), POOL: (Threads=3,QueuedItems=0,CompletedItems=4,Timers=3)
[xUnit.net 00:00:20.21] Not all available tasks completed cleanly (from ReconfigureAsync#1499, timeout 5000ms), IOCP: (Busy=0,Free=1000,Min=1,Max=1000), WORKER: (Busy=5,Free=32762,Min=12,Max=32767), POOL: (Threads=7,QueuedItems=0,CompletedItems=13,Timers=1)
[xUnit.net 00:00:20.21] localhost:6379: OnConnectedAsync completed (Disconnected)
[xUnit.net 00:00:20.21] Server[0] (localhost:6379) Status: WaitingForActivation (inst: 0, qs: 0, in: 0, qu: 0, aw: False, in-pipe: -1, out-pipe: -1, bw: Inactive, rs: NotStarted. ws: Initializing)
[xUnit.net 00:00:20.21] Endpoint summary:
[xUnit.net 00:00:20.21] localhost:6379: Endpoint is (Interactive: Connecting, Subscription: Connecting)
[xUnit.net 00:00:20.21] Task summary:
[xUnit.net 00:00:20.21] localhost:6379: Returned, but incorrectly
[xUnit.net 00:00:20.21] Election summary:
[xUnit.net 00:00:20.21] Election: localhost:6379 had no tiebreaker set
[xUnit.net 00:00:20.21] Election: No primaries detected
[xUnit.net 00:00:20.21] Endpoint Summary:
[xUnit.net 00:00:20.21] localhost:6379: Standalone v3.0.0, primary; keep-alive: 00:01:00; int: Connecting; sub: Connecting; not in use: DidNotRespond
[xUnit.net 00:00:20.21] localhost:6379: 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
[xUnit.net 00:00:20.21] Connection failed: localhost:6379 (Interactive, UnableToConnect): UnableToConnect on localhost:6379/Interactive, Initializing/NotStarted, last: NONE, origin: BeginConnectAsync, outstanding: 0, last-read: 5s ago, last-write: 5s ago, keep-alive: 60s, state: Connecting, mgr: 10 of 10 available, last-heartbeat: never, global: 5s ago, v: 2.8.0.27420
[xUnit.net 00:00:20.21] Connection failed: localhost:6379 (Subscription, UnableToConnect): UnableToConnect on localhost:6379/Subscription, Initializing/NotStarted, last: NONE, origin: BeginConnectAsync, outstanding: 0, last-read: 5s ago, last-write: 5s ago, keep-alive: 60s, state: Connecting, mgr: 10 of 10 available, last-heartbeat: never, global: 5s ago, v: 2.8.0.27420
[xUnit.net 00:00:20.21] localhost:6379: Circular op-count snapshot; int: 0 (0.00 ops/s; spans 10s); sub: 0 (0.00 ops/s; spans 10s)
[xUnit.net 00:00:20.21] Sync timeouts: 0; async timeouts: 0; fire and forget: 0; last heartbeat: -1s ago
[xUnit.net 00:00:20.21] Resetting failing connections to retry...
[xUnit.net 00:00:20.21] Retrying - attempts left: 2...
[xUnit.net 00:00:20.21] 1 unique nodes specified (with tiebreaker)
[xUnit.net 00:00:20.21] localhost:6379: OnConnectedAsync init (State=Connecting)
[xUnit.net 00:00:20.21] Allowing 1 endpoint(s) 00:00:05 to respond...
[xUnit.net 00:00:20.21] Awaiting 1 available task completion(s) for 5000ms, IOCP: (Busy=0,Free=1000,Min=1,Max=1000), WORKER: (Busy=5,Free=32762,Min=12,Max=32767), POOL: (Threads=10,QueuedItems=0,CompletedItems=30,Timers=3)
[xUnit.net 00:00:20.21] Not all available tasks completed cleanly (from ReconfigureAsync#1499, timeout 5000ms), IOCP: (Busy=0,Free=1000,Min=1,Max=1000), WORKER: (Busy=3,Free=32764,Min=12,Max=32767), POOL: (Threads=10,QueuedItems=0,CompletedItems=40,Timers=1)
[xUnit.net 00:00:20.21] Server[0] (localhost:6379) Status: WaitingForActivation (inst: 0, qs: 0, in: -1, qu: 0, aw: False, in-pipe: -1, out-pipe: -1, bw: Inactive, rs: NA. ws: NA)
[xUnit.net 00:00:20.21] Endpoint summary:
[xUnit.net 00:00:20.21] localhost:6379: Endpoint is (Interactive: Disconnected, Subscription: Disconnected)
[xUnit.net 00:00:20.21] Task summary:
[xUnit.net 00:00:20.21] localhost:6379: Did not respond (Task.Status: WaitingForActivation)
[xUnit.net 00:00:20.21] Election summary:
[xUnit.net 00:00:20.21] Election: localhost:6379 had no tiebreaker set
[xUnit.net 00:00:20.21] Election: No primaries detected
[xUnit.net 00:00:20.21] Endpoint Summary:
[xUnit.net 00:00:20.21] localhost:6379: Standalone v3.0.0, primary; keep-alive: 00:01:00; int: Disconnected; sub: Disconnected; not in use: DidNotRespond
[xUnit.net 00:00:20.21] localhost:6379: 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
[xUnit.net 00:00:20.21] localhost:6379: Circular op-count snapshot; int: 0 (0.00 ops/s; spans 10s); sub: 0 (0.00 ops/s; spans 10s)
[xUnit.net 00:00:20.21] Sync timeouts: 0; async timeouts: 0; fire and forget: 0; last heartbeat: -1s ago
[xUnit.net 00:00:20.21] Resetting failing connections to retry...
[xUnit.net 00:00:20.21] Retrying - attempts left: 1...
[xUnit.net 00:00:20.21] 1 unique nodes specified (with tiebreaker)
[xUnit.net 00:00:20.21] localhost:6379: OnConnectedAsync init (State=Connecting)
[xUnit.net 00:00:20.21] Allowing 1 endpoint(s) 00:00:05 to respond...
[xUnit.net 00:00:20.21] Awaiting 1 available task completion(s) for 5000ms, IOCP: (Busy=0,Free=1000,Min=1,Max=1000), WORKER: (Busy=4,Free=32763,Min=12,Max=32767), POOL: (Threads=10,QueuedItems=0,CompletedItems=40,Timers=3)
[xUnit.net 00:00:20.21] Not all available tasks completed cleanly (from ReconfigureAsync#1499, timeout 5000ms), IOCP: (Busy=0,Free=1000,Min=1,Max=1000), WORKER: (Busy=7,Free=32760,Min=12,Max=32767), POOL: (Threads=10,QueuedItems=0,CompletedItems=49,Timers=1)
[xUnit.net 00:00:20.21] localhost:6379: OnConnectedAsync completed (Disconnected)
[xUnit.net 00:00:20.21] localhost:6379: OnConnectedAsync completed (Disconnected)
[xUnit.net 00:00:20.21] Server[0] (localhost:6379) Status: WaitingForActivation (inst: 0, qs: 0, in: -1, qu: 0, aw: False, in-pipe: -1, out-pipe: -1, bw: Inactive, rs: NA. ws: NA)
[xUnit.net 00:00:20.21] Endpoint summary:
[xUnit.net 00:00:20.21] localhost:6379: Endpoint is (Interactive: Disconnected, Subscription: Disconnected)
[xUnit.net 00:00:20.21] Task summary:
[xUnit.net 00:00:20.21] localhost:6379: Returned, but incorrectly
[xUnit.net 00:00:20.21] Election summary:
[xUnit.net 00:00:20.21] Election: localhost:6379 had no tiebreaker set
[xUnit.net 00:00:20.21] Election: No primaries detected
[xUnit.net 00:00:20.21] Endpoint Summary:
[xUnit.net 00:00:20.21] localhost:6379: Standalone v3.0.0, primary; keep-alive: 00:01:00; int: Disconnected; sub: Disconnected; not in use: DidNotRespond
[xUnit.net 00:00:20.21] localhost:6379: 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
[xUnit.net 00:00:20.21] localhost:6379: Circular op-count snapshot; int: 0 (0.00 ops/s; spans 10s); sub: 0 (0.00 ops/s; spans 10s)
[xUnit.net 00:00:20.21] Sync timeouts: 0; async timeouts: 0; fire and forget: 0; last heartbeat: -1s ago
[xUnit.net 00:00:20.21] Starting heartbeat...
[xUnit.net 00:00:20.21] Total connect time: 15,051 ms
[xUnit.net 00:00:20.22] Finished: AidaApi.Tests
Failed AidaApi.Tests.Connectors.LocalRedis.Ping [20 s]
Error Message:
...
Test Run Failed.
Total tests: 1
Failed: 1
Total time: 20.6085 Seconds
C:\xyz\aida\aida-api\tests\AidaApi.Tests\bin\Debug\net8.0>
(由于某种原因,堆栈跟踪和捕获的输出被打印两次 - 一次带有
[xUnit.net 00:00:...]
前缀,然后没有,所以为了简洁起见,我用省略号替换了后者)
我是 Redis 新手,我完全不清楚发生了什么。另外,运行单元测试时我对以下输出感到困惑:
localhost:6379: Standalone v3.0.0, ...
我想知道它的含义是什么,因为 INFO 命令将服务器版本报告为
7.4.0
。
非常感谢您的帮助。
编辑1
我将
localhost
替换为 127.0.0.1
并且成功了。所以现在的问题是 - .NET 中的 localhost
有什么问题,因为它在 git bash 中工作得很好。
发布我的答案,以防其他人遇到同样的问题。 根本原因是我笔记本电脑上的
localhost
分辨率。由于某种原因,它无法解析为127.0.0.1
:
C:\> nslookup localhost
Server: UnKnown
Address: 192.168.86.1
Name: localhost.XYZ.NET
C:\>
这有点疯狂。主机文件没问题:
C:\> sls localhost C:\Windows\System32\drivers\etc\hosts
Windows\System32\drivers\etc\hosts:20:# localhost name resolution is handled within DNS itself.
Windows\System32\drivers\etc\hosts:21:# 127.0.0.1 localhost
Windows\System32\drivers\etc\hosts:22:# ::1 localhost
Windows\System32\drivers\etc\hosts:24:127.0.0.1 local localhost
C:\>
所以这是另一回事。
它适用于 git bash,可能是因为它使用与 .NET 不同的网络堆栈。尽管如此,nslookup 实用程序也返回相同的non环回地址。
浏览器也必须使用与 .NET 不同的网络堆栈。
我不知道如何修复我的本地主机,但那将是一个不同的问题。