gpt4 book ai didi

asp.net - 通过 StackExchange.Redis 连接到 Redis 服务

转载 作者:可可西里 更新时间:2023-11-01 11:12:23 28 4
gpt4 key购买 nike

我尝试使用 Redis 创建一个测试项目服务器,通过 Virtual Box 安装在 Linux Ubuntu 虚拟机上。

Linux 机器通过 Virtual Box 的桥接适配器连接到本地网络,Virtual Box 在我的开发 Windows 7 机器上运行。

Linux Machine 是新的,刚刚创建,我做了

sudo ufw enable

然后重启redis服务器(在linux上)。

现在这是我在 Windows 上的尝试

C:\Users\my-user>nmap -p 6379 10.14.30.51
Starting Nmap 7.70 ( https://nmap.org ) at ...
Nmap scan report for 10.14.30.51
Host is up (0.0010s latency).

PORT STATE SERVICE
6379/tcp filtered redis
MAC Address: 08:00:27:98:94:49 (Oracle VirtualBox virtual NIC)

我用我的 Visual Studio 2017 创建了一个新的 .NET MVC 项目,这里是 Controller 的代码:

using StackExchange.Redis;
using System.Web.Mvc;

namespace RedisClient.Controllers
{
public class HomeController : Controller
{
public struct Server {
public const string IP = "10.14.30.51";
public const int Port = 6379;
}

public ActionResult Index() { return View(); }

public ActionResult About()
{
ConnectionMultiplexer redis =
ConnectionMultiplexer.Connect($"{Server.IP}:{Server.Port}");

IDatabase db = redis.GetDatabase();
string value = "abcdefg";
db.StringSet("mykey", value);
value = db.StringGet("mykey");

ViewBag.Message = $"The value is '{value}'";

return View();
}

public ActionResult Contact() { return View(); }
}
}

因此,当我单击“关于”链接时,我在 .Connect 行上收到以下错误

It was not possible to connect to the redis server(s). UnableToConnect on 10.14.30.51:6379/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.0.601.3402

--编辑

输出

var log = new StringWriter();

ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(
$"{Server.IP}:{Server.Port},abortConnect=False", log);
var logOutput = log.ToString();

如下:

10.14.30.51:6379,abortConnect=False

Connecting 10.14.30.51:6379/Interactive...
BeginConnect: 10.14.30.51:6379
1 unique nodes specified
Requesting tie-break from 10.14.30.51:6379 > __Booksleeve_TieBreak...
Allowing endpoints 00:00:05 to respond...
Awaiting task completion, IOCP: (Busy=0,Free=1000,Min=4,Max=1000), WORKER: (Busy=1,Free=8190,Min=4,Max=8191)
Not all tasks completed cleanly (from ReconfigureAsync#1524, timeout 5000ms), IOCP: (Busy=0,Free=1000,Min=4,Max=1000), WORKER: (Busy=4,Free=8187,Min=4,Max=8191)
10.14.30.51:6379 did not respond
Waiting for tiebreakers...
Awaiting task completion, IOCP: (Busy=0,Free=1000,Min=4,Max=1000), WORKER: (Busy=4,Free=8187,Min=4,Max=8191)
connection failed: 10.14.30.51:6379 (Subscription, UnableToConnect): UnableToConnect on 10.14.30.51: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: 0s ago, v: 2.0.601.3402
connection failed: 10.14.30.51:6379 (Interactive, UnableToConnect): UnableToConnect on 10.14.30.51: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: 0s ago, v: 2.0.601.3402
Finished awaiting tasks, IOCP: (Busy=2,Free=998,Min=4,Max=1000), WORKER: (Busy=4,Free=8187,Min=4,Max=8191)
10.14.30.51:6379 failed to nominate (Faulted)
> UnableToConnect on 10.14.30.51: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: 0s ago, v: 2.0.601.3402
No masters detected
10.14.30.51:6379: Standalone v2.0.0, master; keep-alive: 00:01:00; int: Connecting; sub: Connecting; not in use: DidNotRespond
10.14.30.51: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
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: 2...
1 unique nodes specified
Requesting tie-break from 10.14.30.51:6379 > __Booksleeve_TieBreak...
Allowing endpoints 00:00:05 to respond...
Awaiting task completion, IOCP: (Busy=1,Free=999,Min=4,Max=1000), WORKER: (Busy=2,Free=8189,Min=4,Max=8191)
Not all tasks completed cleanly (from ReconfigureAsync#1524, timeout 5000ms), IOCP: (Busy=0,Free=1000,Min=4,Max=1000), WORKER: (Busy=2,Free=8189,Min=4,Max=8191)
10.14.30.51:6379 did not respond
Waiting for tiebreakers...
Awaiting task completion, IOCP: (Busy=0,Free=1000,Min=4,Max=1000), WORKER: (Busy=2,Free=8189,Min=4,Max=8191)
Not all tasks completed cleanly (from NominatePreferredMaster#1761, timeout 50ms), IOCP: (Busy=0,Free=1000,Min=4,Max=1000), WORKER: (Busy=2,Free=8189,Min=4,Max=8191)
10.14.30.51:6379 failed to nominate (WaitingForActivation)
No masters detected
10.14.30.51:6379: Standalone v2.0.0, master; keep-alive: 00:01:00; int: Disconnected; sub: Disconnected; not in use: DidNotRespond
10.14.30.51: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
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: 1...
1 unique nodes specified
Requesting tie-break from 10.14.30.51:6379 > __Booksleeve_TieBreak...
Allowing endpoints 00:00:05 to respond...
Awaiting task completion, IOCP: (Busy=0,Free=1000,Min=4,Max=1000), WORKER: (Busy=2,Free=8189,Min=4,Max=8191)
Not all tasks completed cleanly (from ReconfigureAsync#1524, timeout 5000ms), IOCP: (Busy=0,Free=1000,Min=4,Max=1000), WORKER: (Busy=5,Free=8186,Min=4,Max=8191)
10.14.30.51:6379 did not respond
Waiting for tiebreakers...
Awaiting task completion, IOCP: (Busy=0,Free=1000,Min=4,Max=1000), WORKER: (Busy=5,Free=8186,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)
10.14.30.51:6379 failed to nominate (Faulted)
> UnableToConnect on 10.14.30.51: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: 10s ago, v: 2.0.601.3402
No masters detected
10.14.30.51:6379: Standalone v2.0.0, master; keep-alive: 00:01:00; int: Disconnected; sub: Disconnected; not in use: DidNotRespond
10.14.30.51: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
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
Starting heartbeat...

==引用

在我的 .csproj 文件中,除其他外

<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Buffers.4.5.0\lib\netstandard2.0\System.Buffers.dll</HintPath>
</Reference>
...
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.5.2\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
...
<Reference Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
</Reference>

相关SuperUser question with another details is here .

window : enter image description here

Linux: enter image description here

最佳答案

感谢this SF answer我能够与主机 Windows 和虚拟 Linux 机器进行通信。

我做的步骤:

在 Redis 服务器(Linux)机器上:

1) ss -tlnp 'sport == :6379'如果你找到127.0.0.1::1在这些行中,您应该编辑 redis.config 文件,注释 bind 127.0.0.1 ::1在配置文件中(如果还没有),并且不要忘记在更改配置后重新启动redis
如果您看到类似 0.0.0.0:6379 的内容,执行下一步。

2) 检查protected mode在同一个配置文件中的 Redis,如果启用并且你想保留它,你可以使用 requirepass <yourConnectionPassToUseOnClientConnectionString>在 Redis 配置文件中。

3) 检查您的客户端 IP 地址(在我的例子中是 Windows 客户端计算机)。将其添加到 Linux iptables :

iptables -A INPUT --src <clientIpAddress> -p tcp --dport 6379 -j ACCEPT

3') 为了使这个添加的条目持久化,查看如何保存iptables 状态here .

在客户端 (Windows) 机器上:

4) 在连接字符串中使用上述密码连接到Linux Redis Server

ConnectionMultiplexer redis = 
ConnectionMultiplexer.Connect($"{Server.IP}:{Server.Port},resolvedns=1,abortConnect=False,password={Server.Password}");

5*) (可选)您可以添加您可以获得 Windows 版本 from choco (安装在 Windows 上),使用 cmd 测试来自客户端 Windows 的连接

redis-cli -h <LinuxServerIP> -p 6379 ping

关于asp.net - 通过 StackExchange.Redis 连接到 Redis 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56080984/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com