[英]Connection to Redis Servier via StackExchange.Redis
我嘗試使用安裝在 Linux Ubuntu 虛擬機上的Redis服務器通過 Virtual Box 制作一個測試項目。
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 服務器。 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 個中的 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>
最好在庫github上問一下; 我會問那么問題是:請做到:
var log = new StringWriter();
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(
$"{Server.IP}:{Server.Port},abortConnect=False", log);
var logOutput = log.ToString();
並告訴我們什么是logOutput
。
我的猜測是:
后一種可能性更大,通常的解決方法是為以下內容添加顯式的 <PackageReference>
引用:
即
<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庫方面存在一些問題。 這是我無法控制的。
感謝這個SF回答,我能夠與主機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狀態。
在客戶端(Windows)機器上:
4)在連接字符串中使用以上密碼連接到Linux Redis Server
ConnectionMultiplexer redis =
ConnectionMultiplexer.Connect($"{Server.IP}:{Server.Port},resolvedns=1,abortConnect=False,password={Server.Password}");
5 *)(可選),您可以添加可以從choco獲取Windows版本(將其安裝在Windows上),以使用cmd測試來自客戶端Windows的連接
redis-cli -h <LinuxServerIP> -p 6379 ping
我通過更改解決了這個問題:
CacheSettings:ConnectionString=basketdb:6379
到
CacheSettings__ConnectionString=basketdb:6379
在docker-compose.override.yml
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.