简体   繁体   English

.NET 服务无法连接到 Redis

[英].NET service can't connect to Redis

I'm trying to get a .NET API to be able to connect to a Redis instance on my docker network.我正在尝试获取一个 .NET API 以便能够连接到我的 docker 网络上的 Redis 实例。 Here's what I've got:这是我所拥有的:

  • Redis container: up and running on custom docker network my-network at localhost:6379 Redis 容器:在localhost:6379自定义 docker 网络my-network上启动并运行
  • .NET service: trying to get up and running on custom docker network my-network .NET 服务:尝试在自定义 docker 网络my-network上启动并运行

(Maybe?) Relevant versioning: (也许?)相关版本控制:

  • Abp.RedisCache 5.4.0 Abp.RedisCache 5.4.0
  • StackExchange.Redis 2.1.58 StackExchange.Redis 2.1.58

I've tried everything, network inspect , adding and removing both containers to the network, even with the special connection string flag resolveDns=true in my .NET code.我已经尝试了一切, network inspect ,添加和删除两个容器到网络,即使在我的 .NET 代码中使用特殊的连接字符串标志resolveDns=true No matter what I do I get:无论我做什么,我都会得到:

StackExchange.Redis.RedisConnectionException: It was not possible to connect to the redis server(s). UnableToConnect on localhost: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.1.58.34321

I'm losing my mind here, Redis is up and running at localhost:6379 on the docker network, why can't .NET connect?我在这里发疯了,Redis 已在 docker 网络上的localhost:6379上启动并运行,为什么无法连接 .NET?

YES I've passed a custom redis.conf which has bind 0.0.0.0 as well.是的,我已经通过了一个自定义的 redis.conf,它也bind 0.0.0.0

Thanks to Hans Kilian's comment, I was able to finally connect!感谢 Hans Kilian 的评论,我终于可以连接了!

Despite what seemingly infinite Redis + Docker tutorials say on the using localhost:6379 , this is in fact not the case when Redis is running in a Docker network.尽管看似无穷无尽的 Redis + Docker 教程在 using localhost:6379上说了些什么,但实际上当 Redis 在 Docker 网络中运行时,情况并非如此。 To find that IP, for example with a container named redis , issue:要查找该 IP,例如使用名为redis的容器,请发出:

docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' redis

It is then the resulting IP address, with port 6379, that you would need to provide as your connection string.然后,您需要将生成的 IP 地址和端口 6379 作为连接字符串提供。

tl;dr; tl;博士;

not:不是:

localhost:6379

but the Docker assigned IP for the container, ex.:但是 Docker 为容器分配了 IP,例如:

172.18.0.7:6379

EDIT: You can also use the container name instead of the IP, ie:编辑:您也可以使用容器名称而不是 IP,即:

redis:6379

Hope this helps anyone who may stumble upon this.希望这可以帮助任何可能偶然发现此问题的人。

I fixed this by changing:我通过更改解决了这个问题:

CacheSettings:ConnectionString=basketdb:6379

to

CacheSettings__ConnectionString=basketdb:6379

in the docker-compose.override.ymldocker-compose.override.yml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用ASP.NET会话状态提供程序连接到Redis服务器 - Can't connect to Redis server using ASP.NET Session State Provider 无法连接到 .NET 6 的 Windows 服务器上的 WCF 服务 - Can not connect to WCF service on a Windows Server with .NET 6 在 WCF 服务中使用 SSH.NET 时无法连接到 OpenSSH SFTP - Can't connect to OpenSSH SFTP when using SSH.NET in a WCF Service 无法使用StackExchange.Redis从主机连接到docker redis容器 - Can't connect to docker redis container from the host using StackExchange.Redis 无法在 .net C# 中连接 AWS Redis 缓存 - RedisConnectionException: '没有连接处于活动状态/可用于服务此操作 - Unable to connect AWS Redis cache in .net C# - RedisConnectionException: 'No connection is active/available to service this operation 无法从 .NET 应用程序连接到 Pusher - Can't connect to Pusher from .NET application .net github API-无法连接到存储库 - .net github api - can't connect to repository 通过服务中的.NET远程连接到服务 - connect to a service with .NET remoting in a service MMC无法看到已安装的.NET服务 - MMC can't see installed .NET service 网站代码无法连接到同一服务器上的Soap服务 - A web site code can't connect to a soap service on the same server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM