【问题标题】:Connection to Redis Servier via StackExchange.Redis通过 StackExchange.Redis 连接到 Redis Servier
【发布时间】:2019-09-28 13:30:26
【问题描述】:

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

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

Linux机器是新的,刚刚创建,我做了

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 项目,这里是控制器的代码:

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 行收到以下错误

无法连接到 redis 服务器。无法连接 在 10.14.30.51:6379/Interactive, Initializing/NotStarted, last: NONE, 来源:BeginConnectAsync,未完成:0,上次阅读:0 秒前, 最后写入:0 秒前,保持活动状态:60 秒,状态:正在连接,经理:10 的 10 可用,最后一次心跳:从不,全局:0 秒前,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

窗户:

Linux:

【问题讨论】:

  • 我投票结束这个问题,因为它是cross-posted to Super User
  • 或更简单地说,如果您有可用的 redis 工具:redis-cli -h 10.14.30.51 -p 6379 ping
  • @Serge 不,我给你的第一个选项只使用telnet - 没有任何redis特定的,只是一个命令行套接字终端;但是:如果你打算做大量的 redis,如果你有可用的 redis 工具,它将为你节省很多时间;你可以得到 windows builds from choco,但我个人使用 WSL 和主要的 linux redis 发行版on Windowsredis-cli 是默认的 REPL 工具,用于在命令行与 redis 交互
  • 我从你的链接中安装了 choco 的工具,我还使用 nmap 命令的屏幕更新了 OP,而你给我的“redis-cli ... ping”命令什么也没做(没有反馈),似乎端口被一些未知的应用程序过滤了,即使在 Windows 或 Linux 虚拟机上...我禁用了防火墙,可能应该是另一个地方的问题...
  • @Serge 直到 操作系统 可以连接, 没有机会 - 所以...抱歉,你将不得不深入了解,但直到redis-cli ... ping 工作,它才不是库问题,而库没有

标签: asp.net .net redis stackexchange.redis


【解决方案1】:

感谢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) 在同一个配置文件中查看Redis的protected mode,如果已启用并想保留,可以在Redis配置文件中使用requirepass &lt;yourConnectionPassToUseOnClientConnectionString&gt;

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

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

【讨论】:

    【解决方案2】:

    这个最好问on the library github;我要问的问题然后是:请做:

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

    告诉我们logOutput 是什么。

    我的猜测是:

    • 由于防火墙规则,没有 TCP 访问权限
    • 如果您使用的是 .NET Framework,通常会出现程序集绑定问题

    后一种可能更有可能,通常的解决方法是添加 explicit &lt;PackageReference&gt; 引用到:

    • System.Buffers (>= 4.4.0)
    • System.Runtime.CompilerServices.Unsafe (>= 4.5.2)

    <PackageReference Include="System.Buffers" Version="4.5.0" />
    <PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.5.2" />
    

    也可能(因为……原因)

    <PackageReference Include="System.Numerics.Vectors" Version="4.5.0" />
    

    基本上,nuget 在传递依赖和上述 Microsoft 库方面存在一些问题。这是我无法控制的。

    【讨论】:

    • 实际上我在 redis.conf 中注释了绑定到 127.0.0。错误消息更改为“无法连接到 redis 服务器”。我会按照你的建议做并做反馈
    • @Serge 不会打开防火墙漏洞
    • 我也做了“sudo ufw enable”
    • 我想你知道,更多细节(和 conf 文件)在我的超级用户问题superuser.com/q/1435346/465922
    • @Serge 请不要将此类问题交叉发布到 Super User 以及 Stack Overflow 上。它只会引起混乱,并会导致人们没有真正认真对待您的问题和相关工作。
    猜你喜欢
    • 2023-03-12
    • 2020-04-20
    • 2016-12-03
    • 2022-11-13
    • 2014-11-11
    • 1970-01-01
    • 1970-01-01
    • 2017-07-18
    • 2014-06-23
    相关资源
    最近更新 更多