通过StackExchange.Redis连接到Redis Servier

时间:2019-05-10 15:49:48

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

我尝试通过安装在Linux Ubuntu虚拟机上的Redis服务器通过Virtual Box进行测试项目。

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

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服务器。无法连接   on 10.14.30.51:6379/Interactive,正在初始化/未启动,最后:无,   来源:BeginConnectAsync,未完成:0,上次阅读:0秒钟前,   last-write:0s ago,keep-alive:60s,state:Connecting,mgr:10 of 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

Windows: enter image description here

Linux: enter image description here

2 个答案:

答案 0 :(得分:1)

最好问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,则存在程序集绑定问题

后一种可能性更大,通常的解决方法是添加显式 <PackageReference>引用:

  • 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 库方面存在一些问题。这是我无法控制的。

答案 1 :(得分:1)

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

我执行的步骤:

在Redis Server(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 <yourConnectionPassToUseOnClientConnectionString>

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